面向对象程序设计第九章:多态

简介: 面向对象程序设计第九章:多态

虚函数–交通工具

【问题描述】

定义一个交通工具vehicle,将他作为基类派生校车类car,卡车类truck和轮船类boat,定义类并定义虚函数来显示各类信息

【输入形式】

【输出形式】

【样例输入】

【样例输出】

【样例说明】

【评分标准】

#include <iostream>
using namespace std;
class vehicle
{   protected:
  double speed;    //速度,公里/小时
  int wheels;     //轮子数
    double weight; //重量
public:
  vehicle(double speed=80,int wheels=4,double weight=1000);
        virtual void show(void)=0; 
};
vehicle::vehicle(double  speed , int  wheels , double  weight )
{
    this->speed = speed;
    this->wheels = wheels;
    this->weight = weight;
}
void vehicle::show()
{
    cout << "Vehicle message\n";
    cout << speed << "  " << wheels << "  " << weight << endl;
}
class car : public vehicle
{  int passenger_load;
public:
  car(double speed=80,int wheels=4,double weight=1000,int passenger_load=4);
        virtual void show(void);
};
void car::show(void) 
{cout << "Car message\n";
cout<<speed<<" "<<wheels<<" "<<weight<<" "<<passenger_load<<endl;
}
class truck : public vehicle
{ double rated_load;   //额定载重
  public:
    truck(double speed=80,int wheels=4,double weight=2500,double rated_load=3000);
          virtual  void show(void);
};
car::car(double  speed , int  wheels , double  weight ,int passenger_load)
{
    this->speed = speed;
    this->wheels = wheels;
    this->weight = weight;
    this->passenger_load = passenger_load;
}
void truck::show()
{
    cout << "truck message\n";
    cout << speed << " " << wheels << " " << weight << " " << rated_load<<endl;
}
truck::truck(double speed, int wheels, double weight, double rated_load)
{
    this->speed = speed;
    this->wheels = wheels;
    this->weight = weight;
    this->rated_load = rated_load;
}
class boat : public vehicle
{ char kind;       //轮船类别,如客轮为'k'
  public:
   boat(double speed=30,int wheels=0,double weight=12000,char kind='k');
        virtual  void show(void);
};
void boat::show()
{
    cout << "boat message\n";
    cout << speed <<" "<< wheels <<" "<< weight <<" "<< kind<<endl;
}
boat::boat(double speed, int wheels, double weight, char kind)
{
    this->speed = speed;
    this->wheels = wheels;
    this->weight = weight;
    this->kind = kind;
}
int main()
{  vehicle *unicycle;  car *BMW;
   unicycle = new car;   unicycle ->show(); 
   BMW= (car *) unicycle;   BMW ->show(); 
   delete unicycle;
   unicycle = new truck;   unicycle ->show(); 
   delete unicycle;
   unicycle = new boat;   unicycle ->show(); 
   delete unicycle;
   return 0;
} 

2. 求员工工资

【问题描述】

设公司有普通员工和经理两类人员。主函数已给出,编程设计普通员工类CommonWorker和经理类Manager,经理类除具有普通员工类的属性外。普通员工类的收入由基本工资和奖金构成,经理类的收入除基本工资和奖金外,还包括职务津贴。实现求两类员工工资的Pay函数。

【输入形式】

【输出形式】

【样例输入】

【样例输出】

【样例说明】

【评分标准】

#include <iostream>
#include<iostream>
using namespace std;
class CommonWorker
{
public:
  virtual void Pay()
  {
    earning = wage + reward;
    cout << earning<<endl;
  }
  friend istream& operator>>(istream& inStream, CommonWorker& data)
  {
    inStream >> data.wage;
    inStream >> data.reward;
    return inStream;
  }
protected:
  double wage;
  double reward;
  double earning;
};
class Manager :public CommonWorker
{
public:
  virtual void Pay()
  {
    earning = wage + reward + place;
    cout << earning<<endl;
  }
  friend istream& operator>>(istream& instream, Manager& data)
  {
    instream >> data.wage;
    instream >> data.reward;
    instream >> data.place;
    return instream;
  }
protected:
  double place;
};
int main()
{
  CommonWorker c1, * pc;
  Manager m1;
  //分别输入员工的基本工资、奖金和津贴
  cin >> c1 >> m1;
  pc = &c1;               
  pc->Pay();      //调用Pay()函数输出该普通员工工资,如:5000
  pc = &m1;             
  pc->Pay();      //调用Pay()函数输出该经理工资,如:8000
  return 0;
}

