Objects and Classes

Q1:
What will be the output of the following program?
#include<iostream.h> 
class A
{
    public:
    void AptitudeFunction(void)
    {
        cout<< 'Class A' << endl;
    }
};
class B: public A
{
    public:
    void AptitudeFunction(void)
    {
        cout<< 'Class B' << endl;
    } 
};
class C : public B
{
    public:
    void AptitudeFunction(void)
    {
        cout<< 'Class C' << endl;
    } 
};
int main()
{
    A *ptr;
    B objB;
    ptr = &objB;
    ptr = new C();
    ptr->AptitudeFunction();
    return 0; 
}

A The program will print the output 100.

B The program will print the output 200.

C The program will print the output 400.

D The program will print the output Garbage-value.

E The program will report compile time error.

ANS:C - The program will print the output 400.

void AptitudeFunction(void)
{
cout<< "Class A" << endl;
}

This method belongs to class A and ptr = &objB means address is stored in ptr and ptr->AptitudeFunction() means func. call to this method.Since AptitudeFunction 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".



img not found
img

For help Students Orientation
Mcqs Questions

One stop destination for examination, preparation, recruitment, and more. Specially designed online test to solve all your preparation worries. Go wherever you want to and practice whenever you want, using the online test platform.