C++ Programming - Objects and Classes - Discussion
Discussion Forum : Objects and Classes - Programs (Q.No. 16)
16.
What will be the output of the following program?
#include<iostream.h>
class A
{
public:
void BixFunction(void)
{
cout<< "Class A" << endl;
}
};
class B: public A
{
public:
void BixFunction(void)
{
cout<< "Class B" << endl;
}
};
class C : public B
{
public:
void BixFunction(void)
{
cout<< "Class C" << endl;
}
};
int main()
{
A *ptr;
B objB;
ptr = &objB;
ptr = new C();
ptr->BixFunction();
return 0;
}
Discussion:
6 comments Page 1 of 1.
Nirhya sathish said:
1 year ago
Output "Class A" is correct.
How it works?
In main fun object ptr is created with type A after in this ptr address of B object is stored then with same ptr is allocated new memory for class C.
Here fun overrides happened. Whatever type of ptr is used that fun only going to execute. This is an early binding method.
If we want to print output "Class C "means we have to use virtual keyword before that fun.
How it works?
In main fun object ptr is created with type A after in this ptr address of B object is stored then with same ptr is allocated new memory for class C.
Here fun overrides happened. Whatever type of ptr is used that fun only going to execute. This is an early binding method.
If we want to print output "Class C "means we have to use virtual keyword before that fun.
(1)
Abnaji said:
1 decade ago
void BixFunction(void)
{
cout<< "Class A" << endl;
}
This method belongs to class A and ptr = &objB means address is stored in ptr and ptr->BixFunction() means func. call to this method.Since BixFunction of superclass is not made virtual , compiler will look at the type of ptr and not the address.Since ptr is of type class A O/P will be "Class A" and not "Class B" or "Class c".
{
cout<< "Class A" << endl;
}
This method belongs to class A and ptr = &objB means address is stored in ptr and ptr->BixFunction() means func. call to this method.Since BixFunction of superclass is not made virtual , compiler will look at the type of ptr and not the address.Since ptr is of type class A O/P will be "Class A" and not "Class B" or "Class c".
SHUBHANGI said:
10 years ago
What is the use of new C() ?
Swapnil said:
9 years ago
Still I am blank. I have no idea for this answer.
Can anyone help me?
Can anyone help me?
Rohit said:
4 years ago
Here, BixFunction in base class in not virtual.
So at runtime ptr is still referring to base class function.
So at runtime ptr is still referring to base class function.
Vikas Kumar Singh said:
2 years ago
In the main function, a pointer ptr of type A is declared.
An object objB of class B is created, and the pointer ptr is assigned the address of objB. Then, a new object of class C is created using new, and the pointer ptr is assigned the address of this new object.
Finally, the BixFunction is called using the pointer ptr. Since ptr is pointing to an object of class C, the version of BixFunction in class C is called.
Therefore, the correct output is "Class C".
An object objB of class B is created, and the pointer ptr is assigned the address of objB. Then, a new object of class C is created using new, and the pointer ptr is assigned the address of this new object.
Finally, the BixFunction is called using the pointer ptr. Since ptr is pointing to an object of class C, the version of BixFunction in class C is called.
Therefore, the correct output is "Class C".
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers