The Difference Between Class And Object Explained (+Example)
Table of content:
- How do Class and Object vary?
- Difference between Class and Object
The concept of class and object is a part of object-oriented programming (OOP). Object-oriented programming (OOP) is a programming paradigm that organizes data based on the concept of objects. Let us see what is the difference between these two –
How do Class and Object vary?
The basic difference between Class and Object is that –
Class is a user-defined datatype that has its own data members and member functions whereas an object is an instance of class by which we can access the data members and member functions of the class.
1. Class
Class is a user-defined datatype that contain its own data members and member functions. The member functions and data members can be accessed with the help of objects. It is the primary concept of object-oriented programming. A class is used to organize information or data so a programmer can reuse the elements in multiple instances.
For example, if a programmer wants to make three instances of Dogs, Golden Retriever, poodle, and Maltese dog. The class dog would store similar information that is unique to each dog (although they are different species, they will have similar property attributes) and appropriate information will be associated with the class Dog.
Syntax of class –
class <className>{
public:
(public data member and member functions)
private:
(private data members and member functions)
};
2. Object
An object is an instance of a class. As already mentioned above, all the data members and member functions of the class can be accessed with the help of objects.
An object in OOP is a component which consists of properties to make a particular data useful. For example, let’s consider a class Student. We can access various student details using some common property attributes like student name, roll number, etc.
An object consists of –
- Name – name of the variable
- Member data - data that describes the object
- Member functions - functions related to the object that also describes the behavior
Example –
class myClass { // Declaring a class
public: // Access specifier
int num;
string String;
};
int main() {
myClass Object; // Creating an object of myClass
// Access attributes and set values
Object.num = 17;
Object.String = “Hello World";
// Print
cout << Object.num << "\n";
cout << Object.String;
return 0;
}
What is the difference between Class and Object?
Here are some of the differences between Class and Object:
Class |
Object |
Class is the blueprint of an object. It is used to declare and create objects. |
Object is an instance of class. |
No memory is allocated when a class is declared. |
Memory is allocated as soon as an object is created. |
A class is a group of similar objects. |
Object is a real-world entity such as book, car, etc. |
Class is a logical entity. |
Object is a physical entity. |
Class can only be declared once. |
Object can be created many times as per requirement. |
Example of class can be car. |
Objects of the class car can be BMW, Mercedes, jaguar, etc. |
We saw above how classes and their objects are linked together. Objects are used to access the data members and functions of the class.
You might also like to read:
Login to continue reading
And access exclusive content, personalized recommendations, and career-boosting opportunities.
Comments
Add comment