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<iostream>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 <cmath>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_cast<const Point *>this));
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
}