Answer Posted / jyoti bajaj
virtual keyword can be used with base classes as well as
with functions.Here we are talking about virtual base
classes.
class A
{
public:
A(){cout<<"it is class A"<<endl;}
};
class B:public A
{
public:
B(){cout<<"it is class B"<<endl;}
};
class C:public A
{
public:
C(){cout<<"it is class C"<<endl;}
};
class D:public B,public C
{
public:
D(){}
};
void main()
{
D obj;
}
output:
it is class A
it is class B
it is class A
it is class C
since class A is constructed twice.but if we want that only
one copy of class A should be shared by both classes A &
B.so we inherit the base class by using virtual keyword.
class A
{
public:
A(){cout<<"it is class A"<<endl;}
};
class B:virtual public A
{
public:
B(){cout<<"it is class B"<<endl;}
};
class C:virtual public A
{
public:
C(){cout<<"it is class C"<<endl;}
};
class D:public B,public C
{
public:
D(){}
};
void main()
{
D obj;
}
output:
it is class A
it is class B
it is class C
| Is This Answer Correct ? | 5 Yes | 0 No |
Post New Answer View All Answers
• What are the desirable attributes for memory managment?
What is the difference between procedural programming and oops?
How to call a non virtual function in the derived class by using base class pointer
Why is object oriented programming so hard?
What is multilevel inheritance in oop?
What does and I oop mean?
Can private class be inherited?
This program numbers the lines found in a text file. Write a program that reads text from a file and outputs each line preceded by a line number. Print the line number right-adjusted in a field of 3 spaces. Follow the line number with a colon, then one space, then the text of the line. You should get a character at a time and write code to ignore leading blanks on each line. You may assume that the lines are short enough to fit within a line on the screen. Otherwise, allow default printer or screen output behavior if the line is too long (i.e., wrap or truncate). A somewhat harder version determines the number of spaces needed in the field for the line numbers by counting lines before processing the lines of the file. This version of the program should insert a new line after the last complete word that will fit within a 72-character line.
What is difference between abstraction and encapsulation?
What does oop mean in snapchat?
Write a java applet that computes and displays the squares of values between 25 and 1 inclusive and displays them in a TextArea box
I have One image (means a group photo ) how to split the faces only from the image?............ please send the answer nagadurgaraju@gmail.com thanks in advace...
What are two types of polymorphism?
What is the full form of oops?
Can a varargs method be overloaded?