Classes & Objects–C++ Notes (C Plus Plus Notes)
C++ Classes & Objects
Classes and Objects are the two main aspects of OOP.
Ex:
Class | Objects |
Apple | |
Fruit | Banana |
Mango |
or:
Class | Object |
Volvo | |
Car | Audi |
Toyota |
A class is a template for objects, and an object is an instance of a class.
When the individual objects are created they inherit all the variables and functions from the class.
Everything in C++ is associated with classes & objects, along with its attributes (data members–such as “weight” & “color”), and methods (member functions–such as “drive” & “brake” for a “car”).
Attributes & methods are basically variables & functions that belong to the class. These are often referred to as “class members”. A function in a class is known as a member function, & performs one of the class’s tasks.
A class is a user-defined data type that we can use in our program, and it works as an object constructor, or a “blueprint” for creating objects.
*First, a class has to be created, known as defining a class, before creating an object of that class. We must create an object of a class before a program can perform the tasks the class describes. First we define a class (create) and a member function of that class, THEN create an object and call that object’s member function.
Creating a Class
Before function ‘main’ [ ‘main()’ ] can create an object we must tell the compiler what member functions & data members belong to the class–known as defining the class.
The class definition begins with keyword ‘class’, followed by our name for the class.
Then, inside the body of the class, ( { } ), we put our variables, called attributes (data members) (Ex. 2 below), and functions, called methods (member functions). (Ex. 1 below.)
Note: When variables are declared in a class, they are called “attributes”.
And finally, the class definition ends with a semicolon(;).
- Ex. 1: //GradeBook class definition
- class GradeBook {
- public:
- void displayMessage()
- { cout << “Welcome to the Grade Book!” << endl; } //end of function
- }; //end of class; every class’s body is enclosed in a pair of braces.
- void displayMessage()
- Ex. 1 Execution:
- int main() {
- Gradebook myGradeBook; // creates a GradeBook object named ‘myGradeBook’
- myGradeBook.displayMessage();//call object’s ‘displayMessage’ function
- } //end of ‘main’ function
- int main() {
- Output: Welcome to the Grade Book!
Note: Recall that a class is like a blueprint (or template), so we need to make an object of class GradeBook and call its ‘displayMessage’ member function to execute and display the welcome message (task).
Note: An object has attributes that are carried with the object as it’s used in a program. These attributes are specified as data members in the objects class.
- Ex. 2: //MyClass class definition
- class MyClass { //The class
- public: //Access specifier
- int myNum; //Attribute (int variable)
- string myString; //Attribute (string variable)
- }; //end of class
- Ex. 2 Execution:
- int main {
- MyClass myObj; //Create an object of MyClass
- //Access attributes and set values (calls the object & sets the values in one step)
- myObj.myNum = 15;
- myObj.myString = “Some text”;
- //Print attribute values
- cout << myObj.myNum << “\n”;
- cout << myObj.myString;
- return 0;
- } //end of ‘main’ function
- int main {
- Output:
- 15
- Some text