多态性与虚拟函数一个典型的例子第一步
生活随笔
收集整理的這篇文章主要介紹了
多态性与虚拟函数一个典型的例子第一步
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
**#include <iostream>
#include <strings.h>
#include <cstring>using namespace std;
class Point
{
public:Point(float x=0,float y=0); //輸入帶有默認參數的構造函數void setPoint(float,float); //設置坐標值float getX()const{return x;} //讀入xfloat getY()const{return y;} //定義讀入y值的常函數friend ostream & operator<<(ostream &,const Point &); //重載運算符“<<”
protected:float x,y;
};//下面定義Point類的成員函數
Point::Point(float a, float b) // 對x,y進行初始化{x=a;y=b;}
void Point::setPoint(float a, float b) { //對x,y賦新值x=a;y=b;
}
ostream & operator <<(ostream &output,const Point &p) {output <<"["<<p.x<<","<<p.y<<"]"<<endl;return output;
}int main()
{Point p(3.5,6.4);cout<<"x="<<p.getX()<<",y="<<p.getY()<<endl;p.setPoint(8.5,6.8);cout<<"p(new):"<<p<<endl;
}**
**/home/andrew/文檔/Clion/untitled5/cmake-build-debug/untitled5
x=3.5,y=6.4
p(new):[8.5,6.8]Process finished with exit code 0
**
函數說明,建立一個典型的例子;
一步一步的增加內容以免調試程序出現錯誤無法糾正
總結
以上是生活随笔為你收集整理的多态性与虚拟函数一个典型的例子第一步的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 冒烟指数:大数据监测互联网金融风险
- 下一篇: 作者:景志刚(1977-),男,就职于中