Classes & Objects
- How many ways of reusing are there in the class hierarchy?
-
View Hint View Answer Discuss in Forum
NA
Correct Option: C
Class hierarchies promote reuse in two ways. They are code sharing and interface sharing.
- What is the output of this program?
#include <iostream>
using namespace std;
class InterfaceClass
{
public:
virtual void Display() = 0;
};
class ClassA : public InterfaceClass
{
public:
void Display()
{
int num = 10;
cout << num;
}
};
class ClassB : public InterfaceClass
{
public:
void Display()
{
cout <<" 14" << endl;
}
};
int main()
{
ClassA object1;
object1.Display();
ClassB object2;
object2.Display();
return 0;
}
-
View Hint View Answer Discuss in Forum
NA
Correct Option: D
In this program, We are displaying the data from the two classes
by using abstract class.
- What is the output of this program?
#include <iostream>
using namespace std;
class ClassA
{
public:
virtual void example() = 0;
};
class ClassB:public ClassA
{
public:
void example()
{
cout << "Interview Mania";
}
};
class ClassC:public ClassA
{
public:
void example()
{
cout << " is awesome.";
}
};
int main()
{
ClassA* array[2];
ClassB B;
ClassC C;
array[0]=&B;
array[1]=&C;
array[0]->example();
array[1]->example();
}
-
View Hint View Answer Discuss in Forum
NA
Correct Option: B
In this program, We are combining the two statements from two classes and printing it by using abstract class.
- What is the output of this program?
#include <iostream>
using namespace std;
class BaseClass
{
public:
virtual void print() const = 0;
};
class DerivedClassA : virtual public BaseClass
{
public:
void print() const
{
cout << " 5 ";
}
};
class DerivedClassB : virtual public BaseClass
{
public:
void print() const
{
cout << "3";
}
};
class Multiple : public DerivedClassA, DerivedClassB
{
public:
void print() const
{
DerivedClassB::print();
}
};
int main()
{
Multiple both;
DerivedClassA objectA;
DerivedClassB objectB;
BaseClass *array[ 3 ];
array[ 0 ] = &both;
array[ 1 ] = &objectA;
array[ 2 ] = &objectB;
for ( int K = 0; K < 3; K++ )
array[ K ] -> print();
return 0;
}
-
View Hint View Answer Discuss in Forum
NA
Correct Option: B
In this program, We are executing these based on the condition given in array. So it is printing as 5 3 5.
- What is meant by pure virtual function?
-
View Hint View Answer Discuss in Forum
NA
Correct Option: C
As the name itself implies, it have to depend on other class only.