问题及代码:
/*
* Copyright (c) 2014, 烟台大学计算机学院
* All rights reserved.
* 文件名称:test.cpp
* 作 者:宋健
* 完成日期:2015年 5月 26日
* 版 本 号:v1.0
*
* 问题描述:
* 程序输入:
* 程序输出:
*/
#include <iostream>
using namespace std;
const double pi=3.14;
class Point
{
protected:
double x,y;
public:
Point() {};
Point(double ,double);
double getx();
double gety();
friend ostream &operator <<(ostream &output,const Point &p);
};
class Circle:public Point
{
protected:
double r;
public:
double area();
Circle()
{
r=0;
};
void setr(double);
};
void Circle::setr(double m)
{
r=m;
}
double Circle::area()
{
double ar;
ar=pi*r*r;
return ar;
}
Point::Point(double m,double n)
{
x=m;
y=n;
}
double Point::getx()
{
return x;
}
double Point::gety()
{
return y;
}
ostream &operator <<(ostream &output,const Point &p)
{
output<<"("<<p.x<<","<<p.y<<")"<<endl;
return output;
}
int main()
{
Point p(2.3,8.4);
Circle c;
double r;
cout<<"x="<<p.getx( )<<",y="<<p.gety( )<<endl;
cout<<"p:"<<p<<endl;
cout<<"输入圆的半径:"<<endl;
cin>>r;
c.setr(r);
cout<<"圆的面积为:"<<c.area();
return 0;
}
运行结果:
知识点总结:
设置好数据类型