Chapter Thirteen: Detailed Explanation of C++ Object-Oriented Programming Thoughts
1 Overview
Object-Oriented Programming (OOP) is a software development method whose core concept is to abstract things in the real world into objects, and describe and solve problems through the interaction between objects. This article will explain in detail the idea of object-oriented programming in C++, and demonstrate it with code and actual cases.
2. Object-oriented basic concepts
2.1 Classes and Objects
In object-oriented programming, a class (Class) is a template used to describe objects with the same properties and behaviors. An object (Object) is an instance of a class, and an object can be created by instantiating a class. Here is an example of a simple class and object:
class Rectangle {
private:
int width;
int height;
public:
void setDimensions(int w, int h) {
width = w;
height = h;
}
int calculateArea() {
return width * height;
}
};
int main() {
// 创建一个Rectangle对象
Rectangle rect;
// 使用成员函数设置对象的宽度和高度
rect.setDimensions(5, 3);
// 调用成员函数计算矩形的面积
int area = rect.calculateArea();
cout << "Rectangular Area: " << area << endl;
return 0;
}
operation result:
Rectangular Area: 15
In the above code, we defined a Rectangle
class named to represent rectangles. By instantiating this class, we create an rect
object named and use member functions to set the width and height of the object. Then, we call the member function to calculate and output the area of the rectangle.
2.2 Packaging
Encapsulation is one of the important features of object-oriented programming, which packages data and functions to form a class. Through encapsulation, we can hide implementation details, so that users of the class can only access and manipulate data through the public interface. Here is an example wrapper:
class Circle {
private:
double radius;
public:
void setRadius(double r) {
if (r >= 0) {
radius = r;
} else {
cout << "Invalid radius." << endl;
}
}
double getRadius() const {
return radius;
}
double calculateArea() const {
return 3.14 * radius * radius;
}
};
int main() {
Circle c;
c.setRadius(5);
cout << "Circle Radius: " << c.getRadius() << endl;
cout << "Circle Area: " << c.calculateArea() << endl;
return 0;
}
operation result:
Circle Radius: 5
Circle Area: 78.5
In the above code, we defined a Circle
class named to represent a circle. radius
It is a private member variable and is not visible to the outside world. Through the public member function setRadius
, we guarantee the legality of the radius. Public member functions getRadius
and calculateArea
can be used to get the radius and calculate the area of a circle.
2.3 Inheritance
Inheritance is another important concept of object-oriented programming, which allows us to define a new class to inherit the properties and methods of the existing class. Inheritance enables code reuse and hierarchical organization. Here is an example:
class Shape {
protected:
int width;
int height;
public:
void setDimensions(int w, int h) {
width = w;
height = h;
}
};
class Rectangle : public Shape {
public:
int calculateArea() {
return width * height;
}
};
int main() {
Rectangle rect;
rect.setDimensions(5, 3);
cout << "Rectangular Area: " << rect.calculateArea() << endl;
return 0;
}
operation result:
Rectangular Area: 15
In the above code, we have used inheritance to create a Rectangle
class named class which inherits Shape
class. The subclass Rectangle
automatically has Shape
the member variables and member functions of the parent class, and can be initialized by calling the constructor. Therefore, we can Rectangle
calculate the area of a rectangle by creating an object and setting its dimensions.
2.4 Polymorphism
Polymorphism is another important feature of object-oriented programming, which refers to the same function or method showing multiple forms due to different behaviors of different objects. Polymorphism is implemented through base class pointers or references, allowing us to deal with different derived class objects in a unified way. Here is an example of polymorphism:
class Shape {
public:
virtual double calculateArea() const = 0;
};
class Rectangle : public Shape {
private:
int width;
int height;
public:
Rectangle(int w, int h) : width(w), height(h) {
}
double calculateArea() const {
return width * height;
}
};
class Circle : public Shape {
private:
double radius;
public:
Circle(double r) : radius(r) {
}
double calculateArea() const {
return 3.14 * radius * radius;
}
};
int main() {
Rectangle rect(5, 3);
Circle circle(2);
// 使用基类指针指向子类对象
Shape* shape1 = ▭
Shape* shape2 = &circle;
cout << "Rectangle Area: " << shape1->calculateArea() << endl;
cout << "Circle Area: " << shape2->calculateArea() << endl;
return 0;
}
operation result:
Rectangle Area: 15
Circle Area: 12.56
In this example, we define an abstract base class Shape
and provide pure virtual functions calculateArea()
. Rectangle
Both Circle
classes inherit from Shape
and implement their own calculateArea()
functions.
In functions, we store objects and objects main()
using base class pointers and call functions through pointers. Due to the existence of the polymorphic mechanism, even if the base class pointer is used, the correct function implementation will be called according to the actually referred to derived class object.Rectangle
Circle
calculateArea()
3. Summary
This article introduces the basic concepts and ideas of object-oriented process designing in C++ in detail. Classes and objects, encapsulation, inheritance and polymorphism are the core concepts of object-oriented programming, which can help us better organize and manage code, and realize complex functions.
By rationally using the idea of object-oriented programming, we can improve the readability, maintainability and reusability of the code, so as to develop software more efficiently.