用C++定义一个shape类

用C++定义一个shape类,第1张

#include<iostream>

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...

#include<iostream>

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

}

这个应该符合要求。

展开

#include <iostream>

#include <math.h>

using namespace std

#define PI 3.14

enum Colors{

Black,

White,

Orange,

Blue // etc...

}

class Shape{

public:

inline int Color() const

{return color}

void SetColor(int color)

{this->color = color}

virtual double Area() const=0

private:

int color

}

class Point{

public:

Point(double a, double b):x(a),y(b){}

double x

double y

}

class Rectangle : public Shape{

public:

Rectangle(double x, double y, double w, double h):

tl(x,y),br(x+w,y+h)

{

}

double Area() const

{return abs((tl.x - br.x)*(tl.y - br.y))}

private:

Point tl

Point br

}

class Circle: public Shape{

public:

Circle(double x, double y, double r):c(x,y){

this->r=r

}

double Area() const{

return PI*r*r

}

private:

Point c

double r

}

int main() {

Shape** shapes = new Shape*[2]

shapes[0] = new Rectangle(0.0,0.0,4.0,5.0)

shapes[0]->SetColor(White)

shapes[1] = new Circle(0.0,0.0,4.0)

shapes[1]->SetColor(Black)

for(int i(0)i<2i++){

cout <<"Area="<<shapes[i]->Area()<<endl

cout <<"Color="<<shapes[i]->Color()<<endl

}

delete[] shapes

return 0

}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/bake/11553279.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-16
下一篇 2023-05-16

发表评论

登录后才能评论

评论列表(0条)

保存