子类在类外只能访问 公有继承来的 父类的公有成员变量和公有成员函数
子类在类内能够访问 继承过来后属性为公有或保护的 父类的成员变量和成员函数
提示:可以利用下方文件目录跳转到三种继承的具体实现代码
文章目录
结论
子类在类外只能访问 公有继承来的 父类的公有成员变量和公有成员函数
子类在类内能够访问 继承过来后属性为公有或保护的 父类的成员变量和成员函数
一、公有继承
1.子类在类内访问父类的成员变量和成员函数
#include <iostream>
using namespace std;
class A {
public:
string variablePublic = "父类公有成员变量";
void function_Public();
protected:
string variableProtected = "父类保护成员变量";
void function_Protected();
private:
string variablePrivate = "父类私有成员变量";
void function_Private();
};
void A::function_Public() {
cout << "父类公有成员函数" << endl;
}
void A::function_Protected() {
cout << "父类保护成员函数" << endl;
}
void A::function_Private() {
cout << "父类私有成员函数" << endl;
}
class B:public A{
public:
void show();
};
void B::show() {
cout << variablePublic << endl;
cout << variableProtected << endl;
//cout << variablePrivate << endl;
function_Public();
function_Protected();
//function_Private();
}
int main() {
B b;
b.show();
return 0;
}
2.子类在类外访问父类的成员变量和成员函数
#include <iostream>
using namespace std;
class A {
public:
string variablePublic = "父类公有成员变量";
void function_Public();
protected:
string variableProtected = "父类保护成员变量";
void function_Protected();
private:
string variablePrivate = "父类私有成员变量";
void function_Private();
};
void A::function_Public() {
cout << "父类公有成员函数" << endl;
}
void A::function_Protected() {
cout << "父类保护成员函数" << endl;
}
void A::function_Private() {
cout << "父类私有成员函数" << endl;
}
class B:public A{
public:
void show();
};
void B::show() {
cout << variablePublic << endl;
cout << variableProtected << endl;
//cout << variablePrivate << endl;
function_Public();
function_Protected();
//function_Private();
}
int main() {
B b;
cout << b.variablePublic<<endl;
//cout << b.variableProtected << endl;
//cout << b.variablePrivate << endl;
b.function_Public();
//b.function_Protected();
//b.function_Private();
return 0;
}
示例:pandas 是基于NumPy 的一种工具,该工具是为了解决数据分析任务而创建的。
二、保护继承
1.子类在类内访问父类的成员变量和成员函数
#include <iostream>
using namespace std;
class A {
public:
string variablePublic = "父类公有成员变量";
void function_Public();
protected:
string variableProtected = "父类保护成员变量";
void function_Protected();
private:
string variablePrivate = "父类私有成员变量";
void function_Private();
};
void A::function_Public() {
cout << "父类公有成员函数" << endl;
}
void A::function_Protected() {
cout << "父类保护成员函数" << endl;
}
void A::function_Private() {
cout << "父类私有成员函数" << endl;
}
class B:protected A{
public:
void show();
};
void B::show() {
cout << variablePublic << endl;
cout << variableProtected << endl;
//cout << variablePrivate << endl; //不可访问
function_Public();
function_Protected();
//function_Private(); //不可访问
}
int main() {
B b;
b.show();
return 0;
}
2.子类在类外访问父类的成员变量和成员函数
#include <iostream>
using namespace std;
class A {
public:
string variablePublic = "父类公有成员变量";
void function_Public();
protected:
string variableProtected = "父类保护成员变量";
void function_Protected();
private:
string variablePrivate = "父类私有成员变量";
void function_Private();
};
void A::function_Public() {
cout << "父类公有成员函数" << endl;
}
void A::function_Protected() {
cout << "父类保护成员函数" << endl;
}
void A::function_Private() {
cout << "父类私有成员函数" << endl;
}
class B :protected A {
public:
void show();
};
void B::show() {
cout << variablePublic << endl;
cout << variableProtected << endl;
//cout << variablePrivate << endl;
function_Public();
function_Protected();
//function_Private();
}
int main() {
B b;
//cout << b.variablePublic << endl; //不可访问
//cout << b.variableProtected << endl; //不可访问
//cout << b.variablePrivate << endl; //不可访问
//b.function_Public(); //不可访问
//b.function_Protected(); //不可访问
//b.function_Private(); //不可访问
return 0;//不可访问
}
没有输出
三、私有继承
1.子类在类内访问父类的成员变量和成员函数
#include <iostream>
using namespace std;
class A {
public:
string variablePublic = "父类公有成员变量";
void function_Public();
protected:
string variableProtected = "父类保护成员变量";
void function_Protected();
private:
string variablePrivate = "父类私有成员变量";
void function_Private();
};
void A::function_Public() {
cout << "父类公有成员函数" << endl;
}
void A::function_Protected() {
cout << "父类保护成员函数" << endl;
}
void A::function_Private() {
cout << "父类私有成员函数" << endl;
}
class B :private A {
public:
void show();
};
void B::show() {
cout << variablePublic << endl;
cout << variableProtected << endl;
//cout << variablePrivate << endl; //不可访问
function_Public();
function_Protected();
//function_Private(); //不可访问
}
int main() {
B b;
b.show();
return 0;//不可访问
}
2.子类在类外访问父类的成员变量和成员函数
#include <iostream>
using namespace std;
class A {
public:
string variablePublic = "父类公有成员变量";
void function_Public();
protected:
string variableProtected = "父类保护成员变量";
void function_Protected();
private:
string variablePrivate = "父类私有成员变量";
void function_Private();
};
void A::function_Public() {
cout << "父类公有成员函数" << endl;
}
void A::function_Protected() {
cout << "父类保护成员函数" << endl;
}
void A::function_Private() {
cout << "父类私有成员函数" << endl;
}
class B :private A {
public:
void show();
};
void B::show() {
cout << variablePublic << endl;
cout << variableProtected << endl;
//cout << variablePrivate << endl; //不可访问
function_Public();
function_Protected();
//function_Private(); //不可访问
}
int main() {
B b;
//cout << b.variablepublic << endl; //不可访问
//cout << b.variableprotected << endl; //不可访问
//cout << b.variableprivate << endl; //不可访问
//b.function_public(); //不可访问
//b.function_protected(); //不可访问
//b.function_private(); //不可访问
return 0;//不可访问
}
没有输出