| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 712 人关注过本帖
标题:请指教:试了N次,编译通不过。,书上的例子,帮忙看一下错在那?
只看楼主 加入收藏
marenshi
Rank: 2
等 级:论坛游民
帖 子:23
专家分:17
注 册:2009-10-31
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:6 
请指教:试了N次,编译通不过。,书上的例子,帮忙看一下错在那?
#include<iostream.h>
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=0,float=0);
void setpoint(float =0,float =0);
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;
    float 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.getX()<<","<<p.getY()<<"]";
return output;
}
class Circle:public Point
{public:
Circle(float x=0,float y=0,float r=0 );
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.14159*radius*radius;}
ostream &operator<<(ostream &output,Circle &c)
{output<<"["<<c.getX()<<","<<c.getY()<<"],r="<<c.getradius();
return output;
}
class Cylinder:public Circle
{public:
Cylinder(float x=0,float y=0,float r=0,float h=0);
void setheight(float);
virtual float area()const;
virtual float volume() const;
virtual void  shapename() const{cout<<"Cylinder:";}
friend ostream &operator<<(ostream &,Cylinder &);
protected:
    float heigth;
};
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.14159*radius*height;}
float Cylinder::volume()
{return Circle::area()*height;
}
ostream &operator<<(ostream &output,Cylinder &cy)
{output<<"["<<cy.getX()<<","<<cy.getY()<<"],r="<<cy.getradius()<<",h="<<cy.getheight();
return output;
}
int main()
{Point point(3.2,4.5);
Circle circle(2.4,1.2,5.6);
Cylinder(3.5,6.4,5.2,10.5);
point.shapename();
cout<<point<<endl;
circle.shapename();
cout<<circle<<endl;
cylinder.shapename();
cout<<cylinder<<endl;
Shape *p;
p->shapename();
cout<<"x="<<point.getX()<<"y="<<point.getY()<<"\narea="<<p->area()<<"\nvolume="<<p->volume()<<"\n\n";
p=&circle;
p->shapename();
cout<<"x="<<circle.getX()<<"y="<<circle.getY()<<"\nnarea="<<p->area()<<"\nvolume="<<p->volume()<<"\n\n";
p=&cylinder;
cout<<"x="<<cylinder.getX()<<"y="<<cylinder.getY()<<"\narea"<<p->area()<<"\nvolume="<<p->volume()<<"\n\n";
return 0;
}
搜索更多相关主题的帖子: 例子 编译 指教 
2010-06-13 23:37
marenshi
Rank: 2
等 级:论坛游民
帖 子:23
专家分:17
注 册:2009-10-31
收藏
得分:0 
用的C++6.0环境
2010-06-13 23:41
lijm1989
Rank: 11Rank: 11Rank: 11Rank: 11
来 自:珠海
等 级:贵宾
威 望:12
帖 子:675
专家分:2844
注 册:2009-10-14
收藏
得分:0 
什么书来的?那么强?什么错误都有~~~
2010-06-14 00:17
marenshi
Rank: 2
等 级:论坛游民
帖 子:23
专家分:17
注 册:2009-10-31
收藏
得分:0 
谭浩强的C++程序设计
2010-06-15 20:50
cnfarer
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:179
帖 子:3330
专家分:21157
注 册:2010-1-19
收藏
得分:20 
编译能通过了,但估计还有错误存在!
程序代码:
#include<iostream>
#include<stdlib.h>
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=0,float=0);
void setpoint(float =0,float =0);
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;
    float 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.getX()<<","<<p.getY()<<"]";
return output;
}
class Circle:public Point
{public:
Circle(float x=0,float y=0,float r=0 );
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.14159*radius*radius;}
ostream &operator<<(ostream &output,Circle &c)
{output<<"["<<c.getX()<<","<<c.getY()<<"],r="<<c.getradius();
return output;
}
class Cylinder:public Circle
{public:
Cylinder(float x=0,float y=0,float r=0,float h=0);
void setheight(float);
virtual float area()const;
virtual float volume() const;
virtual void  shapename() const{cout<<"Cylinder:";}
virtual float getheight() const{return height;}
friend ostream &operator<<(ostream &,Cylinder &);
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.14159*radius*height;}
float Cylinder::volume() const
{return Circle::area()*height;
}
ostream &operator<<(ostream &output,Cylinder &cy)
{output<<"["<<cy.getX()<<","<<cy.getY()<<"],r="<<cy.getradius()<<",h="<<cy.getheight();
return output;
}
int main()
{Point point(3.2,4.5);
Circle circle(2.4,1.2,5.6);
Cylinder cylinder(3.5,6.4,5.2,10.5);
point.shapename();
cout<<point<<endl;
circle.shapename();
cout<<circle<<endl;
cylinder.shapename();
cout<<cylinder<<endl;
Shape *p;
p->shapename(); //这一句大概有问题吧!!! p所指未初始化
cout<<"x="<<point.getX()<<"y="<<point.getY()<<"\narea="<<p->area()<<"\nvolume="<<p->volume()<<"\n\n"; //这一句大概有问题吧!!! p所指未初始化

p=&circle;
p->shapename();
cout<<"x="<<circle.getX()<<"y="<<circle.getY()<<"\nnarea="<<p->area()<<"\nvolume="<<p->volume()<<"\n\n";
p=&cylinder;
cout<<"x="<<cylinder.getX()<<"y="<<cylinder.getY()<<"\narea"<<p->area()<<"\nvolume="<<p->volume()<<"\n\n";
return 0;
}


[ 本帖最后由 cnfarer 于 2010-6-15 22:27 编辑 ]

★★★★★为人民服务★★★★★
2010-06-15 22:18
cnfarer
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:179
帖 子:3330
专家分:21157
注 册:2010-1-19
收藏
得分:0 
在Shape *p;句下面加上p=&point;就彻底OK了!

★★★★★为人民服务★★★★★
2010-06-15 22:35
marenshi
Rank: 2
等 级:论坛游民
帖 子:23
专家分:17
注 册:2009-10-31
收藏
得分:0 
谢谢!看的我头也晕了。
2010-06-16 21:34
快速回复:请指教:试了N次,编译通不过。,书上的例子,帮忙看一下错在那?
数据加载中...
 
   



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

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