设为首页 加入收藏

TOP

C++学习之继承:点和圆
2014-11-24 01:01:08 来源: 作者: 【 】 浏览:0
Tags:习之 继承

C++学习之继承:点和圆


/**************************************************************************/
/*
* main.cpp
*
* Created on: May 20, 2013
* Author: shiguang
*/


#include "point.h"
#include "circle.h"


using namespace std;


int main()
{
Point a(4, 5);
Circle c(a, 7);
cout << "The point is: " << c.getPoint() << endl;
cout << "The radius is: " << c.getRadius() << endl;
cout << "The area is: " << c.getArea() << endl;
cout << "The circumference is: " << c.getCircum() << endl;


c.moveTo(1, 2);
c.modifyRadius(3);
cout << "The point is: " << c.getPoint() << endl;
cout << "The radius is: " << c.getRadius() << endl;
cout << "The area is: " << c.getArea() << endl;
cout << "The circumference is: " << c.getCircum() << endl;
}


/**************************************************************************/
/*
* point.h
*
* Created on: May 20, 2013
* Author: shiguang
*/


#ifndef POINT_H_
#define POINT_H_


#include


using namespace std;


class Point
{
protected:
double x, y;
public:
static double PI;
public:
Point(double a, double b);
double xOffset() const;
double yOffset() const;
double angle() const;
double radius() const;
Point operator+(const Point &d) const;
Point &operator+=(const Point &d);
void moveTo(double a, double b);
friend inline ostream& operator<<(ostream& o, const Point&d)
{
return o << '(' << d.x << ',' << d.y << ')';
}
virtual ~Point();
};


#endif /* POINT_H_ */


/**************************************************************************/
/*
* point.cpp
*
* Created on: May 20, 2013
* Author: shiguang
*/


#include "point.h"
#include


double Point::PI = 3.141592654;


Point::Point(double a, double b) :
x(a), y(b)
{
// TODO Auto-generated constructor stub
}


double Point::xOffset() const
{
return x;
}


double Point::yOffset() const
{
return y;
}


double Point::angle() const
{
return (180 / PI) * atan2(y, x);
}


void Point::moveTo(double a, double b)
{
x = a;
y = b;
}


Point Point::operator +(const Point& d) const
{
return Point(x + d.x, y + d.y);
}


Point &Point::operator +=(const Point& d)
{
x += d.x;
y += d.y;
return *this;
}


Point::~Point()
{
// TODO Auto-generated destructor stub
}


/**************************************************************************/
/*
* circle.h
*
* Created on: May 20, 2013
* Author: shiguang
*/


#ifndef CIRCLE_H_
#define CIRCLE_H_


#include "point.h"


class Circle: public Point
{
private:
double radius;
public:
Circle(const Point& p = Point(0, 0), double r = 0);
double getRadius() const;
Point getPoint() const;
double getArea() const;
double getCircum() const;
void moveTo(double a, double b);
void modifyRadius(double r);
virtual ~Circle();
};


#endif /* CIRCLE_H_ */


/**************************************************************************/
/*
* circle.cpp
*
* Created on: May 20, 2013
* Author: shiguang
*/


#include "circle.h"


Circle::Circle(const Point &p, double r) :
Point(p), radius(r)
{
// TODO Auto-generated constructor stub
}


double Circle::getRadius() const
{
return radius;
}


Point Circle::getPoint() const
{
//return (*(static_castthis));
return *this;
}


double Circle::getArea() const
{
return Point::PI * radius * radius;
}


double Circle::getCircum() const
{
return 2 * Point::PI * radius;
}


void Circle::moveTo(double a, double b)
{
x = a;
y = b;
}


void Circle::modifyRadius(double r)
{
radius = r;
}


Circle::~Circle()
{
// TODO Auto-generated destructor stub
}



】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇C++版的循环缓冲区类 下一篇C++ 异常处理 terminate函数使用

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: