半小时内结贴!一个简单的考试题!大家帮忙啊解决方法
半小时内结贴!一个简单的考试题!大家帮忙啊!
#include <iostream.h>
class Shape{
public:
virtual float area(){return 0.0;}
virtual float volume(){return 0.0;}
virtual void printShapeName()=0;
};
class Point:public Shape{
public:
Point(float a=0,float b=0){x=a;y=b;}
virtual void printShapeName(){cout < < "点: ";}
float GetX(){return x;}
float GetY(){return y;}
private:
float x,y;
};
class Circle:public Point{
public:
Circle(float r=0.0,float x=0.0,float y=0.0):Point(x,y)
{radius=r> 0?r:0;}
float getRadius(){return radius;}
virtual float area(){return 3.1415*radius*radius;}
virtual void printShapeName(){cout < < "圆: ";}
private:
float radius;
};
class Cylinder:public Circle{
Cylinder(float h=0.0,float r=0.0,float x=0.0,float y =0.0):Circle(r,x,y)
{height=h> 0?h:0;}
float volume()
{
float r=Vircle::getRadius();
return 3.14159 * r * r * height;
}
virtual void printShapeName(){cout < < "圆柱: ";}
private:
float height;
};
void main(){
Shape *p;
Point pt(4,5);Circle cr(6,2,10);Cylinder cy(15,8,100,10);
p=&pt;
p-> printShapeName();
cout < < "x= " < <pt.GetX() < < ",y= " < <pt.GetY() < < ",面积= " < <int (p-> area()) < < ",体积= " < <p-> volume() < <endl;
p=&cy;
p-> printShapeName();
cout < < "x= " < <cy.GetX() < < ",y= " < <cy.GetY() < < ",面积= " < <int (p-> area()) < < ",体积= " < <int (p-> volume()) < <endl;
p=&cr;
p-> printShapeName();
cout < < "x= " < <cr.GetX() < < ",y= " < <cr.GetY() < < ",面积= " < <int (p-> area()) < < ",体积= " < <int (p-> volume()) < <endl;
}
实在找不到错错哪了!谢谢了啊!快啊~马上就要交了!
D:\C++\3.cpp(22) : warning C4244: 'return ' : conversion from 'double ' to 'float ', possible loss of data
D:\C++\3.cpp(32) : error C2653: 'Vircle ' : is not a class or namespace name
D:\C++\3.cpp(33) : warning C4244: 'return ' : conversion from 'double ' to 'float ', possible loss of data
D:\C++\3.cpp(41) : error C2248: 'Cylinder::Cylinder ' : cannot access private member declared in class 'Cylinder '
D:\C++\3.cpp(28) : see declaration of 'Cylinder::Cylinder '
执行 cl.exe 时出错.
------解决方案--------------------
你的错误实在太多,我改了一下,代码如下:(编译能过,但是逻辑问题没仔细看了)
#include <iostream.h>
class Shape{
public:
virtual double area(){return 0.0;}
virtual double volume(){return 0.0;}
#include <iostream.h>
class Shape{
public:
virtual float area(){return 0.0;}
virtual float volume(){return 0.0;}
virtual void printShapeName()=0;
};
class Point:public Shape{
public:
Point(float a=0,float b=0){x=a;y=b;}
virtual void printShapeName(){cout < < "点: ";}
float GetX(){return x;}
float GetY(){return y;}
private:
float x,y;
};
class Circle:public Point{
public:
Circle(float r=0.0,float x=0.0,float y=0.0):Point(x,y)
{radius=r> 0?r:0;}
float getRadius(){return radius;}
virtual float area(){return 3.1415*radius*radius;}
virtual void printShapeName(){cout < < "圆: ";}
private:
float radius;
};
class Cylinder:public Circle{
Cylinder(float h=0.0,float r=0.0,float x=0.0,float y =0.0):Circle(r,x,y)
{height=h> 0?h:0;}
float volume()
{
float r=Vircle::getRadius();
return 3.14159 * r * r * height;
}
virtual void printShapeName(){cout < < "圆柱: ";}
private:
float height;
};
void main(){
Shape *p;
Point pt(4,5);Circle cr(6,2,10);Cylinder cy(15,8,100,10);
p=&pt;
p-> printShapeName();
cout < < "x= " < <pt.GetX() < < ",y= " < <pt.GetY() < < ",面积= " < <int (p-> area()) < < ",体积= " < <p-> volume() < <endl;
p=&cy;
p-> printShapeName();
cout < < "x= " < <cy.GetX() < < ",y= " < <cy.GetY() < < ",面积= " < <int (p-> area()) < < ",体积= " < <int (p-> volume()) < <endl;
p=&cr;
p-> printShapeName();
cout < < "x= " < <cr.GetX() < < ",y= " < <cr.GetY() < < ",面积= " < <int (p-> area()) < < ",体积= " < <int (p-> volume()) < <endl;
}
实在找不到错错哪了!谢谢了啊!快啊~马上就要交了!
D:\C++\3.cpp(22) : warning C4244: 'return ' : conversion from 'double ' to 'float ', possible loss of data
D:\C++\3.cpp(32) : error C2653: 'Vircle ' : is not a class or namespace name
D:\C++\3.cpp(33) : warning C4244: 'return ' : conversion from 'double ' to 'float ', possible loss of data
D:\C++\3.cpp(41) : error C2248: 'Cylinder::Cylinder ' : cannot access private member declared in class 'Cylinder '
D:\C++\3.cpp(28) : see declaration of 'Cylinder::Cylinder '
执行 cl.exe 时出错.
------解决方案--------------------
你的错误实在太多,我改了一下,代码如下:(编译能过,但是逻辑问题没仔细看了)
#include <iostream.h>
class Shape{
public:
virtual double area(){return 0.0;}
virtual double volume(){return 0.0;}