Object Oriented Programming in C++

Object oriented programming (OOP) is a programming paradigm that focuses on the concept of objects and their interactions with one another. C++, like many other modern programming languages, supports OOP through the use of classes and objects. In this article, we will explore the basics of OOP in C++ and learn how to use classes and objects to create powerful, reusable, and well-structured code.

Classes and Objects

In OOP, a class is a blueprint for creating objects. A class defines the properties and methods that an object of that class will have. An object, also known as an instance, is a specific instance of a class. Objects are created from classes and can have their own unique state and behavior.

In C++, classes are defined using the class keyword. The class definition includes the data members (also known as properties or attributes) and member functions (also known as methods) that the class objects will have. Here is an example of a simple class definition for a class called Person that has a name and age property and a method that prints the person’s name and age:

class Person {
public:
    string name;
    int age;

    void print() {
        cout << "Name: " << name << endl;
        cout << "Age: " << age << endl;
    }
};

In this example, the class Person has two data members, name and age, both of which are public. The class also has one member function, print(), which is also public. Public data members and member functions can be accessed and modified by any code that has a reference to an object of the class.

Creating Objects

Once a class is defined, we can create objects of that class using the new operator. Here is an example of how to create an object of the Person class and initialize its name and age properties:

Person *p = new Person();
p->name = "John Doe";
p->age = 30;

In this example, we create a new object of the Person class and assign it to a pointer variable p. We then use the arrow operator (->) to access the name and age properties of the object and assign them values. We can also call the print() method of the object to print its name and age:

p->print();

This will output:

Name: John Doe
Age: 30

Constructors and Destructors

Classes in C++ can also have special member functions called constructors and destructors. Constructors are used to initialize the state of an object when it is created, while destructors are used to clean up any resources that the object was using before it is destroyed.

A constructor is a special member function that is automatically called when an object of a class is created. It has the same name as the class and does not have a return type. In C++, constructors can be defined with or without parameters. Here is an example of a constructor for the Person class that initializes the name and age properties:

class Person {
public:
    string name;
    int age;

Person(string n, int a) {
    name = n;
    age = a;
}

void print() {
    cout << "Name: " << name << endl;
    cout << "Age: " << age << endl;
}
};

In this example, the constructor takes in two parameters, a string for the name and an int for the age. When we create a new object of the class, we can pass in these values to initialize the name and age properties:

Person *p = new Person("John Doe", 30);
p->print();

This will output:

Name: John Doe
Age: 30

A destructor is a special member function that is automatically called when an object of a class is destroyed. It has the same name as the class, preceded by a tilde (~), and does not have any parameters or return type. Here is an example of a destructor for the Person class:

class Person {
public:
    string name;
    int age;

Person(string n, int a) {
    name = n;
    age = a;
}

~Person() {
    cout << "Person object destroyed" << endl;
}

void print() {
    cout << "Name: " << name << endl;
    cout << "Age: " << age << endl;
}
};

In this example, the destructor simply prints a message to the console when the object is destroyed. Keep in mind that the new operator used in the constructor needs to be matched with delete operator to release the memory allocated on the heap.

Encapsulation, Inheritance and Polymorphism

Encapsulation, Inheritance, and Polymorphism are the three main concepts of Object Oriented Programming.

Encapsulation is the process of hiding the internal state and behavior of an object from the outside world. This is done by making the data members of a class private and providing public accessor methods to access and modify them. In C++, this is achieved by using the private and public access modifiers.

Inheritance is the mechanism by which a new class is derived from an existing class. The new class inherits the properties and methods of the existing class and can add new properties and methods or override existing ones. This allows for code reuse and a hierarchical relationship between classes. In C++, inheritance is achieved using the class keyword followed by the name of the base class.

Polymorphism is the ability of an object to take on multiple forms. In C++, polymorphism is achieved through the use of virtual functions and virtual tables. A virtual function is a member function that can be overridden by derived classes, allowing for different behavior based on the type of the object. Virtual tables, also known as vtables, are used to store the addresses of virtual functions for each class. This allows for dynamic dispatch, where the correct implementation of a virtual function is called at runtime based on the type of the object. Polymorphism allows for code to be written in a more flexible and generic way, as the same function can be used with objects of different classes without the need for explicit type checking.

Leave a Reply

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