实验一:派生类和派生类对象有什么区别?
实验二:多继承二义性问题
实验三:公有继承/保护继承/私有继承的区别
//将iostream.h的内容复制过来
#include <iostream>
//std意为standard
using namespace std;
// 结论:
// 实验一 :对象永远只能访问public,即使是自己的protected和private也不能访问;而直接继承的类总能访问基类的public和protected和自身所有成员
// 实验二 :二义性不解决会报错,必须指定成员所属才能运行
// 实验三 :公有继承保持继承到的成员本来的权限继承下来
// 保护继承会将继承到的成员变为protected继承下来
// 私有继承会将继承到的成员变为private继承下来
//————Class Test1————
class Test1
{
public:
//声明构造函数
Test1();
//声明析构函数
~Test1();
string pub1 = "pub1\n";
int a = 1;
void AA()
{
cout << "Function AA1\n";
}
protected:
string prot1 = "prot1\n";
private:
string priv1 = "priv1\n";
};
//实现构造函数
Test1::Test1()
{
cout << "Create an instance of Test1!\n";
}
//实现析构函数
Test1::~Test1()
{
}
//————Class Test3————
class Test3
{
public:
//声明构造函数
Test3();
//声明析构函数
~Test3();
string pub3 = "pub3\n";
int a = 3;
void AA()
{
cout << "Function AA3\n";
}
protected:
string prot3 = "prot3\n";
private:
string priv3 = "priv3\n";
};
//实现构造函数
Test3::Test3()
{
cout << "Create an instance of Test3!\n";
}
//实现析构函数
Test3::~Test3()
{
}
//————Class Test2————
// 公开继承Test1 Test3
// 多继承Test1 Test3,1和3中存在二义性变量a和方法AA
class Test2 : public Test1, public Test3
{
public:
//内部实现构造函数
Test2()
{
cout << "Create an instance of Test2!\n";
}
//内部实现析构函数
~Test2()
{
}
string pub2 = "pub2\n";
//可以访问基类public成员
string pub12 = pub1 + pub2;
void Print()
{
cout << pub12;
cout << prot12;
}
protected:
string prot2 = "prot2\n";
//可以访问基类protected成员
string prot12 = prot1 + prot2;
private:
string priv2 = "priv2\n";
//不可以访问基类private成员
//string priv12 = priv1 + priv2;
};
//————Class Test4————
//Test4 公有继承 Test1
//Test4 保护继承 Test1
//Test4 私有继承 Test1
class Test4 : protected Test1
{
public:
//声明构造函数
Test4();
//声明析构函数
~Test4();
string pub4 = "pub4\n";
protected:
string prot4 = "prot4\n";
private:
string priv4 = "priv4\n";
};
//实现构造函数
Test4::Test4()
{
cout << "Create an instance of Test4!\n";
}
//实现析构函数
Test4::~Test4()
{
}
//————Class Test5————
//Test5 任意继承 Test4
class Test5 : public Test4
{
public:
//声明构造函数
Test5();
//声明析构函数
~Test5();
string pub5 = "pub5\n";
//4公有继承1时,5可以访问1的public
//4保护继承1时,5可以访问1的public,不过加上了*号(*号表示保护?)
//4私有继承1时,5不可以访问1的public,因为4将1的public作为private继承下来,而5根本继承不到私有成员
//string pub15 = pub1 + pub5;
protected:
string prot5 = "prot5\n";
//4公有继承1时,5可以访问1的protected
//4保护继承1时,5可以访问1的protected
//4私有继承1时,5不可以访问1的protected,因为4将1的protected作为private继承下来,而5根本继承不到私有成员
//string prot15 = prot1 + prot5;
private:
string priv5 = "priv5\n";
//5不可以访问1的private
//string priv15 = priv1 + priv5;
};
//实现构造函数
Test5::Test5()
{
cout << "Create an instance of Test5!\n";
}
//实现析构函数
Test5::~Test5()
{
}
int main()
{
//实验一:派生类和派生类对象有什么区别?
//调用Test2构造函数,生成一个对象
Test2 test2 = Test2::Test2();
// Test2的类可以访问基类的public和protect
// Test2对象中只能访问到public变量,不能访问基类和自身的protected和private成员
// 访问权限对派生类和派生类的对象影响不同,类可以访问基类protected和public以及自己的所有权限变量
// 但实例化成对象后只能访问基类和自己的public,甚至连自己的protected和private都不能访问
cout << "打印Test2对象中的所有成员变量:\n";
cout << test2.pub1;
cout << test2.pub2;
//cout << test2.prot2;
//cout << test2.priv2;
cout << "______\n";
cout << test2.pub12;
//实验二:多继承二义性问题
//存在二义性时会报错,禁止运行,必须指定成员变量和方法的所属
cout << "访问Test2中的a以及调用AA()\n";
cout << test2.Test3::a;
cout << "\n";
test2.Test1::AA();
//实验三:公有继承/保护继承/私有继承的区别
//当Test4公有继承Test1时,外部能访问1中的public
//当Test4保护继承Test1时,外部不能访问1中的public,因为此时4将1中的public变为protected继承下来,而protected对派生类对象时禁止访问的
//当Test4私有继承Test1时,外部不能访问1中的public,因为此时4将1中的public变为private继承下来,而private对派生类对象时禁止访问的
cout << "\n";
cout << "实验三:公有继承/保护继承/私有继承的区别\n";
Test4 test4 = Test4();
//cout << test4.pub1;
//当Test4公有继承Test1时,外部能通过5的对象访问1中的public,因为此时5中继承到的pub1也是public
//当Test4保护继承Test1时,外部不能通过5的对象访问1中的public,因为此时4将1中的public变为protected继承下来,5继承4中的protected,而protected对派生类对象时禁止访问的
//当Test4私有继承Test1时,外部不能通过5的对象访问1中的public,因为此时4将1中的public变为private继承下来,而5根本不能继承到4中的private
Test5 test5 = Test5();
//cout << test5.pub1;