C++ Advanced (7)-Inheritance and Derivation 2

2. Inheritance

Introduction to inheritance and public inheritance

The influence of different inheritance methods is mainly reflected in:

  • Access rights of derived class members to base class members
  • Access to base class members through derived class objects

Three ways of inheritance

  • Public inheritance
  • Private inheritance
  • Protect inheritance

Public inheritance (public)

  • Inherited access control
    • The public and protected members of the base class: access attributes remain unchanged in the derived class;
    • Private members of the base class: not directly accessible.
  • access permission
    • Member functions in derived classes: you can directly access the public and protected members of the base class, but you cannot directly access the private members of the base class;
    • Through objects of derived classes: only public members can be accessed.

Examples of public inheritance

#ifndef _POINT_H
#define _POINT_H
class Point {   
    //基类Point类的定义
    public:     
    //公有函数成员
        void initPoint(float x = 0, float y = 0){ 
            this->x = x; 
            this->y = y;
        }
        void move(float offX, float offY){ 
            x += offX; 
            y += offY;
        }
        float getX() const { return x; }
        float getY() const { return y; }
    private:        
    //私有数据成员
        float x, y;
};
#endif //_POINT_H
#ifndef _RECTANGLE_H
#define _RECTANGLE_H
#include "Point.h"
class Rectangle: public Point { 
//派生类定义部分
public: 
//新增公有函数成员
    void initRectangle(float x, float y, float w, float h) {
        initPoint(x, y);              //调用基类公有成员函数
        this->w = w;
        this->h = h;
    }
    float getH() const { return h; }
    float getW() const { return w; }
private:    
//新增私有数据成员
    float w, h;
};
#endif //_RECTANGLE_H

You can see that the defined base class point, the Rectangle class inherits the point class. The public members of the base class are still public after being inherited in the public way. After the private members of the base class are inherited, they cannot be directly accessed in the member functions of the derived class. The public functions provided to obtain private properties must be called to access , A bit detour,

#include <iostream>
#include <cmath>
using namespace std;
#include “Rectangle.h”
int main() {
    Rectangle rect; //定义Rectangle类的对象
    //设置矩形的数据
    rect.initRectangle(2, 3, 20, 10);   
    rect.move(3,2); //移动矩形位置
    cout << "The data of rect(x,y,w,h): " << endl;
    //输出矩形的特征参数
    cout << rect.getX() <<", "
        << rect.getY() << ", "
        << rect.getW() << ", "
        << rect.getH() << endl;
    return 0;
}

When you define a derived class object outside the class, you can access your own public functions through the derived class object, or you can access the original public functions inherited from the public base class, because after inheritance, it is still public.

Guess you like

Origin blog.csdn.net/qq_41023026/article/details/108568111