using namespace stdclass Cshape
{
public:
virtual double GetArea() = 0
}class Ccircle:public Cshape
{
public:
Ccircle(double r):m_r(r) {}
virtual double GetArea()
{
return (3.14 *m_r * m_r)
}
private:
double m_r
}class Csquare:public Cshape
{
public:
Csquare(double a):m_a(a) {}
virtual double GetArea()
{
return (m_a * m_a)
}
private:
double m_a
}class Crectangle:public Cshape
{
public:
Crectangle(double a, double b):m_a(a), m_b(b) {}
virtual double GetArea()
{
return (m_a * m_b)
}
private:
double m_a
double m_b
}class Ctrapeziod:public Cshape
{
public:
Ctrapeziod(double a, double b, double h):m_a(a), m_b(b), m_h(h) {}
virtual double GetArea()
{
return ((m_a + m_b) * m_h / 2)
}
private:
double m_a
double m_b
double m_h
}class Ctriangle:public Cshape
{
public:
Ctriangle(double a, double h):m_a(a), m_h(h) {}
virtual double GetArea()
{
return (m_a * m_h / 2)
}
private:
double m_h
double m_a
}int main()
{
Cshape* pShape[5]
pShape[0] = new Ccircle(5)
pShape[1] = new Ctriangle(3.4, 8.6)
pShape[2] = new Csquare(4.6)
pShape[3] = new Ctrapeziod(4.5, 7.3, 4)
pShape[4] = new Crectangle(5, 6)
cout<<"圆形面积:"<<pShape[0]->GetArea()<<endl
cout<<"三角形面积:"<<pShape[1]->GetArea()<<endl
cout<<"正方形面积:"<<pShape[2]->GetArea()<<endl
cout<<"梯形面积:"<<pShape[3]->GetArea()<<endl
cout<<"长方形面积:"<<pShape[4]->GetArea()<<endl
double sum = 0
cout<<"它们面积总和为:"
for (int i=0i!=5++i)
{
sum += pShape[i]->GetArea()
delete pShape[i]
pShape[i] = NULL
}
cout<<sum<<endl
return 0
}
不是C shape是C#(C Sharp)是微软(Microsoft)为.NET Framework量身订做的程序语言,C#拥有C/C++的强大功能以及Visual Basic简易使用的特性,是第一个组件导向(Component-oriented)的程序语言,和C++与Java一样亦为对象导向(object-oriented)程序语言。
http://baike.baidu.com/view/107979.htm?fr=ala0_1
#include <iostream>using namespace std
class CShape
{
public:
CShape(void)
{
}
~CShape(void)
{
}
virtual void area(void) = 0
virtual void cir(void) = 0
}
class CRectangle:public CShape
{
public:
CRectangle(int xx = 0,int yy = 0)
{
x = xx
y = yy
}
void area()
{
cout<<x*y<<endl
}
void cir()
{
cout<<2*(x+y)<<endl
}
private:
int x,y
}
class CCircle:public CShape
{
public:
CCircle(int rr = 0)
{
r = rr
}
void area()
{
cout<<2*3.14*r<<endl
}
void cir()
{
cout<<3.14*r*r<<endl
}
private:
int r
}
int main()
{
CShape* a = new CRectangle(1,2)
a->area()
a->cir()
CShape* b = new CCircle(3)
b->area()
b->cir()
system("pause")
return 0
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)