Table of Contents
C++ implements the Object-Oriented Programming features through classes and objects. The definitions for the data and functions of an object are stored inside a class. Therefore, we must define a class first and then create its objects.
A class cannot be used directly without creating objects, as it is just a definition.
Class – The Blueprint of an object
A class is simply an object’s definition or blueprint. It contains every code that is required to describe and create an object.
Classes create a custom data type from which new objects can be declared and used. Therefore, a class is:
- User-defined – It can be customized and defined by the programmer to model an object.
- Derived data type – Composed of functions and fundamental data types like int, float, char, etc.
The general syntax of a class is given below:
class tagname { private: [data members]; [member functions]; protected: [data members]; [member functions]; public: [data members]; [member functions]; };
Components of a Class
Class Name
To create a class definition, we need to use the class
keyword before its name. This is because the class name plays a vital role in declaring objects.
Any code of the class must be wrapped inside a pair of braces and terminate with a semicolon.
Access Specifiers
A class also contains one or more of the access specifiers (private, protected, public). They determine the levels of access to the class members from the outside code.
Data Members and Member functions
In C++, the variables and functions declared inside an object are respectively termed data members and member functions. They are together known as class members and can be defined under any access specifier.
[adinserter block=”2″]
Object – Instance of a class
An object is an actual entity or implementation of a class. Therefore, we can only create objects from those classes which are defined/available in our program.
classtag objectname;
Declaring an object is similar to declaring normal variables. In addition, an object will have the class members, which is identified by the class tag.
Classes do not occupy any memory except for their definition. But every object created from a class occupies some space in the memory.
This is because each object maintains an independent copy of its data members.
Dot Operator
The dot (.)
operator is a member access operator in C++. Using a dot operator, we can access the data members and member functions of that object.
/* access a data member*/ objectname.data /* invoke a member function */ objectname.func();
Class vs. Object
A class is an idea or a blueprint for something, but it has no real existence. However, an object is an actual implementation that occupies space in the memory and can store details and perform actions.
For example, a car is just a blueprint or an abstract concept (class). It only comes to existence when the car industry implements this idea to manufacture actual cars (objects) to sit, use, and drive.
[adinserter block=”2″]
Points to Remember
- Object-Oriented Programming is implemented using classes and objects.
- Classes are the blueprint for an object. They define the object’s overall structure, which includes the necessary data members and member functions.
- An object is an instance, or a variable, of a class.
- Every object represents an entity that stores and maintains its copy of data members.
- An Object uses the dot operator to access its data members and functions.