The concept of inheritance
C ++ inheritance is an important concept. Inheritance refers to, we can use a class to define another class, when you create this class, we do not need to re-write data members and member functions, which can greatly help us write code and maintain the efficiency of the code.
When we use the definition of a class to another class, the former is called a base class , which is called a derived class .
Class derived way
In order to derive a class, we can specify a class inheritance list, which can have a class, or more classes, class inheritance in the form below:
class Rectangle: public Shape //其中Rerangle 是派生类,shape是继承类
Where: modifiers public position may be: public, protected, Private , meaning they are
- public : When a class derived from a base class, when public, public members of the base class become public members of the derived class; protected members become protected members of the base class of the derived class. Private members of a base class derived class can not be accessed directly, but you can call members of the public and private access to protected members of the base class base class.
- protected : when from a protected base class sub-class, public and protected members of the base class become protected members of the derived class.
- Private : When from a private base class sub-class, public and protected members of the base class become private members of the derived class.
This position may also be vacant, it is private by default
such as:
#include <iostream>
using namespace std;
class Shape
{
public:
Shape(){width = 0,height = 1;};
void init(double w,double h){
width = w;
height = h;
}
double get_w(){return width;}
double get_h(){return height;}
friend class Retangle;
private:
double width;
double height;
};
class Rectangle: public Shape
{
public:
Rectangle(double xx,double yy,double w,double h){
init(w,h);
x = xx;
y = yy;
}
void move(int px,int py){x-=px,y-=py;}
double get_x(){return x;}
double get_y(){return y;}
private:
double x,y;
};
int main(void)
{
Rectangle rect(1,2,3,4);
cout<<rect.get_x()<<" "<<rect.get_y()<<" "<<rect.get_w()<<" "<<rect.get_h()<<endl;
int xx,yy;
cin>>xx>>yy;
rect.move(xx,yy);
cout<<rect.get_x()<<" "<<rect.get_y()<<" "<<rect.get_w()<<" "<<rect.get_h()<<endl;
return 0;
}
Result of the program
1 2 3 4
1 2 //input
0 0 3 4
Shape here is the base class, Retangle is the derived class.
Note that: Either inheritance, static members, static member functions and friend functions can not be inherited
Multiple inheritance class
Application of the above-mentioned succession of single inheritance class, multiple inheritance can be derived to a plurality of base classes.
such as:
#include<iostream>
using namespace std;
class A {
public:
A(int x) : x(x){}
int get_x() {return x;}
private:
int x;
};
class B {
public:
B(int y) : y(y){}
int get_y() {return y;}
private:
int y;
};
class C : public A, public B {
public:
C(int x,int y) : A(x), B(y) {}
void print() {
cout<<get_x()<<endl;
cout<<get_y()<<endl;
}
};
int main() {
C c(1,2);
c.print();
return 0;
}
Output
1
2