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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

第6次cplus实验

發布時間:2024/1/17 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 第6次cplus实验 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一。實驗結論:

1.(1)類的聲明:

#include<iostream> using namespace std; class A {public:A(int m,int n);void plus();void minus();protected:int a;int b; }; class B:public A {public:B(int m,int n):A(m,n){}void multi(); }; class C:public A {public:C(int m,int n):A(m,n){}void divi(); };

(2)類的實現:

#include"math.h" #include<iostream> using namespace std; A::A(int m,int n) {a=m;b=n; } void A::plus() {int p;p=a+b;cout<<"The result is:"<<p<<endl; } void A::minus() {int p;p=a-b;cout<<"The result is:"<<p<<endl; } void B::multi() {int p;p=a*b;cout<<"The result is:"<<p<<endl; } void C::divi() {double p;if(b==0)cout<<"Error!"<<endl;else{p=(double)a/(double)b;cout<<"The result is:"<<p<<endl;} }

(3)主函數:

#include<iostream> #include"math.h" using namespace std; int main() {int m,n;char p;while(1){cout<<"Please input an equation, input 000 to exit:";cin>>m>>p>>n;if(m==0&&p=='0'&&n==0)break;else{if(p=='+'){A a(m,n);B b(m,n);C c(m,n);a.plus();b.plus();c.plus();}else if(p=='-'){A a(m,n);a.minus();}else if(p=='*'){B a(m,n);a.multi();}else if(p=='/'){C a(m,n);a.divi();}else{cout<<"Error!"<<endl;continue;}}}return 0; }

(4)運行結果:

2.(1)代碼:

#include<iostream> using namespace std; class vehicle {public:vehicle(int m,int w):maxspeed(m),weight(w){cout<<"Vehicle is constructing..."<<endl;cout<<"The max speed is:"<<maxspeed<<endl;cout<<"The weight is:"<<weight<<endl;}void run(){cout<<"Running..."<<endl;}void stop(){cout<<"Stopped."<<endl;}~vehicle(){cout<<"Vehicle has been destructed."<<endl;}protected:int maxspeed;int weight; }; class bicycle:virtual public vehicle {public:bicycle(int m,int w,int h):vehicle(m,w),height(h){cout<<"Bicycle is constructing..."<<endl;cout<<"The height is:"<<height<<endl;}~bicycle(){cout<<"Bicycle has been destructed."<<endl;}protected:int height; }; class motorcar:virtual public vehicle {public:motorcar(int m,int w,int s):vehicle(m,w),seatnum(s){cout<<"Motorcar is constructing..."<<endl;cout<<"The seatnum is:"<<seatnum<<endl;}~motorcar(){cout<<"Motorcar has been destructed."<<endl;}protected:int seatnum; }; class motorbike:public bicycle,public motorcar {public:motorbike(int m,int w,int h,int s):vehicle(m,w),bicycle(m,w,h),motorcar(m,w,s){}~motorbike(){cout<<"Motorbike has been destructed."<<endl;} }; int main() {int a,b,c,d;cout<<"Please input the maxspeed,weight,height,seatnum of the motorbike:";cin>>a>>b>>c>>d;motorbike M(a,b,c,d);M.run();M.stop();return 0; }

(2)運行結果:

3.(1)類的聲明:

//#include"Fraction.h" #include<iostream> using namespace std; class iFraction:public Fraction {public:iFraction(int p=0,int q=1,int n=0):Fraction(p,q),number(n){}void show();private:int itop;int ibottom;int number; };

(2)類的實現:

#include"iFraction.h" #include<iostream> using namespace std; void iFraction::show() {if(bottom==0)cout<<"Error!"<<endl;else if(top==0)cout<<number<<endl;else if(top%bottom==0){number=number+top/bottom;cout<<number<<endl;}else if(top>bottom){number=number+top/bottom;top=top%bottom;cout<<" "<<top<<endl;cout<<number<<"------"<<endl;cout<<" "<<bottom<<endl;}else if(number==0){cout<<" "<<top<<endl;cout<<"------"<<endl;cout<<" "<<bottom<<endl;}else{cout<<" "<<top<<endl;cout<<number<<"------"<<endl;cout<<" "<<bottom<<endl;} }

(3)主函數

#include"Fraction.h" #include"iFraction.h" #include<iostream> #include<stdlib.h> using namespace std; int main() {int x,y,z;Fraction a;a.output();Fraction b(3,6);b.output();Fraction c(5);c.output();b.compare(c);cout<<"Please input the top and the bottom:";cin>>x>>y;Fraction d(x,y);d.opera(x,y);d.output();cout<<"Please input a number:";cin>>z;iFraction e(x,y,z);e.opera(x,y);e.show();return 0; }

(4)運行結果:

二。實驗分析與討論

  實驗第三題類的聲明中,有時取消第一行的注釋后編譯成功,而有時注釋第一行后編譯成功,至于什么原因我也沒有搞明白,麻煩大佬們在評論時幫我解答一下。。。。

  通過這次實驗,我初步掌握了最近的新知識繼承與多態的大致概念以及使用方法,也發現了自己在所學知識的掌握方面的諸多不足,在以后的時間里,我還需要多加練習。

轉載于:https://www.cnblogs.com/manganese123/p/9141450.html

總結

以上是生活随笔為你收集整理的第6次cplus实验的全部內容,希望文章能夠幫你解決所遇到的問題。

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