運(yùn)算符重載、繼承、派生程序題及答案_第1頁(yè)
運(yùn)算符重載、繼承、派生程序題及答案_第2頁(yè)
運(yùn)算符重載、繼承、派生程序題及答案_第3頁(yè)
運(yùn)算符重載、繼承、派生程序題及答案_第4頁(yè)
運(yùn)算符重載、繼承、派生程序題及答案_第5頁(yè)
已閱讀5頁(yè),還剩24頁(yè)未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、1. (10分)棧類類模板題目描述用類模板方式設(shè)計(jì)一個(gè)鏈棧類stack,其中有兩個(gè)私有數(shù)據(jù)成員:Node *head(鏈?zhǔn)字羔槪礂m斣刂羔槪?,int num(棧里結(jié)點(diǎn)數(shù))以及3個(gè)公有成員函數(shù):push(元素入棧)、pop(元素出棧)和stackempty(判斷棧是否為空),并建立一個(gè)整數(shù)棧和一個(gè)字符棧。template class stack;注意:為了能夠生成結(jié)點(diǎn)類型不同的鏈?zhǔn)綏?,結(jié)點(diǎn)類的設(shè)計(jì)也需要用類模板。templateclass Node;輸入描述輸入整數(shù)棧的數(shù)據(jù)元素和字符棧的數(shù)據(jù)元素輸出描述輸出整數(shù)棧的數(shù)據(jù)元素出棧序列和字符棧的數(shù)據(jù)元素出棧序列輸入樣例4 8 3 2a d b c

2、輸出樣例2 3 8 4c b d a我的代碼:#includeusing namespace std;template class stacktemplate class NodeT1 a ;Node *next ;public: Node(T1 x):a(x) void show()cout a endl ; friend class stack ;Node *head ;int num ;public:stack()num = 0 ; head = NULL ; void push(T x)Node *p = new Node(x) ;p-next = head ;head = p ;nu

3、m+ ;T pop()Node *p = head ;head = head-next ;T t = p-a ;delete p ;num- ;return t ;bool stackempty()if( head = NULL )return true;elsereturn false;int main()stack a ;stack b ;int x ;char y ;for(int i = 0 ; i x ;a.push(x) ;for( int i = 0 ; i y ;b.push(y);for( int i = 3 ; i = 0 ; i - )a.stackempty() ;co

4、ut a.pop() ;cout = 0 ; i - )b.stackempty();cout b.pop() ;cout endl ;return 0 ;2. (10分)二進(jìn)制類(1)運(yùn)算符重載題目描述將一個(gè)16位二進(jìn)制數(shù)表示成0和1的字符序列,即用一個(gè)字符數(shù)組來(lái)存放這個(gè)二進(jìn)制數(shù)。在這個(gè)類中設(shè)置兩個(gè)構(gòu)造函數(shù),一個(gè)是傳遞整數(shù)參數(shù)的,另一個(gè)是傳遞字符串參數(shù)的。因?yàn)橛脩粼趧?chuàng)建對(duì)象時(shí)傳遞的二進(jìn)制數(shù),可能是以整數(shù)形式給出,也可能是以數(shù)字串形式給出,系統(tǒng)應(yīng)該都能接受。另外有一個(gè)類型轉(zhuǎn)換函數(shù)int(),用來(lái)將類類型向整型轉(zhuǎn)換。兩個(gè)重載運(yùn)算符“+”,“-”,用來(lái)完成兩個(gè)二進(jìn)制數(shù)之間的加減運(yùn)算。class b

5、inary /定義二進(jìn)制類char bits16; /二進(jìn)制字模數(shù)組public:binary(char *); /字符串參數(shù)構(gòu)造函數(shù)binary(int); /整型參數(shù)構(gòu)造函數(shù)friend binary operator +(binary,binary); /重載“+”friend binary operator -(binary,binary); /重載“-”operator int(); /類類型轉(zhuǎn)換函數(shù)void print();主函數(shù)設(shè)計(jì)如下:int main()binary n1=1011;binary n2=int(n1)+15;binary n3=n1-binary(7);n1.

6、print();n2.print();n3.print();coutint(n2)+5endl;coutn2-binary(5)endl;coutn3+binary(5)endl;coutint(n3)-5endl;return 0;輸入描述無(wú)輸出描述略輸入樣例無(wú)輸出樣例00000000000010110000000000011010000000000000010031219-1我的代碼:方法一:#includeusing namespace std ;class binarypublic:binary( char* ) ;binary( int ) ; friend binary opera

