实验五 类的继承与派生

第1关:简易商品系统

任务描述

本关任务:通过归纳与总结类之间的继承与派生关系,设计并编程实现若干个相互有关联的自定义类,而后对这些类进行简单使用。

相关知识

为了完成本关任务,你需要掌握:1.如何获取类的定义,2.如何派生类。

获取数组的长度

某商店有如下几种货品:衬衣、帽子、立柜。每一种货物都有与其关联的说明信息。衬衣:单价、产地、布料;帽子:单价、产地、布料、样式(平顶或尖顶);立柜:单价、产地、木料、颜色。

操作要求: 商品的进库(增加某类商品及其库存量),商品的出库(减少某类商品及其库存量),某类货品总价值的计算,总价值=单价*库存数量。注意:当出库数量大于库存数量时,程序会输出提示“Insufficient number!”,同时将系统中的库存数量置为0。合理设计数据结构,用类的继承与派生关系将上述的各种货品表示出来,并使用类的构造函数来初始化每一类对象的初始数据。而后将上述的商品管理计算机化,完成操作要求的功能。

编程要求

根据提示,在右侧编辑器补充代码,完成程序要求

int main() {

Shirt s1("江西南昌",235,150,"纯棉");//商品产地,单价,数量,材质

Cap p1("四川成都",88,150,"尼龙","平顶");//商品产地,单价,数量,形状

Capboard cup1("云南昆明",3500,10,"云松木","原色");//商品产地,单价,数量,材质

int i,j,k,m; cin>>i>>j>>k>>m; s1.InStorage(i); s1.OutStorage(j); p1.OutStorage(k); cup1.OutStorage(m); s1.Calculate(); p1.Calculate(); cup1.Calculate(); }

测试说明

平台会对你编写的代码进行测试:

测试输入: 50 200 100 8

预期输出: total money=0 total money=4400 total money=7000

测试输入: 100 100 50 5

