c++多继承和多父类同名成员的访问

阅读 142

2022-02-25

class Father1 {//父类
public:
	Father1() {
		a = 100;
	}
	int a;
};

class Father2 {//父类

public:
	int a;
	Father2() {
		a = 200;
	}
};

class Son :public Father1 , public Father2{//子类多继承 calss A: 权限 类名 , 权限 类名 
public:
	Son() {
		a = 300;
	}
	
	int a;
};


void test() {//通过对象调用
	Son son;
	cout << son.a << endl;
	cout << son.Father1::a << endl;//加上对应的作用域进行访问
	cout << son.Father2::a << endl;
	
}

int main() {
	test();
}

/*结果:
300
100
200
*/

精彩评论(0)

0 0 举报