码迷,mamicode.com
首页 > 其他好文 > 详细

第12周 项目四-点、园关系(1)(2)(3)

时间:2015-05-27 10:12:35      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:c++   大一练习   继承和派生   

(1)先建立一个Point(点)类,包含数据成员x,y(坐标点);
(2)以Point为基类,派生出一个Circle(圆)类,增加数据成员(半径),基类的成员表示圆心;
(3)编写上述两类中的构造、析构函数及必要运算符重载函数(本项目主要是输入输出);
代码:

#include <iostream>
using namespace std;
class Point
{
protected:
    int x;
    int y;
public:
    Point(int a,int b):x(a),y(b){}
    friend ostream &operator <<(ostream &out,Point &a);
};
ostream &operator <<(ostream &out,Point &a)
    {
        cout<<"x="<<a.x<<" y="<<a.y<<endl;
        return out;
    }
class Circle:public Point
{
protected:
    int r;
public:
    Circle(int a,int b,int c):Point(a,b),r(c){}
    friend ostream &operator <<(ostream  &out,Circle &a);
};
ostream &operator <<(ostream  &out,Circle &a)
    {
        cout<<"圆心:"<<" ("<<a.x<<"<"<<a.y<<") "<<"半径:"<<a.r<<endl;
        return out;
    }
int main( )
{
	Circle c1(3,2,4),c2(4,5,5);      //c2应该大于c1
	Point p1(1,1),p2(3,-2),p3(7,3);  //分别位于c1内、上、外
	cout<<"圆c1: "<<c1;
    cout<<"圆c2: "<<c2;
	cout<<"点p1: "<<p1;
	cout<<"点p2: "<<p2;
	cout<<"点p3: "<<p3;

	return 0;
}

运行结果:

技术分享

知识点总结:

忘了输出流只能用友元函数重载

第12周 项目四-点、园关系(1)(2)(3)

标签:c++   大一练习   继承和派生   

原文地址:http://blog.csdn.net/ljd939952281/article/details/46041729

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!