【c++程序】模拟抽象类

#include<iostream>
using namespace std;
class Human
{
public:
	Human(){cout<<"构造Human"<<endl;}
	virtual void smart(){}
	virtual void beautiful(){}
	virtual ~Human(){cout<<"析构Human"<<endl;}
};
class father:virtual public Human 
{
public:
	father(){cout<<"构造father"<<endl;}
    virtual void smart(){cout<<"父亲很聪明"<<endl;}
	virtual ~father(){cout<<"析构父亲"<<endl;}
};
class mother:virtual public Human
{
public:
	mother(){cout<<"构造mother"<<endl;}
	virtual void beautiful(){cout<<"mother is very beautiful!"<<endl;}
	virtual ~mother(){cout<<"析构mother"<<endl;}
};
class son:public father,public mother
{
public:
	son(){cout<<"构造son"<<endl;}
	virtual void smart(){cout<<"儿子也很聪明"<<endl;}
	virtual void beautiful(){cout<<"儿子也很帅"<<endl;}
	~son (){cout<<"析构儿子"<<endl;}
};
int main()
{
	Human *p;
	int choice=0;
	while(1)
	{
		bool quit=false;
		cout<<"0-退出1-父亲2-儿子3-母亲"<<endl;
		cin>>choice;
		switch(choice)
		{
		case 0:quit=true;
			break;
		case 1:p=new father;
			p->beautiful();
			delete p;
			break;
		case 2:p=new son;
			p->beautiful();
			p->smart();
			delete p;
			break;
		case 3:p=new mother;
			p->beautiful();
			delete p;
			break;
		default:cout<<"请输入0-3"<<endl;
			break;
		}
		if(quit)
		{
			break;
		}
	}
	cout<<"END!!";
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实验二 C#程序设计练习 一、实验目的 1.掌握C#语言的基本语法、控制语句及异常处理。 2.掌握C#类的基本使用方法以及C#语言面向对象的基本特性。 二、实验内容 1.编写一个函数,用于计算1!+2!+3!+4!+5!,在控制台或页面输出运行结果。 2.在控制台或页面输出九九乘法表。 3.输入10个以内的整数,输出该组整数的降序排列,要求采用数组实现。 4.计算两个数的商,在控制台或页面输出结果,要求包含异常处理。 5.定义一个汽车类,该类具有重量和速度属性;再定义一个跑车类,该类继承汽车类的属性,并拥有自己的颜色属性;然后声明一个汽车类的对象和一个跑车类的对象,并把它们的属性输出到控制台上。 6.假设某动物园管理员每天需要给他所负责饲养的狮子、猴子和鸽子喂食。请用一个程序来模拟他喂食的过程。 要求: (1)饲养员喂食时,不同动物执行不同的吃的功能,例如狮子吃肉、猴子吃香蕉、鸽子吃大米等。 (2)饲养员喂动物时,不能使用判断语句判断动物类型。 (3)使用虚方法或抽象方法实现喂养不同动物的多态,不能使用方法重载。 提示:需要建一个动物类,动物类有一个虚的或抽象的吃方法,动物类下面有几个子类,不同的子类重写父类的吃方法。饲养员类提供喂食方法。然后,在Main方法中一一调用吃的方法。 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Add2._1 { class Program { static void Main(string[] args) { int sum = 0; for (int i = 1; i < 6; i++) { int tmp = 1; for (int j = 1; j <= i; j++) { tmp = tmp * j; } sum += tmp; } Console.WriteLine("1!+2!+3!+4!+5!={0}\r\n", sum.ToString()); } } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值