多繼承語法

多繼承語法示例

#include <iostream>
using namespace std;

class Base1
{
public:
	Base1(int b1)
	{
		this->b1 = b1;
	}
	void printB1()
	{
		cout<<"b1:"<<b1<<endl;
	}
protected:
private:
	int  b1;
};

class Base2
{
public:
	Base2(int b2)
	{
		this->b2 = b2;
	}
	void printB2()
	{
		cout<<"b2:"<<b2<<endl;
	}
protected:
private:
	int  b2;
};

class B : public Base1, public Base2
{
public:
	B(int b1, int b2, int c): Base1(b1), Base2(b2)
	{
		this->c = c;
	}
	void printC()
	{
		cout<<"c:"<<c<<endl;
	}
protected:
private:
	int c;
};

int main()
{
	B b1(1, 2, 3);
	b1.printC();
	b1.printB1();
	b1.printB2();
	cout<<"hello..."<<endl;
	return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章