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 \n"; 
10     }
11 };
12 
13 class derived: public base
14 {
15     int x;
16 public:
17     void show() 
18     { 
19         cout<<"In derived \n"; 
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

转载于:https://www.cnblogs.com/iloveyouforever/p/3446041.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值