1、继承限定词
#include <iostream>
using namespace std;
class stux//基类 父类
{
private:
void funx()
{
cout << "funx" << endl;
}
protected:
void funx1()
{
cout << "funx1" << endl;
}
public:
void funx2()
{
cout << "funx2" << endl;
}
};
class stu:public stux//派生类 子类,在父类中如何,则在子类中就如何
{
public:
int a;
void fun()
{
cout<<"fun" << endl;
}
};
class stu1 :protected stux//派生类 子类,将父类中继承过来的public转为protected,其他不变。
{//注,父类的成员权限不变,只改变子类中的权限。
public:
int a;
void fun()
{
cout << "fun" << endl;
}
};
class stu1 :private stux//派生类 子类,将父类中继承过来的public、protected转为private。
{//注,父类的成员权限不变,只改变子类中的权限。
public:
int a;
void fun()
{
cout << "fun" << endl;
}
};
//class stu1:public stux
//{
//public:
// void fun1()
// {
// cout << "fun1" << endl;
// }
//};
//class stu2:public stux
//{
//public:
// void fun2()
// {
// cout << "fun2" << endl;
// }
//};
int main()
{
stu db;
db.funx();
/*stu2 db1;
db1.funx();*/
system("pause");
return 0;
}
2、继承构造函数的调用顺序
#include <iostream>
using namespace std;
class stux//基类 父类
{
public:
stux()
{
cout << "stux" << endl;
}
};
class stu:public stux
{
public:
stu()
{
cout<<"stu" << endl;
}
};
class stu1 :public stu
{
public:
stu1()
{
cout << "stu1" << endl;
}
};
int main()
{
//stu db;
stu1 db1;//继承构造的调用顺序是先调用父类,再调用子类,依次往下调用。
system("pause");
return 0;
}