继承与多态
来源:互联网 发布:个人域名如何备案 编辑:程序博客网 时间:2024/06/05 09:35
#include <iostream>using namespace std;//声明类Pointclass Point{public: Point(float x = 0, float y = 0); //构造函数 void setPoint(float, float);//设置x\y坐标值 float getX() const { return x; }//getX是Point 类中的常成员函数,可以引用,但不能修改本类的数据成员 float getY() const { return y; } friend ostream &operator << (ostream &, const Point &);//重载<<protected: float x, y;};//定义point类的成员函数//Point 构造函数Point::Point(float a, float b){ x = a; y = b;}//设置x\y坐标值void Point::setPoint(float a, float b){ x = a; y = b;}//重载<<ostream& operator << (ostream &output, const Point &p){ output << "[" << p.x << "," <<p.y<< "]" << endl; return output;}////分步调试//int main()//{// Point p(2.5, 6.4);// cout << "x=" << p.getX() << ".y=" << p.getY() << endl;// p.setPoint(8.8, 6.6);// cout << "p(new):" << p << endl;//}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 &);protected: 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.14158*radius*radius; }ostream &operator <<(ostream &output, const Circle &c){ output << "center = [" << c.x << "," << c.y << "] , r = " << c.radius << ", area = " << c.area() << endl; return output;}//分步调试//int main()//{// Circle c(2.5, 6.4, 5.2);// cout << "original circle:\n x = " << c.getX() << ",y = " << c.getY() << ", r= " << c.getRadius() << ", area = " << c.area() << endl;// c.setRadius(7.5);// c.setPoint(5, 5);//// cout << "new circle:\n" << c;// Point &pRef = c;//pRef 是point类的引用变量,被c初始化;// cout << "pRef:" << pRef;//输出pRef//调用Point<<// return 0;//}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 heigth;};Cylinder::Cylinder(float a, float b, float r, float h) :Circle(a, b, r), heigth(h){}void Cylinder::setHeight(float h){ heigth = h; } float Cylinder::getHeight() const { return heigth; }float Cylinder::area() const { return 2 * Circle::area() + 2 * 3.14159*radius*heigth; }float Cylinder::volume() const { return Circle::area()*heigth; }ostream &operator <<(ostream &output, const Cylinder & cy){ output << "center = [ " << cy.x << "," << cy.y << "],r = " << cy.radius << ".h = " << cy.heigth << "\n area = " << cy.area() << ", volume = " << cy.volume() << endl; return output;}int main(){ Cylinder cy1(3.5, 6.4, 5.2, 10); cy1.setHeight(15); cy1.setRadius(7.5); cy1.setPoint(5, 5); cout << "\n new cylinder:\n" << cy1; Point &pRef = cy1; cout << "\npRef as a Point :" << pRef; Circle &cRef = cy1; cout << "\ncRef as a Circle:" << cRef; return 0;}
阅读全文
0 0
- 继承与多态
- 继承与多态
- 继承与多态
- 继承与多态
- 继承与多态
- 继承与多态
- 继承与多态
- 继承与多态
- 继承与多态
- 继承与多态
- 继承与多态
- 继承与多态
- 继承与多态
- 继承与多态
- 继承与多态
- 继承与多态
- 多态与继承
- 继承与多态
- [C/C++] C/C++语言文件读写函数
- [springcloud]eureka注册服务小案例
- 2017.9.9-2017.9.10---训练赛
- 基于centos的nginx安装配置
- 9月10日周日训练笔记
- 继承与多态
- 最长非递减子序列模板
- 移动端车牌识别嵌入至手机app中
- 赛码网编程练习题_1
- display:none、visibility:hidden和opacity:0之间的区别
- Kafka Replication & Leader election
- 多表查询
- JavaBean技术
- Spark on YARN 笔记