Classes and Objects in CPP

Classes and Objects in CPP

C++ is a powerful object-oriented programming language that allows developers to create classes and objects. Classes are used to define a blueprint for creating objects that share similar characteristics and behaviors. Objects, on the other hand, are instances of a class that contain data members and member functions. In this blog, we will explore classes and objects in C++.

Defining a Class in C++ To define a class in C++, we use the keyword ‘class’ followed by the name of the class. The data members and member functions of the class are declared within the curly braces ‘{ }’. Data members are the variables that hold the state of the object, while member functions are the operations that can be performed on the object.

Here is an example of a simple class in C++:

csharpCopy codeclass Person {
    private:
        string name;
        int age;
    public:
        void setName(string name) {
            this->name = name;
        }
        string getName() {
            return name;
        }
        void setAge(int age) {
            this->age = age;
        }
        int getAge() {
            return age;
        }
};

In this example, we have defined a class called ‘Person’ with two data members – ‘name’ and ‘age’ – and four member functions – ‘setName’, ‘getName’, ‘setAge’, and ‘getAge’. The ‘setName’ and ‘setAge’ functions set the values of the ‘name’ and ‘age’ data members, while the ‘getName’ and ‘getAge’ functions retrieve the values of these data members.

Creating Objects in C++ Once we have defined a class, we can create objects of that class. To create an object in C++, we use the ‘new’ keyword followed by the name of the class and a set of parentheses ‘( )’. We can also assign values to the data members of the object using the member functions.

Here is an example of how to create an object of the ‘Person’ class:

scssCopy codeint main() {
    Person* p = new Person();
    p->setName("John");
    p->setAge(30);
    cout << p->getName() << " is " << p->getAge() << " years old." << endl;
    return 0;
}

In this example, we have created a new object of the ‘Person’ class using the ‘new’ keyword. We have then assigned values to the ‘name’ and ‘age’ data members using the ‘setName’ and ‘setAge’ member functions. Finally, we have printed the values of the ‘name’ and ‘age’ data members using the ‘getName’ and ‘getAge’ member functions.

Accessing Data Members and Member Functions We can access the data members and member functions of an object using the dot notation ‘.’ or the arrow notation ‘->’. The dot notation is used when accessing the data members and member functions of an object directly, while the arrow notation is used when accessing the data members and member functions of an object through a pointer.

Here is an example of how to access the data members and member functions of an object using the dot and arrow notations:

scssCopy codePerson p;
p.setName("John");
p.setAge(30);
cout << p.getName() << " is " << p.getAge() << " years old." << endl;

Person* q = new Person();
q->setName("Mary");
q->setAge(25);
cout << q->getName() << " is " << q->getAge() << " years old." << endl;

In this example, we have created two objects of the ‘Person’ class – ‘p’ and ‘q’ – using both the dot notation and the arrow notation