用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - c++代码库

c++ 调用多个构造函数

2012-11-26 作者: 程序猿style举报

[c++]代码库

#include <iostream>
using namespace std;

typedef int HANDS;
enum COLOR {
	Red, Green, Blue, Yellow, White, Black, Brown
};

class Horse {
public:
	Horse(COLOR color, HANDS height);
	virtual ~Horse() {
		cout << "Horse destructor...\n";
	}
	virtual void Whinny() const {
		cout << "Whinny!... ";
	}
	virtual HANDS GetHeight() const {
		return itsHeight;
	}
	virtual COLOR GetColor() const {
		return itsColor;
	}
private:
	HANDS itsHeight;
	COLOR itsColor;
};

Horse::Horse(COLOR color, HANDS height) :
		itsColor(color), itsHeight(height) {
	cout << "Horse constructor...\n";
}

class Bird {
public:
	Bird(COLOR color, bool migrates);
	virtual ~Bird() {
		cout << "Bird destructor...\n";
	}
	virtual void Chirp() const {
		cout << "Chirp... ";
	}
	virtual void Fly() const {
		cout << "I can fly! I can fly! I can fly! ";
	}
	virtual COLOR GetColor() const {
		return itsColor;
	}
	virtual bool GetMigration() const {
		return itsMigration;
	}

private:
	COLOR itsColor;
	bool itsMigration;
};

Bird::Bird(COLOR color, bool migrates) :
		itsColor(color), itsMigration(migrates) {
	cout << "Bird constructor...\n";
}

class Pegasus: public Horse, public Bird {
public:
	void Chirp() const {
		Whinny();
	}
	Pegasus(COLOR, HANDS, bool, long);
	~Pegasus() {
		cout << "Pegasus destructor...\n";
	}
	virtual long GetNumberBelievers() const {
		return itsNumberBelievers;
	}

private:
	long itsNumberBelievers;
};

Pegasus::Pegasus(COLOR aColor, HANDS height, bool migrates, long NumBelieve) :
		Horse(aColor, height), Bird(aColor, migrates), itsNumberBelievers(
				NumBelieve) {
	cout << "Pegasus constructor...\n";
}

int main() {
	Pegasus *pPeg = new Pegasus(Red, 5, true, 10);
	pPeg->Fly();
	pPeg->Whinny();
	cout << "\nYour Pegasus is " << pPeg->GetHeight();
	cout << " hands tall and ";
	if (pPeg->GetMigration())
		cout << "it does migrate.";
	else
		cout << "it does not migrate.";
	cout << "\nA total of " << pPeg->GetNumberBelievers();
	cout << " people believe it exists." << endl;
	delete pPeg;
	return 0;
}


网友评论    (发表评论)

共1 条评论 1/1页

发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...