【文档说明】《面向对象程序设计》习题答案-c部分课件.ppt,共(59)页,546.559 KB,由小橙橙上传
转载请保留链接:https://www.ichengzhen.cn/view-44821.html
以下为本文档部分文字说明:
面向对象程序设计习题习题2面向对象程序设计习题2-1-4:比较值调用和引用调用?•值调用是指发生函数调用时,给形参分配内存空间,并直接将实参的值传递给形参。这一过程是参数的单向传递过程,一旦形参获得了值便与实参脱离关系,此后无论形参发生了怎样的改变,都不会影响到实
参。•引用调用不为形参分配内存,在执行主调函数中的调用语句时,系统自动用实参来初始化形参。形参是实参的一个别名,对形参的任何操作也就直接作用于实参。•传址是把实参的地址传给形参,从而形参和实参同占一个内存空间,所以形参变实参也变;传值是实参把值
赋值给形参,两个参数占有不同的地址,所以实参不随形参变化而变化!面向对象程序设计习题(7)编写程序,打印如下的杨辉三角形:111121133114641#include<iostream.h>#include<iomanip.h>//#include<stdio.h>voidmain()
{inti,j,a[5][5]={1};//第一行第一列赋值1,其他为0for(i=1;i<5;i++){a[i][0]=1;//第一列全置为1for(j=1;j<=i;j++)a[i][j]=a[i-1][j-1]+a[i-1][j];//每个数是上面两数
之和}for(i=0;i<5;i++)//输出杨辉三角{cout<<setw(5-i)<<"";//for(j=0;j<5-i;j++)printf("");for(j=0;j<=i;j++)//只输出a[5][5]的下三角cout<<setw(2)<<a[i][j];//printf("%2
d",a[i][j]);cout<<endl;}}方法一1000011000121001331014641面向对象程序设计习题#include<iostream.h>#include<iomanip.h>//#include<stdio.h>voidmai
n(){inti,j,a[5][5];for(i=0;i<5;i++){a[i][0]=1;a[i][i]=1;//第一列、对角全置为1for(j=1;j<i;j++)a[i][j]=a[i-1][j-1]+a[i-1][j];//每个数是上面两数之和
}for(i=0;i<5;i++)//输出杨辉三角{cout<<setw(5-i)<<"";//for(j=0;j<5-i;j++)printf("");for(j=0;j<=i;j++)//只输出a[5][5]的下三角cout<<s
etw(2)<<a[i][j];//printf("%2d",a[i][j]);cout<<endl;}}方法一变形111121133114641面向对象程序设计习题#include<iostream.h>voidmain(){inta[5][9],i,
j;for(i=0;i<5;i++)for(j=0;j<9;j++)a[i][j]=0;//所有元素赋值0for(i=0;i<5;i++)//左右两斜边赋值1{a[i][4-i]=1;a[i][4+i]=1;}for(i=2;i<5;i++)fo
r(j=8;j>1;j--)a[i][j]=a[i-1][j-1]+a[i-1][j+1];for(i=0;i<5;i++){for(j=0;j<9;j++)if(a[i][j]==0)cout<<"";//值为0的元素输出为空格elsecout<<a[i][j];cout<<endl;
}}方法二000010000000101000001020100010303010104060401面向对象程序设计习题(8)将一个一维数组中相同的元素删除到只保留一个,然后按由大到小的顺序输出。#include<io
stream.h>voidmain(){inta[6]={1,7,3,7,22,9};inti,j,t,flag;intm=6,n;n=m;//n控制输出不同的元素个数//删除相同元素for(i=0;i<m;i++)for(j=i+1;j<n;j++){if(a
[i]==a[j]){for(intk=j;k<n;k++)a[k]=a[k+1];//前移n--;j--;}}//冒泡排序for(i=0;i<n;i++){flag=0;for(j=0;j<n-i-1;j++)if(a[j]<a[j+1]){t=a[j];a[j
]=a[j+1];a[j+1]=t;flag=1;}if(flag==0)break;}cout<<"排序后的数组为:\n";for(i=0;i<n;i++)cout<<""<<a[i]<<"\n";}面向对象程序设计习题习题
33-1填空题(1)生存期与存储区域关系密切,一般存储区域分为代码区(codearea)、数据区域(dataarea)、栈区(stackarea)和堆区(heaparea)。代码区用来存放程序代码,与其他存储区相对应的生存期分别为静态生存期、局部生存期、和动态生存期。(2)局部变
量一般具有块作用域,还可能具有作用域;全局变量具有文件作用域。(3)静态成员属于类,而不属于对象,它由同一个类的所有对象共同维护,为这些对象所共享。静态函数成员可以直接引用该类的静态数据和函数成员,而不能直接引用非静态数据成员。对于公有的静
态函数成员,可以通过类名或对象名来调用;而一般的非静态函数成员只能通过对象名来调用。(4)在实际程序设计中,一个源程序可划分为三个文件:类声明文件、类实现文件、和类的使用文件。面向对象程序设计习题3-3阅读程序题(1)以下是一个类中包含另一个类对象成员(类的组合)的例子,试分析并
给出以下程序的运行结果。面向对象程序设计习题#include<iostream.h>classSon{intage;public:Son(){age=1;}Son(inti){age=i;}voidprint(){cout<<"儿子的年龄是:"<<age<<endl;}};classFather
{intage;Sons1,s2;//类的组合public:Father(inta1,inta2,intf):s2(a2),s1(a1){age=f;}voidprint(){cout<<“父亲的年龄是:"<<age<<endl;}Son&gets1(){cout<<"第一个
";returns1;}Son&gets2(){cout<<"第二个";returns2;}};voidmain(){Fatherf(10,5,38);f.print();f.gets1().print();f.gets2().print(
);}父亲的年龄是:38第一个儿子的年龄是:10第二个儿子的年龄是:5面向对象程序设计习题#include<iostream.h>#include<string.h>classstudent{charname[10];intage;public:student(chari
n_name[],intin_age){strcpy(name,in_name);age=in_age;}intget_age(){returnage;}char*get_name(){returnname;}friendintcompare(student&s1,studen
t&s2){if(s1.age>s2.age)return1;elseif(s1.age==s2.age)return0;elsereturn-1;}};(2)程序代码如下:面向对象程序设计习题voidmain(){studentstu[]={stude
nt("王红",18),student("吴伟",19),student("李丽",17)};inti,min=0,max=0;for(i=1;i<3;i++){if(compare(stu[max],stu[i])==-1)max=i;
elseif(compare(stu[max],stu[i])==1)min=i;}cout<<"最大年龄:"<<stu[max].get_age()<<",姓名:"<<stu[max].get_name()<<endl;cout<<"最小年龄:"<<stu[min].get_age
()<<",姓名:"<<stu[min].get_name()<<endl;}程序运行结果为:最大年龄:19,姓名:吴伟最小年龄:17,姓名:李丽面向对象程序设计习题#include<iostream.h>
classstack;classnode{intdata;node*prev;public:node(intd,node*n){data=d;prev=n;}friendclassstack;};(3)下列程序实现的是堆栈的压入和弹出。其中有两个类,一个是结点类,它包含结点值和指向上
一结点的指针;另一个类是堆栈类,数据成员为堆栈的头指针,它是结点类的友员。试分析程序,并说明堆栈的压入和弹出过程。classstack{node*top;public:stack(){top=0;}vo
idpush(inti);intpop();};voidstack::push(inti){node*n=newnode(i,top);top=n;}intstack::pop(){node*t=top;if(top){top=top->prev;intc=t->data
;deletet;returnc;}return0;}main(){intc;stacks;for(intj=0;j<10;j++)//10个整数进栈{cin>>c;s.push(c);}for(j=0;j<10;j++)//10个整数出栈,cout<<s.pop()<<“”;//实现了输入
的顺序与输出的顺序相反cout<<"\n";return1;}分析:实现了输入的顺序与输出的顺序相反面向对象程序设计习题3-4完成下列程序(1)下面是一个计算器类的定义,请完成该类的实现(成员函数的定义),并在主函数中
先将计算器给定初值99,然后进行二次加1,一次减1,最后显示计算器的值。classcounter{intvalue;public:counter(intnumber);//构造函数voidincrement();//给原值加lvoiddecrement();//给原值减1int
getvalue();//取得计数值voiddisplay();//显示计算器值};面向对象程序设计习题#include<iostream.h>classcounter{intvalue;public:counter(intnumber);voidincrement();/
/给原值加lvoiddecrement();//给原值减1intgetvalue();//取得计数值voiddisplay();//显示计算器值};counter::counter(intnumber){value=number;}voidcounter::increment(){
value++;}voidcounter::decrement(){value--;}intcounter::getvalue(){returnvalue;}voidcounter::display(){cout<<"value="<<value<<endl;}voidmain(){coun
tercounter1(99);counter1.increment();//+counter1.increment();//+counter1.decrement();//-counter1.display();}面向对象程序设计习题(2)下列程序接收若干用户的姓名和电话,然后输出。
#include"iostream.h"#include"string.h"#include"iomanip.h"constintN=5;classperson{charname[10];charnum[10];public:voidgetdata(①){strcpy(name,n
a);strcpy(num,nu);}voidoutdata(personpn[N]);};voidperson::outdata(personpn[N]){inti;for(i=0;i<N;i++){cout.width(10);②cout.width(10);③}}voidm
ain(){char*na[5]={"li","zh","li","zh","li"};char*nu[5]={"01","02","03","04","05"};personobj[5];//对象数组for(
inti=0;i<5;i++)obj[i].getdata(na[i],nu[i]);④*pt=obj;⑤;}①char*na,char*nu或:charna[],charnu[]②cout<<pn[i].name<<endl;③cout<<pn[i].num<<endl;④perso
n⑤pt->outdata(pt);或:pt->outdata(obj);面向对象程序设计习题3-5编程题(1)定义一个名为rectangle的矩形类,其属性数据为矩形左上角和右下角点的坐标,要求通过对该
类对象的调用计算矩形的面积。#include<iostream.h>#include<math.h>classRectangle{intX1,Y1,X2,Y2;public:Rectangle(int
x1,inty1,intx2,inty2){X1=x1;Y1=y1;X2=x2;Y2=y2;}intArea();};intRectangle::Area(){returnfabs((X2-X1)*(Y2-Y1));}voidmain(){RectangleA(10,20,30
,50);cout<<"面积为"<<A.Area()<<endl;}面向对象程序设计习题(2)定义一个名为complex的复数类,其属性数据为复数的实部和虚部,要求构造函数和拷贝构造函数,并定义成员函数打印复数的值。#include<iostream.h>classcomplex//
复数类声明{doublereal;doubleimag;public:complex(doubler=0.0,doublei=0.0)//构造函数{real=r;imag=i;}complex(complex&c)//拷贝构造函数{re
al=c.real;imag=c.imag;}voiddisplay()//显示复数的值{cout<<“实部为:”<<real<<“虚部为:"<<imag<<endl;}};voidmain(){complexA;A.display(
);complexB(2,3);B.display();complexC(B);C.display();}面向对象程序设计习题(3)定义一个名为circle的圆类,其属性数据为圆的半径,定义成员函数计算圆的面积。编写主函数计算一个内径和外径分别为5和8的圆环的面积。#include<iostr
eam.h>constfloatPI=3.14159;//definePI3.14159classcircle//类声明{doubleradius;public:circle(doubler=0.0){radius=r;}//构造函数doub
learea(){returnPI*radius*radius;}};voidmain(){circleA(5);circleB(8);cout<<"圆环面积"<<B.area()-A.area()<<endl;}面向对象程序设计习题(4)试定义一个字符串类string,使其至少具有内容
(contents)和长度(length)两个数据成员,分别定义不同的成员函数,用于显示字符串、求字符串长度、给原字符串后连接另一个字符串。#include<iostream.h>#include<string.h>classstring//类声明{intlength;char*cont
ents;public:string(){length=0;contents=0;}string(constchar*str);//构造函数voiddisplay()const;//显示值voidge
tlength();voidappend(constchar*tail);};string::string(constchar*str){length=strlen(str);contents=newchar[length+1];s
trcpy(contents,str);}面向对象程序设计习题voidstring::display()const//显示{if(contents==0)cout<<"empty.\n";elsecout<<"字符串的内容为"<<contents<<end
l;}voidstring::getlength()//获取长度{length=strlen(contents);cout<<"字符串的长度为"<<length<<endl;}voidstring::append(constchar*tail)//连接{char
*tmp;length+=strlen(tail);tmp=newchar[length+1];strcpy(tmp,contents);strcat(tmp,tail);deletecontents;contents=tmp;}voidmain(){strings0,s1("astrin
g");s0.display();s1.display();s1.getlength();strings2("this");s2.append("astring");cout<<"连接的";s2.display();}面向对象程序设计习题(5)编写一个程序:声明一个Cat类,拥有静态
数据成员HowManyCats,用以记录Cat的个体数目;拥有静态成员函数GetHowMany(),用以存取HowManyCats。设计程序并测试这个类,体会静态数据成员和静态成员函数的用法。#include<iostream.h>clas
sCat{staticintHowManyCat;//引用性说明public:Cat(){HowManyCat++;}~Cat(){HowManyCat--;}staticvoidGet_HowManyCat(){cout<<"总数:"<
<HowManyCat<<endl;}};intCat::HowManyCat=0;//定义性说明voidmain(){Catf1;Cat::Get_HowManyCat();Catf2;f2.Get_HowManyCat
();f1.~Cat();Cat::Get_HowManyCat();}面向对象程序设计习题(7)实现一单链表的逆置,并输出逆置前后的结果。structNode{intdata;Node*next;};cl
assList{Node*head;public:List(){head=NULL;}voidInsertList(intaData,intbData);//链表结点的插入voidDeleteList(intaData
);//链表结点的删除voidOutputList();//链表结点的输出Node*Gethead(){returnhead;}Node*reverse(Node*head);};voidList::OutputList()//链表输出函数{Node*current=head;while(
current!=NULL){cout<<current->data<<"";current=current->next;}cout<<endl;}面向对象程序设计习题(7)实现一单链表的逆置,并输出逆置前后
的结果。Node*reverse(Node*head){node*p,*q,*r;p=head;//p指向1号q=p->next;//q指向2号while(q!=NULL){r=q->next;//r指向3号
q->next=p;//2号的next指向1号p=q;//p指向2号q=r;//q指向3号}head->next=NULL;//1号的next指向NULL,head=p;//循环结束之后,p指向原来的末结点,现在是头结点ret
urnhead;//返回头结点}面向对象程序设计习题习题44-1填空题(1)派生新类的过程经历三个过程:吸收基类成员、改造基类成员和添加新的成员。(2)在类族中,直接参与派生出某类的基类称为直接基类;基类的基类甚至更高层的基类称为间接基类
。(3)在继承中,如果只有一个基类,则这种继承方式称为单继承;如果基类名有多个,则这种继承方式称为多继承。(4)C++中的运算符除了类属关系运算符“.”、作用域分辨符“::”、成员指针运算符“*”、sizeof运算符和三目运算符“?:”之外,全部可以重载,而且
只能重载C++中已有的运算符,不能臆造新的运算符。(5)如果用普通函数重载双目运算符,需要1个操作数;重载单目运算符,需要0个操作数。如果用友员函数重载双目运算符,需要2个操作数;重载单目运算符,需要1个操作数。(6)当基类中的某个成员
函数被声明为虚函数后,此虚函数就可以在一个或多个派生类中被重新定义,在派生类中重新定义时,其函数原型,包括返回类型、函数名和参数个数,以及参数类型和参数的顺序都必须与基类中的原型完全相同。面向对象程序设计习题4-3阅读程序题分析下列程序,写出运行结果(1)程序代码如下:考察
:继承关系#include<iostream.h>classB{intx1,x2;public:voidInit(intn1,intn2){x1=n1;x2=n2;}intinc1(){return++x1;}in
tinc2(){return++x2;}voiddisp(){cout<<"B,x1="<<x1<<",x2="<<x2<<endl;}};面向对象程序设计习题classD1:B{intx3;public:D1(intn3){x3=n3;}voidIni
t(intn1,intn2){B::Init(n1,n2);}intinc1(){returnB::inc1();}intinc2(){returnB::inc2();}intinc3(){return++x3;}voiddisp(){cout<<"D1,x3="<<x
3<<endl;}};classD2:publicB{intx4;public:D2(intn4){x4=n4;}intinc1(){inttemp=B::inc1();temp=B::inc1();temp=B::inc1();returnB::inc1();}int
inc4(){return++x4;}voiddisp(){cout<<"D2,x4="<<x4<<endl;}};面向对象程序设计习题B,x1=-2,x2=-2D1,x3=3D2,x4=6D2,x4=6D2,x4=6voidmain(){Bb;b.Init(
-2,-2);b.disp();D1d1(3);d1.Init(5,5);d1.inc1();d1.disp();D2d2(6);d2.Init(-4,-4);d2.B::disp();d2.disp();d2.inc1();d2.
inc2();d2.B::disp();d2.disp();d2.B::inc1();d2.B::disp();d2.disp();}B,x1=-2,x2=-2D1,x3=3B,x1=-4,x2=-4D2,x4=6B,x1=0,x2=-3D2,x4=6B,x1=1,x
2=-3D2,x4=6面向对象程序设计习题(2)程序代码如下(课件例题):考察:虚拟派生中构造函数的执行顺序#include<iostream.h>classLevel1//声明基类Level1{public:intn1;Level1(i
ntin_n1){n1=in_n1;cout<<"ThisisLevel1,n1="<<n1<<endl;}};classLevel21:virtualpublicLevel1//Level1为虚基类,派生类
Level21{public:intn21;Level21(inta):Level1(a){n21=a;cout<<"ThisisLevel21,n21="<<n21<<endl;}};面向对象程序设计习题classLevel22:virtualpublicLevel1//Lev
el1为虚基类,派生类Level22{public:intn22;Level22(inta):Level1(a){n22=a;cout<<"ThisisLevel22,n22="<<n22<<endl;}};classL
evel3:publicLevel21,publicLevel22//声明派生类Level3{public:intn3;Level3(inta):Level1(a),Level21(a),Level22
(a){n3=a;cout<<"ThisisLevel3,n3="<<n3<<endl;}};voidmain(){Level3obj(3);}//定义Level3类对象objThisisLevel1,n1=3ThisisLev
el21,n21=3ThisisLevel22,n22=3ThisisLevel3,n3=3程序运行结果为:面向对象程序设计习题(3)下列程序中,基类base和派生类d1、d2中都含有私有、保护和公有成员,d1类是base的派生类,d2是d1的派生类。试分析下列程序的
访问权限。考察:继承中成员访问属性的变化面向对象程序设计习题#include<iostream.h>classbase{private:intn1;protected:intk1;public:base(){
n1=0;k1=1;}voidfun1(){cout<<n1<<k1<<endl;}};classd1:publicbase{intn2;protected:intk2;public:d1(){n2=10;k2=11;}voidfun2(){cout<<n1<<k1<<endl;cou
t<<n2<<k2<<endl;}};classd2:publicd1{intn3;protected:intk3;public:d2(){n3=20;k3=21;}voidfun3(){cout<<
n1<<k1<<endl;cout<<n2<<k2<<endl;cout<<n3<<k3<<endl;}};面向对象程序设计习题voidmain(){basebaseobj;d1d1obj;d2d2obj;baseobj.f
un1();d1obj.fun2();d2obj.fun3();}①回答下列问题:l派生类d1中成员函数fun2()能否访问基类base中的成员fun1()、n1和k1?l派生类d1的对象能否访问基类base中的成员fun1()、n1和k1?l派生类d2中成员函数fun3()能否访问直接
基类d1中的成员fun2()、n2和k2?能否访问基类base中的成员fun1()、n1和k1?l派生类d2的对象能否访问直接基类d1中的成员fun2()、n2和k2?能否访问基类base中的成员fun1()
、n1和k1?②以上程序有错,请改正,并上机验证。privateprotectedpublicbasen1k1base(),fun1()d1n2k2d1(),fun2()d2n3k3d2(),fun3()priv
ateprotectedpublicbasen1k1base(),fun1()d1n2k2k1d1(),fun2()fun1()d2n3k3k2k1d2(),fun3()fun2()fun1()面向对象程序设计习题①(1)能访问base中公有的fun1
()和保护的k1,不能访问私有的n1(2)能访问base中公有的fun1(),不能访问n1和k1(3)能访问d1中公有的fun2()和k2,不能访问私有的n2能访问base中公有的fun1()和k1,不能访问私有的n
1(4)能访问d1中公有的fun2(),不能访问私有的n2和k2能访问base中公有的fun1(),不能访问私有的n1和k1②fun2函数中:cout<<n1<<k1<<endl;替换为fun1();fun3函数中:cout<<n1<<k1<<endl;cout<<n2<<k2<<endl;
替换为fun2();面向对象程序设计习题(4)下列程序是一个有关虚基类及其派生类的初始化的程序。如果虚基类定义有非默认形式的(即带形参)构造函数,在整个继承结构中,直接或间接继承虚基类的所有派生类,都必须在构造函数的成员初始化表中列出对虚基类的初始化。面向对象程序设计习题#incl
ude<iostream.h>classB1{public:intn1;B1(intin_n1){n1=in_n1;cout<<"B1,n1="<<n1<<endl;}};classB21:virtualpub
licB1{public:intn21;B21(inta):B1(a){n21=a;cout<<"B21,n21="<<n21<<endl;}};classB22:virtualpublicB1{public:intn22;B22(inta)
:B1(a){n22=a;cout<<"B22,n22="<<n22<<endl;}};classB3:publicB21,publicB22{public:intn3;B3(inta):B1(a),B21(a),B22(a){n3=a;cout<<"B3,n3="<<n3<<endl;}};
voidmain(){B3obj(5);}B1,n1=5B21,n21=5B22,n22=5B3,n3=5程序运行结果为:面向对象程序设计习题①如果程序运行结果为:B1,n1=5B22,n22=5B21,n21=5B3,n3=5上述程序应该怎么改?②如果将B3(inta):B1(a),B
21(a),B22(a){n3=a;cout<<"B3,n3="<<n3<<endl;}中的B21(a)和B22(a)的位置调换,程序的运行结果是否会有变化?①classB3:publicB21,public
B22改为classB3:publicB22,publicB21②没有变化,初始化的顺序与继承顺序有关,与初始化列表的顺序无关面向对象程序设计习题4-4完成下列程序(1)下列程序是一个从Point类私有派生新
的矩形Rectangle类的程序。请填空完成程序,并上机运行验证。面向对象程序设计习题classRectangle:②{private:floatW,H;public:voidInitR(floatx,floaty,floatw,floath){③;W=w;H=h;}voidMove(fl
oatxOff,floatyOff){Point::Move(xOff,yOff);}floatGetX(){returnPoint::GetX();}floatGetY(){returnPoint::GetY();}floatGetH(){returnH
;}floatGetW(){returnW;}};#include<iostream.h>#include<math.h>classPoint{private:floatX,Y;public:voidInitP(floatxx=
0,floatyy=0){X=xx;Y=yy;}voidMove(floatxOff,floatyOff){①;}floatGetX(){returnX;}floatGetY(){returnY;}};c
lassRectangle:②{private:floatW,H;public:voidInitR(floatx,floaty,floatw,floath){③;W=w;H=h;}voidMove(floatxOff,floatyOff){Point::Mo
ve(xOff,yOff);}floatGetX(){returnPoint::GetX();}floatGetY(){returnPoint::GetY();}floatGetH(){returnH;}floatGetW(){returnW;}};面向对象
程序设计习题voidmain(){Rectanglerect;rect.InitR(2,3,20,10);rect.Move(3,2);cout<<"Thedataofrect(X,Y,W,H):"<<endl;cout<<rect.GetX()<<","<<rect
.GetY()<<","<<rect.GetW()<<","<<rect.GetH()<<endl;}程序运行结果为:Thedataofrect(X,Y,W,H):5,5,20,10①X+=xOff;Y+=yOf
f;②privatePoint③InitP(x,y);面向对象程序设计习题(2)下列程序中声明一个圆类circle和一个桌子类table,另外声明一个圆桌类roundtable,它是由circle和table两个类派生的,要求声明一个圆桌类
对象,并输出圆桌的高度、面积和颜色。请填空完成程序,并上机运行验证。面向对象程序设计习题classtable{doubleheight;public:table(doubleh){height=h;}doubleget_height(){returnhe
ight;}};classroundtable:publictable,publiccircle{char*color;public:roundtable(doubleh,doubler,charc[]):②{color=newchar[s
trlen(c)+1];③;}char*get_color(){returncolor;}};#include<iostream.h>#include<string.h>classcircle{doubleradius;public:circle(doubler){radiu
s=r;}doubleget_area(){return①;}};classtable{doubleheight;public:table(doubleh){height=h;}doubleget_height(){
returnheight;}};classroundtable:publictable,publiccircle{char*color;public:roundtable(doubleh,doubler,charc[]):②{color=newchar[strlen(
c)+1];③;}char*get_color(){returncolor;}};面向对象程序设计习题voidmain(){roundtablert(0.8,1.0,"白色");cout<<"圆桌数据
:"<<endl;cout<<"圆桌高度:"<<rt.get_height()<<endl;cout<<"圆桌面积:"<<rt.get_area()<<endl;cout<<"圆桌颜色:"<<rt.get_color()
<<endl;}①3.14*radius*radius②circle(r),table(h)③strcpy(color,c)面向对象程序设计习题(3)这是一个用运算符重载为友元重载的方法重做复数加减法的运算,请填空完成程序并
上机运行验证(类似课件例题)。面向对象程序设计习题cl=(5,4)c2=(2,10)c3=c1-c2=(3,-6)c4=c1+c2=(7,14)①real=r;imag=i;②complex(c1.real+c2.real,c1.imag+c2.imag
)③complex(c1.real-c2.real,c1.imag-c2.imag)#include<iostream.h>classcomplex{private:doublereal;doubleimag;public:complex(do
ubler=0.0,doublei=0.0){①;}friendcomplexoperator+(complexc1,complexc2);friendcomplexoperator-(complexc1
,complexc2);voiddisplay();};complexoperator+(complexc1,complexc2){return②;}complexoperator-(complexc
1,complexc2){return③;}voidcomplex::display(){cout<<"("<<real<<","<<imag<<")"<<endl;}voidmain(){complexc1(5,4),c2(2,10),c3,c
4;cout<<"cl=";c1.display();cout<<"c2=";c2.display();c3=c1-c2;cout<<"c3=c1-c2=";c3.display();c4=c1+c2;cout<<"c4=c1
+c2=";c4.display();}面向对象程序设计习题4-5编程题(1)声明一个水果类和树类,并描述它们的一些特征,在此基础上派生出苹果类和桃子类,定义它们的一些基本功能。(2)编写程序声明一个Shape基类,再派生出Rectangle和Circle类,二者
都有GetArea()函数计算对象的面积。#include<iostream.h>constdoublePI=3.14159;classShapes//基类Shapes{intx,y;public:S
hapes(intxx,intyy=0){x=xx;y=yy;}voidGetArea(){cout<<"Theareaofshapesisundefined"<<endl;}};classRectangle:publicShapes//派生类Rectangle{intwide,high;publ
ic:Rectangle(intxx,intyy,intw,inth):Shapes(xx,yy){wide=w;high=h;}voidGetArea(){cout<<"Theareaofrectangleis:"<<wide*high<<endl;}};方法一classC
ircle:publicShapes//派生类Circle{intradius;public:Circle(intxx,intyy,intr):Shapes(xx,yy){radius=r;}voidGetArea(){cout<<"Theareaofcircleis
:"<<PI*radius*radius<<endl;}};voidmain(){Shapessh(5,8);sh.GetArea();Rectanglerect(5,8,10,10);rect.GetArea()
;Circlecir(5,8,10);cir.GetArea();}方法一#include<iostream.h>constdoublePI=3.14159;classShapes//基类Shapes{protected:intx,y;public:voidset
value(intxx,intyy=0){x=xx;y=yy;}voidGetArea(){cout<<"Theareaofshapesisundefined"<<endl;}};classRectangle:publicShapes//派生类Rect
angle{public:voidGetArea(){cout<<"Theareaofrectangleis:"<<x*y<<endl;}};classCircle:publicShapes//派生类Circle{public:voidGetArea(){cout<<"Theareaofci
rcleis:"<<PI*x*x<<endl;}};voidmain(){Shapessh;Rectanglerect;Circlecir;sh.setvalue(5,8);sh.GetArea();rect.setvalue(5,8);rect.GetArea();cir.set
value(10);cir.GetArea();}方法二面向对象程序设计习题(3)编写一个能输入和输出学生和教师数据的程序,学生数据有编号、姓名、班号和成绩;教师数据有编号、姓名、职称和部门。要求声明
一个person类,并作为学生数据操作类student和教师数据操作类teacher的基类。面向对象程序设计习题#include<iostream.h>#include<string.h>classperson{priv
ate:char*name;intno;public:person(char*n1,intn2){name=newchar[strlen(n1)+1];strcpy(name,n1);no=n2;}char*get_name(){r
eturnname;}intget_no(){returnno;}};classstudent:publicperson{private:chardepart[10];intdegree;public:student(char*n1,int
n2,char*n3,intn4):person(n1,n2){strcpy(depart,n3);degree=n4;}intget_degree(){returndegree;}char*get_depart(){returndepart;}};方法一:说明指针与数组的赋值问题面向对象程序设
计习题classteacher:publicperson{private:charprof[10];chardepart[10];public:teacher(char*n1,intn2,char*n3,char*n4):person(n1,n2)
{strcpy(prof,n3);strcpy(depart,n4);}char*get_prof(){returnprof;}char*get_depart(){returndepart;}};voidmain(){students1("李明",1001,"计算机",10)
;teachert1("曾二",899,"教授","理学院");cout<<s1.get_name()<<""<<s1.get_no()<<""<<s1.get_degree()<<""<<s1.get_depart()<<endl;cout<<s1.get_na
me()<<""<<s1.get_no()<<""<<t1.get_prof()<<""<<t1.get_depart()<<endl;}面向对象程序设计习题#include<iostream.h>#include<string.h>classperson
{char*name,*num;public:person(char*str1,char*str2){name=newchar[strlen(str1)+1];strcpy(name,str1);num=newchar[strlen(str2)+1];strcpy(num,str2);}char*
get_num(){returnnum;}char*get_name(){returnname;}};classstudent:publicperson{char*classnum;intsorce;public:student(char*str1,cha
r*str2,char*classnum1,intsorce1):person(str1,str2){classnum=newchar[strlen(classnum1)+1];strcpy(classnum,classnum1);sorce=sorce1;}voidget_
student(){cout<<person::get_num()<<endl;cout<<person::get_name()<<endl;cout<<classnum<<endl;cout<<sorce<<endl;}};方法二:说明在主函数中输入值问题面向对象程序设计习题
classteacher:publicperson{char*dep,*pos;public:teacher(char*str1,char*str2,char*dep1,char*pos1):person(str1,str2){dep=newchar[strlen(dep1)
+1];strcpy(dep,dep1);pos=newchar[strlen(pos1)+1];strcpy(pos,pos1);}voidget_teacher(){cout<<person::get_num()<<endl
;cout<<person::get_name()<<endl;cout<<dep<<endl;cout<<pos<<endl;voidmain(){charnum[10],name[10],classn
um[10],;chardep[10],pos[10];intsorce;cout<<"Enterastudentdata"<<endl;cin>>num>>name>>classnum>>sorce;cout<<num<<","<<name<<","<<classnum<<","<<
sorce;studenta(num,name,classnum,sorce),*ptr1;//studenta("er","rt","ty",58),*ptr1;ptr1=&a;ptr1->get_student();cout<<"Enterateacherdata"<<en
dl;cin>>num>>name>>dep>>pos;teacherb(num,name,dep,pos),*ptr2;ptr2=&b;ptr2->get_teacher();}面向对象程序设计习题数组与指针•数组的实质是内存中一块连续的空间,存储着同
一类型变量的数据。要访问数组中的某个元素,可以通过“数组名[下标]”这样的语法形式来实现。•结论:如果知道数组第1个(下标为0)元素的地址,将这个地址保存在某个指针变量中,那么对数组元素的访问就完全可以转换为指针操作。•C++语言就是这样对数组元素进行访问的。–在声明一个数
组时,C++语言将在内存中开辟两个空间,一个用于保存数组元素,另一个用于保存数组的第1个元素的地址。–数组名就是用于保存数组第1个元素的地址的指针型常量。通过保存数组第1个元素地址的数组名这个指针型常量,可以使用“[
]”运算符访问数组元素,也可以直接使用指针的运算规则访问数组元素。面向对象程序设计习题字符串赋值•函数strcpy将第2个参数(字符串)复制到第1个参数(字符数组)中,这个字符数组的长度应当足以放下字符串及其NULL终止符。•函数strncpy与strcpy相似,只是s
trncpy指定了从字符串复制到字符数组的字符数。#include<iostream.h>#include<string.h>voidmain(){charpSource[]="Iamateacher.";charpDestination[
]="youareastudent.";//pDestination字符串长度≥pSource字符串长度strcpy(pDestination,pSource);//等价于strncpy(pDestination,pSourc
e,16);cout<<pSource<<endl;cout<<pDestination<<endl;}面向对象程序设计习题(4)重载函数Double(x),要求返回值为输入参数的两倍;参数分别为整型、长整型、浮点型、双精度型,返回值类型与参数一样。#include<
iostream.h>constdoublePI=3.14159;classMyclass{public:intDouble(intx){return2*x;}longDouble(longx){retu
rn2*x;}floatDouble(floatx){return2*x;}doubleDouble(doublex){return2*x;}};voidmain(){Myclasstest;inta=2;cout<<test.Double(a)<<endl;longb=2;cout<<test
.Double(b)<<endl;floatc=2.0;cout<<test.Double(c)<<endl;doubled=2.0;cout<<test.Double(d)<<endl;}voidmain(){Myclasstest;cout<<test.Double(5)<<endl;co
ut<<test.Double(5l)<<endl;cout<<test.Double(5.0f)<<endl;cout<<test.Double(5.0)<<endl;}voidmain(){Myclasstest;cout<<test.Double(5)<<endl;cout<<te
st.Double((long)5)<<endl;cout<<test.Double((float)5.0)<<endl;cout<<test.Double(5.0)<<endl;}面向对象程序设计习题#incl
ude<iostream.h>constdoublePI=3.14159;classShapes//抽象基类Shapes{intx,y;public:Shapes(intxx,intyy){x=xx;y=yy;}virtualvoidGetA
rea()=0;//纯虚函数成员virtualvoidGetPrim()=0;//纯虚函数成员};classRectangle:publicShapes//派生类Rectangle{intwide,high;public:Rectangle(intxx,in
tyy,intw,inth):Shapes(xx,yy){wide=w;high=h;}(5)声明一个Shape抽象类,在此基础上派生出Rectangle和Circle类,二者都使用GetArea()函数计算对象的面积,GetPerim()
函数计算对象的周长(同课件例题)。面向对象程序设计习题voidGetArea(){cout<<"Theareaofrectangleis:"<<wide*high<<endl;}voidGetPrim(){co
ut<<"Theareaofrectangleis:"<<2*(wide+high)<<endl;}};classCircle:publicShapes//派生类Circle{intradius;public:Circle
(intxx,intyy,intr):Shapes(xx,yy){radius=r;}voidGetArea(){cout<<"Theareaofcircleis:"<<PI*radius*radius<<endl;}voidGetPrim(){cout<<"Th
eareaofrectangleis:"<<2*PI*radius<<endl;}};voidmain(){Shapes*ptr[2];//声明抽象基类指针Rectanglerect(5,8,10,10);Circlecir(5,8,10);ptr[0
]=▭ptr[0]->GetArea();ptr[0]->GetPrim();ptr[1]=○ptr[1]->GetArea();ptr[1]->GetPrim();}