7、tor +( const binary & , const binary & ) ; friend binary operator -( const binary & , const binary & ) ; operator int() ; void print() ;private:char bits16 ; ;binary:binary( char *num )int len1 = strlen( num ) - 1 ;/字符串長(zhǎng)度減一為最低位。int len2 = 15 ;while( len1 = 0 & len2 = 0 )bitslen2- = numlen1- ;/逐位賦值wh

8、ile( len2 = 0 )bitslen2- = 0 ;/空高位值0 binary:binary( int num )for( int i = 15 ; i = 0 ; i - )bitsi = ( num % 2 ) + 0 ;/求余數(shù)num = 1 ;/向右移一位。相當(dāng)于整除2 binary operator +( const binary & n1 , const binary & n2 )binary n3 = binary (0000);int sum , num1 = 0 , num2 = 0 ;for( int i = 0 ; i = 15 ; i + )num1 = ( n

9、1.bitsi = 0 ? 0 : 1 ) + 2 * num1 ; for( int i = 0 ; i = 0 ; i - )n3.bitsi = ( sum % 2 ) + 0;sum = 1 ;return n3 ;binary operator -( const binary & n1 , const binary & n2 )binary n3 = binary(0000) ;int cha , num1 = 0 , num2 = 0 ;for( int i = 0 ; i = 15 ; i + )num1 = ( n1.bitsi = 0 ? 0 : 1 ) + 2 * num1

10、 ; for( int i = 0 ; i = 0 ; i - )n3.bitsi = ( cha % 2 ) + 0;cha = 1 ;return n3 ;binary:operator int()int sum = 0 ;for( int i = 0 ; i = 15 ; i + )sum = 2*sum + ( bitsi = 0 ? 0 : 1 ) ;return sum ;void binary:print()char str17 ;strncpy( str , bits , 16 ) ;str16 = 0 ;cout str n ;int main()binary n1 = 10

11、11 ;binary n2 = int( n1 ) + 15 ;binary n3 = n1 - binary(7) ;n1.print();n2.print();n3.print();cout int(n2) + 5 endl ;cout n2 - binary( 5 ) endl ;cout n3 + binary( 5 ) endl ;cout int( n3 ) - 5 endl ;return 0;方法二:#include#include using namespace std ;class binary /定義二進(jìn)制類 char bits16; /二進(jìn)制字模數(shù)組public:bin

12、ary(char *); /字符串參數(shù)構(gòu)造函數(shù)binary(int); /整型參數(shù)構(gòu)造函數(shù)friend binary operator +(binary,binary); /重載“+”friend binary operator -(binary,binary); /重載“-”operator int(); /類類型轉(zhuǎn)換函數(shù)void print();binary:binary( char *num )int isrc = strlen( num ) - 1 ; /字符串長(zhǎng)度-1為最低位int idest = 15 ;while( isrc = 0 & idest = 0 )bitsidest-

13、 = ( numisrc- = 0 ? 0 : 1 ) ; / 逐位賦值while(idest=0)bitsidest- = 0 ; / 空高位值0 binary:binary(int num)for( int i = 15 ; i = 0 ; i - ) bitsi=( num % 2 ) + 0 ; /求余數(shù)num = 1; /移位,相當(dāng)于整除2binary operator +( binary n1 , binary n2 ) unsigned carry = 0 ; unsigned value ; binary res = 0 ; for( int i = 15 ; i = 0 ;

14、i - ) value=( n1.bitsi = 0 ? 0 : 1 ) + ( n2.bitsi = 0 ? 0 : 1 ) + carry; res.bitsi = ( value % 2 = 0 ? 0 : 1 ) ; carry = value 1 ; return res;binary operator -(binary n1, binary n2)unsigned borrow = 0 ;int value ;binary res = 0 ;for(int i=15; i=0; i-)value = ( n1.bitsi = 0 ? 0 : 1 ) - ( n2.bitsi = 0

15、 ? 0 : 1 ) + borrow ; res.bitsi = ( value = -1 | value = 1 ? 1 : 0 ) ; borrow = ( value = -1 | borrow != 0 & ( value = 0 | value = 1 ) ? 1 : 0 ) ;return res;binary:operator int()unsigned value = 0 ;for( int i = 0 ; i = 15 ; i + )value=( value*2 ) + ( bitsi = 0 ? 0 : 1 ) ;return value;void binary:pri

