• 第三周:程序设计预算法(三)


    题面

    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 }

  • 相关阅读:
    Web Designer Intern
    Internship UI/UX Web Designer
    HTML / CSS Frontend Software Engineer Internship
    CSS 07 文本
    CSS 06 背景
    CSS 05 尺寸大小
    CSS 04 注释
    CSS 03 选择器
    Why Ancient Greeks are Always Nude
    C#表示空字符
  • 原文地址:https://www.cnblogs.com/waryan/p/12182265.html
Copyright © 2020-2023  润新知