C++ derived class definition

In C++, the general definition syntax for derived classes is:

class 派生类名:继承方式 基类名1,继承方式 基类名2,....,继承方式 基类名n
{
	派生类成员声明;
};

Derived class members indicate all the members inherited from the base class, newly added data and function members.

#include <iostream>
 
using namespace std;
 
// 基类
class Shape {
    
    
	protected:
		int width = 0;
		int height = 0;
	  
	public:
	void setWidth(int width){
    
    
        this -> width = width;
    }
      
    void setHeight(int height){
    
    
        this ->  height = height;
    }
    
   	int getWidth(){
    
    
        return this -> width;
    }
      
    int getHeight(){
    
    
        return this -> height;
    }
    
};
 
// 派生类
class Rectangle: public Shape{
    
    
   public:
      int getPerimeter(){
    
     
         return (width + height) * 2; 
      }
      
       int getArea(){
    
     
         return (width * height); 
      }
};
 
int main(void){
    
    
	Rectangle *rectangle = new Rectangle();
 
	rectangle -> setWidth(4);
	rectangle -> setHeight(3);
 
 	cout << "长:" << rectangle -> getHeight() << endl;
 	cout << "宽:" << rectangle -> getWidth() << endl;
	cout << "周长:" << rectangle -> getPerimeter() << endl;
	cout << "面积: " << rectangle -> getArea() << endl;
	
	delete rectangle;

	return 0;
}

Note:

  • A derived class can have multiple base classes at the same time. This situation is called multiple inheritance. At this time, who is it to be fooled? This class has the characteristics of multiple existing classes at the same time.
  • The situation where a derived class has only one direct base class is called single inheritance.
  • In the derivation process, the derived new class can also be used as a base class and continue to derive new classes. In addition, a base class can derive multiple derived classes at the same time.
  • The characteristics of a class inherited from the parent class can also be inherited by other new classes. The characteristics of a parent class can be inherited by multiple subclasses at the same time, forming a family of interrelated classes, sometimes called a class family.
  • In the class family, the base class that directly participates in the derivation of a certain class becomes the direct base class, and the base class of the base class or even the higher-level base class becomes the indirect base class.
  1. In the definition of a derived class, in addition to specifying the base class, you also need to specify the inheritance method. The inheritance mode specifies how to access the members inherited from the base class.
  2. In the definition statement of a derived class, each "inheritance method" is limited to the base class that immediately follows it.
  3. Inheritance keywords: public, protectedand private, respectively, public inheritance, inheritance and protection of private inheritance.
  4. If the inheritance method keyword is not explicitly given, the system default value is considered to be private inheritance ( private).
  5. The inheritance method of a class specifies the access rights of members of derived classes and objects outside the class to members inherited from the base class.

Guess you like

Origin blog.csdn.net/qq_44989881/article/details/112300803