| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 505 人关注过本帖
标题:简单问题
只看楼主 加入收藏
心动音符
Rank: 1
等 级:禁止访问
威 望:1
帖 子:832
专家分:0
注 册:2005-9-15
收藏
 问题点数:0 回复次数:2 
简单问题

#include <iostream>
using namespace std;
class shape
{public:
virtual float area() const {return 0.0;}
virtual float volume() const {return 0.0;}
virtual void shapename() const =0;
};

class point:public shape
{public:
point(float,float);
void setpoint(float,float);
float getX()const {return x;}
float getY()const {return y;}
virtual void shapename() const{cout<<"point:";}
friend ostream & operator << (ostream &,const point &);
protected:
float x,y;
};

point::point(float a,float b)
{x=a;y=b;}

void point::setpoint(float a,float b)
{x=a;y=b;}

ostream&operator<<(ostream &output,const point &p)
{
output<<"["<<p.x<<","<<p.y<<"]";
return output;
}
class circle:public point
{public:
circle(float,float,float);
void setRadius(float);
float getradius()const;
virtual float area() const;
virtual void shapename()const {cout<<"circle:";}
friend ostream&operator<<(ostream &,const circle &);
protected:
float radius;
};
circle::circle(float a,float b,float r):point(a,b),radius(r){}
void circle::setRadius(float r){radius=r;}
float circle::getRadius()const{return radius;}
float circle::area()const {return 3.14*radius*radius;}

ostream&operator<<(ostream &output,const circle &c)
{output<<"["<<c.x<<","<<c.y<<"],r="<<c.radius;
return output;
}
class cylinder:public circle
{public :
cylinder(float x,float y,float r,float h);
void setheight(float);
virtual float area()const;
virtual float volume()const;;
virtual void shapename()const{cout<<"cylinder:";}
friend ostream&operator<<(ostream &,const cylinder&);
protected:
float height;
};
cylinder::cylinder(float a,float b,float r,float h):circle(a,b,r),height(h){}
void cylinder::getheight(flaot h){height=h;}
float cylinder::area()const
{return 2*circle::area()+2*3.14*radius*height;}
float cylinder::volume()const
{return circle::area()*height;}
ostream&operator<<(ostream &output,const cylinder &cy)
{output<<"["<<cy.x<<","<<cy.y<<"],r="<<cy.radius<<",h="<<cy.height;
return output;
}
int main()
{point point(3.2,4.3);
circle circle(2.4,1.2,5.6);
cylinder cylinder(3.2,4.4,5.4,4.6);
point.shapename();
cout<<point<<endl;
circle.shapename();
cout<<circle<<endl;
cylinder.shapename();
cout<<cylinder<<endl<<endl;
shape *pt;
pt=&point;
pt->shapename();
cout<<"x="<<point.getX()<<",y="<<point.getY()<<"\area="<<pt->area()
<<"\nvolume="<<pt->volume()<<"\n\n";
pt=&circle;
pt->shapename();
cout<<"x="<<circle.getX()<<",y="<<circle.getY()<<"\narea="<<pt->area()
<<"\nvolume="<<pt->volume()<<"\n\n";
pt=&cylinder;
pt->shapename();
cout<<"x="<<circle.getX()<<",y="<<circle.getY()<<"\narea="<<pt->area()
<<"\nvolume="<<pt->volume()<<"\n\n";
return 0;
}
找了半天不知道错在哪啊 我按书上打的啊
大家帮我看看

搜索更多相关主题的帖子: 简单问题 
2006-10-01 19:51
wfpb
Rank: 6Rank: 6
等 级:贵宾
威 望:29
帖 子:2188
专家分:0
注 册:2006-4-2
收藏
得分:0 

呵呵,你可真喜欢偷懒,以后调试吧。

程序代码:

#include <iostream>
using namespace std;
class shape
{
public:
virtual float area() const {return 0.0;}
virtual float volume() const {return 0.0;}
virtual void shapename() const =0;
};

class point:public shape
{
public:
point(float,float);
void setpoint(float,float);
float getX()const {return x;}
float getY()const {return y;}
virtual void shapename() const{cout<<\"point:\";}
friend ostream&operator<<(ostream &output,const point &p)
{
output<<\"[\"<<p.x<<\",\"<<p.y<<\"]\";
return output;
}
protected:
float x,y;
};

point::point(float a,float b)
{
x=a;
y=b;
}

void point::setpoint(float a,float b)
{
x=a;
y=b;
}


class circle:public point
{
public:
circle(float,float,float);
void setRadius(float);
float getRadius()const;
virtual float area() const;
virtual void shapename()const {cout<<\"circle:\";}
friend ostream&operator<<(ostream &output,const circle &c)
{
output<<\"[\"<<c.x<<\",\"<<c.y<<\"],r=\"<<c.radius;
return output;
}
protected:
float radius;
};

circle::circle(float a,float b,float r)
: point(a,b)
, radius(r)
{

}

void circle::setRadius(float r)
{
radius=r;
}
float circle::getRadius()const
{
return radius;
}
float circle::area()const
{
return 3.14*radius*radius;
}


class cylinder:public circle
{
public :
cylinder(float x,float y,float r,float h);
void setheight(float);
virtual float area()const;
virtual float volume()const;;
virtual void shapename()const{cout<<\"cylinder:\";}
friend ostream&operator<<(ostream &output,const cylinder &cy)
{
output<<\"[\"<<cy.x<<\",\"<<cy.y<<\"],r=\"<<cy.radius<<\",h=\"<<cy.height;
return output;
}
protected:
float height;
};

cylinder::cylinder(float a,float b,float r,float h)
: circle(a,b,r)
, height(h)
{

}
void cylinder::setheight(float h)
{
height=h;
}

float cylinder::area()const
{
return 2*circle::area()+2*3.14*radius*height;
}

float cylinder::volume()const
{
return circle::area()*height;
}

int main()
{
point point(3.2f,4.3f);
circle circle(2.4f,1.2f,5.6f);
cylinder cylinder(3.2f,4.4f,5.4f,4.6f);
point.shapename();
cout<<point<<endl;
circle.shapename();
cout<<circle<<endl;
cylinder.shapename();
cout<<cylinder<<endl<<endl;
shape *pt;
pt=&point;
pt->shapename();
cout<<\"x=\"<<point.getX()<<\",y=\"<<point.getY()<<\"\area=\"<<pt->area()
<<\"\nvolume=\"<<pt->volume()<<\"\n\n\";
pt=&circle;
pt->shapename();
cout<<\"x=\"<<circle.getX()<<\",y=\"<<circle.getY()<<\"\narea=\"<<pt->area()
<<\"\nvolume=\"<<pt->volume()<<\"\n\n\";
pt=&cylinder;
pt->shapename();
cout<<\"x=\"<<circle.getX()<<\",y=\"<<circle.getY()<<\"\narea=\"<<pt->area()
<<\"\nvolume=\"<<pt->volume()<<\"\n\n\";
return 0;
}


[glow=255,red,2]wfpb的部落格[/glow] 学习成为生活的重要组成部分!
2006-10-01 22:00
song4
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:38
帖 子:1533
专家分:4
注 册:2006-3-25
收藏
得分:0 
wfpb:你解没解决也不说,解决了我就不看了
哎,就当你解决了,先不看了,对了,你快毕业了吧
怎么样了

嵌入式 ARM 单片机 驱动 RT操作系统 J2ME LINUX  Symbian C C++ 数据结构 JAVA Oracle 设计模式 软件工程 JSP
2006-10-03 19:23
快速回复:简单问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.041071 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved