zoukankan      html  css  js  c++  java
  • 《C++程序设计POJ》《WEEK3 类和对象进阶》《第三周-编程填空》

    填空题-1

    1 满分 (计入成绩)
    总时间限制:
    1000ms
    内存限制:
    65536kB
    描述

    下面程序输出的结果是:

    4,6

    请填空:

    #include <iostream>
    using namespace std;
    class A {
            int val;
        public:
            A(int n) { val = n; }
            int GetVal() { return val; }
    };
    class B: public A {
        private:
            int val;
        public:
            B(int n):
    // 在此处补充你的代码
            { }
            int GetVal() { return val; }
    };
    int main() {
        B b1(2);
        cout << b1.GetVal() << "," << b1.A::GetVal() << endl;
        return 0;
    }
    输入
    输出
    4,6
    样例输入
    样例输出
    4,6
    样例输入
    #include <iostream>
    using namespace std;
    class A {
        int val;
    public:
        A(int n) { val = n; }
        int GetVal() { return val; }
    };
    class B : public A {
    private:
        int val;
    public:
        /*继承,初始化列表,继承类必须初始化基类吗???*/
        B(int n) :val(2 * n),A(3*n) {}        // 在此处补充你的代码  4,6
    
        int GetVal() { return val; }
    };
    int main() {
        B b1(2);
        cout << b1.GetVal() << "," << b1.A::GetVal() << endl;
        while (1);
        return 0;
    }

    填空题-2

    1 满分 (计入成绩)
    总时间限制:
    1000ms
    内存限制:
    65536kB
    描述

    下面程序输出的结果是:

    0

    5

    请填空:

    #include <iostream>
    using namespace std;
    class A {
    public:
        int val;
    // 在此处补充你的代码
    };
    main()  {
        A a;
        cout << a.val << endl;
        a.GetObj() = 5;
        cout << a.val << endl;
    }
    输入
    输出
    0
    5
    样例输入
    样例输出
    0
    5
    提示
    所缺代码具有如下形式:
    A(_________________ ){ val = n; };
    ________________ GetObj() {
    return _________________;
    }
    #include <iostream>
    using namespace std;
    class A {
    public:
        int val;
    
        // 在此处补充你的代码
        A(int n = 0)
        {
            val = n;
        }
        int& GetObj()
        {
            return val;//返回val的引用  
        }
        //A& GetObj()
        //{
        //    return *this;
        //}
    
    };
    int main() {
        A a;
        cout << a.val << endl;
        a.GetObj() = 5;
        cout << a.val << endl;
        while (1);
    }

    填空题-3

    1 满分 (计入成绩)
    总时间限制:
    1000ms
    内存限制:
    65536kB
    描述

    下面程序的输出是:

    10

    请补足Sample类的成员函数。不能增加成员变量。

    #include <iostream>
    using namespace std;
    class Sample{
    public:
        int v;
        Sample(int n):v(n) { }
    // 在此处补充你的代码
    };
    int main() {
        Sample a(5);
        Sample b = a;
        cout << b.v;
        return 0;
    }
    输入
    输出
    10
    样例输入
    样例输出
    10
    #include <iostream>
    using namespace std;
    class Sample {
    public:
        int v;
        Sample(int n) :v(n) { }
        Sample(Sample &c)
        {
            v = 2 * c.v;//考察复制构造函数
        }
    
        // 在此处补充你的代码
    };
    int main() {
        Sample a(5);
        Sample b = a;
        cout << b.v;
        while (1);
        return 0;
    }

    填空题-4

    1 满分 (计入成绩)
    总时间限制:
    1000ms
    内存限制:
    65536kB
    描述

    下面程序的输出结果是:

    5,5

    5,5

    请填空:

    #include <iostream>
    using namespace std;
    class Base {
    public:
        int k;
        Base(int n):k(n) { }
    };
    class Big  {
    public:
        int v; Base b;
    // 在此处补充你的代码
    };
    int main()  {
        Big a1(5);    Big a2 = a1;
        cout << a1.v << "," << a1.b.k << endl;
        cout << a2.v << "," << a2.b.k << endl;
        return 0;
    }
    输入
    输出
    5,5
    5,5
    样例输入
    样例输出
    5,5
    5,5
    提示
    所缺代码具有如下形式:
    Big ________________{ }
    Big ________________{ }


    #include <iostream>
    using namespace std;
    class Base {
    public:
        int k;
        Base(int n) :k(n) { }
    };
    class Big {
    public:
        int v; Base b;
        Big(int n) :v(n), b(n) {}
        Big(Big &a) :v(a.v), b(a.v) {} //考察成员对象
        // 在此处补充你的代码
    };
    int main() {
        Big a1(5);    Big a2 = a1;
        cout << a1.v << "," << a1.b.k << endl;
        cout << a2.v << "," << a2.b.k << endl;
        while (1);
        return 0;
    }
    //5, 5
    //5, 5
      1 #if 0
      2 #include <iostream>
      3 using namespace std;
      4 class A {
      5     int val;
      6 public:
      7     A(int n) { val = n; }
      8     int GetVal() { return val; }
      9 };
     10 class B : public A {
     11 private:
     12     int val;
     13 public:
     14     /*继承,初始化列表,继承类必须初始化基类吗???*/
     15     B(int n) :val(2 * n),A(3*n) {}        // 在此处补充你的代码  4,6
     16 
     17     int GetVal() { return val; }
     18 };
     19 int main() {
     20     B b1(2);
     21     cout << b1.GetVal() << "," << b1.A::GetVal() << endl;
     22     while (1);
     23     return 0;
     24 }
     25 #endif
     26 
     27 #if 0
     28 #include <iostream>
     29 using namespace std;
     30 class A {
     31 public:
     32     int val;
     33 
     34     // 在此处补充你的代码
     35     A(int n = 0)
     36     {
     37         val = n;
     38     }
     39     int& GetObj()
     40     {
     41         return val;//返回val的引用  
     42     }
     43     //A& GetObj()
     44     //{
     45     //    return *this;
     46     //}
     47 
     48 };
     49 int main() {
     50     A a;
     51     cout << a.val << endl;
     52     a.GetObj() = 5;
     53     cout << a.val << endl;
     54     while (1);
     55 }
     56 
     57 #endif
     58 
     59 #if 0
     60 #include <iostream>
     61 using namespace std;
     62 class Sample {
     63 public:
     64     int v;
     65     Sample(int n) :v(n) { }
     66     Sample(Sample &c)
     67     {
     68         v = 2 * c.v;//考察复制构造函数
     69     }
     70 
     71     // 在此处补充你的代码
     72 };
     73 int main() {
     74     Sample a(5);
     75     Sample b = a;
     76     cout << b.v;
     77     while (1);
     78     return 0;
     79 }
     80 #endif
     81 
     82 #if 0
     83 #include <iostream>
     84 using namespace std;
     85 class Base {
     86 public:
     87     int k;
     88     Base(int n) :k(n) { }
     89 };
     90 class Big {
     91 public:
     92     int v; Base b;
     93     Big(int n) :v(n), b(n) {}
     94     Big(Big &a) :v(a.v), b(a.v) {} //考察成员对象
     95     // 在此处补充你的代码
     96 };
     97 int main() {
     98     Big a1(5);    Big a2 = a1;
     99     cout << a1.v << "," << a1.b.k << endl;
    100     cout << a2.v << "," << a2.b.k << endl;
    101     while (1);
    102     return 0;
    103 }
    104 //5, 5
    105 //5, 5
    106 #endif
    107 
    108 #if 0
    109 #include <iostream>
    110 using namespace std;
    111 class CSample {
    112 
    113     int x;
    114 
    115 public:
    116 
    117     CSample() { cout << "C1" << endl; }
    118 
    119     CSample(int n) {
    120 
    121         x = n;
    122 
    123         cout << "C2,x=" << n << endl;
    124     }
    125 
    126 };
    127 
    128 int main() {
    129 
    130     CSample array1[2]; // C1 C1
    131 
    132     CSample array2[2] = { 6,8 }; // C2,X=6  C2,X=8
    133 
    134     CSample array3[2] = { 12 }; // C2,X=12  C1
    135 
    136     CSample * array4 = new CSample[3];// C1
    137     while (1);
    138 
    139     return 0;
    140 
    141 }
    142 #endif
    143 
    144 #if 0
    145 #include <iostream>
    146 
    147 using namespace std;
    148 
    149 class Sample {
    150 
    151 public:
    152 
    153     int v;
    154 
    155     Sample() { };
    156 
    157     Sample(int n) :v(n) { };
    158 
    159     Sample(const Sample & x) { v = 2 + x.v; }
    160 
    161 };
    162 //1)当用一个对象去初始化同类的另一个对象时。
    163 //2)如果某函数有一个参数是类 A 的对象,
    164 //那么该函数被调用时,类A的复制构造函数将被调用。
    165 //3) 如果函数的返回值是类A的对象时,则函数返回时,
    166 //A的复制构造函数被调用 :
    167 Sample PrintAndDouble(Sample o) {
    168 
    169     cout << o.v;
    170 
    171     o.v = 2 * o.v;
    172 
    173     return o;
    174 
    175 }
    176 
    177 int main() {
    178 
    179     Sample a(5); //v=5
    180 
    181     Sample b = a;//v = 7
    182 
    183     Sample c = PrintAndDouble(b); //print 9 v = 18 ,20
    184 
    185     cout << endl;
    186 
    187     cout << c.v << endl;///print 20
    188 
    189     Sample d;
    190 
    191     d = a;
    192 
    193     cout << d.v;//print 5
    194     while (1);
    195 
    196 }
    197 
    198 #endif
    199 
    200 
    201 #include <iostream>
    202 using namespace std;
    203 class A
    204 {
    205 public:
    206     int v;
    207     A(int n) { v = n; };
    208     A(const A & a) {
    209         v = a.v;
    210         cout << "Copy constructor called" << endl;
    211     }
    212 };
    213 A Func() {
    214     A b(4);
    215     return b;
    216 }
    217 int main() {
    218     cout << Func().v << endl;
    219     //while (1);
    220     return 0;
    221 }
    222 
    223 //输出结果:
    224 //Copy constructor called
    225 //4


  • 相关阅读:
    MAVEN[08]
    MAVEN[04]
    hdu 1757【A Simple Math Problem】
    hdu 1507【Uncle Tom's Inherited Land*】
    hdu 2768【Cat vs. Dog】
    hdu 1392【Surround the Trees】
    hdu 1348【wall】
    hdu 2824【The Euler function】
    hdu 1147【Pickup sticks】
    hdu 1528【Card Game Cheater】
  • 原文地址:https://www.cnblogs.com/focus-z/p/10993778.html
Copyright © 2011-2022 走看看