Lab P14 β Implement inheritance.
Aim
Aim: Demonstrate single inheritance.
Theory
Demonstrate single inheritance.
C++ Program
#include
using namespace std;
class Animal {
public:
void eat(){ cout<<"Eating\n"; }
};
class Dog : public Animal {
public:
void bark(){ cout<<"Barking\n"; }
};
int main(){ Dog d; d.eat(); d.bark(); return 0; } Expected Output
Eating
BarkingExplanation
- Dog inherits eat() from Animal.
- Dog adds bark().
Viva / Important Questions
Short Questions
- Define inheritance.
- Types of inheritance?
Long Questions
- Explain the program in detail.
- Discuss key concepts demonstrated.
MCQ Practice
1. Inheritance represents:
- is-a relationship
- has-a
- uses-a
- none
Answer: is-a relationship.
2. Derived class inherits:
- Members of base
- Nothing
- Only private
- Only static
Answer: Members of base.
3. public inheritance gives:
- is-a
- has-a
- uses-a
- none
Answer: is-a.
4. Base class also called:
- Parent/super
- Child
- Derived
- None
Answer: Parent/super.
5. Derived class also called:
- Child/sub
- Parent
- Base
- None
Answer: Child/sub.
Quick Revision
- Inheritance = is-a.
- Code reuse.