- c++继承代表父类属性在子类中会存在一份。理解起来为生物学的继承即可,子承父业,传承血脉
- ==继承:==子类中不会产生新的属性,派生子类会有新属性产生。继承分为父类和子类,派生分为基类和派生类
class 父类{};
class 子类名:继承方式 父类名
{
};
//继承方式
//所谓的权限限定词
//public protected privatr
//公有继承 保护继承 私有继承
继承中权限问题:代表就是继承下来的属性在子类中的呈现
继承方式 | public继承 | protected继承 | private继承 |
---|---|---|---|
public | public | protected | private |
protected | protected | protected | private |
private | 不可访问 | 不可访问 | 不可访问 |
继承中的构造函数
#include
#include
using namespace std;
class MM
{
public:
string name;
MM(string name,int age,int money):name(name),age(age),money(money){}
int Getmoney()
{
return this->money;
}
protected:
int age;
private:
int money;
};
class Son:public MM
{
public:
Son(string name,int age,int money,int num):MM(name,age,money),num(num){}
void print()
{
cout << name << "\t" << age << "\t" << Getmoney() << "\t" << num << endl;
}
protected:
int num;
};
int main()
{
Son son("king", 19, 100, 1001);
son.print();
return 0;
}
- 存在两个或者是两个以上的父类称之为多继承
- 构造顺序问题:只和继承顺序有关,和初始化参数列表一点关系都没有
#include
#include
using namespace std;
class MM
{
public:
MM(string firstMMname,string seconedMMname)
{
this->firstMMname = firstMMname;
this->seconedMMname = seconedMMname;
}
protected:
string firstMMname;
string seconedMMname;
};
class GG
{
public:
GG(string firstGGname, string seconedGGname)
{
this->firstGGname = firstGGname;
this->seconedGGname = seconedGGname;
}
protected:
string firstGGname;
string seconedGGname;
};
class son:public MM,protected GG
{
public:
son(string firstMMname, string seconedMMname, string firstGGname,
string seconedGGname, string seconedsonname) :
MM(firstMMname, seconedMMname), GG(firstGGname, seconedGGname)
{
this->firstsonname = firstMMname + firstGGname;
this->seconedsonname = seconedsonname;
}
void print()
{
cout << (firstsonname + seconedsonname) << endl;
}
protected:
string firstsonname;
string seconedsonname;
};
using namespace std;
int main()
{
son son("K", "i", "n", "g", "_");
son.print();
return 0;
}
继承无论被继承多少次,属性一直都在,所以一般类不能被继承很多层。继承太多次数,会导致子类臃肿
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-dUb43bqh-1673712417658)(./image/%E8%8F%B1%E5%BF%83%E7%BB%A7%E6%89%BF.png)]
#include
using namespace std;
class A
{
public:
A(int a):a(a){}
protected:
int a;
};
class B:virtual public A
{
public:
B(int a,int b):A(a),b(b){}
protected:
int b;
};
class C:virtual public A
{
public:
C(int a,int c):A(a),c(c){}
protected:
int c;
};
class D:public B,public C
{
public:
D(int a,int b,int c,int d):B(a,b),C(a,b),A(a),d(d){}
D():B(1,2),C(1,5),A(90),d(33){} //a来自爷爷
void print()
{
cout << a << endl;
cout << b << endl;
cout << c << endl;
cout << d << endl;
cout << A::a << endl;
}
protected:
int d;
};
int main()
{
D d(1, 2, 3, 4);
d.print();
D x;
x.print();
return 0;
}
- 没有用类名限定的基础上,就近原则,在哪个类里调用哪个类的方法
- 用类名限定,可以访问指定类中的成员
- 类指针被子类对象初始化
- 父类没有
virtual
,就看指针类型- 有
virtual
,就看对象类型
#include
#include
using namespace std;
class MM
{
public:
void print()
{
cout << "MM" << endl;
}
protected:
string name = "MM";
};
class Son:public MM
{
public:
void print()
{
cout << "son" << endl;
cout << name << endl; //就近原则
}
protected:
string name = "son";
};
int main()
{
Son son;
son.print();
Son* pson = new Son;
pson->print(); //覆盖-->正常调用,无法访问父类同名
pson->MM::print(); //类名限定访问
//非正常调用
MM* pmm = new Son; //c++允许父类指针被子类指针初始化
pmm->print(); //virtual
//父类指针不能访问子类中父类没有属性
//注意点: 子类指针被父类对象初始化,有危险。一般不被允许
return 0;
}