作业及其答案汇总集团文件版本号:(M928-T898-M248-WU2669-I2896-DQ586-M1988)一、类和对象基本概念1) 写出下面程序的运行结果:#include <iostream.h>class Apple {private :static int nTotalNumber;public:Apple(){ nTotalNumber ++; }~Apple( ) { nTotalNumber --; }static void PrintTotal(){ cout << nTotalNumber << endl; } };int Apple::nTotalNumber = 0;Apple Fun( const Apple & a ){ a.PrintTotal(); return a ; }int main () {Apple * p = new Apple[4];Fun( p[2]);Apple p1, p2;delete [] p;p1.PrintTotal();}/*41*/2) 写出下面程序的运行结果:#include <iostream.h>class Sample{public:int v;Sample() { };Sample(int n):v(n) { };Sample( Sample & x) { v = 2 + x.v ; } };Sample PrintAndDouble( Sample o){cout << o.v;o.v = 2 * o.v;return o;}int main(){Sample a(5);Sample b = a;Sample c = PrintAndDouble( b );cout << endl;cout << c.v << endl;Sample d;d = a;cout << d.v ;}/*9225*/3) 下面的程序输出结果是:5请填空补足程序。
所填内容不允许包含分号。
class A {public:int val;A(____________ ){ val = n; };___A &________ GetObj() {return _* this_______;}};main() {A a;cout <<a.val << endl;a.GetObj() = 5;cout << a.val << endl;}/*int n =0A &* this*/4) 下面程序的输出是:3+4i5+6i请补足Complex类的成员函数。
不能增加成员变量。
#include <iostream>#include <string.h>using namespace std;class Complex {private:double r,i;public:void Print() {cout << r << "+" << i << "i" << endl;}};int main(){Complex a;a = "3+4i";a.Print();a = "5+6i";a.Print();}/*Complex() { };Complex( char * s) {r = s[0] - '0';i = s[2] - '0';}*/5) 下面程序的输出是:10请补足Sample类的成员函数。
不能增加成员变量。
#include <iostream.h>class Sample{public:int v;Sample(int n):v(n) { };};int main(){Sample a(5);Sample b = a;cout << b.v ;}/*Sample( Sample & x) { v = 2 * x.v ; }*/6)下面程序的输出是:ThisHello请补足MyString类的成员函数。
不能增加成员变量。
#include <iostream.h>#include <string.h>class MyString{char * p;public:MyString( char * s ) {p = new char[strlen(s)+1];strcpy(p,s);}~MyString() { delete [] p;}const char * c_str() { return p;}};int main(){MyString s1("This"), s2 =s1;s2.Copy ( "Hello");cout << s1.c_str () << endl << s2.c_str () ; }/*void Copy( char * s) {delete [] p;p = new char[strlen(s)+1];strcpy(p,s);}MyString( MyString & o ) {p = new char[strlen(o.p ) + 1 ];strcpy( p,o.p);}*/7)下面程序的输出结果是:5,55,5请填空#include <iostream.h>#include <string.h>class Base {public:int k;Base(int n):k(n) { }};class Big{public:int v;Base b;Big ________________ { }Big ________________{ }};int main(){Big a1(5);Big a2 = a1;cout << a1.v << "," << a1.b.k << endl;cout << a2.v << "," << a2.b.k << endl;}/*Big(int n):v(n),b(n){ }Big(Big & x):v(x.v),b(x.b.k){ }*/二、运算符重载1)下面的MyInt类只有一个成员变量。
MyInt类内部的部分代码被隐藏了。
假设下面的程序能编译通过,且输出结果是:4,1请写出被隐藏的部分。
(您写的内容必须是能全部放进 MyInt类内部的,MyInt的成员函数里不允许使用静态变量)。
#include <iostream.h>class MyInt{int nVal;public:MyInt( int n) { nVal = n ;}int ReturnVal() { return nVal;}………………….};main () {MyInt objInt(10);objInt-2-1-3;cout << objInt.ReturnVal();cout <<",";objInt-2-1;cout << objInt.ReturnVal();}/*MyInt & operator -( int x) {nVal -= x;return * this;}*/2) 下面的程序输出结果是:(4,5)(7,8)请填空。
填写的内容不能包含分号#include <iostream.h>class Point {private:int x;int y;public:Point(int x_,int y_ ):x(x_),y(y_) { };__________________________________________________;};____________ operator << ( ________________, const Point & p) {___________________________________;return _________________;}main(){cout << Point(4,5) << Point(7,8);}/*friend ostream & operator << ( ostream & o, const Point & p); ostream &ostream & oo << "(" << p.x << "," << p.y << ")" << endlo*/3)写一个二维数组类 Array2,使得下面程序的输出结果是:0,1,2,3,4,5,6,7,8,9,10,11,next0,1,2,3,4,5,6,7,8,9,10,11,#include <iostream>using std::cout;using std::endl;int main(){Array2 a(3,4);int i,j;for( i = 0;i < 3; i ++ )for( j = 0; j < 4; j ++ )a[i][j] = i * 4 + j;for( i = 0;i < 3; i ++ ) {for( j = 0; j < 4; j ++ ) {cout << a(i,j) << ",";}cout << endl;}cout << "next" << endl;Array2 b;b = a;for( i = 0;i < 3; i ++ ) {for( j = 0; j < 4; j ++ ) {cout << b[i][j] << ",";}cout << endl;}return 0;}/*class Array2{private:int * p;int r,c;public:Array2() { p = NULL ; }Array2( int r_, int c_ ) :r(r_),c(c_){p = new int [ r * c];}Array2( Array2 & a ):r(a.r),c(a.c) {p = new int [r * c];memcpy( p, a.p,sizeof(int ) * r * c);}Array2 & operator=( const Array2 & a ) {if( p )delete [] p;r = a.r;c = a.c;p = new int [r * c];memcpy( p, a.p,sizeof(int ) * r * c);return * this;}~Array2() {if( p)delete [] p;}int * operator [] ( int i ) {return p + i * c;}int & operator() ( int i,int j ){return p[ i * c + j];}};*/4)编写HugeInt类,使得下面程序的输出结果是:3)100004)100005)100016)10006#include <iostream>#include <string.h>#include <stdlib.h>#include <stdio.h>using std::ostream;using std::cout;using std::cin;using std::endl;const int MAX = 110;void main(){CHugeInt d(9999);cout << "1)" << temp << endl;cout << "2)" << temp2 << endl;cout << "3)" << ++d << endl;cout << "4)" << d++ << endl;cout << "5)" << d << endl;d += 5;cout << "6)" << d << endl;cout << "7)" << d + temp;}/*class CHugeInt{private:int Number[MAX];public:CHugeInt( ) {memset(Number,0,sizeof(Number)); };CHugeInt( const char * s ) {int i,j;memset(Number,0,sizeof(Number));for( i = strlen(s) -1, j = 0; i >= 0; i -- )Number[j++] = s[i] - '0';}CHugeInt( int n ) {char s[20];sprintf(s,"%d",n);int i,j;memset(Number,0,sizeof(Number));for( i = strlen(s) -1, j = 0; i >= 0; i -- )Number[j++] = s[i] - '0';}CHugeInt operator +( const CHugeInt & n) const {CHugeInt tmp( * this);for( int i = 0;i < MAX ; i ++ ) {tmp.Number[i] += n.Number[i]; //逐位相加if( tmp.Number[i] >= 10 ) { //看是否要进位tmp.Number[i] -= 10;tmp.Number[i+1] ++; //进位}}return tmp;}const CHugeInt & operator++() {* this = ( * this ) + 1;return * this;}const CHugeInt operator++(int n) {CHugeInt tmp = * this;* this = ( * this ) + 1;return tmp;}const CHugeInt operator +=( const CHugeInt & n){* this = ( * this ) + n;return * this;}friend ostream & operator << (ostream & o, const CHugeInt & n);friend CHugeInt operator+ ( int n1 , const CHugeInt & n2); };CHugeInt operator + ( int n1 ,const CHugeInt & n2){return n2 + n1;}ostream & operator << ( ostream & o, const CHugeInt & n){bool bStart = false;for( int i = MAX -1; i >= 0 ; i -- ) {if( n.Number[i] ) {bStart = true;}if( bStart )cout << n.Number[i];}if( !bStart )cout << 0;return o;}*/三、继承和多态1) 写一个MyString 类,使得下面程序的输出结果是:1. abcd-efgh-abcd-2. abcd-3.4. abcd-efgh-5. efgh-6. c7. abcd-8. ijAl-9. ijAl-mnop10. qrst-abcd-11. abcd-qrst-abcd- uvw xyzaboutbigmetakeabcdqrst-abcd-程序:#include <string.h>#include <stdlib.h>#include <string>#include <iostream>using namespace std;int CompareString( const void * e1, const void * e2) {MyString * s1 = (MyString * ) e1;MyString * s2 = (MyString * ) e2;if( * s1 < *s2 )return -1;else if( *s1 == *s2)return 0;else if( *s1 > *s2 )return 1;}main(){MyString s1("abcd-"),s2,s3("efgh-"),s4(s1);MyString SArray[4] = {"big","me","about","take"}; cout << "1. " << s1 << s2 << s3<< s4<< endl;s4 = s3;s3 = s1 + s3;cout << "2. " << s1 << endl;cout << "3. " << s2 << endl;cout << "4. " << s3 << endl;cout << "5. " << s4 << endl;cout << "6. " << s1[2] << endl;s2 = s1;s1 = "ijkl-";s1[2] = 'A' ;cout << "7. " << s2 << endl;cout << "8. " << s1 << endl;s1 += "mnop";cout << "9. " << s1 << endl;s4 = "qrst-" + s2;cout << "10. " << s4 << endl;s1 = s2 + s4 + " uvw " + "xyz";cout << "11. " << s1 << endl;qsort(SArray,4,sizeof(MyString),CompareString);for( int i = 0;i < 4;i ++ )cout << SArray[i] << endl;//输出s1的下标为0的字符开始长度为4的子串cout << s1(0,4) << endl;//输出s1的下标为5的字符开始长度为10的子串cout << s1(5,10) << endl;}不允许使用C++的string类,完全自己实现交上去的程序文件名为mystring1.cpp提示:如果将程序中所有 "MyString" 用 "string" 替换,那么除了最后两条红色的语句编译无法通过外,其他语句都没有问题,而且输出和前面给的结果吻合。