金山学院C++课程实验报告
本题的代码如下:
#include<iostream.h>
const double pai=3.14;
class Shape
{public:double area()const{return 0.0;}
void display(){};
};
class twoDimShape:virtual public Shape
{};
class threeDimShape:virtual public Shape
{};
//三角形类
class Triangle:public twoDimShape
{public:Triangle(double myg,double myd){gao=myg;di=myd;}
double area ()const {return (di*gao)/2;}
void display(){cout<<"Area of Square is";}
private:double gao,di;
};
//正方形类
class Square:public twoDimShape
{public:Square(double myb){bianchang =myb;}
double area ()const {return bianchang*bianchang;}
void display(){cout<<"Area of Square is";}
private:double bianchang;
};
//正方体
class Squarer:public threeDimShape
{public:Squarer(double myb){bianchang =myb;}
double area ()const {return bianchang*bianchang*bianchang;}
void display(){cout<<"Superficial area of Cone is";}
private:double bianchang;
};
//球
class Ball:public threeDimShape
{public:Ball(double myr){r =myr;}
double area ()const {return (4*pai*r*r*r)/3;}
void display(){cout<<"Superficial area of Ball is";}
private:double r;
};
int main()
{ double a,b,c,d,e;
cout<<"请输入三角形的高和底"<<endl;
cin>>a>>b;
Triangle t(a,b);
double area;
area=t.area();