|
Answer» Let us first look at the definitions of a class and an object before looking at the differences between the two: - Class: A class is the fundamental building unit of Object-Oriented Programming. It is a user-defined data type with its own set of data members and member FUNCTIONS which can be accessed and used by establishing a class instance (or an object). It is the blueprint of any item. For instance, consider the class "Accountant". There may be a lot of accounts with DIFFERENT names and categories, but they will all have some similar qualities, such as balances, account holder names, and so on. The account is the class, and the amount, as well as the account holder's name, are the properties.
- Object: A class's instance is an object. Objects can be used to access all of the class's data members and member functions. When a class is defined, no memory is ALLOCATED; nevertheless, memory is allocated when it is instantiated (that is when an object is formed). Consider the objects in the Account class: Savings account, Current account, and so on.
Now that we UNDERSTAND what classes and objects are, let us take a look at the differences between a class and an object: | CLASS | OBJECT |
|---|
| Class is a blueprint or an object factory. | Objects are instances of classes. | | On the creation of a class, no memory is allocated as such. Due to not being available in the memory, classes cannot be manipulated. | On the creation of an object, memory is allocated. This allows for the manipulation of objects. | | It is a logical entity. | Objects are PHYSICAL entities. | | There are no values in the class that can be linked to the field. | Each object has its own set of values that it is associated with. | | class <nameOfClass> {}; | // Class Declarationclass Account{ public: void foo(){ cout << "This is an Account" << endl; }}; // Main function of the programint main(){ Account newAccount; // Creation of Object newAccount.foo(); //Calling the object's foo member function} |
|