Object-Oriented Programming in C++

When programs become large, keeping everything inside variables and functions can become difficult. We need a better way to organize related data and the functions that work with that data. This is where C++ Object Oriented Programming comes in.

OOP allows us to organize a program around objects. A student, bank account, employee, or car can be represented as an object with its own data and functions.

The main ideas of OOP Concepts in C++ are classes, objects, encapsulation, inheritance, polymorphism, and abstraction. In this tutorial, we will start with the basic concepts and understand how classes and objects work.

OOP Concepts in C++

Before writing an OOP program, let’s understand the basic idea.

  • Class – A blueprint used to create objects.
  • Object – A real instance of a class.
  • Encapsulation – Keeping data and related functions together.
  • Inheritance – Creating a new class from an existing class.
  • Polymorphism – Allowing the same function or operation to behave differently.
  • Abstraction – Showing only the necessary details and hiding the internal working.

We will focus on the first few concepts here and cover advanced OOP concepts separately.

Class in C++

A class is like a blueprint. It describes what information an object will have and what it can do.

For example, if you want to create student objects, you can make a Student class:

class Student
{
public:
    string name;
    int marks;

    void display()
    {
        cout << name << " " << marks;
    }
};

The class itself does not represent one particular student. It simply describes what a student object will contain.

Object in C++

An object is created from a class.

Student s1;

Here, s1 is an object of the Student class.

You can give values to its members:

s1.name = "Aman";
s1.marks = 85;

s1.display();

Output

Aman 85

You can create multiple objects from the same class:

Student s1;
Student s2;

Both objects have the same structure, but they can contain different values.

This is the basic idea behind Classes and Objects in C++.

Access Specifiers in C++

Sometimes you don’t want every part of your program to directly access a class member. C++ provides access specifiers to control this.

There are three main access specifiers:

  • public
  • private
  • protected

Public members can be accessed from outside the class.

Private members can only be accessed from inside the class.

Protected members can be accessed inside the class and by its derived classes.

If you don’t specify an access specifier in a class, its members are private by default.

class Student
{
private:
    int marks;

public:
    void setMarks(int m)
    {
        marks = m;
    }

    int getMarks()
    {
        return marks;
    }
};

Here, marks cannot be changed directly from outside the class. The functions are used to access it.

These Access Specifiers in C++ are an important part of keeping data protected.

Constructors

A constructor is a special function that runs automatically when an object is created. It has the same name as the class and does not have a return type.

class Student
{
public:
    Student()
    {
        cout << "Object created";
    }
};

Now:

Student s1;

The constructor runs automatically when s1 is created.

A constructor can also receive values:

Student(string n)
{
    name = n;
}

This is useful when you want to give an object its initial values.

Destructor

A destructor runs automatically when an object is destroyed. It has the same name as the class but starts with a ~.

class Student
{
public:
    ~Student()
    {
        cout << "Object destroyed";
    }
};

A destructor is commonly used when some cleanup is required before an object is removed.

Together, Constructors and Destructors in C++ help manage what happens when objects are created and destroyed.

this Pointer

The this pointer refers to the current object.

It is especially useful when a parameter and a class member have the same name.

class Student
{
    int marks;

public:
    void setMarks(int marks)
    {
        this->marks = marks;
    }
};

Here, this->marks means the marks belonging to the current object.

Static Members

A static member belongs to the class rather than to each individual object.

class Student
{
public:
    static int count;
};

int Student::count = 0;

All objects of the class share the same static member.

Friend Function

A friend function is not a member of the class, but it can access the class’s private and protected members when declared with friend.

class Student
{
private:
    int marks = 90;

    friend void show(Student s);
};

void show(Student s)
{
    cout << s.marks;
}

Here, show() can access the private marks member.

Friend Class

A class can also be declared as a friend of another class.

class Teacher;

class Student
{
private:
    int marks = 90;

    friend class Teacher;
};

Now the Teacher class can access the private members of Student.


Frequently Asked Questions (FAQs)

Q1. What is object-oriented programming in C++?

Object-Oriented Programming in C++ is a programming approach that organizes code around objects and classes. It helps structure programs by combining data and functions into reusable units.

Q2. Why are classes and objects important in C++?

Classes and Objects in C++ provide the foundation for creating and working with real-world entities in programs. A class defines the structure, while an object represents an instance of that class.

Q3. What are the four main OOP concepts in C++?

The four main OOP Concepts in C++ are encapsulation, abstraction, inheritance, and polymorphism. They help create organized, reusable, and maintainable programs.

Q4. What is the difference between a constructor and destructor?

A constructor initializes an object when it is created, while a destructor is called when the object is destroyed. Constructors and Destructors in C++ help manage an object’s initialization and cleanup.

Q5. Why are access specifiers used in C++?

Access Specifiers in C++ control how class members can be accessed. The main specifiers are public, private, and protected.

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top