16、nt()char str17;strncpy( str , bits , 16 ) ;str16 = 0 ;cout str n ; int main() binary n1 = 1011 ; binary n2 = int(n1) + 15 ; binary n3 = n1 - binary(7) ; n1.print() ; n2.print() ; n3.print(); cout int( n2 ) + 5 endl ; cout n2 - binary(5) endl ; cout n3 + binary(5) endl ; cout int(n3) - 5 endl ; retur

17、n 0; 3. (10分)二進(jìn)制類(2)運(yùn)算符重載題目描述在上一題的基礎(chǔ)上,將 + 、 - 運(yùn)算符定義為binary類的成員函數(shù)。并且重載運(yùn)算符 、 & 、 | ,分別將二進(jìn)制數(shù)按位取反、數(shù)按位與及按位或。主函數(shù)設(shè)計(jì)如下:int main()binary n1=1011;binary n2=int(n1)+15;binary n3=n1-binary(7);n1.print();n2.print();n3.print();binary n4=n1&n2;binary n5=n1|n2;binary n6=n1; n4.print();n5.print();n6.print();return 0

18、;輸入描述無(wú)輸出描述略輸入樣例無(wú)輸出樣例000000000000101100000000000110100000000000000100000000000000101000000000000110111111111111110100我的代碼:#includeusing namespace std ;class binarypublic:binary( char* ) ;binary( int ) ;binary operator +( const binary & ) ; binary operator -( const binary & ) ;friend binary operator (

19、 binary &t1 ) ;friend binary operator & ( binary &t1 , binary &t2 ) ;friend binary operator | ( binary &t1 , binary &t2 ) ; operator int() ; void print() ;private:char bits16 ; ;binary:binary( char *num )int len1 = strlen( num ) - 1 ;/字符串長(zhǎng)度減一為最低位。int len2 = 15 ;while( len1 = 0 & len2 = 0 )bitslen2-

20、= numlen1- ;/逐位賦值while( len2 = 0 )bitslen2- = 0 ;/空高位值0 binary:binary( int num )for( int i = 15 ; i = 0 ; i - )bitsi = ( num % 2 ) + 0 ;/求余數(shù)num = 1 ;/向右移一位。相當(dāng)于整除2 binary binary:operator +( const binary & n1 )binary n3 = binary (0000);int sum , num = 0 , num1 = 0 ;for( int i = 0 ; i = 15 ; i + )num =

21、 ( bitsi = 0 ? 0 : 1 ) + 2 * num ; for( int i = 0 ; i = 0 ; i - )n3.bitsi = ( sum % 2 ) + 0;sum = 1 ;return n3 ;binary binary:operator -( const binary & n1 )binary n3 = binary(0000) ;int cha , num = 0 , num1 = 0 ;for( int i = 0 ; i = 15 ; i + )num = ( bitsi = 0 ? 0 : 1 ) + 2 * num ; for( int i = 0 ;

22、 i = 0 ; i - )n3.bitsi = ( cha % 2 ) + 0;cha = 1 ;return n3 ;binary operator ( binary &t1 ) for( int i = 0 ; i = 0 ; i- )t3.bitsi = num % 2 + 0 ;num = 1 ;return t3 ;binary operator | ( binary &t1 , binary &t2 ) binary t3 = 0000 ;int num1 = int(t1) , num2 = int(t2) , num ;num = num1 | num2 ;for( int

23、i = 15 ; i = 0 ; i- )t3.bitsi = num % 2 + 0 ;num = 1 ;return t3 ;binary:operator int()int sum = 0 ;for( int i = 0 ; i = 15 ; i + )sum = 2*sum + ( bitsi = 0 ? 0 : 1 ) ;return sum ;void binary:print()char str17 ;strncpy( str , bits , 16 ) ;str16 = 0 ;cout str n ;int main()binary n1 = 1011 ;binary n2 =

24、 int(n1) + 15 ;binary n3 = n1 - binary(7) ; n1.print() ; n2.print() ; n3.print() ; binary n4 = n1 & n2 ; binary n5 = n1 | n2 ; binary n6 = n1 ; n4.print() ; n5.print() ; n6.print() ; return 0 ;4. (10分)圖形類繼承和派生題目描述定義一個(gè)圖形類,其中有保護(hù)類型的成員數(shù)據(jù):高度和寬度,一個(gè)公有的構(gòu)造函數(shù)。由該圖形類建立兩個(gè)派生類:矩形類和等腰三角形類。在每個(gè)派生類中都包含一個(gè)函數(shù)Area(),分別用來(lái)計(jì)

