Objects in C++
Define C++ Objects:
A class provides the overview for objects, so an object is created from a class. We declare objects of a class with exactly the same order of declaration that we declare variables of basic types. Following statements declare two objects of class named as pen:
pen pen1; // Declare pen1 of type Box pen pen2; // Declare pen2 of type Box
Both of the objects pen1 and pen2 will have their own copy of data members.
Accessing the Data Members:
The public data members of objects of a class can be accessed using the direct member access operator (.). Let us try the following example to make the things clear:
#include <iostream> using namespace std; class pen { public: double length; // Length of a pen double breadth; // Breadth of a pen double height; // Height of a pen }; int main( ) { pen pen1; // Declare pen1 of type Box pen pen2; // Declare pen2 of type Box double volume = 0.0; // Store the volume of a pen here // pen 1 specification pen1.height = 5.0; pen1.length = 6.0; pen1.breadth = 7.0; // pen 2 specification pen2.height = 10.0; pen2.length = 12.0; pen2.breadth = 13.0; // volume of pen 1 volume = pen1.height * pen1.length * pen1.breadth; cout << "Volume of pen1 : " << volume <<endl; // volume of pen 2 volume = pen2.height * pen2.length * pen2.breadth; cout << "Volume of pen2 : " << volume <<endl; return 0; }
When the above code is compiled and executed, it produces the following result:
Volume of pen1 : 210 Volume of pen2 : 1560
It is important to note that private and protected members can not be accessed directly using direct member access operator (.). We will learn how private and protected members can be accessed.
Classes & Objects in Detail:
So far, you have got very basic idea about C++ Classes and Objects. There are further interesting concepts related to C++ Classes and Objects which we will discuss in various sub-sections listed below:
Concept | Description |
---|---|
Class member functions | A member function of a class is a function that has its definition or its prototype within the class definition like any other variable. |
Class access modifiers | A class member can be defined as public, private or protected. By default members would be assumed as private. |
Constructor & destructor | A class constructor is a special function in a class that is called when a new object of the class is created. A destructor is also a special function which is called when created object is deleted. |
C++ copy constructor | The copy constructor is a constructor which creates an object by initializing it with an object of the same class, which has been created previously. |
C++ friend functions | A friend function is permitted full access to private and protected members of a class. |
C++ inline functions | With an inline function, the compiler tries to expand the code in the body of the function in place of a call to the function. |
The this pointer in C++ | Every object has a special pointer this which points to the object itself. |
Pointer to C++ classes | A pointer to a class is done exactly the same way a pointer to a structure is. In fact a class is really just a structure with functions in it. |
Static members of a class | Both data members and function members of a class can be declared as static. |