3i Infotech Placement: Sample Questions 152 - 152 of 1245
Glide to success with Doorsteptutor material for competitive exams : get questions, notes, tests, video lectures and more- for all subjects of your exam.
Question 152
Describe in Detail Essay▾
Find the output of the following program
class base
{
public:
Void baseFun ()
{
cout≪ “from base”≪endl;
}
};
class deri:Public base
{
public:
Void baseFun ()
{
cout≪ “from derived” ≪endl;
}
};
void SomeFunc (base ⚹baseObj)
{
baseObj ⇾ baseFun ();
}
int main ()
{
base baseObject;
SomeFunc (&baseObject);
deri deriObject;
SomeFunc (&deriObject);
}
EditExplanation
Table Showing the Programclass base { public: Void baseFun () { cout << “from base” << endl; } };
| - In base class define the void baseFun () function
- In a baseFun () cout prints “from base”
|
class deri:Public base { public: Void baseFun () { cout << “from derived” << endl; } };
| - In deri class define the void baseFun () function
- In a baseFun () cout prints “from derived”
|
void SomeFunc (base ⚹baseObj) { baseObj ⇾ baseFun (); }
| SomeFunc expects a pointer to a base class. |
int main () { base baseObject; SomeFunc (&baseObject); deri deriObject; SomeFunc (&deriObject); }
| - Define the base () function object baseObject
- SomeFunc () function pass the baseobject address
- Define the deri () function object deriObject
- SomeFunc () function pass the deriobject address
- A pointer to a derived class object is passed but SomeFunc treats it as a base class pointer and the corresponding base function is called.
- Remember that baseFunc is not a virtual function and does not support polymorphism.
|
PreviousNext