3. 求图形周长和面积

【问题描述】

根据给出的程序片段编程,使程序正确运行。由基类Shape派生出圆类Circle(数据成员:半径)、正方形类Square(数据成员:边长)、三角形类Triangle(数据成员:三条边边长),3个派生类都有输入和显示信息函数Input、Output,计算面积的函数Area,计算周长的函数Perim。【输入形式】

【输出形式】

【样例输入】

【样例输出】

【样例说明】

【评分标准】

#include <iostream>
#include<iomanip>
#include <cmath>
using namespace std;
const double PI = 3.14159;
//定义基类Shape
class Shape
{
public:
  virtual double Area() const
  {   return 0;   }
  virtual double Perim() const  
  {   return 0; }
  virtual void Input() {} 
  virtual void Output() { }
  static void SetFormat()
  {   cout << setiosflags(ios::fixed) << setprecision(2); }
protected:
  Shape(){}
};
class Circle:public Shape
{
private:
    double mr;
public:
    virtual void Input()
    {
        double r;
        cin >> r;
        this->mr = r;
    }
    Circle(double r = 0)
    {
        mr = r;
    }
    virtual void Output()
    {
        cout << PI * 2 * mr << "," << PI * mr * mr<<endl;
    }
};
class Square :public Shape
{
private:
    double ma;
public:
    Square(double a = 0, double b = 0)
    {
        ma = a;
    }
    virtual void Input()
    {
        double a;
        cin >> a;
        this->ma = a;
    }
    virtual void Output()
    {
        cout<<ma*4<<","<<ma*ma<<endl;
    }
};
class Triangle :public Shape
{
private:
    double ma, mb, mc;
public:
    Triangle(double a = 0, double b = 0, double c = 0)
    {
        ma = a; mb = b; mc = c;
    }
    virtual void Input()
    {
        double a, b, c;
        cin >> a >> b >> c;
        ma = a; mb = b; mc = c;
    }
    virtual void Output()
    {
        cout << ma + mb + mc << ",";
        double p, s;
        p = (ma + mb + mc) / 2;
        s = sqrt(p * (p - ma) * (p - mb) * (p - mc));
        cout << s<<endl;
    }
};
int main()
{
  Circle circle;
  Square square;
  Triangle triangle;
  Shape* pt[3] = { &circle,&square,&triangle };
  Shape::SetFormat(); 
  //输入各图形数据,空白字符隔开
  for (int i = 0; i < 3; i++)
    pt[i]->Input();
  //输出各图形周长和面积,周长和面积值之间逗号隔开
  for (int i = 0; i < 3; i++) 
    pt[i]->Output();  
  return 0;
}


目录
相关文章
|
程序员
如何理解面向过程和面向对象?
有人说:“如果上帝是程序员,他怎么创造世界上的所有动物。”,理解这个问题就理解了面向对像。
|
7月前
|
Java 物联网 测试技术
Java面向对象程序设计3面向对象基础
Java面向对象程序设计3面向对象基础
343 0
|
4月前
|
算法 Java 程序员
类和对象 | 面向过程与面向对象的初步认识
类和对象 | 面向过程与面向对象的初步认识
59 5
|
7月前
对面向对象编程的理解,面向过程和面向对象有什么区别
对面向对象编程的理解,面向过程和面向对象有什么区别
74 0
|
7月前
|
存储 数据安全/隐私保护 C++
基于C++的面向对象程序设计:类与对象的深入剖析
基于C++的面向对象程序设计:类与对象的深入剖析
82 1
|
7月前
|
设计模式 Java 数据安全/隐私保护
Java面向对象程序设计
Java面向对象程序设计
38 0
|
7月前
面向对象编程的理解,面向过程和面向对象有什么区别
面向对象编程的理解,面向过程和面向对象有什么区别
64 0
|
7月前
|
关系型数据库
编程思想 - 面向对象编程和面向过程编程的区别理解
编程思想 - 面向对象编程和面向过程编程的区别理解
63 0
|
算法 Java Unix
面向对象和面向过程的区别?什么是面向过程?什么是面向对象?
面向对象和面向过程的区别?什么是面向过程?什么是面向对象?
3987 1