c++继承是c++的三大特性之一,目的是解决代码的复用性,子类继承基类的所有成员,除了构造函数和析构函数
还有operator=也不能被继承
继承有三种继承方式,最常用的是public继承
三种继承方式:
public : 公有继承
private : 私有继承
protected : 保护继承
改变权限如下:
公有派生
基类属性
派生类权限
私有 不可访问
保护 保护
公有 公有
代码:
#include<iostream>
using namespace std;
//多继承公有派生
class grandfather
{
public:
grandfather()
{
this->Gfa = 10;
this->Gfb = 20;
this->Gfc = 30;
}
public:
int Gfa;
protected:
int Gfb;
private:
int Gfc;
};
class father :public grandfather
{
public:
void test()
{
cout << Gfa << endl;//公有可以访问,继承后成员变量还是公有
cout << Gfb << endl;//保护可以访问,继承后成员变量还是保护
//cout << Gfc << endl;私有不能访问
}
};
class son :public father
{
public:
void test()
{
cout << Gfa << endl;//公有可以访问,继承后成员变量还是公有
cout << Gfb << endl;//保护可以访问,继承后成员变量还是保护
//cout << Gfc << endl;私有不能访问
}
};
int main()
{
father F;
son S;
cout<<S.Gfa << endl;//类外只能访问公有属性
cout << F.Gfa << endl;
system("pause");
}
基类属性 派生类权限
私有 不可访问
保护 私有
公有 私有
代码
#include<iostream>
using namespace std;
//多继承私有派生
class grandfather
{
public:
grandfather()
{
this->Gfa = 10;
this->Gfb = 20;
this->Gfc = 30;
}
public:
int Gfa;
protected:
int Gfb;
private:
int Gfc;
};
class father :private grandfather
{
public:
void test()
{
cout << Gfa << endl;//私有继承后公有变私有
cout << Gfb << endl;//保护继承后保护变私有
//cout << Gfc << endl;私有不能访问
}
};
class son :public father
{
public:
void test()
{
//cout << Gfa << endl;//私有继承后公有不可访问,继承后公有变私有
//cout << Gfb << endl;//保护继承保护不可访问,继承后保护变私有
//cout << Gfc << endl;私有不能访问
}
};
int main()
{
father F;
F.test();
//cout<<S.Gfa << endl;//类外只能访问公有属性
//cout << F.Gfa << endl;
system("pause");
}
保护派生
基类属性 派生类权限
私有 不可访问
保护 保护
公有 保护
代码案例:
#include<iostream>
using namespace std;
//保护继承
class grandfather
{
public:
grandfather()
{
this->Gfa = 10;
this->Gfb = 20;
this->Gfc = 30;
}
public:
int Gfa;
protected:
int Gfb;
private:
int Gfc;
};
class father :protected grandfather
{
public:
void test()
{
cout << Gfa << endl;//保护继承后公有变保护,相当与代码变成class father{protected:Gfa;Gfb;};
cout << Gfb << endl;//保护继承后保护还是保护
//cout << Gfc << endl;私有不能访问
}
};
class son :public father
{
public:
void test()
{
cout << Gfa << endl;//保护继承后公有变保护,保护子类可以访问
cout << Gfb << endl;//保护继承后保护还是保护,保护子类可以访问
//cout << Gfc << endl;私有不能访问
}
};
int main()
{
father F;
son S;
F.test();
//cout << F.Gfa << endl;//权限变为保护的所以不能访问
//cout << S.Gfa << endl;//权限变为保护的所以不能访问
system("pause");
}
继承中的构造和析构的调用顺序:创建子类对象的时候先构造父类在构造子类,析构相反
#include<iostream>
using namespace std;
class father
{
public:
father()
{
cout << "父类的构造函数" << endl;
}
~father()
{
cout << "父类的析构函数" << endl;
}
};
class son:public father
{
public:
son()
{
cout << "子类的构造函数" << endl;
}
~son()
{
cout << "子类的析构函数" << endl;
}
};
void test()
{
son S;
}
int main()
{
test();
system("pause");
}
结果如下:
继承中成员同名结果:子类和基类成员同名,在子类中,从基类继承过来的同名成员都会隐藏,子类外部不可访问,要想调用,必须显示指定调用
继承中重新定义或者重名一个基类的重载函数结果是基类所有的重载版本全部隐藏,子类外部不可访问,要想调用,必须显示指定调用
代码如下
#include<iostream>
using namespace std;
class father
{
public:
father()
{
val = 10;
}
void test()
{
cout << val << endl;
}
void fun()
{
cout << "father::void fun()" << endl;
}
void fun(int n)
{
val = n;
cout << "father::void fun(int n)" << endl;
}
int val;
};
class son:public father
{
public:
son()
{
val = 100;
}
void test()
{
cout << val << endl;
cout << father::val << endl;//子类和基类成员同名时,子类同样继承基类的同名成员,只是同名成员隐藏而已,想要访问必须显示调用
}
/*
void fun(int a, int b)
{
cout << "son::void fun(int a,int b)" << endl;
}
*/
int fun()//在子类中任何时候定义基类的重载函数,基类的重载版本的函数全部隐藏,外部能访问,调用只能显示调用;
{
cout << "son::int fun(int a, int b)" << endl;
//father::fun(5);
//father::fun();
return 0;
}
int val;
};
int main()
{
son S;
cout << S.val << endl;//同名时,默认调用的是子类的成员,就近原则
S.fun();
system("pause");
}
结果如下: