网站建设资讯

NEWS

网站建设资讯

c语言shape函数 c++shape类

用c++编写程序,定义CShape类,其中包括两个纯虚函数,分别用于计算面积和周长,派生出两个派

#include iostream

创新互联建站专业为企业提供东坡网站建设、东坡做网站、东坡网站设计、东坡网站制作等企业网站建设、网页设计与制作、东坡企业网站模板建站服务,10余年东坡做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。

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()

{

coutx*yendl;

}

void cir()

{

cout2*(x+y)endl;

}

private:

int x,y;

};

class CCircle:public CShape

{

public:

CCircle(int rr = 0)

{

r = rr;

}

void area()

{

cout2*3.14*rendl;

}

void cir()

{

cout3.14*r*rendl;

}

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;

}

关于Shape类用C++编

1、 设计一个Shape类, a) Shape具有一个成员函数来计算面积,Shape具有public Shape{ public: Circle(double x, double y, double r):c(x,y)

用C++定义一个shape类

#includeiostream

using

namespace

std;

class

shape

{

public:

virtual

float

area()=0;//纯虚函数

virtual

void

show()=0;//纯虚函数

};

class

circle:public

shape//公共继承

{

public:

circle(float

a=0,float

b=0,float

r=0):x(a),y(b),radius(r){}//用参数初始化表对构造函数进行初始化

virtual

void

show(){cout"x="x",y="y",radius="radius",area="area()endl;}//对虚函数进行重新定义

virtual

float

area(){return

3.14*radius*radius;}//同上

private:

float

x,y,radius;//数据成员

};

class

Rectangle:public

shape//公共继承

{

public:

Rectangle(float

w=0,float

l=0):width(w),length(l){}//同上

virtual

float

area(){return

width*length;}//同上

virtual

void

show(){cout"width="width",length="length",area="area()endl;}//同上

private:

float

width,length;

};

int

main()

{

circle

c(1.0,1.0,1.0);//定义circle类的对象c

c.show();//用对象c调用show函数

Rectangle

r(10.0,10.0);//定义Rectangle类对象r

r.show();//用对象r调用s...

#includeiostream

using

namespace

std;

class

shape

{

public:

virtual

float

area()=0;//纯虚函数

virtual

void

show()=0;//纯虚函数

};

class

circle:public

shape//公共继承

{

public:

circle(float

a=0,float

b=0,float

r=0):x(a),y(b),radius(r){}//用参数初始化表对构造函数进行初始化

virtual

void

show(){cout"x="x",y="y",radius="radius",area="area()endl;}//对虚函数进行重新定义

virtual

float

area(){return

3.14*radius*radius;}//同上

private:

float

x,y,radius;//数据成员

};

class

Rectangle:public

shape//公共继承

{

public:

Rectangle(float

w=0,float

l=0):width(w),length(l){}//同上

virtual

float

area(){return

width*length;}//同上

virtual

void

show(){cout"width="width",length="length",area="area()endl;}//同上

private:

float

width,length;

};

int

main()

{

circle

c(1.0,1.0,1.0);//定义circle类的对象c

c.show();//用对象c调用show函数

Rectangle

r(10.0,10.0);//定义Rectangle类对象r

r.show();//用对象r调用show函数

return

0;

}

这个应该符合要求。

展开

用C#定义基类Shape(形状类)计算面积和周长的windows界面程序

C++知识:定义抽象类shape应计算面积派计算形梯形圆形面积派类程序通基类指针调用派类虚函数计算同形状面积


名称栏目:c语言shape函数 c++shape类
文章URL:http://cdweb.net/article/hehgsj.html