Lab P14 β€” Implement inheritance.

NBC351 Β· Program 14⏱ 8 min read❓ 5 MCQs

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
Barking

Explanation

  • Dog inherits eat() from Animal.
  • Dog adds bark().

Viva / Important Questions

Short Questions

  1. Define inheritance.
  2. Types of inheritance?

Long Questions

  1. Explain the program in detail.
  2. 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.