预期输出: `total money=35250 total money=8800 total money=17500

#include<iostream>
#include<string>
using namespace std;
/***********begin**********/
//此处完成各个类的书写,并实现题目输出
class Shirt {
private:
	string place;
	int price;
	int num;
	string mar;
public:
	Shirt(string, int, int, string);
	void InStorage(int i);
	void OutStorage(int j);
	void Calculate();
};
Shirt::Shirt(string a, int b, int c, string d)
{
	place = a;
	price = b;
	num = c;
	mar = d;
}
void Shirt::InStorage(int i)
{
	num = num + i;
}
void Shirt::OutStorage(int j)
{
	if (j > num)
	{
		cout << "Insufficient  number!" << endl;
		num = 0;
	}
	else {
		num = num - j;
	}
}
void Shirt::Calculate()
{
	int m;
	m = num * price;
	cout << "total money=" << m << endl;
} 
class Cap:public Shirt
{
public:
	Cap(string, int, int, string, string);
private:
	string shape;
};
Cap::Cap(string a, int b, int c, string d, string e):Shirt(a,b,c,d),shape(e){}

class Capboard:public Shirt
{
public:
	Capboard(string, int, int, string, string);
private:
	string color;
};
Capboard::Capboard(string a,int b,int c,string d,string f):Shirt(a,b,c,d),color(f){}
	/**********end***********/
	int main() {

	Shirt s1("江西南昌", 235, 150, "纯棉");
	Cap p1("四川成都", 88, 150, "尼龙", "平顶");
	Capboard cup1("云南昆明", 3500, 10, "云松木", "原色");
	int i, j, k, m;
	cin >> i >> j >> k >> m;
	s1.InStorage(i);
	s1.OutStorage(j);
	p1.OutStorage(k);
	cup1.OutStorage(m);
	s1.Calculate();
	p1.Calculate();
	cup1.Calculate();
}

第2关:公司支出计算

任务描述

本关任务:编写一个能计算公司一年支出的程序。

已知某company有员工30人,分三类:WeekWorker每周580¥,MonthWorker月薪每月2500 ¥ ,YearWorker年薪每年22000 ¥ 。 已知有Employee类,有成员函数earning()用于计算Employee一年的收入。在conmpany类中有Employee[]存放公司员工,成员函数salariesPay()计算并打印公司的所有一年薪水之和。 请编写类Company,Employee,WeekWorker,MonthWorker,YearWorker,假定公司三类员工各自的数量用随机数来模拟,打印该公司一年的薪水支出。

编程要求

根据提示,在右侧编辑器补充代码。

上答案:

#include<iostream>
#include<stdlib.h>
#include<string>
using namespace std;
//请在此处完成YearWork,MonthWorker,WeekWoker及Company类的编码
/***********begin***********/
class Employee{
    public:
    virtual int earning()=0;
};
class YearWorker:public Employee{
    int salary;
    public:
    YearWorker(int s)
    {
        salary=s;
    }
    virtual int earning()
    {
        return salary;
    }
};
class MonthWorker:public Employee{
    int salary;
    public:
    MonthWorker(int s)
    {
        salary=s;
    }
    virtual int earning()
    {
        return salary*12;
    }
};
class WeekWorker:public Employee{
    int salary;
    public:
    WeekWorker(int s)
    {
        salary=s;
    }
    virtual int earning()
    {
        return salary*52;
    }
};
class Company{
    public:
    Employee *emp[30];
    Company(){
        for(int i=0;i<30;i++){
            emp[i]=NULL;
        }
    }
    int salarypay(){
        int total=0;
        for(int i=0;i<30;i++)
        {
            if(emp[i]!=NULL){
                total+=emp[i]->earning();
            }
        }
        return total;
    }  
};
/************end**********/
int main() {
    Company co;
    for (int i = 0; i < 30; i++)
    {
        int r = rand() % 3 + 1;
        switch (r) {
        case 1:
            co.emp[i] = new WeekWorker(580);
            break;
        case 2:
            co.emp[i] = new MonthWorker(2500);
            break;
        case 3:
            co.emp[i] = new YearWorker(22000);
            break;
        default:
            break;
        }
    }
    cout << "company total pay=" << co.salarypay();
    return 0;
}

第3关:棱柱体问题

任务描述

本关任务:掌握虚基类以及上转型对象的赋值使用。

相关知识

为了完成本关任务,你需要掌握:1.虚基类的使用,2.上转型对象的使用。

题目

C++语言编写面向对象程序,实现柱体体积和表面积的计算(等柱体,比如三棱柱,四棱柱,五棱柱等,截面积相同的情况下,体积=底面积*高)。 例如底面半径为 2、高为 4 的圆柱,体积为 50.27,表面积为75.40;以长为 3、宽为 2 的长方形为底面,高为 5 的四棱柱,体积为 30,表面积为 62。 注意:

  1. 定义一个描述平面图形的基类 Plane
  2. 定义一个描述柱体的基类 Body
  3. 从虚基类 Plane 派生出具体类(如长方形类 Rectangle、圆形类 Circle 和三角形类triangle,由Rectangle派生出正方形Square类),根据实际情况,覆盖基类 Plane 的求面积函数 area() 和Body的求体积函数volume()。 4、从具体triangle类、square及Circle和Body类派生出Triangularprism(三棱柱), quadrangular(四棱柱), circularcolumn(圆柱)类 5、已知一组棱柱体,由不同的柱体组成,求该组柱体的体积之和和表面积之和

    编程要求

根据提示,在右侧编辑器补充代码,实现输入一组三棱柱,四棱柱,及圆柱体,求并输出该组棱柱体的体积之和

测试说明

部分代码如下:

class Plane {

public: double _area;

double area()

{return _area;}

};

class Body {

public : int num;//棱的数量,0为圆柱体

double height;

Plane plane;

double _superficialarea;

double _volume;

public:

Body() {}

double volume(){return _volume;}

Body(int n,double h,Plane pl):num(n),height(h),plane(pl){}

double superficialarea(){return _superficialarea; }

};

上答案:
 

#include <iostream>
using namespace std;
#include<string>
#include"time.h"
#include"math.h"
#define PI 3.14
//亲在begin和end之间完成各个类的定义及实现
/*********begin**********/
class Plane
{
public:
    virtual double area() = 0;//求面积函数
};
class Rectangle :public virtual Plane
{
public:
    double length, width;//长和宽

    Rectangle(double l, double w) :length(l), width(w) {};
    virtual double area()
    {
        return length * width;//覆盖求面积函数
    }
};
class Square :public Rectangle{
public:
    Square(double l) :Rectangle(l,l) {};//析造函数
};
class Circle :public virtual Plane
{
public:
    double radius;//半径

    Circle(double r) :radius(r) {};
    virtual double area()
    {
        return 3.14 * radius * radius;
    }
};
class Triangle :public virtual Plane
{
   
public:
    double a, b, c;
    Triangle(double a, double b, double c) :a(a), b(b), c(c) {};
    virtual double area()
    {
        double p;
        p = (a + b + c) / 2;
        return sqrt(p * (p - a) * (p - b) * (p - c));
    }
};
class Body
{
public:
    
    virtual double volume() = 0;//体积
    virtual double superficialarea() = 0;//表面积
};
class Triangularprism :public Triangle,public Body
{
private:
    double height;
public:
    Triangularprism( int n,double h, double a, double b, double c) :Triangle(a, b, c), height(h) {};
    virtual double volume()
    {
        return Triangle::area() * height;
    }
   virtual double superficialarea() {
        return (Triangle::area() * 2 + Triangle::a * height + Triangle::b * height + Triangle::c * height);
    }
};
class Circularcolumn :public Circle,public Body
{
private:
    double height;
public:
    Circularcolumn(int n, double h, double r) :Circle(r), height(h) {};
   virtual double volume()
    {
        return Circle::area() * height;
    }
   virtual double superficialarea()
    {
        return (Circle::radius* 2*3.14*height + Circle::area() * 2 );
    }
};
class Quadrangular :public Rectangle,public Body
{
private:
    double height;
public:
    Quadrangular(int n, double h, double l) :Rectangle(l,l), height(h) {};
    virtual double volume()
    {
        return Rectangle::area() * height;
    }
    virtual double superficialarea()
    {
        return (Rectangle::area() * 2 + Rectangle::width * height * 2 + Rectangle::length * height*2);
    }
};


/**********end********/
int main() {
    int n;
    double height,r,t1,t2,t3,l;
    cin>>n>>height>>r;//输入n=0,表示圆柱体
    Circularcolumn c1(n,height,r);
    cin>>n>>height>>t1>>t2>>t3;//输入n=3,表示三棱柱
    Triangularprism t(n,height,t1,t2,t3);
    cin>>n>>height>>l;//输入n=4表示正四棱柱
    Quadrangular qu(n,height,l);
    Body *body[3];
    body[0]=&c1;
    body[1]=&t;
    body[2]=&qu;
    double superficalsum=0;
    double volumesum=0;
    for(int i=0;i<3;i++)
       {
          volumesum+=body[i]->volume();//volume()获取该体的体积
          superficalsum+=body[i]->superficialarea();//获取该体的表面积
       }
    cout<<"all volume="<<volumesum<<endl;
    cout<<"all superfilarea="<<superficalsum<<endl;
}

  • 23
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 10
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Galaxy*★

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值