第1个回答 2013-07-10
class RecT//矩形
{
public:
virtual int Perim()//周长
{
} virtual int Area()//面积
{
}
};class Square : public RecT//正方形
{
public:
Square(int side)
{
m_side = side;
}
virtual int Perim()//
{
return m_side*4;
}
virtual int Area()//
{
return m_side*m_side;
}
private:
int m_side;
};class Rectangle : public RecT//长方形
{
public:
Rectangle(int height, int width)
{
m_height = height;
m_width = width;
}
virtual int Perim()//
{
return (m_height+m_width)*2;
}
virtual int Area()//
{
return m_height*m_width;
}
private:
int m_width;
int m_height;
};
第2个回答 2018-12-01
#include<iostream>
using namespace std;
class graph
{
protected:
float high,wide;
public:
graph();
graph(float h,float w)
{
high=h;wide=w;cout<<"高为:"<<h<<"\t宽为:"<<w<<endl;} };
class retangle:public graph
{
public:
retangle(float h,float w):graph(h,w){}
void area()
{ cout<<"矩形的面积是:"<<high*wide<<endl;}
};
class triangle:public graph
{
public:
triangle(float h,float w):graph(h,w){}
void area()
{ cout<<"等腰三角形的面积是:"<<high*wide/2<<endl;}
};
void main()
{ retangle g(2,3);
g.area();
triangle h(2,3);
h.area();
}