《实验七面向对象1.doc》由会员分享,可在线阅读,更多相关《实验七面向对象1.doc(6页珍藏版)》请在金锄头文库上搜索。
1、实验七 面向对象1信科09-1班 朱云超 学号:08093526第一题#include using namespace std;class Time public: void set_time(); void show_time(); private: int hour; int minute; int sec;void Time:set_time()cinhour;cinminute;cinsec;void Time:show_time()couthour:minute:secendl;int main() Time t; t.set_time(); t.show_time(); retur
2、n 0;输出结果:第二题#include using namespace std;class circlepublic:double length(); double square();void set_value();private:double b;double circle:length()return(b*2*3.14);double circle:square()return(b*b*3.14);void circle:set_value()cinb;coutendl;int main()double money1,money2,length2,square1,square2;cir
3、cle c1,c2;cout请输入游泳池半径:; c1.set_value();square1=c1.square(); cout请输入栅栏距圆心的距离(比游泳池半径大3):;c2.set_value();length2=c2.length(); square2=c2.square();money1=length2*35;cout栅栏的造价:money1endl;money2=(square2-square1)*20;cout过道的造价:money2endl;return 0;输出结果:第三题#includeusing namespace std;class complexpublic:com
4、plex(double a1=0.0,double b1=0.0,double a2=0.0,double b2=0.0);void display();void add();void subtruct();private:double a1,a2;double b1,b2;complex:complex(double x1,double y1,double x2,double y2)a1=x1;b1=y1;a2=x2;b2=y2;void complex:add()cout(a1+a2,b1+b2)endl;void complex:subtruct()cout(a1-a2,b1-b2)en
5、dl;void complex:display()cout(a1,b1)endl;cout(a2,b2)endl;int main()complex c1(3.2,1.2,4.5,3.6);c1.display(); c1.add();c1.subtruct();return 0;输出结果:第四题#includeusing namespace std;class sumpublic:void set_value(int n);void clear_value(int n);void search(int n);private:int a1000;int b1000;void sum:set_v
6、alue(int n)int i;cout对a、b两个集合初始化endl;cout集合a:;for(i=0;iai;cout集合b:;for(i=0;ibi;void sum:clear_value(int n)int m,i;cout输入0对两个集合进行置空m;for(i=0;in;i+)ai=NULL;for(i=0;in;i+)bi=NULL;void sum:search(int n)int i,j;cout求交集:;for(i=0;in;i+)for(j=0;jn;j+)if(ai=bj)coutai,;coutendl;int x;bool flag;coutx;for(i=0;in;i+)if(x=ai)cout所查的数在集合a中!endl;flag=true; if(flag=false)cout所查的数不在集合a中!endl;int main( )sum s;int n;coutn;s.set_value(n);s.search(n);s.clear_value(n);return 0;输出结果: