第三周:程序設計預算法(三)


 

題面

001:返回什么才好呢

未懂

1.A& GetObj()//沒有明白此處的this指針,定義這個函數的作用為沒有明白

 1 #include <iostream>
 2 using namespace std;
 3 class A {
 4 public:
 5     int val;
 6 
 7     A(int
 8 // 在此處補充你的代碼
 9  n=123)
10     {
11         val = n;
12     }
13     A& GetObj()//沒有明白此處的this指針,定義這個函數的作用為沒有明白
14     {
15         return *this;
16     }
17 };
18 int main()
19 {
20     int m,n;
21     A a;
22     cout << a.val << endl;
23     while(cin >> m >> n) {
24         a.GetObj() = m;
25         cout << a.val << endl;
26         a.GetObj() = A(n);
27         cout << a.val<< endl;
28     }
29     return 0;
30 }

 

002:Big & Base 封閉類問題

 1 #include <iostream>
 2 #include <string>
 3 using namespace std;
 4 class Base {
 5 public:
 6     int k;
 7     Base(int n):k(n) { }
 8 };
 9 class Big
10 {
11 public:
12     int v;
13     Base b;
14 // 在此處補充你的代碼
15 Big(int n):v(n),b(n){}//忘記叫啥鏈表了,反正是賦值用的!
16 };
17 int main()
18 {
19     int n;
20     while(cin >>n) {
21         Big a1(n);
22         Big a2 = a1;
23         cout << a1.v << "," << a1.b.k << endl;
24         cout << a2.v << "," << a2.b.k << endl;
25     }
26 }

 

003:編程填空:統計動物數量(不會標記)

恕我直言整道題我都不懂

 1 #include <iostream>
 2 using namespace std;
 3 // 在此處補充你的代碼
 4 class Animal{
 5 public:
 6     static int number;
 7     Animal(){
 8         number++;
 9     }
10     virtual ~Animal()
11     {
12         number--;
13     }
14 };
15 int Animal::number=0;
16 class Dog:public Animal{
17 public:
18     static int number;
19     Dog()
20     {
21         number++;
22     }
23     ~Dog()
24     {
25         number--;
26     }
27 };
28 int Dog::number=0;
29 class Cat :public Animal{
30 public:
31     static int number;
32     Cat()
33     {
34         number++;
35     }
36     ~Cat()
37     {
38         number--;
39     }
40 };
41 int Cat::number=0;
42 void print() {
43     cout << Animal::number << " animals in the zoo, " << Dog::number << " of them are dogs, " << Cat::number << " of them are cats" << endl;
44 }
45 void print() {
46     cout << Animal::number << " animals in the zoo, " << Dog::number << " of them are dogs, " << Cat::number << " of them are cats" << endl;
47 }
48 
49 int main() {
50     print();
51     Dog d1, d2;
52     Cat c1;
53     print();
54     Dog* d3 = new Dog();
55     Animal* c2 = new Cat;
56     Cat* c3 = new Cat;
57     print();
58     delete c3;
59     delete c2;
60     delete d3;
61     print();
62 }

 

004:這個指針哪來的

要學常變量對象的定義形式

 1 #include <iostream>
 2 using namespace std;
 3 
 4 struct A
 5 {
 6     int v;
 7     A(int vv):v(vv) { }
 8 // 在此處補充你的代碼
 9     const A* getPointer() const//如果定義的不是指針的返回的只是地址不是值
10     {
11         return this;
12     }
13 };
14 
15 int main()
16 {
17     const A a(10);
18     const A * p = a.getPointer();//常變量只能調用常變量函數,故定義的函數也必須是常變量函數
19     cout << p->v << endl;
20     return 0;
21 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM