C++学习笔记__类的派生和多态性
2015-06-22 13:37
549 查看
类的派生和多态性的应用举例
例子:先建立一个Point类,包含数据成员x,y(坐标点)。以它为基类,派生出一个Circle类,增加数据成员r(半径),再以Circle类为直接基类,派生出一个Cylinder(圆柱体)类,再增加数据成员h(高)。编写程序,重载运算符“<<”和“>>”,使之能用于输出以上类对象。
#include<iostream> using namespace std; class Point { public: Point(float x = 0, float y = 0); void setPoint(float, float); float getX() const { return x; } float getY() const { return y; } friend ostream & operator<<(ostream &, const Point &); protected: float x, y; }; Point::Point(float a, float b) { x = a; y = b; } void Point::setPoint(float a, float b) { x = a; x = b; } ostream & operator<<(ostream &output, const Point &p) { output << "[" << p.x << "," << p.y << "]" << endl; return output; } class Circle:public Point { public: Circle(float x = 0, float y = 0, float r = 0); void setRadius(float); float getRadius() const; float area() const; friend ostream &operator<<(ostream &, const Circle &); private: float radius; }; Circle::Circle(float a, float b, float r) :Point(a, b), radius(r) { } void Circle::setRadius(float r) { radius = r; } float Circle::getRadius() const { return radius; } float Circle::area() const { return 3.14159*radius*radius; } ostream &operator<<(ostream &output, const Circle &c) { output << "Center=[" << c.x << "," << c.y << "],r=" << c.radius << ",area=" << c.area() << endl; return output; } class Cylinder :public Circle { public: Cylinder(float x = 0, float y = 0, float r = 0, float h = 0); void setHeight(float); float getHeight() const; float area() const; float volume() const; friend ostream& operator<<(ostream &, const Cylinder&); protected: float height; }; Cylinder::Cylinder(float a, float b, float r, float h) :Circle(a, b, r), height(h){} void Cylinder::setHeight(float h) { height = h; } float Cylinder::getHeight()const { return height; } float Cylinder::area()const { return 2 * Circle::area() + 2 * 3.14159*radius*height; } float Cylinder::volume()const { return Circle::area()*height; } ostream &operator <<(ostream &output, const Cylinder&cy) { output << "Center = [<< cy.x << , << cy.y << ], r = "<< cy.radius << ", h = "<< cy.height << "area = "<< cy.area() << ", volume = " << cy.volume() << endl; return output; } int main() { Cylinder cy1(3.5, 6.4, 5.2, 10); cout << "original cylinder : x = " << cy1.getX() << ", y = "<< cy1.getY() << ",r = " << cy1.getRadius() << ", h = "<< cy1.getHeight() << "area = "<< cy1.area() << ", volume = "<< cy1.volume() << endl; cy1.setHeight(15); cy1.setRadius(7.5); cy1.setPoint(5, 5); cout << "new cylinder : "<< cy1; Point &pRef = cy1; cout << "pRef as a Point : "<< pRef; Circle &cRef = cy1; cout << "cRef as a Circle : " << cRef; system("pause"); return 0; }
相关文章推荐
- C++程序设计--对象分册(第5章)
- C++中模板声明与实现的分离问题
- 学习笔记---单例的C++实现(从大神那看到的)
- 1 通过JNI混合使用Java和C++ -----> 操作字符串
- 输出以下图案
- c++使用流迭代器istream_iterator和ostream_iterator
- C语言学习笔记(六)——其他编程知识
- C语言学习笔记(五)——指针【C语言的灵魂】
- C语言(2)数据类型、常量、Printf、sizeof、scanf的使用
- C++函数模板
- C++类构造函数初始化列表,子类向父类传参数
- c++ 子类调用父类有参构造函数
- C语言学习笔记(四)——函数(重点)
- 转载:c++ sort用法
- 用二分法求下面方程在(-10,10)之间的根:2x^3-4x^2+3x-6=0
- c++中的explicit
- C++ new A和new A()的区别详解
- c++ auto_ptr(memory)
- Effective C++ 条款9
- 【读书笔记:C++ primer plus 第六版 中文版】第5章 循环和关系表达式