2024年2月8日发(作者:)
多态与虚函数实验
一、实验目的
1.理解多态的概念
2.掌握如何用虚函数实现运行时多态
3.掌握如何利用抽象类
二、实验内容及步骤
1. 设计一个图形类(Shape),由它派生出5个派生类:三角形类(Triangle)、正方形类(Square)、圆形类(Circle)、矩形类(Rectangle)、梯形类(triangle)类,利用虚函数计算图形面积,用一个函数printArea分别输出以上5者的面积。
#include
#include
#include
using namespace std;
const double PI = 3.1415926;
class Shape
{
public:
virtual double GetArea() = 0;
};
class Triangle : public Shape
{
private:
double a, b, c;
public:
double TArea;
double GetArea();
};
double Triangle::GetArea()
{
1
double tmp;
cout << "请输入三角形的三边长: ";
cin >> a >> b >> c;
while((a < 0) || (b < 0) || (c < 0) || (a + b <= c) || (a +
c <= b) || (b + c <= a))
{
cout << "输入的边长小于零或输入的边长不能构成三角形!" <<
endl;
cout << "请重新输入三角形的三边长: ";
cin >> a >> b >> c;
}
tmp = (a + b + c) / 2.0;
TArea = sqrt(tmp * (tmp - a) * (tmp - b) * (tmp - c));
return TArea;
}
class Rectangle : public Shape
{
private:
double length, width;
public:
double RArea;
double GetArea();
};
double Rectangle::GetArea()
{
cout << "请输入矩形的长和宽: ";
cin >> length >> width;
while(length < 0 || width < 0)
{
cout << "矩形的长和宽不能小于零!" << endl;
cout << "请重新输入矩形的长和宽: ";
cin >> length >> width;
}
RArea = length * width;
return RArea;
}
class Circle : public Shape
{
private:
double Radius;
public:
2
double CArea;
double GetArea();
};
double Circle::GetArea()
{
cout << "请输入圆的半径: ";
cin >> Radius;
while(Radius < 0)
{
cout << "圆的半径不能小于零!" << endl;
cout << "请重新输入圆的半径: ";
cin >> Radius;
}
CArea = 2 * PI * Radius;
return CArea;
}
class Square : public Shape
{
private:
double side;
public:
double SArea;
double GetArea();
};
double Square::GetArea()
{
cout << "请输入正方形的边长: ";
cin >> side;
while(side < 0)
{
cout << "正方形的边长不能小于零!" << endl;
cout << "请重输入正方形的边长: ";
cin >> side;
}
SArea = side * side;
return SArea;
}
class triangle : public Shape
{
private:
3
double UpBase, DownBase , Higth;
public:
double TArea;
double GetArea();
};
double triangle::GetArea()
{
cout << "请输入梯形的上底,下底和高: ";
cin >> UpBase >> DownBase >> Higth;
while(UpBase < 0 || DownBase < 0 || Higth < 0)
{
cout << "梯形的上底,下底和高不能小于零!" << endl;
cout << "请重新输入梯形的上底,下底和高: ";
cin >> UpBase >> DownBase >> Higth;
}
TArea = (UpBase + DownBase)* Higth / 2.0;
return TArea;
}
void PrintArea()
{
Triangle myTri;
Square mysqu;
Circle mycir;
Rectangle myrec;
triangle mytri;
cout << fixed << setprecision(2) << "三角形的面积: " <<
a() << endl;
cout << fixed << setprecision(2) << "正方形的面积: " <<
a() << endl;
cout << fixed << setprecision(2) << "圆形的面积: " <<
a() << endl;
cout << fixed << setprecision(2) << "矩形的面积: " <<
a() << endl;
cout << fixed << setprecision(2) << "梯形的面积: " <<
a() << endl;
}
int main(void)
{
PrintArea();
return 0;
4
2. 定义一个教师类,由教师类派生出讲师、副教授、教授类。教师的工资分别由基本工资、课时费和津贴构成。假设讲师、副教授、教授的基本工资分别为800、900、1000元,课时费分别为每小时40、45、50元,津贴分别为1300、1800、2300。定义虚函数来计算教师的工资,并通过主函数来进行验证。
#include
using namespace std;
class Teacher
{
public:
virtual int GetAllPay() = 0;
};
class Lecturer : public Teacher
{
private:
5
int CommonPay, ClassperPay, Subsidy, LAllPay;
public:
Lecturer(){CommonPay = 800; ClassperPay = 40; Subsidy = 1300;}
int GetAllPay();
};
int Lecturer::GetAllPay()
{
LAllPay = CommonPay + ClassperPay + Subsidy;
return LAllPay;
}
class AssProfessor : public Teacher
{
private:
int CommonPay, ClassperPay, Subsidy, AAllPay;
public:
AssProfessor(){CommonPay = 900; ClassperPay = 45; Subsidy = 1800;}
int GetAllPay();
};
int AssProfessor::GetAllPay()
{
AAllPay = CommonPay + ClassperPay + Subsidy;
return AAllPay;
}
class Professor : public Teacher
{
private:
int CommonPay, ClassperPay, Subsidy, PAllPay;
public:
Professor(){CommonPay = 1000; ClassperPay = 50; Subsidy = 2300;}
int GetAllPay();
};
int Professor::GetAllPay()
{
PAllPay = CommonPay + ClassperPay + Subsidy;
return PAllPay;
}
int main(void)
{
6
}
Teacher *ptr;
Lecturer xiaozhang;
ptr = &xiaozhang;
cout << "讲师的总工资: " << ptr->GetAllPay() << endl;
AssProfessor xiaoming;
ptr = &xiaoming;
cout << "副教授的总工资: " << ptr->GetAllPay() << endl;
Professor xiaoli;
ptr = &xiaoli;
cout << "教授的总工资: " << ptr->GetAllPay() << endl;
return 0;
3.定义一个基类为哺乳动物类Mammal,其中有数据成员年龄、重量、品种,有成员函数move()、speak()等,以此表示动物的行为。由这个基类派生出狗、猫、马、猪等哺乳动物,它们有各自的行为。编程分别使各个动物表现出不同的行为。
要求如下:
1、从基类分别派生出各种动物类,通过虚函数实现不同动物表现出的不同行为。
2、今有狗: CAIRN:3岁,3kg;
DORE:4岁,2kg;
猫: CAT:5 岁,4kg;
马: HORSE,5岁,60kg;
猪: PIG,2岁,45kg。
3、设置一个 Mammal 类数组,设计一个屏幕菜单,选择不同的动物或不同的品种,则显示出动物相对应的动作,直到选择结束。
4、对应的动作中要先显示出动物的名称,然后显示年龄、重量、品种、叫声及其他特征。
程序代码:
7
#include
#include
#include
using namespace std;
class Mammal //建立动物基类 Mammal
{
protected:
string name;
int age;
float weight;
public:
Mammal(string n,int a,float w)
{name=n;
age=a;
weight=w;}
virtual string move()=0;
virtual string speak()=0;
virtual void setout()=0; //纯虚函数实现类继承时的多态性
};
class Dog:public Mammal //建立四个类似的类,分别为Dog,Cat,Horse,Pig
{ public:
Dog(string n,int a,float w): Mammal(n,a,w){};
string move()
{return "看门或者作为宠物";}
string speak()
{return "汪汪";}
void setout()
{ cout<<"狗类的信息:"< cout<<"昵称:"< cout<<"年龄:"< cout<<"体重:"< cout<<"行为:"< cout<<"叫声: "< } }; class Cat:public Mammal { public: Cat(string n,int a,float w): Mammal(n,a,w){}; string move() {return "捉老鼠";} string speak() {return "喵~";} 8 void setout() { cout<<"猫类的信息:"< cout<<"昵称:"< cout<<"年龄:"< cout<<"体重:"< cout<<"行为:"< cout<<"叫声 :"< } }; class Horse:public Mammal { public: Horse(string n,int a,float w): Mammal(n,a,w){}; string move() {return "在草原上奔跑";} string speak() {return "鞥";} void setout() { cout<<"马的信息:"< cout<<"昵称:"< cout<<"年龄:"< cout<<"体重:"< cout<<"行为:"< cout<<"叫声 :"< } }; class Pig:public Mammal { public: Pig(string n,int a,float w): Mammal(n,a,w){}; string move() {return "睡觉";} string speak() {return "哼哼";} void setout() { cout<<"猪的信息:"< cout<<"昵称:"< cout<<"年龄:"< cout<<"体重:"< cout<<"行为:"< cout<<"叫声 :"< } }; int main() { Dog d1 ("CAIRN",3,3); 9 Dog d2 ("DORE",4,2); Cat c ("CAT",5,4); Horse h ("HORSE",5,60); Pig p ("PIG",2,45); Mammal *animals[6]; animals[0]=&d1; animals[1]=&d2; animals[2]=&c; animals[3]=&h; animals[4]=&p; cout<<"选择动物的种类:1 Dog;2 Cat;3 Horse;4 Pig……"< char s='y'; int num,k; while(s!='n') { cout<<"请输入动物种类的代号:"; cin>>num; if(num==1) {cout<<"任选一只小狗: 0 CAIRN,1 DORE"< cin>>k; animals[k]->setout(); } else animals[num]->setout(); cout<<"是否继续?"; cin>>s;} system("PAUSE"); } 运行结果: 10 11
本文发布于:2024-02-08 03:45:59,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170733515966429.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |