程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> C++學習之繼承:點和圓

C++學習之繼承:點和圓

編輯:C++入門知識

[cpp]
/**************************************************************************/ 
/*
 * 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  

/**************************************************************************/
/*
 * 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
}

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved