C++题目练习04
4.1 用抽象类设计计算二维平面图形面积的程序
用抽象类设计计算二维平面图形面积的程序,在基类 TDshape 中设计纯虚函数 area() 和 printName() 。
area() 用于计算几何图形的面积,printName() 用于打印输出几何图形的类名,如 Triangle 类的对象就打印输出“Triangle”。
每个具体形状的类则从抽象类 TDshape 派生,各自定义其独有的数据成员和成员函数,并定义 area() 和 printName() 的具体实现代码。
函数 fp 和函数 fr 是以 TDshape 为接口的函数,借以访问具体类如 Triangle 和 Rectangle 类的成员函数 area() 和 printName() 。
#include<iostream>
using namespace std;
class TDshape
{
public:
virtual double area() = 0;
virtual void printName() = 0;
};
class Triangle:public TDshape
{
private:
double width;
double height;
public:
Triangle(double w,double h)
{
width = w;
height = h;
}
double area()
{
double area;
area = (width * height)/2;
return area;
}
void printName()
{
cout<<"Triangle"<<endl;
}
void setWidth(double w)
{
width = w;
}
double setHeight(double h)
{
height = h;
}
double getWidth()
{
return width;
}
double getHeight()
{
return height;
}
};
class Rectangle:public TDshape
{
private:
double width;
double height;
public:
Rectangle(double w,double h)
{
width = w;
height = h;
}
double area()
{
double area;
area = width * height;
return area;
}
void printName()
{
cout<<"Rectangle"<<endl;
}
void setWidth(double w)
{
width = w;
}
void setHeight(double h)
{
height = h;
}
double getWidth()
{
return width;
}
double getHeight()
{
return height;
}
} ;
void fp( TDshape *p ) {
cout << "area:" << p->area() << endl;
p->printName();
}
void fr( TDshape &r ) {
cout << "area:" << r.area() << endl;
r.printName();
}
int main() {
Triangle triangle( 3, 4 ); /// width为3, height为4
Rectangle rectangle( 4, 9 ); /// width为4, height为9
rectangle.setWidth( 10 );
cout << "******from fp:" << endl;
fp( &triangle );
fp( &rectangle );
cout << "******from fr:" << endl;
fr( triangle );
fr( rectangle );
return 0;
}
4.2 多重继承:设计飞机类Plane及其派生类
给出一个飞机Plane基类,由它派生出歼击机Fighter类和轰炸机Bomber类,歼击机Fighter类和轰炸机Bomber类又共同派生出歼轰机(多用途战斗机)Fighter_Bomber类。虚基类描述飞机类及其派生的类族。
提示:
1、Plane类有数据成员:wing(机翼长度),body(机身长度),tail(尾翼长度),voyage(航程),guest(旅客人数);成员函数display,用于显示数据成员的值;
2、Fighter类(歼击机类)有新的数据成员missile(导弹数),重定义了display函数,新定义了fight函数(输出"Fight!");
3、Bomber类(轰炸机类)有新的数据成员bomb(载弹量),重定义display函数,新定义attack函数(输出"Attack!")、getbomb函数。
4、Fighter_Bomber类(歼轰机类), 重定义了display函数。
5、Plane类(飞机类)为虚基类。
#include <iostream>
using namespace std;
/// 基类:飞机
class Plane {
private:
float wing; // 机翼长度
float body; // 机身长度
float tail; // 尾翼长度
float voyage; // 航程
int guest; // 旅客人数
public:
Plane( float, float, float, float, int );
void display();
};
void Plane::display( ) {
cout << "Plane:wing:" << wing << " body:" << body << " tail:" << tail << " voyage:" << voyage << " guest:" << guest;
}
Plane::Plane( float w, float b, float t, float v, int n ) {
wing = w;
body = b;
tail = t;
voyage = v;
guest = n;
}
class Fighter:virtual public Plane
{
private:
int missile;
public:
Fighter(float w, float b, float t, float v, int n,int m)
:Plane(w,b,t,v,n)
{
missile = m;
}
void display()
{
cout<<"This is a fighter!"<<endl;
Plane::display();
cout<<endl;
cout<<"missile:"<<missile<<endl;
}
void fight()
{
cout<<"Fight!"<<endl;
}
};
class Bomber:virtual public Plane
{
private:
int bomb;
public:
Bomber(float w, float b, float t, float v, int n,int bm)
:Plane(w,b,t,v,n)
{
bomb = bm;
}
void display()
{
cout<<"This is a bomber!"<<endl;
Plane::display();
cout<<endl;
cout<<"bomb:"<<bomb<<endl;
}
void attack()
{
cout<<"Attack!"<<endl;
}
double getbomb()
{
cout<<"bomb:"<<bomb<<endl;
}
};
class Fighter_Bomber:public Fighter,public Bomber
{
public:
Fighter_Bomber(float w, float b, float t, float v, int n,int m,int bm)
:Plane(w,b,t,v,n),Fighter(w, b, t, v, n, m), Bomber(w, b, t, v, n, bm)
{
}
void display()
{
cout<<"This is a fighter_bomber!"<<endl;
Fighter::display();
Bomber::getbomb();
Fighter::fight();
Bomber::attack();
}
};
int main() {
Fighter f( 10.0, 6.0, 2.5, 1800, 1, 8 ); //歼击机
f.display();
Bomber b( 30, 9, 6, 12000, 12, 6000 ); //轰炸机
b.display();
Fighter_Bomber fb( 20, 7, 3.2, 4000, 2, 6, 2500 ); //歼轰机
fb.display();
return 0;
}
4.3 使用虚基类:定义类Shape、Rectangle、Circle和Square
定义一个基类 Shape,在此基础上派生出 Rectangle 和 Circle,两者都有 getArea() 函数计算对象的面积。使用 Rectangle 类创建一个派生类 Square。
1、在 Rectangle 类中有长和宽两个数据成员,在 Circle 类中有一个数据成员即半径。
2、本题圆周率使用3.14
#include <iostream>
using namespace std;
class Shape {
public:
virtual float getArea() = 0;
virtual ~Shape() { }
};
class Rectangle:public Shape
{
private:
double length;
double width;
public:
Rectangle(double l,double w)
{
length = l;
width = w;
}
float getArea()
{
int area;
area =length * width;
return area;
}
};
class Circle:public Shape
{
private:
double R;
public:
Circle(double r)
{
R = r;
}
float getArea()
{
double area;
area = 3.14 * R * R;
return area;
}
};
class Square:public Rectangle
{
public:
Square(double l)
:Rectangle(l,l)
{
}
};
int main( ) {
Shape *ps;
ps = new Circle( 5 );
cout << "The area of the Circle is " << ps->getArea() << endl;
delete ps;
Rectangle *pr;
pr = new Rectangle( 5, 6 );
cout << "The area of the Rectagle is " << pr->getArea() << endl;
delete pr;
Square s( 8 );
pr = &s;
cout << "The area of the Square is " << pr->getArea() << endl;
// delete pr; 这里不能 delete 。为什么?
return 0;
}
4.4 用虚函数和虚基类描述飞机类及其派生的类族
设计一个飞机Plane类,由它派生出歼击机Fighter类和轰炸机Bomber类,歼击机Fighter类和轰炸机Bomber类又共同派生出歼轰机(多用途战斗机)Fighter_Bomber类。用虚函数和虚基类描述飞机类及其派生的类族。
约定:
1、Plane类有数据成员:wing(机翼长度),body(机身长度),tail(尾翼长度),voyage(航程),guest(旅客人数);成员函数display,用于显示数据成员的值;
2、Fighter类(歼击机类)有新的数据成员missile(导弹数),重定义了display函数,新定义了fight函数(输出"Fight!");
3、Bomber类(轰炸机类)有新的数据成员bomb(载弹量),重定义display函数,新定义attack函数(输出"Attack!")、getbomb函数。
4、Fighter_Bomber类(歼轰机类), 重定义了display函数。
5、Plane类(飞机类)为虚基类。
6、display为虚函数。
#include <iostream>
using namespace std;
// 基类:飞机
class Plane {
private:
float wing; //机翼长度
float body; //机身长度
float tail; //尾翼长度
float voyage; //航程
int guest; //旅客人数
public:
Plane( float w, float b, float t, float v, int n ) {
wing = w;
body = b;
tail = t;
voyage = v;
guest = n;
}
virtual void display()
{
cout<<"Plane:wing:"<<wing<<" body:"<<body<<" tail:"<<tail<<" voyage:"<<voyage<<" guest:"<<guest<<endl;
}
};
class Fighter:virtual public Plane
{
private:
int missile;
public:
Fighter(float w, float b, float t, float v, int n,int m)
:Plane(w,b,t,v,n)
{
missile = m;
}
void display()override
{
cout<<"This is a fighter!"<<endl;
Plane::display();
cout<<"missile:"<<missile<<endl;
}
void fight()
{
cout<<"Fight!"<<endl;
}
};
class Bomber:virtual public Plane
{
private:
int bomb;
public:
Bomber(float w, float b, float t, float v, int n,int bm)
:Plane(w,b,t,v,n)
{
bomb = bm;
}
void display()override
{
cout<<"This is a bomber!"<<endl;
Plane::display();
cout<<"bomb:"<<bomb<<endl;
}
void attack()
{
cout<<"Attack!"<<endl;
}
void getbomb()
{
cout<<"bomb:"<<bomb<<endl;
}
};
class Fighter_Bomber:public Fighter,public Bomber
{
public:
Fighter_Bomber(float w, float b, float t, float v, int n ,int m,int bm)
:Plane(w,b,t,v,n),Fighter(w,b,t,v,n,m),Bomber(w,b,t,v,n,bm)
{
}
void display()override
{
cout<<"This is a fighter_bomber!"<<endl;
Fighter::display();
Bomber::getbomb();
Fighter::fight();
Bomber::attack();
}
};
int main() {
Plane *p;
Fighter f( 10.0, 6.0, 2.5, 1800, 1, 8 ); // 歼击机
Bomber b( 30, 9, 6, 12000, 12, 6000 ); // 轰炸机
Fighter_Bomber fb( 20, 7, 3.2, 4000, 2, 6, 2500 ); // 歼轰机
p = &f;
cout << "********p=&f" << endl;
p->display();
p = &b;
cout << "\n********p=&b" << endl;
p->display();
p = &fb;
cout << "\n********p=&fb" << endl;
p->display();
return 0;
}