欢迎来到天天文库
浏览记录
ID:20527001
大小:67.00 KB
页数:11页
时间:2018-10-13
《c++程序设计试题》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、C++程序设计试题系专业班姓名学号一、写出下列程序的执行结果:(每小题5分,共50分)1、#includeclasssample{inti;doubled;public:voidsetdata(intn){i=n;}voidsetdata(doublex){d=x;}voiddisp(){cout<<”i=”<2、m.h>double&fn(constdouble&pd){staticdoublead=32;ad+=pd;cout<<”fnbeingcalled…thevalueis:”<classsample{public:sample();sample(int);~sample();v3、oiddisplay();protected:intx;};sample::sample(){x=0;cout<<”constructingnormally”;}sample::sample(intm){x=m;cout<<”constructingwithanumber:”<4、sampleobj2(20);obj1.display();obj2.display();}答案:4、#includeclasscomplex{public:complex(double,double);complex(complex&);complex();complexoperator+(complex&);complex&operator=(complex&);voiddisplay();protected:doublereal;doubleimage;};complex::complex(doublex5、,doubley){real=x,image=y;}complex::complex(complex&x){cout<<”Runingcopyconstructor”real=x.real;image=x.image;}complex::complex(){real=image=0.0;}complexcomplex::operator+(complex&x){returncomplex(real+x.real,image+x.image);}complex&complex::operator=(complex&x){cout<<6、”Runingassignoperator”;real+=x.real;image+=x.image;return*this;}viodcomplex::display(){cout<templateclasssample{public:sample(){};sa7、mple(Tx){n=x;}sample&operator+(constsample&);voiddisp(){cout<<”n=”<sample&sample::operator+(constsample&s){staticsampletemp;temp.n=n+s.n;returntemp;}voidmain(){samples1(10),s2(20),s3;s3=s1+s2;s3.disp();}答案:8、6、#includeclassbase{intn;public:base(inta){cout<<”constructingbaseclasswithanumber=”<
2、m.h>double&fn(constdouble&pd){staticdoublead=32;ad+=pd;cout<<”fnbeingcalled…thevalueis:”<classsample{public:sample();sample(int);~sample();v
3、oiddisplay();protected:intx;};sample::sample(){x=0;cout<<”constructingnormally”;}sample::sample(intm){x=m;cout<<”constructingwithanumber:”<4、sampleobj2(20);obj1.display();obj2.display();}答案:4、#includeclasscomplex{public:complex(double,double);complex(complex&);complex();complexoperator+(complex&);complex&operator=(complex&);voiddisplay();protected:doublereal;doubleimage;};complex::complex(doublex5、,doubley){real=x,image=y;}complex::complex(complex&x){cout<<”Runingcopyconstructor”real=x.real;image=x.image;}complex::complex(){real=image=0.0;}complexcomplex::operator+(complex&x){returncomplex(real+x.real,image+x.image);}complex&complex::operator=(complex&x){cout<<6、”Runingassignoperator”;real+=x.real;image+=x.image;return*this;}viodcomplex::display(){cout<templateclasssample{public:sample(){};sa7、mple(Tx){n=x;}sample&operator+(constsample&);voiddisp(){cout<<”n=”<sample&sample::operator+(constsample&s){staticsampletemp;temp.n=n+s.n;returntemp;}voidmain(){samples1(10),s2(20),s3;s3=s1+s2;s3.disp();}答案:8、6、#includeclassbase{intn;public:base(inta){cout<<”constructingbaseclasswithanumber=”<
4、sampleobj2(20);obj1.display();obj2.display();}答案:4、#includeclasscomplex{public:complex(double,double);complex(complex&);complex();complexoperator+(complex&);complex&operator=(complex&);voiddisplay();protected:doublereal;doubleimage;};complex::complex(doublex
5、,doubley){real=x,image=y;}complex::complex(complex&x){cout<<”Runingcopyconstructor”real=x.real;image=x.image;}complex::complex(){real=image=0.0;}complexcomplex::operator+(complex&x){returncomplex(real+x.real,image+x.image);}complex&complex::operator=(complex&x){cout<<
6、”Runingassignoperator”;real+=x.real;image+=x.image;return*this;}viodcomplex::display(){cout<templateclasssample{public:sample(){};sa
7、mple(Tx){n=x;}sample&operator+(constsample&);voiddisp(){cout<<”n=”<sample&sample::operator+(constsample&s){staticsampletemp;temp.n=n+s.n;returntemp;}voidmain(){samples1(10),s2(20),s3;s3=s1+s2;s3.disp();}答案:
8、6、#includeclassbase{intn;public:base(inta){cout<<”constructingbaseclasswithanumber=”<
此文档下载收益归作者所有