定义一个抽象类CShape,包含纯虚函数Area(用来计算面积)和SetData(用来重设形状大小)。然后派生出三角
定义一个抽象类CShape,包含纯虚函数Area(用来计算面积)和SetData(用来重设形状大小)。然后派生出三角形CTriangle类、矩形CRect类、圆CCirc...
定义一个抽象类CShape,包含纯虚函数Area(用来计算面积)和SetData(用来重设形状大小)。然后派生出三角形CTriangle类、矩形CRect类、圆CCircle类,分别计算其面积。最后用main函数测试。
展开
展开全部
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
class CShape
{
public:
virtual void Area()=0;
virtual void SetData()=0;
};
class CTriange:public CShape
{
float floor;//底边长度
float higth;//高
public:
void SetData();
void Area(){cout<<"三角形面积为:"<<floor*higth/2<<endl;}
};
void CTriange::SetData()
{
cout<<"输入三角形的底和高:"<<endl;
cin>>floor>>higth;
}
class CRect:public CShape
{
float lenth;//长
float wide;//宽
public:
void SetData();
void Area(){cout<<"矩形面积为:"<<lenth*wide<<endl;}
};
void CRect::SetData()
{
cout<<"输入矩形的长和宽:"<<endl;
cin>>lenth>>wide;
}
class CCircle:public CShape
{
float radius;
public:
void SetData();
void Area(){cout<<"圆面积为:"<<3.14*radius<<endl;}
};
void CCircle::SetData()
{
cout<<"请输入圆的半径:"<<endl;
cin>>radius;
}
int main()
{
CShape *shape1 = new CTriange() ;
shape1->SetData();
shape1->Area();
CShape *shape2 = new CRect() ;
shape2->SetData();
shape2->Area();
CShape *shape3 = new CCircle() ;
shape3->SetData();
shape3->Area();
getchar();
return 0;
}
#include <iostream>
#include <string>
using namespace std;
class CShape
{
public:
virtual void Area()=0;
virtual void SetData()=0;
};
class CTriange:public CShape
{
float floor;//底边长度
float higth;//高
public:
void SetData();
void Area(){cout<<"三角形面积为:"<<floor*higth/2<<endl;}
};
void CTriange::SetData()
{
cout<<"输入三角形的底和高:"<<endl;
cin>>floor>>higth;
}
class CRect:public CShape
{
float lenth;//长
float wide;//宽
public:
void SetData();
void Area(){cout<<"矩形面积为:"<<lenth*wide<<endl;}
};
void CRect::SetData()
{
cout<<"输入矩形的长和宽:"<<endl;
cin>>lenth>>wide;
}
class CCircle:public CShape
{
float radius;
public:
void SetData();
void Area(){cout<<"圆面积为:"<<3.14*radius<<endl;}
};
void CCircle::SetData()
{
cout<<"请输入圆的半径:"<<endl;
cin>>radius;
}
int main()
{
CShape *shape1 = new CTriange() ;
shape1->SetData();
shape1->Area();
CShape *shape2 = new CRect() ;
shape2->SetData();
shape2->Area();
CShape *shape3 = new CCircle() ;
shape3->SetData();
shape3->Area();
getchar();
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询