C++ Classes and Objects (original) (raw)
**In C++, classes and objects are the basic building block that leads to Object-Oriented programming in C++. We will learn about C++ classes, objects, look at how they work and how to implement them in our C++ program.
C++ Classes
A class is a **user-defined data type, which holds its own data members and member functions that can be accessed and used by creating an instance of that class. A C++ class is like a blueprint for an object.
**For Example: Consider the Class of Cars. There may be many cars with different names and brands but all of them will share some common properties like all of them will have _4 wheels, _Speed Limit, _Mileage range, etc. The car can also _accelerate, turn, apply brakes, etc. So here, the Car is the class, wheels, speed limits, and mileage are its attributes (data members) and _accelerate, turn, apply brakes are its methods (member functions).
**Create a Class
A class must be defined before its use. C++ class is defined using the keyword **class keyword as shown:
C++ `
class className { access_specifier:
// data member
// member method
};
`
where,
- **Data Members: These are the variables that are defined inside the class.
- **Member Functions: Functions declared inside a class. Also referred to as a member method.
**Example:
C++ `
class GfG { public:
// Data member
int val;
// Member function
void show() {
cout << "Value: " << val << endl;
}
};
`
In the above, **GfG class is created with a data member **val and member function **show(). Here, member function is defined inside the class, but they can also be just declared in the class and then defined outside using **scope resolution operator ::
The above is called class definition or template.
**C++ Objects
When a class is defined, only the specification (attributes and behaviour) for the object is defined. No memory is allocated to the class definition. To use the data and access functions defined in the class, we need to create its **objects.
**Objects are the actual entities that are created as an instance of a class. There can be as many objects of a class as desired. For example, in the above, we discussed the class of **Cars. If we create an actual car based on the properties of the Car class, the car we made is the object of that class.
Create Object
Once the class is defined, we can create its object in the same way we declare the variables of any other inbuilt data type.
C++ `
className objectName;
`
This statement creates an object of **className class.
Member Access
Members of the class can be accessed inside the class itself simply by using their assigned name.
To access them outside, the ****(.) dot operator**is used with the object of the class.
C++ `
obj.member1 // For data members obj.member2(..) // For functions
`
There **obj is the name of the object of the given class, **member1 is data member and **member2 is member function.
**Access Modifiers
In C++ classes, we can control the access to the members of the class using **Access Specifiers. Also known as access modifier, they are the keywords that are specified in the class and all the members of the class under that access specifier will have particular access level.
In C++, there are 3 access specifiers that are as follows:
- **Public: Members of the class can be accessed from outside the class.
- **Private: Private members can only be accessed within the class itself.
- **Protected: Protected members can be accessed within the class and by derived classes.
If we do not specify the access specifier, the private specifier is applied to every member by default.
**Example:
C++ `
#include <bits/stdc++.h> using namespace std;
// Creating a class class GfG { public:
// Data member
int val;
// Member function
void show() {
cout << "Value: " << val << endl;
}
};
int main() {
// Create Object
GfG obj;
// Access data member and assign
// it some value
obj.val = 10;
// Access member method
obj.show();
return 0;
}
`
Whether we can access a member of a class depends on the access specifier in which it is declared. In the above example, if the **val variable was declared as **private, then we would not have been able to access it in the **main function.
**Explanation:
Special Member Functions
In C++ classes, there are some special member functions that are essential to manage objects and provide some basic functionalities.
Constructor
**Constructors are special class members which are called by the compiler every time an object of that class is instantiated. They are used to construct the objects and making them ready for use. Constructors have the same name as the class.
**Example:
C++ `
#include <bits/stdc++.h> using namespace std;
class MyClass { public:
// Constructor
MyClass() {
cout << "Constructor called!";
}
};
int main() {
// Constructor automatically
// called when object is created.
MyClass obj;
return 0;
}
`
Output
Constructor called!
**Note: If the programmer does not define the constructor, the compiler automatically creates the default, copy and move constructor.
Destructors
**Destructor is another special member function that is called by the compiler when the scope of the object ends. It deallocates all the memory previously used by the object of the class so that there will be no memory leaks. The destructor also has the same name as the class but with **tilde (~) as prefix.
**Example:
C++ `
#include <bits/stdc++.h> using namespace std;
class MyClass { public: MyClass() { cout << "Constructor called!" << endl; }
// Destructor
~MyClass() {
cout << "Destructor called!";
}
};
int main() { MyClass obj; // Destructor will be called // automatically when obj goes out of scope return 0; }
`
Output
Constructor called! Destructor called!
Static Members
Members of the class can be declared as static. These **static members of a class are not associated with the objects of the class but with the class itself. The main feature of these members is that they are accessible directly through the class without creating any objects. Both data members and member methods can be static:
Static Data Members
**Static data members shared by all objects of the class, meaning only one copy exists for all objects of the class and they are declared with the static keyword.
**Example:
C++ `
#include using namespace std;
class GfG { public: static int val; };
// Initialize static member int GfG::val = 22; int main() {
// Access without creating object
cout << GfG::val << endl;
}
`
Static Member Function
**Static member functions are associated with the class itself rather than any specific object. They can only access static data members and cannot access instance data members. Static member functions are called using the class name, not the object.
We defined the member function inside the class, but we can also define the member function outside the class. To define a member function outside the class definition, we use scop resolution ****(::) operator******.**
**Example:
C++ `
#include using namespace std;
class GfG { public: static void printHello(); };
// Definintion of static member function void GfG::printHello() { cout << "Hello World"; } int main() {
// Access without creating object
GfG::printHello();
return 0;
}
`
Friend Class and Function
In C++, friend classes and functions allow access to the private and protected members of other classes:
A **friend class has the ability to access the private and protected members of other classes where it is declared as a friend. This feature can be useful when it is necessary for one class to access the private and protected members of another class.
A **friend function in C++ is similar to a friend class. It can be given special permission to access the private and protected members of a class. Although it is not a member function of the class, it can still access and modify those private and protected members because it is declared as a friend.
Local Class
Classes are generally declared in global scope and are accessible to every function or other classes once they are defined. But C++ also provides facility to define a class within a function. It is called **local class in C++ and is only accessible in that function.
Nested Class
A **nested class is a class defined within another enclosing class. As a member of the enclosing class, it has the same access rights as any other member. The members of the enclosing class do not have special access to the members of the nested class; the standard access rules apply.
Enum Class
**Enum classes in C++ are a safer and more organized way of using **enums. They help to group related constants together while avoiding naming problems and ensuring better type safety.
this Pointer
In C++, **this pointer is a pointer that points to the current instance of a class. It is used within the member functions of a class to refer to the object of that class. This pointer allows access to the calling object's data and methods within its own member functions.
C++ `
class A{ int n; A(int n) { this->n = n; } }
`
Class vs Object
The following table lists the primary differences between the classes and objects in C++:
**Class | **Object |
---|---|
A blueprint or template for creating objects. | An instance of a class with actual values. |
No memory is allocated for a class until an object is created. | Memory is allocated when an object is created. |
Conceptual entity describing structure and behaviour. | A real-world entity created from the class. |
Defines properties and functions common to all objects of that type. | Stores specific data and manipulates it using class functions. |
Represents a general concept or type. | Represents a specific instance of the class. |