Class and Object in C++ – A complete overview

Filed Under: C++
Classes And Objects In C

Hey, readers! In this article, we will be focusing on Class and Object in C++. So, let us begin!


Object-Oriented Programming Pillars – Class and Object in C++

Object Oriented Programming Paradigm deals with the functioning of a model based on the objective representation of the attributes and behavior of an entity.

In programming terms, an attribute is a variable that depicts a certain trait or behavior through the methods, also known as functions. Object-Oriented Programming combines the attributes and behavior of an entity.

The important pillars of Object Oriented Programming are as follows–

  • Class
  • Object

Class combines the functions, variables, and other paradigms of programming into a single unit and that is represented through objects.

So, having understood the overview of Classes and Objects, let us now dive deep into the concepts of Classes and Objects in C++.


Classes in C++

Class is a user-defined data type that clubs attributes and behavior entities together and represents it as a single unit altogether.

A Class consists of the following members:

  • data variables
  • functions

The data members and methods of the class can be accessed through objects which will be covered in the later part of this article.

Classes are a blueprint of objects. That is, let us consider an example:

If we were to build a new model of car, at first, we would draw the sketch of the car wherein we provide the details of all the features of the car. Thus, Class can be considered as the sketch of the car and here Car is the actual Object.


Declaring and defining a Class in C++

Have a look at the below syntax–

The keyword ‘class‘ is used to declare a Class in C++.

class Class-name{      
  access-specifier:            
    data_variable1;
    data_variableN;
  member-function{ body }
};
  • access-specifier: Access Specifiers in C++, defines the accessibility of the data member by the objects and other classes.

Having understood the definition of a class, let us now jump to understanding Objects in C++.


Objects in C++

Objects are the representatives of a Class. They represent the members of the Class such as functions, data values, and so on.

Moreover, objects access and manipulate the data values of the Class in accordance with the access specifiers.

Have a look at the below syntax to understand the declaration of objects in C++:

Class-name object-name;

Objects access the data variables of the class as shown below–

object.data_variable = value;

Let us now implement the concept of Classes and Objects through an example.


Implementing the Concept of Class and Object in C++

In this example, we have created a class ‘Info’ with the access specifier as public which makes the data variables and member functions accessible to all the other classes publically.

Further, we have created data members to access and display information about the user. An object of the class is created to access the data members.

Example:

#include <iostream>

using namespace std;

class Info {
   public:
      string Name;
      double marks;   
      int roll_num;
};

int main() {
   Info obj;
   obj.Name = "Python with JournalDev";
   obj.marks = 95.34;
   obj.roll_num = 12;
   cout<<"Details of the Student----\n";
   cout<<"Name:\n";
   cout<<obj.Name<<endl;
   cout<<"Roll-num:\n";
   cout<<obj.roll_num<<endl;
   cout<<"Marks:\n";
   cout<<obj.marks;
   
   return 0;
}

Output:

Details of the Student----
Name:
Python with JournalDev
Roll-num:
12
Marks:
95.34

Conclusion

By this, we have come to the end of this topic. Feel free to comment below in case you come across any questions. For more such posts related to C++, do visit C++ with JournalDev.

Till then, Happy Learning!!


References

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages