⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 c50.cpp

📁 《C++程序设计习题及解答》配套代码VC版
💻 CPP
字号:
// c50.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream.h>
#include <iomanip.h>
#include <string.h>

// --------------------------------------------------------------------------
#ifndef FALSE
#define FALSE 0
#endif

#ifndef TRUE
#define TRUE 1
#endif

// --------------------------------------------------------------------------
// class Animal
class Animal
{
public:
	Animal( const char* s );
	const char* GetName();
	virtual void ShowInfo();
private:
	char name[30]; 
};

// --------------------------------------------------------------------------
// class Mammal
class Mammal : public Animal
{
public:
	Mammal( const char* s, int nc );
	int NumOffspring();
	virtual void ShowInfo();
private:
	int offspring;
};

// --------------------------------------------------------------------------
// class Bird
class Bird : public Animal 
{
public:
	Bird( const char* s, int ne, int nests );
	int GetEggs();
	const char* BuildNest();
	virtual void ShowInfo();
private:
	int eggs;
	int nesting;
};

// --------------------------------------------------------------------------
void ShowInfo( Animal& );

// --------------------------------------------------------------------------
int main(int argc, char* argv[])
{
	Mammal homoSapiens( "Homo Sapiens", 1 );
	Mammal gopher( "Gophper", 9 );
	Mammal armadillo( "Armadillo", 4 );
	Mammal houseMouse( "House Mouse", 12 );

	Bird woodDuck( "Wood Duck", 15, FALSE );
	Bird sandhillCrane( "Sandhill Crane", 2, TRUE );
	Bird loon( "Loon", 3, TRUE );

	cout << "\nMammals:\n";
	ShowInfo( homoSapiens );
	ShowInfo( gopher );
	ShowInfo( armadillo );
	ShowInfo( houseMouse );

	cout << "\nBirds:\n";
	ShowInfo( woodDuck );
	ShowInfo( sandhillCrane );
	ShowInfo( loon );

	return 0;
}

// --------------------------------------------------------------------------
// function ShowInfo's implement
void ShowInfo( Animal& ani )
{
	ani.ShowInfo();
}

// --------------------------------------------------------------------------
// class Animal's implement
inline const char* Animal::GetName()
{
	return name;
}

Animal::Animal( const char* s )
{
	strncpy( name, s, 29 );
}

void Animal::ShowInfo()
{
	cout << setw(20) << "Name" << setw(20) << GetName() << endl;
}

// --------------------------------------------------------------------------
// class Mammal's implement
inline int Mammal::NumOffspring()
{
	return offspring;
}

Mammal::Mammal( const char* s, int nc ) : Animal( s )
{
	offspring = nc;
}

void Mammal::ShowInfo()
{
	cout << setw(20) << "Name" << setw(20) << GetName() << endl;
	cout << setw(20) << "Avg offspring" << setw(20) << NumOffspring() << endl;
}

// --------------------------------------------------------------------------
// class Bird's implement
inline int Bird::GetEggs()
{
	return eggs;
}

Bird::Bird( const char* s, int ne, int nests ) : Animal( s )
{
	eggs = ne;
}

const char* Bird::BuildNest()
{
	if ( nesting )
		return "True";
	else
		return "False";
}

void Bird::ShowInfo()
{
	cout << setw(20) << "Name" << setw(20) << GetName() << endl;
	cout << setw(20) << "Avg no. eggs" << setw(20) << GetEggs() << endl;
	cout << setw(20) << "Builds a nest" << setw(20) << BuildNest() << endl;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -