求各位大神。帮忙

C++语言 码拜 9年前 (2015-05-11) 1098次浏览 0个评论
 

#include<iostream>
#include <cmath>
using namespace std;

const double PI = 3.14159;
class Point
{
private:
double x, y;
public:
Point(double xx=0, double yy=0){
x = xx;
y = yy;
}
void set(double xx = 0, double yy = 0)
{
x = xx;
y = yy;
}

void display()
{
cout <<  “(” << x << “,” << y << “)” << endl;
}
double getX(){

return x;
}
double getY(){
return y;
}
double distance(Point n){
double dx = x – n.x;
double dy = y – n.y;
return sqrt(dx*dx + dy*dy);

}
};

class Rectangle :public Point  //矩形 
{
protected:
double length, width;
Point center;
public:
Rectangle(double l=1, double w=1, double xx=0, double yy=0) :Point(xx, yy), length(l), width(w){}
void set(double l = 0, double w = 0, double xx = 0, double yy = 0)
{
Point::set(xx,yy);
length = l;
width = w;
}

double Area()
{
return length*width;
}

double Peri()
{
return (length + width) * 2;
}
Point  getcenter(){
return center;
}

void display()
{
cout << “矩形的中心坐标:”;
Point::display();
cout << “矩形的长度: ” << length << endl;
cout << “矩形的宽度: ” << width << endl;
}
};

class Circle : public Point   //圆形 
{
protected:
double radius;
Point center;
public:
Circle(double R=1, double xx=0, double yy=0) :Point(xx, yy), radius(R){}
void set(double R = 0, double xx = 0, double yy = 0)
{
Point::set(xx, yy);
radius = R;
}

double Area()
{
return PI*radius*radius;
}

double Peri()
{
return 2 * PI*radius;
}
Point  getcenter(){
return center;
}
void display()
{
cout << “圆形的中心坐标::” ;
Point::display();
cout << “圆形的半径: ” << radius << endl;

}
};

class Triangle : public Point     // 三角形 
{

protected:
double SideA, SideB, SideC;
double l;
Point center;

public:
Triangle(double sa=3, double sb=4, double sc=5, double xx=0, double yy=0) :Point(xx, yy), SideA(sa), SideB(sb), SideC(sc){}
void set(double sa = 0, double sb = 0, double sc = 0, double xx = 0, double yy = 0)
{
Point::set(xx, yy);
SideA = sa;
SideB = sb;
SideC = sc;
}

double Area()
{
l = (SideA + SideB + SideC) / 2;
return sqrt(l*(l – SideA)*(l – SideB)*(l – SideC));
}

double Peri()
{
return SideA + SideB + SideC;
}
Point  center(){
return center;
}
void display()
{
cout << “三角形的中心坐标:” ;
Point::display();
cout << “三角形的三条边:” << SideA << ” ” << SideB << ” ” << SideC << endl;
}
};

class RecCuboid :public Rectangle     //长方体 
{
private:
double high;
public:
RecCuboid(double h=1, double l=1, double w=1, double xx=0, double yy=0) :Rectangle(l, w, xx, yy), high(h){}
void set(double h = 0, double l = 0, double w = 0, double xx = 0, double yy = 0)
{
Rectangle::set(l, w, xx, yy);
high = h;
}

double  Volume()
{
cout << “长方体的体积 :” << length*width*high << endl;
return 0;
}
void display(){
Rectangle::display();
cout << “长方体的高度:” << high << endl;

}
};

class CirCuboid :public Circle   //圆柱 
{
private:
double high;
public:
CirCuboid(double h=1, double r=1, double xx=0, double yy=0) :Circle(r, xx, yy), high(h){}
void set(double h = 0, double r = 0, double xx = 0, double yy = 0)
{
Circle::set(r, xx, yy);
high = h;
}

double  Volume()
{
cout << “圆柱的体积 :” << PI*radius*radius*high << endl;
return 0;
}
void display(){
Circle::display();
cout << “圆柱的高度:” << high << endl;
 
}
};

class TriCuboid :public Triangle      //三菱住 
{
private:
double high;
public:
TriCuboid(double h=1, double sa=3, double sb=4, double sc=5, double xx=0, double yy=0) :Triangle(sa, sb, sc, xx, yy), high(h){}
void set(double h = 1, double sa = 0, double sb = 0, double sc = 0, double xx = 0, double yy = 0)
{
Triangle::set(sa, sb, sc, xx, yy);
high = h;
}

 double Volume()
{
cout << “三菱住的体积 :” << sqrt(0.33*l*(l – SideA)*(l – SideB)*(l – SideC))*high << endl;
return 0;
}
void display(){
Triangle::display();
cout << “三菱柱的高度:” << high << endl;

}
};
  void display(Point &pp,int type){
Rectangle *pr;
Circle *pc;
Triangle *pt;
CirCuboid *pcu;//长方体、
RecCuboid *pcy;//圆柱
TriCuboid *ptr;  //三菱柱
switch (type){
  case 1:
pr = reinterpret_cast<Rectangle *>(&pp);
cout << “矩形的面积;” << pr->Area() <<“/t矩形的周长:”<<pr->Peri()<< endl;
break;
case 2:
pc = reinterpret_cast<Circle *>(&pp);
cout << “圆形的面积;” << pc->Area() << “/t圆形的周长:” << pc->Peri() << endl;
break;
case 3:
pt = reinterpret_cast<Triangle *>(&pp);
cout << “三角形的面积;” << pt->Area() << “/t三角形的周长:” << pt->Peri() << endl;
break;
case 11:
pcu = reinterpret_cast<CirCuboid *>(&pp);
cout << “长方体的体积:” << pcu->Volume() << endl;
break;
case 12:
pcy = reinterpret_cast<RecCuboid *>(&pp);
cout << “圆柱的体积:” << pcy->Volume() << endl;
break;
case 13:
ptr = reinterpret_cast<TriCuboid *>(&pp);
cout << “三菱柱的体积:” << ptr->Volume() << endl;
break;

}

}
  double distance(const Point &rp1,const Point &rp2){
  double dx = rp1.getX() – rp2.getX();
  double dy = rp1.getY() – rp2.getY();
  return sqrt(dx*dx + dy*dy);
  }
int main()
{  
Point objPoint;
objPoint.set(5.10);
cout << “点的坐标为:”;
objPoint.display();
cout << ”                                        ” << endl;

Rectangle objRectangle;
objRectangle.set(10, 15, 20, 40);
objRectangle.display();
cout << “矩形的面积:” << objRectangle.Area() << endl;
cout << “矩形的周长:” << objRectangle.Peri() <<endl;
cout << ”                                     ” << endl;

Circle objCircle;
objCircle.set(10, 15, 30);
objCircle.display();
cout << “圆形的面积:” <<objCircle.Area()<< endl;
cout << “圆形的周长:” << objCircle.Peri() << endl;
cout << ”                                  ” << endl;
Triangle objTriangle;
objTriangle.set(30, 35, 40, 15, 20);
objTriangle.display();
cout << “三角形的面积:” << objTriangle.Area() << endl;
cout << “三角形的周长:” << objTriangle.Peri() << endl;
cout << ”                                ” << endl;

RecCuboid objRecCuboid;
objRecCuboid.set(10, 15, 20, 40,16);
objRecCuboid.display();
cout << “长方体的体积:” << objRecCuboid.Volume() << endl;
cout << ”                        ” << endl;

CirCuboid objCirCuboid;
objCirCuboid.set(10,15,30,20);
objCirCuboid.display();
cout << “圆柱的体积:” <<objCirCuboid.Volume()<<endl;
cout << ”                                     ” << endl;

TriCuboid objTriCuboid;
objTriCuboid.set(20, 25, 22,13, 10, 5);
objTriCuboid.display();
cout << “三菱柱的体积:” <<objTriCuboid.Volume() <<endl;
cout << ”                                    ” << endl;

cout << “矩形中心和三角形中心的距离:” << distance(objRectangle, objTriangle) << endl;
cout << “圆形与三角形的距离:” << objCircle.distance(objTriangle) << endl;

cout << “利用全局函数显示信息:” << endl;
display(objRectangle, 1);
display(objCircle, 2);
display(objTriangle, 3);
}
总是提示错误信息:
1>g:\用vs编程的acm\派生类的继承,兼容类型\派生类的继承,兼容类型\派生类的继承和兼容类型.cpp(138): error C2365: “Triangle::center”: 重定义;以前的定义是“数据成员”
1>          g:\用vs编程的acm\派生类的继承,兼容类型\派生类的继承,兼容类型\派生类的继承和兼容类型.cpp(116) : 参见“Triangle::center”的声明
1>g:\用vs编程的acm\派生类的继承,兼容类型\派生类的继承,兼容类型\派生类的继承和兼容类型.cpp(261): error C2662: “double Point::getX(void)”: 不能将“this”指针从“const Point”转换为“Point &”
1>          转换丢失限定符
1>g:\用vs编程的acm\派生类的继承,兼容类型\派生类的继承,兼容类型\派生类的继承和兼容类型.cpp(262): error C2662: “double Point::getY(void)”: 不能将“this”指针从“const Point”转换为“Point &”
1>          转换丢失限定符

40分
Point  center(){  // 改成getcenter
return center;
}

double getX() {
改成  double getX() const {

double getY() {
改成  double getY() const {

http://blog.csdn.net/lihao21/article/details/8634876

谢谢。。解决了

那个\用vs编程的acm\派生类的继承,兼容类型\派生类的继承,兼容类型\派生类的继承和兼容类型.cpp(138): error C2365: “Triangle::center”: 重定义;以前的定义是“数据成员”
1>          g:\用vs编程的acm\派生类的继承,兼容类型\派生类的继承,兼容类型\派生类的继承和兼容类型.cpp(116) : 参见“Triangle::center”的声明
 是那个成员函数和数据成员 的同名了。。谢谢了,。谢谢大神


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明求各位大神。帮忙
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!