Classes and Objects in C++
Learn via video course

Overview
C++ is an object-oriented programming language that is used to implement real-world entities like inheritance, hiding, polymorphism, etc into programs. Object-oriented programming languages achieve this using classes and objects.
Scope
In this article we will discuss below topics :
- Definition and declaration of class and object in C++.
- Significance of using class and object in C++ which gives rise to object-oriented programming.
- Differentiate between structures and classes in C++.
- Various access modifiers and ways we can access them with the help of C++ code implementation.
This article does not cover any algorithm on object-oriented programming and only covers cpp language to understand classes and objects.
What is a Class in C++?
A class in C++ is a user-defined type or data structure declared with a keyword class that has data and functions as its members. A class can be used by declaring an instance of that class which is nothing but an object in C++.
In real life, we see dogs with different breeds or classes but there are some common characteristics such as they have a tail, they bark, they sniff, etc. So, instead of creating a different class for every dog, we can define a single class with common properties of dogs. Hence, Classes act as a user-defined data type to create objects with similar properties.
What is an Object in C++?
A class is merely a blueprint of data. An object is a data structure that is an instance of a class. So when a class is created no memory is allocated but when an instance is created by declaring an object, memory is then allocated to store data and perform required functions on them. We can visualize trees, birds, and dogs as different classes and their instances like neem tree, peacock, and german shepherd dog respectively. Hence, class and object in C++ are the main ingredients of object-oriented programming.
Defining Class in C++
A Class is defined by a keyword class followed by a class name (user's choice) and a block of curly brackets with semicolons after the block. The block starts with access specifiers followed by data members and member functions.
Note: Access specifier defines how the members of the class can be accessed. In C++, there are 3 types of access specifiers: public, private, and protected.
- public: members can be accessed outside the class.
- private: members cannot be accessed outside the class.
- protected: members cannot be accessed(viewed) from outside the class, but can be accessed in inherited classes(subclasses).
Syntax:
Let's take an example of a Dog class having data members breed and color followed by member functions.
Declaring Objects in C++
When a class is defined only the blueprint of data structure is defined as no memory is allocated. To use data and its member function we can declare objects. We can declare objects by mentioning the class followed by the user-defined object name.
Syntax:
Example:
Significance of Class and Object in C++
The concept of class and object in C++ allows real-life analogies to be included in the programming. Using classes, data is given the highest importance. The following are some of the significant points to keep in mind:
- Data hiding: Using access specifiers, a class stops data from being accessed from the outside world. Classes have the ability to set permissions to limit who has access to the data.
- Code Reusability: With the aid of inheritance, you may reduce code redundancy by employing reusable code. Similar functionality and features can be inherited by other classes, making the code cleaner.
- Data binding: The data elements and their associated operations are bonded together under one umbrella, increasing the data's security.
- Flexibility: The principle of polymorphism allows you to use a class in a variety of ways. This increases the flexibility of a program.
Array of Objects
An array in C++ is a collection of data stored in contagious memory locations in the memory. An array can be of int, string, or char as well as of some derived data types like structures, pointers, etc. An array of objects is a collection of objects of some class type.
We can declare an array of objects by providing the total number of objects in closed brackets after the object name.
Syntax:
Example:
Objects as Function Arguments
To pass an object to a function we pass the object in the same way as we pass any other primitive data(int, string, etc). We just mention the object in the function's argument and call the function. Syntax:
Example: Here, we are printing all the properties(breed, color) of object dog by passing it to printProperties() function.
Difference Between Structure and Class
The main point of difference between a class and structure is security. A structure cannot hide information from the user whereas a class has access rules which can be set to provide security to its data members and member functions. Following is the point to point difference between Class and Structure:
Features | Structure | Class |
---|---|---|
Definition | A structure is a collection of variables of different data types with the same name. | A class in C++ is a single structure that contains a collection of linked variables and functions. |
Basic | All members are set to 'public' if no access specifier is supplied. | All members are set to 'private' if no access specifier is given. |
Declaration | Struct structure_name {type struct_member1; }; | Class class_name{ data_member; member functions;} |
Instance | The 'structural variable' is the name given to the instance of a structure. | A class instance is called 'object'. |
Inheritance | It doesn't allow for inheritance. | It is in favour of inheritance. |
Memory Allocated | The stack is used to allocate memory. | The heap is where memory is allocated. |
Nature | Value Type | Reference Type |
Purpose | Grouping of data | Data abstraction and further inheritance. |
Usage | For lower amounts of data, it is employed. | It is used to store a large amount of information. |
Null values | Not possible | It may have null values. |
Requires constructor | It may have only parameterized constructor. | It could contain any number of constructors. |
Access modifiers
Now, we will learn about the access modifiers in detail. Data hiding is one of the most important characteristics of object-oriented programming languages like C++.
Note: Data hiding refers to limiting access to a class's data members. This is done to keep other functions and classes from messing with the data in the class. However, some member functions and member data must be made public so that hidden data can be modified indirectly.
C++'s access modifiers let us choose which class members are available to other classes and functions and which aren't. In C++ there are 3 access modifiers:
- Public: As the name suggests a public data member can be accessed anywhere in the program by the (.) operator.
- Private: Private data members cannot be accessed from outside the class. We need to use some special member functions of that class to access the data members called getters and setters. We will discuss these member functions later in this article.
- Protected: A protected member variable or function is similar to a private member, but it has the added benefit of being accessible in child classes or subclass derived from the class.
Accessing Data Members and C++ Program to demonstrate accessing of data members
Access to a data member is solely determined by the data member's access control.
- If the *data member is public, the object of that class can readily access it using the direct member access (.) operator. Let's take a C++ program to understand accessing of public data members:
- If the data member is set to private or protected, we can't access the data variables directly. Then, in order to access, use, or initialize the private and protected data members, we'll need to construct unique public member functions. These member functions are known as getter and setter functions. Let's take an example of another class:
Here we can access the data members indirectly by the help of getter and setter like getBreed() and setBreed() respectively.
Protected data members can be simply accessed by the (.) operator by the subclass of the class; whereas for a non-subclass, we have to implement getters and setters in the same way as we accessed private data members above.
Member Functions in Classes
Member functions of a class are used to access, use or modify the data members of that class. We can define member functions in two ways:
- Inside Class definition: We can define a member function inside the class directly without declaring it first in class.
- Outside Class definition: To define a member function outside the class we need to declare it first inside the class and then define it outside the class according to the following syntax:
Example:
Conclusion
-
A class in C++ is a user-defined data type that behaves same as the built in data types of C++, whereas an object is a data structure that is an instance of a class. Class and object in C++ are the main ingredients of object-oriented programming.
-
A Class is defined by a keyword class followed by a class name and a block of curly brackets with a semicolon after the block.
-
We declare objects by mentioning the class followed by the user-defined object name.
-
We pass objects as arguments the same way as we pass any other primitive data like int, string, etc.
-
A structure cannot hide information from the user whereas a class has access rules which can be set to provide security to its data members and member functions.
-
There are 3 types of access modifiers in C++ namely, public, private, and protected. We can access public data members by using the (.) operator whereas we need to use getters and setters for private and protected modifiers.
-
We can define member functions in two ways namely, inside class definition and outside the class definition.