(提示:此任务可以分为三个子任务分成若干步骤进行。先声明基类,再声明派生类,逐级进行,分步调试。——这种方法适用于做任何的项目)
运行代码:
/* *Copyright (c)2014,烟台大学计算机与控制工程学院 *All rights reserved. *文件名称:d.cpp *作 者:张旺华 *完成日期:2015年6月1日 *版 本 号:v1.0 */ #include <iostream> using namespace std; const double pi=3.1415926; class Point { public: Point (double X=0,double Y=0):x(X),y(Y){} ~Point(); void setPoint(double,double); double getx()const {return x;}; double gety()const {return y;}; friend ostream &operator<<(ostream &output,Point & p); protected: double x,y; }; Point::~Point() {} void Point::setPoint(double a,double b) { x=a; y=b; } ostream &operator<<(ostream &output,Point & p) { output<<"["<<p.x<<","<<p.y<<"]"; return output; } class Circle:public Point { public : Circle(double a=0,double b=0,double r=0); ~Circle(); void setCircle(double a,double b,double r); friend ostream &operator<<(ostream &output,Circle & c); protected: double R,area,girth; }; Circle::~Circle(){} void Circle::setCircle(double a,double b,double r) { Point(a,b); R=r; area=pi*R*R; girth=pi*R*2; } Circle::Circle(double a,double b,double r):Point(a,b),R(r) { area=pi*R*R; girth=pi*R*2; } ostream &operator<<(ostream &output,Circle & c) { output<<"R="<<c.R<<" Center=["<<c.x<<", "<<c.y<<"], r="<<c.R<<" area="<<c.area<<" girth="<<c.girth; } int main() { Circle c(3.5,6.4,5.2); cout<<"original circle:\nx="<<c.getx()<<", y="<<c.gety()<<endl; c.setPoint(5,5); cout<<"new circle:\n"<<c; return 0; }
知识点运用及学习心得:
我在这里遇到问题就是在构造函数时候没有处理好,造成bug 。真是悲哀呀。
原文地址:http://blog.csdn.net/wh201458501106/article/details/46314263