25、算矩形和等腰三角形的面積。提示:class pic;class recg:public pic;class tag:public pic;輸入描述輸入矩形的長(zhǎng)和寬以及等腰三角形的底邊長(zhǎng)和高。輸出描述矩形的面積和等腰三角形的面積輸入樣例3 53 5輸出樣例157.5我的代碼:#includeusing namespace std ;class picpublic:pic( double w = 0 , double h = 0 );protected:double width , height ;class recg: public picpublic:recg( double w , doub

26、le h );double Area() ;class tag: public picpublic:tag( double w , double h ) ;double Area() ;pic:pic( double w , double h )width = w ;height = h ;tag:tag( double w , double h ): pic( w , h ) width = w ;height = h ;recg:recg( double w , double h ): pic( w , h ) width = w ;height = h ;double recg:Area

27、()double area ;area = width * height ;return area ;double tag:Area()double area ;area = width * height / 2 ;return area ;int main ()int x , y , a , b ;cin x y a b ;recg t1 = recg( x , y ) ;tag t2 = tag( a , b ) ;cout t1.Area() endl t2.Area() ;return 0 ;5. (10分)人員管理信息系統(tǒng)繼承與派生題目描述閱讀以下程序,并調(diào)試運(yùn)行。#include#

28、includeusing namespace std;class employeeprotected:char *name;/姓名int individualEmpNo;/個(gè)人編號(hào)int grade;/級(jí)別float accumPay;/月薪總額static int employeeNo;/本公司職員編號(hào)目前最大值public:employee();/構(gòu)造函數(shù)employee();/析構(gòu)函數(shù)void pay();/汁算月薪函數(shù)void promote(int);/升級(jí)函數(shù)void displayStatus();/顯示人員信息;class technician:public employee/

29、兼職技術(shù)人員類private:float hourlyRate;/每小時(shí)酬金int workHours;/當(dāng)月工作時(shí)數(shù)public:technician();/構(gòu)造函數(shù)void pay();/計(jì)算月薪函數(shù)void displayStatus();/顯示人員信息;class salesman: virtual public employee/兼職推銷員類protected:float CommRate;/按銷售額提取酬金的百分比f(wàn)loat sales;/當(dāng)月銷售額public:salesman();/構(gòu)造函數(shù)void pay();/計(jì)算月薪函數(shù)void displayStatus();/顯示人員

30、信息;class manager: virtual public employee/經(jīng)理類protected:float monthlyPay;/固定月薪數(shù)public:manager();/構(gòu)造函數(shù)void pay();/計(jì)算月薪函數(shù)void displayStatus();/顯示人員信息;class salesmanager:public manager,public salesman/銷售經(jīng)理類public:salesmanager();/構(gòu)造函數(shù)void pay();/計(jì)算月薪函數(shù)void displayStatus();/顯示人員信息;int employee:employeeNo=

31、1000;/員工編號(hào)基數(shù)為1000employee:employee()char namestr50;/輸入雇員姓名時(shí)首先臨時(shí)存放在namestr中coutnamestr;name=new charstrlen(namestr)+1;/動(dòng)態(tài)申請(qǐng)用于存放姓名的內(nèi)存空間strcpy(name,namestr);/將臨時(shí)存放的姓名復(fù)制到nameindividualEmpNo=employeeNo+;/新輸入的員工,編號(hào)為目前最大編號(hào)加1grade=1;/級(jí)別初值為1accumPay=0.0;/月薪總額初值為0employee:employee()delete name;/在析構(gòu)函數(shù)中刪除為存放姓名動(dòng)

32、態(tài)分配的內(nèi)存空間void employee:pay()/計(jì)算月薪,空函數(shù)void employee:promote(int increment)grade+=increment;/升級(jí),提升的級(jí)數(shù)由increment指定void employee:displayStatus()/顯示入員信息,空函數(shù)technician:technician()hourlyRate=100;/每小時(shí)酬金100元void technician:pay()cout請(qǐng)輸入nameworkHours;accumPay=hourlyRate*workHours;/計(jì)算月薪,按小時(shí)計(jì)酬cout兼職技術(shù)人員name編號(hào)ind

