《作业及其答案汇总》由会员分享,可在线阅读,更多相关《作业及其答案汇总(31页珍藏版)》请在金锄头文库上搜索。
1、一、类和对象基本概念1) 写出下面程序的运行结果:#include class Apple private :static int nTotalNumber;public:Apple() nTotalNumber +; Apple( ) nTotalNumber -; static void PrintTotal() cout class Samplepublic:int v;Sample() ;Sample(int n):v(n) ;Sample( Sample ;Sample PrintAndDouble( Sample o) cout #include using namespace s
2、td;class Complex private:double r,i;public:void Print() cout class Samplepublic:int v;Sample(int n):v(n) ;int main()Sample a(5);Sample b = a;cout #include class MyStringchar * p;public:MyString( char * s ) p = new charstrlen(s)+1;strcpy(p,s);MyString() delete p; const char * c_str() return p;int mai
3、n()MyString s1(This), s2 =s1;s2.Copy ( Hello);cout #include class Base public:int k;Base(int n):k(n) ;class Bigpublic:int v;Base b;Big _ Big _ ;int main()Big a1(5);Big a2 = a1;cout class MyIntint nVal;public:MyInt( int n) nVal = n ;int ReturnVal() return nVal;.;main () MyInt objInt(10);objInt-2-1-3;
4、cout class Point private:int x;int y;public:Point(int x_,int y_ ):x(x_),y(y_) ;_;_ operator using std:cout;using std:endl;int main()Array2 a(3,4);int i,j;for( i = 0;i #include #include #include using std:ostream;using std:cout;using std:cin;using std:endl;const int MAX = 110;void main()CHugeInt a(12
5、34545436342424354354365289899834234235);CHugeInt d(9999);CHugeInt temp = CHugeInt(100000088888888) + 111112;CHugeInt temp2 = 111112 + CHugeInt(100000088888888);cout = 0; i - )Numberj+ = si - 0;CHugeInt( int n ) char s20;sprintf(s,%d,n);int i,j;memset(Number,0,sizeof(Number);for( i = strlen(s) -1, j
6、= 0; i = 0; i - )Numberj+ = si - 0;CHugeInt operator +( const CHugeInt & n) const CHugeInt tmp( * this);for( int i = 0;i = 10 ) /看是否要进位tmp.Numberi -= 10;tmp.Numberi+1 +; /进位return tmp;const CHugeInt & operator+() * this = ( * this ) + 1;return * this;const CHugeInt operator+(int n) CHugeInt tmp = *
7、this;* this = ( * this ) + 1;return tmp;const CHugeInt operator +=( const CHugeInt & n)* this = ( * this ) + n;return * this;friend ostream friend CHugeInt operator+ ( int n1 , const CHugeInt ;CHugeInt operator + ( int n1 ,const CHugeInt & n2)return n2 + n1;ostream & operator = 0 ; i - ) if( n.Numbe
8、ri ) bStart = true;if( bStart )cout #include #include #include using namespace std;int CompareString( const void * e1, const void * e2)MyString * s1 = (MyString * ) e1;MyString * s2 = (MyString * ) e2;if( * s1 *s2 )return 1;main()MyString s1(abcd-),s2,s3(efgh-),s4(s1);MyString SArray4 = big,me,about
9、,take;cout substr(s,l);*/1)看程序写结果:class B private:int nBVal;public:void Print() cout Fun(); pd-Fun();pb-Print (); pd-Print ();pb = pb-Print();/*D:FunB:FunD:FunnBVal=2nBVal=24nDVal=8B:FunnBVal=12*/2)看程序写结果:class A public:A( ) virtual void func() cout class A private:int nVal;public:void Fun() cout cl
10、ass A ;class B:public A public:B() cout class A private:int nVal;public:void Fun() cout Fun(); p-Do();main() Call( new A();Call( new C();/* A * p*/四、模板1) CLinkList 是一个带表头节点的单链表的类模板。带表头节点的单链表的特点是:当链表为空时,表中仍有一个节点,即表头节点。请完整写出 CLinkList 类模板中列出的AppendNode 和 PrintList 成员函数,使得下面程序的输出结果是:0,1,2,3,0,1,2,3,9,1
11、0,11,注意:1)不得调用任何库函数,库模板,不得使用 static 关键字,不得使用除 NULL 以外的任何常量2)不得为 Node 和 CLinkList 模板添加任何成员3)不得添加任何全局变量,不得添加其他函数#include template class Node public:D data;Node * next;templateclass CLinkList private:Node * pHead;public:CLinkList();void AppendNode( D data);void PrintList();templateCLinkList:CLinkList()
12、 pHead = new Node;pHead-next = NULL;main()CLinkList l;for( int i = 0;i void CLinkList:AppendNode( D data)Node * p = pHead;while( p-next )p = p-next;p-next = new Node;p = p-next;p-data = data;p-next = NULL;templatevoid CLinkList:PrintList()Node * p = pHead;while( p-next ) cout next-data next;*/2)填空使程
13、序能编译通过,并写出运行的输出结果#include template /(a)class myclass T i;public:myclass (T a) i = a; void show( ) cout obj(This); /(b)obj.show();该程序输出结果为:_ /(c)/*class Tchar *This*/3)下面的程序输出是:TomHanks请填空。注意,不允许使用任何常量。#include #include using namespace std;template class myclass _;int nSize;public:myclass ( _, int n)
14、 p = new Tn;for( int i = 0;i obj(_);obj.Show();/* T * pT * aszName,strlen(szName)*/五、STL1) 看程序写结果#include #include using namespace std;class A private :int nId;public:A(int n) nId = n; cout vp;vp.push_back(new A(1);vp.push_back(new A(2);vector v;v.push_back (3);/*1 contructor2 contructor3 contructor
15、3 copy constructor3 destructor3 destructor*/2) 下面的程序输出结果为:Tom,Jack,Mary,John,请填空程序:template class MyClass T arrayT2;public:MyClass( T * begin ) copy( begin, begin + T2, array);void List() T * i;for( i = array; i != array + T2;i +)cout obj(array)*/3)下面的程序输出结果是:1 2 6 7 8 9请填空main()int a = 8,7,8,9,6,2,1