C++ 继承

#include<iostream>
using namespace std;

class Base {

    int a;

public:

    Base() 
    {
        cout << "嘿嘿" << endl;
    }
   
};

class BBB {
public:
    BBB()
    {
        cout << "哈哈"<<endl;
    }
};

class Derived :public BBB ,public Base{
    int b;
public:
    Derived() { }

};

int main() {

    Derived myd1;


    return 0;

}

输出结果:

哈哈
嘿嘿

换一下位置:

class Derived : public Base,public BBB

输出:

嘿嘿

哈哈

#include<iostream>
using namespace std;

class Base {
public:

	Base() 
	{ 
		cout << 'A';
	}

	Base(char c) 
	{
		cout << c;
	}

};

class Derived :public Base {

public:

	Derived(char c) //输出AB
//如果改为Derived(char c):Base(c) 输出BB
	{
		cout << c; 
	}

};

int main() {

	Derived d('B');

	return 0;

}

输出:AB

如果改为:Derived(char c):Base(c) 

输出:BB

#include<iostream>
using namespace std;

class b1 {

public:

	b1(int x) { cout << x; }

	~b1()
	{ 
		cout << 'b';
	}

};

class b2 {

public:

	b2(int x) { cout << x; }

	~b2()
	{ 
		cout << 'd';
	}

};

class derived :public b2, public b1 {

public:

	derived(int x, int y) :b1(y), b2(x)

	{
		cout << 'e';
	}

	~derived() 
	{ 
		cout << 'f'; 
	}

};

int main() {

	derived obj(5, 7);

	return 0;

}

输出

57efbd

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值