33、ividualEmpNo本月工資accumPayendl;void technician:displayStatus()cout兼職技術(shù)人員name編號(hào)individualEmpNo級(jí)別為grade級(jí),已付本月工資accumPayendl;salesman:salesman()CommRate=0.04;/銷售提成比例4void salesman:pay()cout請(qǐng)輸入namesales;accumPay=sales*CommRate;/月薪銷售提成cout推銷員name編號(hào)individualEmpNo本月工資accumPayendl;void salesman:displayStatus

34、()cout推銷員name編號(hào)individualEmpNo級(jí)別為grade級(jí),已付本月工資accumPayendl;manager:manager()monthlyPay=8000;/固定月薪80m元void manager:pay()accumPay=monthlyPay;/月薪總額即固定月薪數(shù)cout經(jīng)理name編號(hào)individualEmpNo本月工資accumPayendl;void manager:displayStatus()cout經(jīng)理name編號(hào)individualEmpNo級(jí)別為grade級(jí),已付本月工資accumPayendl;salesmanager:salesmana

35、ger()monthlyPay=5000;CommRate=0.005;void salesmanager:pay()cout請(qǐng)輸入namesales;employee:accumPay=monthlyPay+sales*CommRate;/月薪=固定月薪銷售提成cout銷售經(jīng)理name編號(hào)individualEmpNo本月工資accumPayendl;void salesmanager:displayStatus()cout銷售經(jīng)理name編號(hào)individualEmpNo級(jí)別為grade級(jí),已付本月工資accumPayendl;int main()manager m1;technician

36、 t1;salesmanager sm1;salesman s1;mote(3);/經(jīng)理m1提升3級(jí)m1.pay();m1.displayStatus();mote(2);t1.pay();t1.displayStatus();mote(2);sm1.pay();sm1.displayStatus();s1.pay();s1.displayStatus();return 0;輸入描述略輸出描述略輸入樣例wanglizhangliu11213輸出樣例請(qǐng)輸入下一個(gè)雇員的姓名:請(qǐng)輸入下一個(gè)雇員的姓名:請(qǐng)輸入下一個(gè)雇員的姓名:請(qǐng)輸入下一個(gè)雇員的姓名:經(jīng)理wang編

37、號(hào)1000本月工資8000經(jīng)理wang編號(hào)1000級(jí)別為4級(jí),已付本月工資8000請(qǐng)輸入li本月的工作時(shí)數(shù):兼職技術(shù)人員li編號(hào)1001本月工資100兼職技術(shù)人員li編號(hào)1001級(jí)別為3級(jí),已付本月工資100請(qǐng)輸入zhang所管轄部門(mén)本月的銷售總額:銷售經(jīng)理zhang編號(hào)1002本月工資5000.06銷售經(jīng)理zhang編號(hào)1002級(jí)別為3級(jí),已付本月工資5000.06請(qǐng)輸入liu本月的銷售額:推銷員liu編號(hào)1003本月工資0.52推銷員liu編號(hào)1003級(jí)別為1級(jí),已付本月工資0.52窗體頂端窗體底端6. (10分)構(gòu)造析構(gòu)函數(shù)繼承題目描述閱讀下面的程序,請(qǐng)編寫(xiě)一個(gè)簡(jiǎn)單的主函數(shù),使其滿足對(duì)應(yīng)

38、的輸出要求。#includeusing namespace std;class CBase1int x ;public:CBase1( )x=0 ; cout調(diào)用構(gòu)造函數(shù)CBase1( )!n;CBase1( int a)x=1;cout調(diào)用構(gòu)造函數(shù)CBase1( int )!n;CBase1( )cout調(diào)用析構(gòu)函數(shù)CBase1( )!n;class CBase2int y;public:CBase2( )y=0 ;cout調(diào)用構(gòu)造函數(shù)CBase2( )!n;CBase2(int a)y=a ;cout調(diào)用構(gòu)造函數(shù)CBase2(int )!n;CBase2()cout調(diào)用析造函數(shù)CBase2( )!n;class Aint x;public:A () x=0 ; cout調(diào)用構(gòu)造函數(shù)A( )!n;A(int a)x=a;cout調(diào)用構(gòu)造函數(shù)A(int )!n;A()cout調(diào)用析構(gòu)函數(shù)A()!n;class CDerived:public CBase1, virtual public CBase2A a;public:CDerived()cout調(diào)用構(gòu)造函數(shù)CDerived( )!n;CDerived(int x,

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論