C++ is an object-oriented programming language that allows developers to create classes and objects. A class is a blueprint for creating objects, while an object is an instance of a class. In this article, we will discuss C++ classes and objects in detail.
A class is a user-defined data type that encapsulates data and functions into a single unit. It is a blueprint for creating objects that have similar attributes and behaviors. A class can contain data members, member functions, constructors, and destructors.
Data members are variables that hold the state of an object, while member functions are methods that define the behavior of an object. Constructors are special member functions that are called when an object is created, while destructors are called when an object is destroyed.
Here is an example of a simple C++ class:
class Person {
private:
string name;
int age;
public:
Person(string n, int a) {
name = n;
age = a;
}
void display() {
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
}
};
In this example, we have defined a class called Person that has two data members (name and age) and one member function (display). The constructor takes two arguments (name and age) and initializes the data members.
An object is an instance of a class. It is a variable that holds the state of a class and can invoke the member functions of the class. Objects are created using the new operator and destroyed using the delete operator.
Here is an example of creating and using an object of the Person class:
int main() {
Person p("John", 30);
p.display();
return 0;
}
In this example, we have created an object of the Person class called p and initialized it with the name "John" and age 30. We then called the display() function of the object to print its state.
C++ classes have three access modifiers: private, public, and protected. Private members can only be accessed within the class, while public members can be accessed from anywhere. Protected members can be accessed within the class and its derived classes.
Here is an example of a class with private and public members:
class Rectangle {
private:
int length;
int width;
public:
void setLength(int l) {
length = l;
}
void setWidth(int w) {
width = w;
}
int getArea() {
return length * width;
}
};
In this example, the length and width data members are private, while the setLength(), setWidth(), and getArea() member functions are public. This means that the data members can only be accessed within the class, while the member functions can be accessed from anywhere.
C++ supports inheritance, which allows a class to inherit the properties of another class. The class that is being inherited from is called the base class, while the class that is inheriting is called the derived class.
Here is an example of a derived class:
class Square : public Rectangle {
public:
void setSide(int s) {
setLength(s);
setWidth(s);
}
};
In this example, the Square class is derived from the Rectangle class using the public keyword. This means that the public members of the Rectangle class are accessible from the Square class. The setSide() function sets the length and width of the square to the same value.
C++ classes and objects are an essential part of object-oriented programming. They allow developers to encapsulate data and functions into a single unit and create objects that have similar attributes and behaviors. Inheritance allows classes to inherit the properties of other classes, making it easier to reuse code and create more complex programs.