定义点类,从点类派生矩形类和圆类,主程序实现用同一个方法显示矩形和圆的面积
1个回答
展开全部
老师布置的作业要独立完成,我现在就后悔大学动脑太少,现在要补充太多东西。代码里我本来用了运算符重载,那样更方便,后来我给注视掉了,你可以自己去掉注释,然后修改主函数试试、、
#include <iostream>
using namespace std;
//声明类Point
class Point
{
public:
Point(float x=0,float y=0); //有默认参数的构造函数
void setPoint(float,float); //设置坐标值
float getX( ) const {return x;} //读x坐标
float getY( ) const {return y;} //读y坐标
// friend ostream & operator<<(ostream &,const Point &);//重载运算符“<<”
public: //公用成员
float x,y;
};
//下面定义Point类的成员函数
Point::Point(float a,float b) //Point的构造函数设置x和y的坐标值,
{x=a;y=b;}
void Point::setPoint(float a,float b) //为x,y赋新值
{x=a;y=b;}
//ostream & operator<<(ostream &output,const Point &p)//重载运算符“<<”,使之能输出点的坐标
//{
// output<<"["<<p.x<<","<<p.y<<"]"<<endl;
// return output;
//}
//rect是Point类的公用派生类
class Rect:public Point
{
public:
Rect(float x=0,float y=0);
float area() const;
// friend ostream &operator<<(ostream &,const Rect &);//重载运算符“<<”
};
//下面定义Rect类的成员函数
Rect::Rect(float a,float b):Point(a,b){}//初始化列表
float Rect::area() const
{return x*y;}
//ostream &operator<<(ostream &output,const Rect &r)//重载运算符“<<”,使之按规定的形式输出圆的信息
//{
// output<<"Rect=["<<r.x<<","<<r.y<<"],area="<<r.area( )<<endl;
// return output;
//}
//circle是Point类的公用派生类
class Circle:public Point
{
public:
Circle(float x=0,float y=0,float r=0); //构造函数
void setRadius(float); //设置半径值
float getRadius() const; //读取半径值
float area() const; //计算圆面积
// friend ostream &operator<<(ostream &,const Circle &);//重载运算符“<<”
private:
float radius;
};
//下面定义Circle类的成员函数
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,const Circle &c)//重载运算符“<<”,使之按规定的形式输出圆的信息
//{
// output<<"Center=["<<c.x<<","<<c.y<<"],r="<<c.radius<<",area="<<c.area( )<<endl;
// return output;
//}
int main( )
{
Circle m(5.5,5.5,10); //定义Circle类对象m
cout<<"圆心x坐标"<<m.getX()<<endl;
cout<<"圆心y坐标"<<m.getY()<<endl;
m.getRadius();
cout<<"圆的面积:"<<m.area()<<endl;
Point pRef(1,1);
cout<<"点的坐标"<<pRef.x <<" , "<<pRef.y<<endl;
Rect cRef(10,10);
cout<<"矩形的面积"<<cRef.area()<<endl;
return 0;
}
#include <iostream>
using namespace std;
//声明类Point
class Point
{
public:
Point(float x=0,float y=0); //有默认参数的构造函数
void setPoint(float,float); //设置坐标值
float getX( ) const {return x;} //读x坐标
float getY( ) const {return y;} //读y坐标
// friend ostream & operator<<(ostream &,const Point &);//重载运算符“<<”
public: //公用成员
float x,y;
};
//下面定义Point类的成员函数
Point::Point(float a,float b) //Point的构造函数设置x和y的坐标值,
{x=a;y=b;}
void Point::setPoint(float a,float b) //为x,y赋新值
{x=a;y=b;}
//ostream & operator<<(ostream &output,const Point &p)//重载运算符“<<”,使之能输出点的坐标
//{
// output<<"["<<p.x<<","<<p.y<<"]"<<endl;
// return output;
//}
//rect是Point类的公用派生类
class Rect:public Point
{
public:
Rect(float x=0,float y=0);
float area() const;
// friend ostream &operator<<(ostream &,const Rect &);//重载运算符“<<”
};
//下面定义Rect类的成员函数
Rect::Rect(float a,float b):Point(a,b){}//初始化列表
float Rect::area() const
{return x*y;}
//ostream &operator<<(ostream &output,const Rect &r)//重载运算符“<<”,使之按规定的形式输出圆的信息
//{
// output<<"Rect=["<<r.x<<","<<r.y<<"],area="<<r.area( )<<endl;
// return output;
//}
//circle是Point类的公用派生类
class Circle:public Point
{
public:
Circle(float x=0,float y=0,float r=0); //构造函数
void setRadius(float); //设置半径值
float getRadius() const; //读取半径值
float area() const; //计算圆面积
// friend ostream &operator<<(ostream &,const Circle &);//重载运算符“<<”
private:
float radius;
};
//下面定义Circle类的成员函数
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,const Circle &c)//重载运算符“<<”,使之按规定的形式输出圆的信息
//{
// output<<"Center=["<<c.x<<","<<c.y<<"],r="<<c.radius<<",area="<<c.area( )<<endl;
// return output;
//}
int main( )
{
Circle m(5.5,5.5,10); //定义Circle类对象m
cout<<"圆心x坐标"<<m.getX()<<endl;
cout<<"圆心y坐标"<<m.getY()<<endl;
m.getRadius();
cout<<"圆的面积:"<<m.area()<<endl;
Point pRef(1,1);
cout<<"点的坐标"<<pRef.x <<" , "<<pRef.y<<endl;
Rect cRef(10,10);
cout<<"矩形的面积"<<cRef.area()<<endl;
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询