C++ inheritance polymorphic function overloading

There is no concept of inheritance in C, so there is no inherited overloading. C++ supports inheritance and overloading, here are some sample codes:

  1. inherit

Inheritance is a mechanism in object-oriented programming, through which code reuse and extension can be realized. In inheritance, subclasses can inherit the member variables and member functions of the parent class, and can override the member functions of the parent class in the subclass.

Sample code:

class Animal {
    
    
public:
    virtual void eat() {
    
    
        cout << "Animal is eating." << endl;
    }
};

class Dog : public Animal {
    
    
public:
    void eat() override {
    
    
        cout << "Dog is eating." << endl;
    }
};

In the above sample code, the subclass Dog inherits the member function eat of the parent class Animal, and rewrites this function. Using the override keyword in a subclass allows the compiler to detect whether the subclass actually implements the function in the superclass.

class Base {
public:
    int x;
    Base(int x) : x(x) {}
};

class Derived : public Base {
public:
    int y;
    Derived(int x, int y) : Base(x), y(y) {}
};

int main() {
    Derived d(1, 2);
    return 0;
}
  1. polymorphism

Polymorphism is a concept in object-oriented programming, which means that the same function or the same method can exhibit different behaviors in different objects. Polymorphism in C++ comes in two flavors: virtual functions and templates.

Sample code:

class Shape {
    
    
public:
    virtual void draw() const = 0;
};

class Circle : public Shape {
    
    
public:
    void draw() const override {
    
    
        cout << "Circle::draw" << endl;
    }
};

class Square : public Shape {
    
    
public:
    void draw() const override {
    
    
        cout << "Square::draw" << endl;
    }
};

void render(const Shape& shape) {
    
    
    shape.draw();
}

int main() {
    
    
    Circle circle;
    Square square;

    render(circle);
    render(square);

    return 0;
}

In the sample code above, Shape is an abstract class with a pure virtual function draw. Both Circle and Square are derived classes of Shape, and they implement the draw function respectively. In the render function, we implemented polymorphism by using the parameter type as Shape reference.

  1. function overloading

Function overloading is an important concept in C++ that allows us to define multiple functions that can accept different numbers or types of parameters with the same name.

Sample code:

void print(int a) {
    
    
    cout << "print int: " << a << endl;
}

void print(double b) {
    
    
    cout << "print double: " << b << endl;
}

void print(const char* c) {
    
    
    cout << "print string: " << c << endl;
}

int main() {
    
    
    print(10);
    print(3.14);
    print("hello world");

    return 0;
}

In the sample code above, we define three functions print with the same name, which respectively accept parameters of type int, double and const char*, which is the embodiment of function overloading.

int add(int a, int b) {
    return a + b;
}

double add(double a, double b) {
    return a + b;
}

int main() {
    int a = 1, b = 2;
    double c = 1.5, d = 2.5;
    int sum1 = add(a, b);
    double sum2 = add(c, d);
    return 0;
}

In the above code, we have overloaded addthe function and defined the parameter versions of inttype and doubletype respectively. The two versions of the function are called in mainthe function , and the results are assigned to different variables.

  1. operator overloading

Operator overloading is allowed in C++ to achieve effects similar to class member functions. Operator overloading can make us more intuitive when using class objects, and the code is more elegant.

Sample code:

class Vector {
    
    
public:
    Vector(int x = 0, int y = 0) : m_x(x), m_y(y) {
    
    }

    Vector operator+(const Vector& v) {
    
    
        return Vector(m_x + v.m_x, m_y + v.m_y);
    }

    friend ostream& operator<<(ostream& os, const Vector& v) {
    
    
        os << "(" << v.m_x << ", " << v.m_y << ")";
        return os;
    }

private:
    int m_x;
    int m_y;
};

int main() {
    
    
    Vector v1(1, 2);
    Vector v2(3, 4);

    Vector v3 = v1 + v2;
    cout << v3 << endl;

    return 0;
}

In the above sample code, we defined class Vector and overloaded plus operator and output operator. By overloading the plus operator, we can directly use the plus sign to add two Vector objects without calling a special addition function in the class. By overloading the output operator, we can be more concise and clear when outputting Vector objects.

I hope these codes and explanations help you understand inheritance polymorphism, function overloading, and operator overloading in C++.

Guess you like

Origin blog.csdn.net/qq_39506862/article/details/130893710