C++ Programming - Objects and Classes - Discussion

Discussion :: Objects and Classes - General Questions (Q.No.1)

1. 

What happens when we try to compile the class definition in following code snippet?

class Birds {};
class Peacock : protected Birds {};

[A]. It will not compile because class body of Birds is not defined.
[B]. It will not compile because class body of Peacock is not defined.
[C]. It will not compile because a class cannot be protectedly inherited from other class.
[D]. It will compile succesfully.

Answer: Option D

Explanation:

No answer description available for this question.

Sree said: (Sep 8, 2012)  
Program compile successfully but no o/p will be providing!

Shashi said: (Mar 31, 2013)  
There is no error in this program. It does not matter whether the body is defined or not. It will compile successfully.

Anirban Sanyal said: (May 3, 2013)  
Ya it will compile because class can be protected inherited from other class.

Arjun said: (Jul 29, 2013)  
It is under the other defined class.

Nara said: (Aug 29, 2013)  
It will compile successfully. A class can have no members.

Irshad said: (Sep 16, 2013)  
Will it run successfully without declared main method ?

Pankaj Sharma said: (Oct 3, 2013)  
It will compile successfully but will not execute as main is the very first function to be called and it is called every time by say a master program running somewhere behind and from there the further references are given by us to do whatever required acc to need.

So it will only compile not execute.

Suman said: (Dec 27, 2013)  
The code will compile properly. Since the existence of class is only by object of that particular class and there are know syntax error in the code.

Prince Jha said: (Mar 2, 2014)  
But scope resolution operator is not used properly. Can any body tell elaborately?

Love Juneja said: (Mar 27, 2014)  
Scope resolution operator is not used properly.

Sumasree said: (May 4, 2014)  
Without main method it's shows like this message -undefined reference to 'main'.

C++ said: (Aug 1, 2014)  
This particular code "snippet" does not have any errors. THUS option D is correct. Of Course to run you will need a main function.

The : is not scope resolution operators. That is how classes are inherited in C++.

Srijeth said: (Sep 24, 2014)  
What does class peacock:protected birds{}; mean?

Ashwinrpai said: (Oct 12, 2014)  
We can read the above statement as "peacock is a a bird". Class peacock is a class which inherits from class bird, in a protected manner.

Bhuvnesh Kumar said: (Oct 17, 2014)  
It is will compile properly because peacock is class which protected inherits the class bird.

Amit said: (Oct 25, 2014)  
The above program compile successfully. Because in this program a class is defined in another class with protected specifier.

Kirti said: (Jan 5, 2015)  
In the above program peacock is a class which protected inherits the class bird.

Debasmita Manna said: (Jan 14, 2015)  
I do think that option d is correct because compiler is already mention bird as a class.

Eknath said: (Feb 5, 2015)  
It will compile successfully. We can have a empty class and can inherit protected from any class. Nothing will be displayed on console as we are not writing anything to console.

Nirbhay Singh said: (Nov 17, 2015)  
This program is correct but no output. Because no constructor and destructor are use.

Sann said: (Jan 11, 2016)  
What situation we should inherit protected or private if class is public?

Prabavathy said: (Jan 20, 2016)  
Ya it will inherited when we inherit using public and protected.

Akshay said: (Mar 9, 2016)  
Their is no output because their is no use of constructor and destructor but program compile successfully.

Xxx said: (Jul 26, 2016)  
Agree with you @Akshay. So confused to choose the answer.

Naveen said: (Sep 29, 2016)  
There is no output but it compiled sucessfully.

Utkarsh Rai said: (Jun 18, 2017)  
It Can be compiled successfully because as the protected (access specifier) helps peacock class only (child class) to get accessthrough the methods and variables of birds class (parent class).

But here there will be no output because there are no methods or variables in both the class.

Ramkishore Gautam said: (Jul 1, 2017)  
It will not compile successfully without main ().

Bhn said: (Jul 13, 2017)  
Class can be protected & inherited from other class.

Raees said: (Aug 4, 2017)  
Yeah, it will compile.

Anuja said: (Dec 15, 2019)  
It'll compile as protected access specified provides inheritance or data member functions accessibility only to the next immediate derived class.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.