一个C++简单程序的问题
题目是:先建立一个Point(点)类,包含数据成员x,y(坐标点)。以它为基类,派生出一个Circle(圆)类,增加数据成员r(半径),再以Circle类为直接基类,派生...
题目是:先建立一个Point(点)类,包含数据成员x,y(坐标点)。以它为基类,派生出一个Circle(圆)类,增加数据成员r(半径),再以Circle类为直接基类,派生出一个Cylinder(圆柱体)类,再增加数据成员h(高)。要求编写程序,重载运算符“<<” 和“>>”,使之能用于输出以上类对象。
我写的程序:
#include<iostream.h>
#include<stdlib.h>
class Point
{
private:
int x;
int y;
public:
Point(int x,int y)
{
this->x=x;
this->y=y;
}
int getX()
{
return x;
}
int getY()
{
return y;
}
~Point()
{}
};
class Circle: public Point
{
private:
float r;
public:
Circle(int x,int y,float r)
:Point(x,y)
{
this->r=r;
}
float getR()
{
return r;
}
~Circle()
{}
};
class Cylinder:public Circle
{
private:
float h;
public:
Cylinder(int x,int y,float r,float h)
:Circle(x,y,r)
{
this->h=h;
}
float getH()
{
return h;
}
friend istream & operator >> (istream &in, Cylinder &c1);
friend ostream & operator << (ostream &out, Cylinder &c1);
};
istream & operator>>(istream &in,Cylinder &c1)
{
cout<<"请输入坐标:\n";
in>>c1.x>>c1.y;
cout<<"请输入半径:\n";
in>>c1.r;
cout<<"请输入高:\n";
in>>c1.h;
return in;
}
ostream & operator << (ostream &out,Cylinder &c1)
{
out<<"坐标为"<<"("<<c1.getX()<<","<<c1.getY()<<")"<<endl;
out<<"半径为"<<c1.getR()<<endl;
out<<"高为"<<c1.getH()<<endl;
return out;
}
int main()
{
Cylinder a;
cin>>a;
cout<<a;
return 0;
}
但是编译器报错::\Backup\我的文档\C++程序\3\3.cpp(65) : error C2248: 'x' : cannot access private member declared in class 'Point'
D:\Backup\我的文档\C++程序\3\3.cpp(6) : see declaration of 'x'
D:\Backup\我的文档\C++程序\3\3.cpp(65) : error C2248: 'y' : cannot access private member declared in class 'Point'
D:\Backup\我的文档\C++程序\3\3.cpp(7) : see declaration of 'y'
D:\Backup\我的文档\C++程序\3\3.cpp(67) : error C2248: 'r' : cannot access private member declared in class 'Circle'
D:\Backup\我的文档\C++程序\3\3.cpp(29) : see declaration of 'r'
D:\Backup\我的文档\C++程序\3\3.cpp(85) : error C2512: 'Cylinder' : no appropriate default constructor available
执行 cl.exe 时出错.
哪位大侠可以帮俺改改啊~~让它可以运行~~~ 展开
我写的程序:
#include<iostream.h>
#include<stdlib.h>
class Point
{
private:
int x;
int y;
public:
Point(int x,int y)
{
this->x=x;
this->y=y;
}
int getX()
{
return x;
}
int getY()
{
return y;
}
~Point()
{}
};
class Circle: public Point
{
private:
float r;
public:
Circle(int x,int y,float r)
:Point(x,y)
{
this->r=r;
}
float getR()
{
return r;
}
~Circle()
{}
};
class Cylinder:public Circle
{
private:
float h;
public:
Cylinder(int x,int y,float r,float h)
:Circle(x,y,r)
{
this->h=h;
}
float getH()
{
return h;
}
friend istream & operator >> (istream &in, Cylinder &c1);
friend ostream & operator << (ostream &out, Cylinder &c1);
};
istream & operator>>(istream &in,Cylinder &c1)
{
cout<<"请输入坐标:\n";
in>>c1.x>>c1.y;
cout<<"请输入半径:\n";
in>>c1.r;
cout<<"请输入高:\n";
in>>c1.h;
return in;
}
ostream & operator << (ostream &out,Cylinder &c1)
{
out<<"坐标为"<<"("<<c1.getX()<<","<<c1.getY()<<")"<<endl;
out<<"半径为"<<c1.getR()<<endl;
out<<"高为"<<c1.getH()<<endl;
return out;
}
int main()
{
Cylinder a;
cin>>a;
cout<<a;
return 0;
}
但是编译器报错::\Backup\我的文档\C++程序\3\3.cpp(65) : error C2248: 'x' : cannot access private member declared in class 'Point'
D:\Backup\我的文档\C++程序\3\3.cpp(6) : see declaration of 'x'
D:\Backup\我的文档\C++程序\3\3.cpp(65) : error C2248: 'y' : cannot access private member declared in class 'Point'
D:\Backup\我的文档\C++程序\3\3.cpp(7) : see declaration of 'y'
D:\Backup\我的文档\C++程序\3\3.cpp(67) : error C2248: 'r' : cannot access private member declared in class 'Circle'
D:\Backup\我的文档\C++程序\3\3.cpp(29) : see declaration of 'r'
D:\Backup\我的文档\C++程序\3\3.cpp(85) : error C2512: 'Cylinder' : no appropriate default constructor available
执行 cl.exe 时出错.
哪位大侠可以帮俺改改啊~~让它可以运行~~~ 展开
展开全部
#include<iostream.h>
#include<stdlib.h>
class Point
{
private:
int x;
int y;
public:
Point(int x,int y)
{
this->x=x;
this->y=y;
}
void setX(int a)
{
x=a;
}
void setY(int a)
{
y=a;
}
int getX()
{
return x;
}
int getY()
{
return y;
}
~Point()
{}
};
class Circle: public Point
{
private:
float r;
public:
Circle(int x,int y,float r)
:Point(x,y)
{
this->r=r;
}
void setR(float a)
{
r=a;
}
float getR()
{
return r;
}
~Circle()
{}
};
class Cylinder:public Circle
{
private:
float h;
public:
Cylinder():Circle(0,0,0),h(0){}
Cylinder(int x,int y,float r,float h)
:Circle(x,y,r)
{
this->h=h;
}
void setH(float a)
{
h=a;
}
float getH()
{
return h;
}
friend istream & operator >> (istream &in, Cylinder &c1);
friend ostream & operator << (ostream &out, Cylinder &c1);
};
istream & operator>>(istream &in,Cylinder &c1)
{
int _x,_y;
float _h,_r;
cout<<"请输入坐标:\n";
in>>_x>>_y;
cout<<"请输入半径:\n";
in>>_r;
cout<<"请输入高:\n";
in>>_h;
c1.setX(_x);
c1.setY(_y);
c1.setR(_r);
c1.setH(_h);
return in;
}
ostream & operator << (ostream &out,Cylinder &c1)
{
out<<"坐标为"<<"("<<c1.getX()<<","<<c1.getY()<<")"<<endl;
out<<"半径为"<<c1.getR()<<endl;
out<<"高为"<<c1.getH()<<endl;
return out;
}
int main()
{
Cylinder a;
cin>>a;
cout<<a;
return 0;
}
楼上的都说错了! 即使的getX() 也只是返回的一个拷贝! 仅仅返回的是一个值!不能改变原来对象的field!应该写一个set 函数! 值传递的知识 楼上的根本就不懂啊! 汗!
#include<stdlib.h>
class Point
{
private:
int x;
int y;
public:
Point(int x,int y)
{
this->x=x;
this->y=y;
}
void setX(int a)
{
x=a;
}
void setY(int a)
{
y=a;
}
int getX()
{
return x;
}
int getY()
{
return y;
}
~Point()
{}
};
class Circle: public Point
{
private:
float r;
public:
Circle(int x,int y,float r)
:Point(x,y)
{
this->r=r;
}
void setR(float a)
{
r=a;
}
float getR()
{
return r;
}
~Circle()
{}
};
class Cylinder:public Circle
{
private:
float h;
public:
Cylinder():Circle(0,0,0),h(0){}
Cylinder(int x,int y,float r,float h)
:Circle(x,y,r)
{
this->h=h;
}
void setH(float a)
{
h=a;
}
float getH()
{
return h;
}
friend istream & operator >> (istream &in, Cylinder &c1);
friend ostream & operator << (ostream &out, Cylinder &c1);
};
istream & operator>>(istream &in,Cylinder &c1)
{
int _x,_y;
float _h,_r;
cout<<"请输入坐标:\n";
in>>_x>>_y;
cout<<"请输入半径:\n";
in>>_r;
cout<<"请输入高:\n";
in>>_h;
c1.setX(_x);
c1.setY(_y);
c1.setR(_r);
c1.setH(_h);
return in;
}
ostream & operator << (ostream &out,Cylinder &c1)
{
out<<"坐标为"<<"("<<c1.getX()<<","<<c1.getY()<<")"<<endl;
out<<"半径为"<<c1.getR()<<endl;
out<<"高为"<<c1.getH()<<endl;
return out;
}
int main()
{
Cylinder a;
cin>>a;
cout<<a;
return 0;
}
楼上的都说错了! 即使的getX() 也只是返回的一个拷贝! 仅仅返回的是一个值!不能改变原来对象的field!应该写一个set 函数! 值传递的知识 楼上的根本就不懂啊! 汗!
展开全部
#include<iostream>
#include<stdlib.h>
using namespace std;
class Point
{
protected: //改成保护成员
int x;
int y;
public:
Point(){} //添加默认构造函数
Point(int x,int y)
{
this->x=x;
this->y=y;
}
int getX()
{
return x;
}
int getY()
{
return y;
}
~Point()
{}
};
class Circle: public Point
{
protected: //改成保护成员
float r;
public:
Circle(){} //添加默认构造函数
Circle(int x,int y,float r)
:Point(x,y)
{
this->r=r;
}
float getR()
{
return r;
}
~Circle()
{}
};
class Cylinder:public Circle
{
private: //改成保护成员
float h;
public:
Cylinder() //添加默认构造函数
{}
Cylinder(int x,int y,float r,float h)
:Circle(x,y,r)
{
this->h=h;
}
float getH()
{
return h;
}
friend istream & operator >> (istream &in, Cylinder &c1);
friend ostream & operator << (ostream &out, Cylinder &c1);
~Cylinder() //添加析构函数
{}
};
istream & operator>>(istream &in,Cylinder &c1)
{
cout<<"请输入坐标:\n";
in>>c1.x>>c1.y;
cout<<"请输入半径:\n";
in>>c1.r;
cout<<"请输入高:\n";
in>>c1.h;
return in;
}
ostream & operator << (ostream &out,Cylinder &c1)
{
out<<"坐标为"<<"("<<c1.getX()<<","<<c1.getY()<<")"<<endl;
out<<"半径为"<<c1.getR()<<endl;
out<<"高为"<<c1.getH()<<endl;
return out;
}
int main()
{
Cylinder a;
cin>>a;
cout<<a;
return 0;
}
ps.1.创建对象时,要调用构造函数,当你自己创建了一个构造函数后,程序便不再自动生成默认构造函数,所以你要自己加上(无参数);
2.每个类都要有析构函数;
3.公有继承的派生类,不能访问基函数的私有成员。
#include<stdlib.h>
using namespace std;
class Point
{
protected: //改成保护成员
int x;
int y;
public:
Point(){} //添加默认构造函数
Point(int x,int y)
{
this->x=x;
this->y=y;
}
int getX()
{
return x;
}
int getY()
{
return y;
}
~Point()
{}
};
class Circle: public Point
{
protected: //改成保护成员
float r;
public:
Circle(){} //添加默认构造函数
Circle(int x,int y,float r)
:Point(x,y)
{
this->r=r;
}
float getR()
{
return r;
}
~Circle()
{}
};
class Cylinder:public Circle
{
private: //改成保护成员
float h;
public:
Cylinder() //添加默认构造函数
{}
Cylinder(int x,int y,float r,float h)
:Circle(x,y,r)
{
this->h=h;
}
float getH()
{
return h;
}
friend istream & operator >> (istream &in, Cylinder &c1);
friend ostream & operator << (ostream &out, Cylinder &c1);
~Cylinder() //添加析构函数
{}
};
istream & operator>>(istream &in,Cylinder &c1)
{
cout<<"请输入坐标:\n";
in>>c1.x>>c1.y;
cout<<"请输入半径:\n";
in>>c1.r;
cout<<"请输入高:\n";
in>>c1.h;
return in;
}
ostream & operator << (ostream &out,Cylinder &c1)
{
out<<"坐标为"<<"("<<c1.getX()<<","<<c1.getY()<<")"<<endl;
out<<"半径为"<<c1.getR()<<endl;
out<<"高为"<<c1.getH()<<endl;
return out;
}
int main()
{
Cylinder a;
cin>>a;
cout<<a;
return 0;
}
ps.1.创建对象时,要调用构造函数,当你自己创建了一个构造函数后,程序便不再自动生成默认构造函数,所以你要自己加上(无参数);
2.每个类都要有析构函数;
3.公有继承的派生类,不能访问基函数的私有成员。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
private:
int x;
int y;
你定义了,x,y是私用的,当然不能直接用了,所以,编译时候,会报错.
若要用到x,y的值,你可以用下面2个函数,这也是为什么要定义这2个函数的原因
int getX()
{
return x;
}
int getY()
{
return y;
}
这2个函数的返回值都是int ,你可以把它的返回值赋给你要用的变量.
这个程序,好像在哪里的书上看过.....认真对比下书,就懂了。
int x;
int y;
你定义了,x,y是私用的,当然不能直接用了,所以,编译时候,会报错.
若要用到x,y的值,你可以用下面2个函数,这也是为什么要定义这2个函数的原因
int getX()
{
return x;
}
int getY()
{
return y;
}
这2个函数的返回值都是int ,你可以把它的返回值赋给你要用的变量.
这个程序,好像在哪里的书上看过.....认真对比下书,就懂了。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你那个x,y是private的,不能直接c1.x
写个getx()
写个getx()
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询