zoukankan      html  css  js  c++  java
  • Output of C++ Program | Set 15

      Predict the output of following C++ programs.

    Question 1

     1 #include <iostream>
     2 using namespace std;
     3 
     4 class A
     5 {
     6 public:
     7     void print() 
     8     { 
     9         cout << "A::print()"; 
    10     }
    11 };
    12 
    13 class B : private A
    14 {
    15 public:
    16     void print() 
    17     { 
    18         cout << "B::print()"; 
    19     }
    20 };
    21 
    22 class C : public B
    23 {
    24 public:
    25     void print() 
    26     { 
    27         A::print(); 
    28     }
    29 };
    30 
    31 int main()
    32 {
    33     C b;
    34     b.print();
    35 }

      Output: Compiler Error: ‘A’ is not an accessible base of ‘C’
      There is multilevel inheritance in the above code. Note the access specifier in “class B : private A”. Since private access specifier is used, all members of ‘A’ become private in ‘B’. Class ‘C’ is a inherited class of ‘B’. An inherited class can not access private data members of the parent class, but print() of ‘C’ tries to access private member, that is why we get the error.

     

    Question 2

     1 #include<iostream>
     2 using namespace std;
     3 
     4 class base
     5 {
     6 public:
     7     virtual void show()  
     8     { 
     9         cout<<" In Base 
    "; 
    10     }
    11 };
    12 
    13 class derived: public base
    14 {
    15     int x;
    16 public:
    17     void show() 
    18     { 
    19         cout<<"In derived 
    "; 
    20     }
    21     derived()   
    22     { 
    23         x = 10; 
    24     }
    25     int getX() const 
    26     { 
    27         return x;
    28     }
    29 };
    30 
    31 int main()
    32 {
    33     derived d;
    34     base *bp = &d;
    35     bp->show();
    36     cout << bp->getX();
    37     return 0;
    38 }

      Output: Compiler Error: ‘class base’ has no member named ‘getX’
      In the above program, there is pointer ‘bp’ of type ‘base’ which points to an object of type derived. The call of show() through ‘bp’ is fine because ‘show()’ is present in base class. In fact, it calls the derived class ‘show()’ because ‘show()’ is virtual in base class. But the call to ‘getX()’ is invalid, because getX() is not present in base class. When a base class pointer points to a derived class object, it can access only those methods of derived class which are present in base class and are virtual.

     

    Question 3

     1 #include<iostream>
     2 using namespace std;
     3 
     4 class Test
     5 {
     6     int value;
     7 public:
     8     Test(int v = 0) 
     9     { 
    10         value = v; 
    11     }
    12     int getValue()  
    13     { 
    14         return value; 
    15     }
    16 };
    17 
    18 int main()
    19 {
    20     const Test t;
    21     cout << t.getValue();
    22     return 0;
    23 }

      Output: Compiler Error
      In the above program, object ‘t’ is declared as a const object. A const object can only call const functions. To fix the error, we must make getValue() a const function.

      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:28:45

  • 相关阅读:
    mac搭建mamp环境
    iOS开发UI篇—UITabBarController生命周期(使用storyoard搭建)
    iOS开发UI篇—UITabBarController简单介绍
    OS开发UI篇—ios应用数据存储方式(归档)
    iOS开发UI篇—ios应用数据存储方式(偏好设置)
    IOS开发UI篇—ios应用数据存储方式(XML属性列表-plist)
    iOS开发UI篇—控制器的View的创建
    iOS开发UI篇—UIWindow简单介绍
    iOS开发UI篇—使用storyboard创建导航控制器以及控制器的生命周期
    IOS开发UI篇—导航控制器属性和基本使用
  • 原文地址:https://www.cnblogs.com/iloveyouforever/p/3446041.html
Copyright © 2011-2022 走看看