C++实现一些简单二维三维物体的属性计算(附简单测试用例,粘贴可用)

Posted Jing Sir

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C++实现一些简单二维三维物体的属性计算(附简单测试用例,粘贴可用)相关的知识,希望对你有一定的参考价值。

题目:
实现下图中的Shape层次结构。
每个二维图形类应包括图形名、平面位置(Point2D点对象)、边长或半径等数据成员,
成员函数包括有参构造函数、输出以及计算二维图形的面积和周长。
每个三维图形类应包括图形名、空间位置(Point3D点对象)、边长或半径等数据成员,
包含有参构造函数、输出以及计算三维图形的表面积和体积的成员函数。
用一个图形数组存放各种图形类对象,并用循环结构统一输出对象得相应信息,
二维图形输出类名、各自数据成员、周长和面积,
三维图形输出类名、各自数据成员、表面积及体积。
 

代码:

#include <iostream>
#include <cstring>
#include <cmath>
#define pi 3.1415926
using namespace std;
class Shape
{
protected:
    Shape *Shapes[100];
public:
    virtual void show()=0;
    virtual void add(Shape &p)=0;
};

class Point
{
protected:
    double x,y;
public:
    Point(double xx=0.0,double yy=0.0):x(xx),y(yy){}
    void show(){cout<<"POINT:"<<"("<<x<<","<<y<<")"<<endl;}
};
static int num=0;
class TwoDimensionalShape:public Shape
{
protected:
    char *name;

public:
    TwoDimensionalShape (char *name)
    {
        this->name=new char [30];
        strcpy(this->name,name);
    }
    ~TwoDimensionalShape(){delete[]name;}
    //virtual void show(){}
    void add(Shape &p)
    {
        Shapes[num]=&p;
        num++;
    }
    void  show()
    {
        for(int  i=0;i<num;i++)
        Shapes[i]->show();
    }
};

class Circle:public TwoDimensionalShape,public Point
{
    Point p1;
    double radium;
public:
    Circle (double radium=0.0,double xx=0.0,double yy=0.0):TwoDimensionalShape("TwoDimensionalShape Circle"),Point(xx,yy)
    {
        this->radium=radium;
    }
    void show();
    double area();//面积
    double lenth();//周长
};
double Circle::area()
{
    return pi*radium*radium;
}
double Circle::lenth()
{
    return 2*pi*radium;
}
void Circle::show()
{
    cout<<"NAME:"<<name<<endl;
    p1.show();
    cout<<"AREA:"<<area()<<endl;
    cout<<"LENTH:"<<lenth()<<endl;
}
class Square:public TwoDimensionalShape,public Point
{
private:
    Point p1;
    double l1,l2;
public:
    Square(double l1=0.0,double l2=0.0,double xx=0.0,double yy=0.0):TwoDimensionalShape("TwoDimensionalShape Square"),p1(xx,yy)
    {
        this->l1=l1;
        this->l2=l2;
    }
    void show();
    double area();
    double lenth();
};
double Square::area()
{
    return l1*l2;
}
double Square::lenth()
{
    return 2*(l1+l2);
}
void Square::show()
{
    cout<<"NAME:"<<name<<endl;
    p1.show();
    cout<<"AREA:"<<area()<<endl;
    cout<<"LENTH:"<<lenth()<<endl;
}
class Triangle:public Point,public TwoDimensionalShape
{
private:
    double l1,l2,l3;
    Point p1;
public:
    Triangle(double l1=0.0,double l2=0.0,double l3=0.0,double xx=0.0,double yy=0.0):p1(xx,yy),TwoDimensionalShape("TwoDimensionalShape Triangle")
    {
        this->l1=l1;
        this->l2=l2;
        this->l3=l3;
    }
    void show();
    double area();
    double lenth();
};
double Triangle::area()
{
    double p=(l1+l2+l3)/2;
    return sqrt(p*(p-l1)*(p-l2)*(p-l3));
}
double Triangle::lenth()
{
    return l1+l2+l3;
}
void Triangle::show()
{
    cout<<"NAME:"<<name<<endl;
    p1.show();
    cout<<"AREA:"<<area()<<endl;
    cout<<"LENTH:"<<lenth()<<endl;
}
class ThreeDimensionalShape:public Shape//,public TwoDimensionalShape
{
protected:
    char *name;

public:
    ThreeDimensionalShape (char *name)
    {
        this->name=new char [30];
        strcpy(this->name,name);
    }
    ~ThreeDimensionalShape(){delete[]name;}
    //virtual void show(){}
    void add(Shape &p)
    {

        Shapes[num]=&p;
        num++;
    }
    void  show()
    {
        for(int i=0;i<num;i++)
        Shapes[i]->show();
    }

};

class Cube:public ThreeDimensionalShape,public Point
{
private:
    double l1,l2,l3;
    Point p1;
public:
    Cube(double l1=0.0,double l2=0.0,double l3=0.0,double xx=0.0,double yy=0.0):p1(xx,yy),ThreeDimensionalShape("ThreeDimensionalShape Cube")
    {
        this->l1=l1;
        this->l2=l2;
        this->l3=l3;
    }
    void show();
    double area();
    double V();
};
double Cube::area(){return 2*l1*l2+2*l1*l3+2*l2*l3;}
double Cube::V(){return l1*l2*l3;}
void Cube::show()
{
    cout<<"NAME:"<<name<<endl;
    p1.show();
    cout<<"AREA:"<<area()<<endl;
    cout<<"VOLUME:"<<V()<<endl;
}
class Sphere:public ThreeDimensionalShape,public Point
{
private:
    double radium;
    Point p1;
public:
    Sphere(double radium=0.0,double xx=0.0,double yy=0.0):ThreeDimensionalShape("ThreeDimensionalShape Sphere"),Point(xx,yy)
    {
        this->radium=radium;
    }
    void show();
    double area();
    double V();
};
double Sphere::area(){return 4*pi*radium*radium;}
double Sphere::V(){return (4*pi*radium*radium*radium)/3;}
void Sphere::show()
{
    cout<<"NAME:"<<name<<endl;
    p1.show();
    cout<<"AREA:"<<area()<<endl;
    cout<<"VOLUME:"<<V()<<endl;
}

int main()
{
    Circle C1(2,1,1);
    Square S1(3,5,2,3);
    Triangle T1(3,4,5,1,8);
    Cube Cb1(2,3,4,6,6);
    Sphere Sp1(2,2,1);
    Shape *p;
    p=&C1;
    p->add(*p);
    p->show();
    p=&S1;
    p->add(*p);
    p->show();
    p=&T1;
    p->add(*p);
    p->show();
    p=&Cb1;
    p->add(*p);
    p->show();
    p=&Sp1;
    p->add(*p);
    p->show();
    cout << "Hello world!" << endl;
    return 0;
}

 

 

 

以上是关于C++实现一些简单二维三维物体的属性计算(附简单测试用例,粘贴可用)的主要内容,如果未能解决你的问题,请参考以下文章

一个四维的物体,任意切一刀,就成为三维的物体,怎么理解?

请问如何得到一个三维空间物体映射到二维空间的轮廓?

C++如何将一幅图像读入,并存为二维矩阵?

cad三维圆柱怎么旋转角度 步骤如下

四维物体怎么穿过三维空间

WPF三维图形