Home  >  Article  >  Backend Development  >  How to do object-oriented programming in C++?

How to do object-oriented programming in C++?

WBOY
WBOYOriginal
2023-08-27 08:34:51869browse

How to do object-oriented programming in C++?

How to do object-oriented programming in C?

Object-Oriented Programming (OOP) is a very common and important software development paradigm. C is a multi-paradigm programming language that includes support for object-oriented programming. In C, through the concepts of class and object, we can easily implement object-oriented programming.

First, we need to define a class. A class is a custom data type that encapsulates data members and member functions. Data members describe the properties of a class, and member functions define the behavior of the class.

The following example shows the definition of a simple class:

#include <iostream>

class Shape {
    private:
        int width;
        int height;
    
    public:
        void setWidth(int w) {
            width = w;
        }
        
        void setHeight(int h) {
            height = h;
        }
        
        int getArea() {
            return width * height;
        }
};

int main() {
    Shape rectangle;
    rectangle.setWidth(10);
    rectangle.setHeight(5);
    int area = rectangle.getArea();
    std::cout << "矩形的面积是:" << area << std::endl;
    return 0;
}

In the above example, we defined a class named Shape. The Shape class has two data members: width and height, which represent the width and height of the rectangle respectively. The three member functions in the class are setWidth, setHeight and getArea. The setWidth and setHeight functions are used to set the width and height of the rectangle, while the getArea function returns the area of ​​the rectangle.

In the main function, we create a Shape object rectangle and set its width and height by calling the setWidth and setHeight functions. Finally, we call the getArea function to calculate the area of ​​the rectangle and output it to the console.

Through the concept of classes, we can create multiple objects to represent different instances. Each object has its own data members but shares the same member functions. This allows us to call the same member function on different objects to achieve code reuse.

In addition to encapsulating data members and member functions, classes also support inheritance and polymorphism. Inheritance allows one class to inherit members of another class, which enables code reuse and extension. Polymorphism allows using pointers or references of the parent class to point to objects of the subclass, achieving flexible programming.

The following example shows the use of inheritance and polymorphism:

#include <iostream>

class Shape {
    protected:
        int width;
        int height;

    public:
        void setWidth(int w) {
            width = w;
        }
        
        void setHeight(int h) {
            height = h;
        }
        
        virtual int getArea() = 0;
};

class Rectangle : public Shape {
    public:
        int getArea() {
            return width * height;
        }
};

class Triangle : public Shape {
    public:
        int getArea() {
            return width * height / 2;
        }
};

int main() {
    Shape* shape1 = new Rectangle();
    shape1->setWidth(10);
    shape1->setHeight(5);
    std::cout << "矩形的面积是:" << shape1->getArea() << std::endl;
    
    Shape* shape2 = new Triangle();
    shape2->setWidth(10);
    shape2->setHeight(5);
    std::cout << "三角形的面积是:" << shape2->getArea() << std::endl;
    
    delete shape1;
    delete shape2;
    
    return 0;
}

In the above example, we defined three classes: Shape, Rectangle and Triangle. Both the Rectangle and Triangle classes inherit from the Shape class, which means they inherit the setWidth, setHeight, and getArea functions from the Shape class. The Rectangle class overrides the getArea function to calculate the area of ​​a rectangle; the Triangle class also overrides the getArea function to calculate the area of ​​a triangle.

In the main function, we create two pointers of the Shape class, shape1 and shape2, and point to objects of the Rectangle and Triangle classes respectively. Through the shape1 and shape2 pointers, we can call the setWidth, setHeight and getArea functions. Since the getArea function is declared as a pure virtual function (virtual) in the Shape class, the shape1 and shape2 pointers will call different getArea functions according to the actual object type, achieving polymorphism.

Through the above examples, we have learned how to perform object-oriented programming in C, and mastered the basic concepts of class definition and use, inheritance and polymorphism. Object-oriented programming is a very powerful and flexible programming method. It can help us better organize and manage code, improve development efficiency and code reusability. I hope this article can help you understand and apply object-oriented programming!

The above is the detailed content of How to do object-oriented programming in C++?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn