日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

多态性与虚拟函数一个典型的例子第一步

發布時間:2025/3/15 编程问答 14 豆豆
生活随笔 收集整理的這篇文章主要介紹了 多态性与虚拟函数一个典型的例子第一步 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
**#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 **

函數說明,建立一個典型的例子;
一步一步的增加內容以免調試程序出現錯誤無法糾正

**#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; }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.1415926*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(3.5,6.4,5.2);cout<<"original circle :\nx="<<c.getX()<<",y="<<c.getY()<<",r="<<c.area()<<endl;return 0; }** **/home/andrew/文檔/Clion/untitled5/cmake-build-debug/untitled5 original circle : x=3.5,y=6.4,r=84.9487Process finished with exit code 0 **

總結

以上是生活随笔為你收集整理的多态性与虚拟函数一个典型的例子第一步的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。