C++-派生类的构造函数和析构函数

构造和析构的顺序: 

 构造函数:

#include <iostream>

using namespace std;
class Base1
{
public:
	Base1(int i)
	{
		b1 = i;
		cout << "Base1的构造函数被调用" << endl;
	}
	void Print() const
	{
		cout << b1 << endl;
	}
private:
	int b1;
};

class Base2
{
public:
	Base2(int i)
	{
		b2 = i;
		cout << "Base2的构造函数被调用" << endl;
	}
	void Print()
	{
		cout << b2 << endl;
	}
private:
	int b2;
};

class Base3
{
public:
	Base3()
	{
		b3 = 0;
		cout << "Base3缺省的构造函数被调用" << endl;
	}
	void Print()
	{
		cout << b3 << endl;
	}
private:
	int b3;
};
class Member
{
public:
	Member(int i)
	{
		m = i;
		cout << "Member的构造函数被调用" << endl;
	}
	int GetM()
	{
		return m;
	}
private:
	int m;
};
class Derived : public Base2, public Base1,
	public Base3
{
public:
	Derived(int i, int j, int k, int l);
	void Print();
private:
	int d;
	Member mem;
};
Derived::Derived(int i, int j, int k, int l) :Base1(i), Base2(j), mem(k)
{
	d = l;
	cout << "Derived的构造函数被调用" << endl;
}
void Derived::Print()
{
	Base1::Print();
	Base2::Print();
	Base3::Print();
	cout << mem.GetM() << endl;
	cout << d << endl;
}
int main()
{
	Derived obj(1, 2, 3, 4);
	obj.Print();
	return 0;
}

析构

#include <iostream>

using namespace std;
class Base
{
public:
	Base()
	{
		b1 = b2 = 0;
	}
	Base(int i, int j);
	~Base();
	void Print() const
	{
		cout << b1 << ", " << b2 << ", " ;
	}
private:
	int b1, b2;
};
Base::Base(int i, int j)
{
	b1 = i;
	b2 = j;
	cout << "Base的构造函数被调用:" << b1 << ", " << b2 << endl;
}
Base::~Base()
{
	cout << "Base的析构函数被调用:" << b1 << ", " << b2 << endl;
}

class Derived : public Base
{
public:
	Derived()
	{
		d = 0;
	}
	Derived(int i, int j, int k);
	~Derived();
	void Print();
private:
	int d;
};
Derived::Derived(int i, int j, int k) :Base(i, j), d(k)
{
	cout << "Derived的构造函数被调用" << d << endl;
}
Derived::~Derived()
{
	cout << "Derived的析构函数被调用:" << d << endl;
}
void Derived::Print()
{
	Base::Print();
	cout << d << endl;
}
int main()
{
	Derived objD1(1, 2, 3), objD2(-4, -5, -6);

	objD1.Print();
	objD2.Print();
	return 0;
}

一个例子: 

#include <iostream>

using namespace std;

enum BREED { GOLDEN, CAIRN, DANDIE, SHETLAND, LAB };	//狗的品种

class Mammal
{
public:
	Mammal();
	~Mammal();

	//存取器成员函数
	int GetAge() const { return itsAge; }
	void SetAgee(int age) { itsAge = age; }
	int GetWeight() const { return itsWeight; }
	void SetWeight(int weight) { itsWeight = weight; }

	void Speak() const { cout << "Mammal的声音!\n"; }
	void Sleep() const { cout << "shhh, I'm sleeping.\n"; }
protected:
	int itsAge;
	int itsWeight;
};

class Dog :public Mammal
{
public:
	Dog();
	~Dog();

	BREED GetBreed() const { return itsBreed; }
	void SetBreed(BREED breed) { itsBreed = breed; }
	void WagTail() const { cout << "Tail wagging\n"; }
	void BegForFood() const { cout << "Begging for food...\n"; }
private:
	BREED itsBreed;
};
Mammal::Mammal() :itsAge(3), itsWeight(5)
{
	cout << "Mammal的构造函数被调用..." << endl;
}
Mammal::~Mammal()
{
	cout << "Mammal的析构函数被调用..." << endl;
}
Dog::Dog() : itsBreed(GOLDEN)
{
	cout << "Dog的构造函数被调用..." << endl;
}
Dog::~Dog()
{
	cout << "Dog的析构函数被调用..." << endl;
}
int main()
{
	Dog Fido;
	Fido.Speak();
	Fido.WagTail();
	cout << "Fido is " << Fido.GetAge() << " years old" << endl;
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值