c++填空题
5-1
閱讀下面的程序,完成其中復制構造函數的代碼。
#include <iostream> using namespace std; class CAT { public:CAT();CAT(const CAT&);~CAT();int GetAge() const { return *itsAge; }void SetAge(int age){ *itsAge=age; }protected:int* itsAge; }; CAT::CAT() { itsAge=new int;*itsAge =5; } CAT::CAT(const CAT& c) { itsAge=new int(5分); *itsAge=*c.itsAge(5分); } CAT::~CAT() { delete itsAge; }5-2
using namespace std; class A{int i; public:A(int k=0){i=k;}void display(){cout<<"i="<<i<<endl;} }; int main() {A*p=new A;(2分)//動態創建對象p,創建的是對象,不是int * p =new int(不是傳統的了,可以那么理解叭?)p->display();delete p;(2分)//刪除對象p,送分題p=new A(8);p->display();delete p;p=new A[3];(2分)//p指向對象數組A *q=p;for(int i=0;i<3;i++){q++->display()(2分)//輸出對象數組數據}delete []p;(2分)//刪除對象數組指針p,這里給有了一定的提示,所以比較容易return 0; } //本題主要考察如何動態內存分配,包括如何創建,指向,刪除的過程 程序輸出如下: i=0 i=8 i=0 i=0 i=05-3
填寫程序中的空白,完成指定的功能
#include<iostream> using namespace std; class Point{double x,y;static int total;(2分)//定義靜態變量 public:Point(double a=0,double b=0):x(a),y(b){total=2;(2分)}~Point(){total--;(2分)}void show(){cout<<"the number of Point is "<<total(2分)<<endl;} }; int Point::total;(2分)//在類外一定要注意記得寫這個!! int main(){Point p1;Point *p=new Point(1,2);p->show();delete p;p1.show();return 0;//整題都是在描述著靜態變量的作用,它不屬于任何對象它像是一個全局函數,掌握靜態成員函數的寫法和作用是該題解題的關鍵程序輸出如下:
the number of Point is 2 the number of Point is 1總結
- 上一篇: Linux选择填空练习题
- 下一篇: Zint 库:Zint库的编译及使用(二