操作符重载问题

pt2000good 2005-02-21 08:57:36
class point
{
friend ostream &operator<<(ostream &,const point &);
public:point(int=0,int=0);
int getx()const{return x;}
int gety()const{return y;}
void setpoint(int,int);
protected:int x,y;
};
.......

ostream &operator<<(ostream &output,const point &p)
{
output<<'['<<p.x<<","<<p.y<<']';
return output;
}
class square:public point
{
friend ostream &operator<<(ostream &output,square &s);
public:
square(int=0,int=0,double=0.0);
void setside(double);
double getside()const{return side;};
double area()const{return side*side;};
protected:
int side;
point p1;
point p2;
point p3;
};
square::square(int a,int b,double c)
:p1(a+c,b),
p2(a+c,b+c),
p3(a,b+c),
point(a,b)
{
setside(c);
}
......
ostream &operator<<(ostream &output,square &s)
{

output<<"square:"<<endl;
output<<"p1="<<'['<<s.p1.getx()<<","<<s.p1.gety()<<']'<<endl;
output<<"p2="<<'['<<s.p2.getx()<<","<<s.p2.gety()<<']'<<endl;
output<<"p3="<<'['<<s.p3.getx()<<","<<s.p3.gety()<<']'<<endl;
output<<"p4="<<static_cast<point>(s)<<endl;
output<<"side="<<s.side<<endl;
return output;
}
class cube:public square
{
friend ostream &operator<<(ostream &output,cube &d);
public:
cube(int=0,int=0,double=0,double=0);
void setheight(double);
double getheight()const{return height;};
double area(){return square::area()*height;};
protected:
double height;
};
cube::cube(int a,int b,double c,double d)
:square(a,b,c)
{
setheight(d);
}
ostream &operator<<(ostream &output,cube &c)
{
output<<static_cast<square>(c)<<endl;
output<<"height="<<c.height<<endl;
return output;
}
void main()
{
cube d(1,2,3,4);
cout<<d<<endl;
}
输出结果没有执行ostream &operator<<(ostream &output,square &s)是为什么?

...全文
114 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
pt2000good 2005-02-22
  • 打赏
  • 举报
回复
你的运行结果怎么样的?
我的显示没有执行output<<"p1="<<'['<<s.p1.getx()<<","<<s.p1.gety()<<']'<<endl;
output<<"p2="<<'['<<s.p2.getx()<<","<<s.p2.gety()<<']'<<endl;
output<<"p3="<<'['<<s.p3.getx()<<","<<s.p3.gety()<<']'<<endl;
调试跟踪也说明没执行ostream &operator<<(ostream &output,square &s);
Dong 2005-02-21
  • 打赏
  • 举报
回复
你自己看看是不是的,我是没有问题的

#include <iostream>
using namespace std;
class point;
ostream &operator<<(ostream &,const point &);
class point
{
friend ostream &operator<<(ostream &,const point &);
public:point(int i,int j):x(i),y(j){};
int getx()const{return x;}
int gety()const{return y;}
void setpoint(int,int);
protected:int x,y;
};


ostream &operator<<(ostream &output,const point &p)
{
output<<'['<<p.x<<","<<p.y<<']';
return output;
}

class square;
ostream &operator<<(ostream &output,square &s);
class square:public point
{
friend ostream &operator<<(ostream &output,square &s);
public:
square(int=0,int=0,double=0.0);
void setside(double d){side = d;}
double getside()const{return side;};
double area()const{return side*side;};
protected:
int side;
point p1;
point p2;
point p3;
};
square::square(int a,int b,double c)
:p1(a+c,b),
p2(a+c,b+c),
p3(a,b+c),
point(a,b)
{
setside(c);
}

ostream &operator<<(ostream &output,square &s)
{

output<<"square:"<<endl;
output<<"p1="<<'['<<s.p1.getx()<<","<<s.p1.gety()<<']'<<endl;
output<<"p2="<<'['<<s.p2.getx()<<","<<s.p2.gety()<<']'<<endl;
output<<"p3="<<'['<<s.p3.getx()<<","<<s.p3.gety()<<']'<<endl;
output<<"p4="<<static_cast<point>(s)<<endl;
output<<"side="<<s.side<<endl;
return output;
}
class cube:public square
{
friend ostream &operator<<(ostream &output,cube &d);
public:
cube(int=0,int=0,double=0,double=0);
void setheight(double d){height = d;}
double getheight()const{return height;};
double area(){return square::area()*height;};
public:
double height;
};
cube::cube(int a,int b,double c,double d)
:square(a,b,c)
{
setheight(d);
}
ostream &operator<<(ostream &output,cube &c)
{
output<<static_cast<square>(c)<<endl;
output<<"height="<<c.height<<endl;
return output;
}
void main()
{
cube d(1,2,3,4);
cout<<d<<endl;
}

64,680

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