Object Oriented Programming Using C++ (313304) Practical No.17: Write programs for Pointer to derive class in single inheritance Answer
The key feature of class inheritance is that a pointer to a derived class is type-compatible with a pointer to its base class.
Exercise:
1. State output of the following code:
#include<iostream.h>
class base {
public:
void show()
cout << "base\n";
} ;
class derived: public base
public:
void show() {
cout << "derived\n";
} ;
void main()
{
base bl;
bl.show();
derived dl;
dl.show();
base *pb = &bl;
pb->show();
pb = &dl;
pb->show();
}
Answer:
Base
derived
Base
base
2. A pointer to the base class can hold the address of
A) only base class object B) only derived class object
C) base class object as well as derived class object D) None of the above
Answer:
C) base class object as well as derived class object
3. A pointer can be initialized with
a) Null b) zero c)Address of an object of the same type d) All of them
Answer:
d) All of them
4. Which variable stores the memory address of another variable?
A) Reference B) Pointer C) Array D) None of the above
Answer:
B) Pointer
Practical Related Questions
1. Write a C++ program declare a class "polygon" having data members width and height. Derive classes "rectangle" and "triangle" from "polygon" having area() as a member function. Calculate area of triangle and rectangle using pointer to derived class object.
Answer:
2. Write a program that shows the use of Pointer to derive class in multilevel inheritance.
Answer:
3. Complete the following table:
Conclusion
We successfully completed Object Oriented Programming Using C++ (313304) Practical No.17 in which we Write programs for Pointer to derive classes in single inheritance.