• Output of C++ Program | Set 16


      Predict the output of following C++ programs.

    Question 1

     1 #include<iostream>
     2 using namespace std;
     3 
     4 class Base 
     5 {
     6 public:
     7     int fun()      
     8     { 
     9         cout << "Base::fun() called"; 
    10     }
    11     int fun(int i) 
    12     { 
    13         cout << "Base::fun(int i) called"; 
    14     }
    15 };
    16 
    17 class Derived: public Base 
    18 {
    19 public:
    20     int fun(char x)   
    21     { 
    22         cout << "Derived::fun(char ) called"; 
    23     }
    24 };
    25 
    26 int main() 
    27 {
    28     Derived d;
    29     d.fun();
    30     return 0;
    31 }

      Output: Compiler Error.
      In the above program, fun() of base class is not accessible in the derived class. If a derived class creates a member method with name same as one of the methods in base class, then all the base class methods with this name become hidden in derived class.

     

    Question 2

     1 #include<iostream>
     2 using namespace std;
     3 class Base 
     4 {
     5 protected:
     6     int x;
     7 public:
     8     Base (int i)
     9     { 
    10         x = i;
    11     }
    12 };
    13 
    14 class Derived : public Base 
    15 {
    16 public:
    17     Derived (int i):x(i) 
    18     { 
    19     }
    20     void print() 
    21     { 
    22         cout << x ; 
    23     }
    24 };
    25 
    26 int main()
    27 {
    28     Derived d(10);
    29     d.print();
    30 }

      Output: Compiler Error
      In the above program, x is protected, so it is accessible in derived class. Derived class constructor tries to use initializer list to directly initialize x, which is not allowed even if x is accessible. The members of base class can only be initialized through a constructor call of base class.

      Following is the corrected program.

     1 #include<iostream>
     2 using namespace std;
     3 class Base 
     4 {
     5 protected:
     6     int x;
     7 public:
     8     Base (int i)
     9     { 
    10         x = i;
    11     }
    12 };
    13 
    14 class Derived : public Base 
    15 {
    16 public:
    17     Derived (int i):Base(i) 
    18     { 
    19     }
    20     void print() 
    21     { 
    22         cout << x; 
    23     }
    24 };
    25 
    26 int main()
    27 {
    28     Derived d(10);
    29     d.print();
    30 }

      Output:

       10

      Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


      转载请注明:http://www.cnblogs.com/iloveyouforever/

      2013-11-27  16:34:25

  • 相关阅读:
    爬虫|如何在Pycharm中调试JS代码
    nexus 6p 输入8.1和获取root权限
    年近30的我,离开了北京,回家做个老百姓,等待那一刻的发生!
    Azure认知服务的实际应用-资讯采集推送
    C#类库推荐 拼多多.Net SDK,开源免费!
    [翻译]EntityFramework Core 2.2 发布
    4-如何学习和解决问题
    3-WIN10系统及开发工具支持
    2-选择学习的目标和方向
    1-编程的基本条件和起步
  • 原文地址:https://www.cnblogs.com/iloveyouforever/p/3446063.html
Copyright © 2020-2023  润新知