实验七 多态性

7-1 抽象基类Shape派生3个类

分数 25

全屏浏览

作者 沙金

单位 石家庄铁道大学

声明抽象基类Shape,由它派生出三个类,圆形Circle,矩形Rectangle,三角形Triangle,用一个函数输出三个面积。

输入格式:

在一行中依次输入5个数,圆的半径,长方形的高和宽,三角形的高和底,中间用空格分隔

输出格式:

圆的面积,长方形的面积,三角形的面积,小数点后保留2位有效数字,每个面积占一行。

输入样例:

在这里给出一组输入。例如:

3 3 4 3 4

输出样例:

在这里给出相应的输出。例如:

28.27
12.00
6.00
#include <iostream>
#include <iomanip>
using namespace std;
class Shape
{
public:
	double Area;
 
public:
	void Show()
	{
		cout << fixed << setprecision(2) << Area << endl;
	}
};
class Circle : public Shape
{
private:
	double r;
 
public:
	Circle(double tr)
	{
		r = tr;
		Area = 3.1415926 * r * r;
	}
};
class Rectangle : public Shape
{
private:
	double w, h;
 
public:
	Rectangle(double tw, double th)
	{
		w = tw;
		h = th;
		Area = w * h;
	}
};
class Triangle : public Shape
{
private:
	double d, h;
 
public:
	Triangle(double td, double th)
	{
		d = td;
		h = th;
		Area = d * h / 2;
	}
};
int main()
{
	double a, b, c, d, e;
	cin >> a >> b >> c >> d >> e;
	Circle s1(a);
	s1.Show();
	Rectangle s2(b, c);
	s2.Show();
	Triangle s3(d, e);
	s3.Show();
	return 0;
}

 

7-2 动物爱吃什么

分数 25

全屏浏览

作者 杨雪华

单位 沈阳师范大学

1.设计一个基类动物类(animal),包含private数据成员:动物编号int num;和动物名称string name;public成员函数:getnum( )用于获取其编号, getname( )用于获取其名称和一个纯虚函数eat( ),以及构造函数。

2.由animal类派生出狗类Dog和猫类Cat,每个类中均有自己的构造函数,根据输出结果设计这两个类并在主函数中完成设计类的输出测试

3.要求在主函数中必须使用基类指针调用虚函数eat( ).

输入格式:

输入两行,每一行分别是动物编号num和动物名称name,中间空格分隔。

输出格式:

输出两行,每一行显示动物编号num、动物名称name和爱吃的食物。

输入样例:

在这里给出一组输入。例如:

1 金毛
2 波斯

输出样例:

在这里给出相应的输出。例如:

1号金毛啃骨头
2号波斯吃小鱼

#include<iostream>
using namespace std;
class animal
{
private:
    string name; 
    int num;
public:
    animal(string name,int num)
    {
        this->name = name;
        this->num = num;
    }
    int getnum(){
        return this->num;
    }
    string getname() { return this->name; }
    virtual void eat() = 0;
};
class Cat:public animal
{
public:
    Cat(string name ,int num):animal(name,num){}
    void eat()//1号金毛啃骨头
    {
        cout << this->getnum() << "号" << this->getname() << "吃小鱼" << endl;
    }
};
class Dog:public animal
{
public:
    Dog(string name,int num):animal(name,num){}
    void eat()
    {
        cout << this->getnum() << "号" << this->getname() << "啃骨头" << endl;
    }
};
int main()
{
    string name; int num;
    cin >> num>> name;
    Dog D(name, num);
    animal& a = D;
    a.eat();
    cin >> num >> name;
    Cat C(name, num);
    animal* ap = &C;
    ap->eat();
    system("pause");
}

 

7-3 车辆计费

分数 50

全屏浏览

作者 Yan Guo

单位 浙江大学

现在要开发一个系统,管理对多种汽车的收费工作。 给出下面的一个基类框架
class Vehicle
{ protected:
string NO;//编号
public:
virtual void display()=0;//输出应收费用
}
以Vehicle为基类,构建出Car、Truck和Bus三个类。
Car的收费公式为: 载客数8+重量2
Truck的收费公式为:重量5
Bus的收费公式为: 载客数
3
生成上述类并编写主函数,要求主函数中有一个基类Vehicle指针数组,数组元素不超过10个。
Vehicle *pv[10];
主函数根据输入的信息,相应建立Car,Truck或Bus类对象,对于Car给出载客数和重量,Truck给出重量,Bus给出载客数。假设载客数和重量均为整数
输入格式:每个测试用例占一行,每行给出汽车的基本信息,每一个为当前汽车的类型1为car,2为Truck,3为Bus。接下来为它的编号,接下来Car是载客数和重量,Truck给出重量,Bus给出载客数。最后一行为0,表示输入的结束。 要求输出各车的编号和收费。
提示:应用虚函数实现多态

输入格式:

每个测试用例占一行,每行给出汽车的基本信息,每一个为当前汽车的类型1为car,2为Truck,3为Bus。接下来为它的编号,接下来Car是载客数和重量,Truck给出重量,Bus给出载客数。最后一行为0,表示输入的结束。

输出格式:

输出各车的编号和收费。

输入样例:

在这里给出一组输入。例如:

1 001 20 5
2 002 20
3 003 25
0

输出样例:

在这里给出相应的输出。例如:

001 170
002 100
003 75

#include <iostream>
using namespace std;
 
class Vehicle   //基类
{ protected:
string NO;//编号
public:
virtual void display()=0;//输出应收费用
};
 
class Car:public Vehicle  //Car的收费公式为: 载客数8+重量2
{
public:
    virtual void display()
    {
    int x,y;
    cin>>NO>>x>>y;   //因为car和bus、truck的计算不一样,car多了个数,所以把x、y的输入放函数里
   cout<<NO<<" "<<(8*x+2*y)<<endl;
    }
};
 
class Truck: public Vehicle  //Truck的收费公式为:重量5
{
public:
    virtual void display()
    {
        int x;
        cin>>NO>>x;   //这里就只有x
        cout<<NO<<" "<<(5*x)<<endl;
    }
 
};
 
class Bus: public Vehicle //Bus的收费公式为: 载客数3
{
public:
    virtual void display()
    {
    int x;
    cin>>NO>>x;
    cout<<NO<<" "<<(3*x)<<endl;
    }
};
 
int main()
{
    int x,y,mode=1;//mode为车辆编号
     Vehicle *pv[10];
     Car      C;
     pv[0]=&C;
     Truck  T;
     pv[1]=&T;
     Bus     B;
     pv[2]=&B;
    while(mode!=0)   //用一个while循环持续输入
    {
        cin>>mode;
        if(mode==1)   //就是car
        {
             pv[0]->display();
        }
    if(mode==2)     //就是truck
    {
            pv[1]->display();
    }
     if(mode==3)   //就是bus
     {
           pv[2]->display();
     }
   }
return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值