0% found this document useful (0 votes)
13 views6 pages

14-Mark Master Blueprint: OOP Concepts in C++: 1. What Is Inheritance? Explain Its Advantages With Example

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views6 pages

14-Mark Master Blueprint: OOP Concepts in C++: 1. What Is Inheritance? Explain Its Advantages With Example

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

C++ Concepts Gurnoor Singh

14-Mark Master Blueprint: OOP Concepts


in C++
Inheritance, Polymorphism, Virtual Functions, Operator Overloading

1. What is Inheritance? Explain its advantages with


example.
Definition
Inheritance is the mechanism by which a class (derived) acquires properties of another
class (base). It supports the concept of hierarchical classification and promotes reusability.

Advantages
• Code Reusability – Eliminates redundancy by reusing existing code.
• Extensibility – Allows extending the functionality of existing code.
• Maintainability – Central changes reflect across all derived classes.
• Logical Hierarchy – Represents real-world relationships like IS-A.
• Modularity – Simplifies code management by separating concerns.
• Scalability – New features can be added easily.
• Improved Productivity – Developers spend less time rewriting logic.

Syntax
class Base {
// members
};
class Derived : public Base {
// additional members
};

Example
class Animal {
public :
void eat () { cout << " Eating \ n " ; }
};

class Dog : public Animal {


public :
void bark () { cout << " Barking \ n " ; }
};

1
C++ Concepts Gurnoor Singh

2. Explain types of Inheritance with examples and


diagrams.
Types
1. Single Inheritance
• One class derives from one base class.
• Easy to manage and understand.

class A {};
class B : public A {};

2. Multilevel Inheritance
• One derived class acts as base for another.
• Shows transitive nature of inheritance.

class A {};
class B : public A {};
class C : public B {};

3. Multiple Inheritance
• One class inherits from multiple base classes.
• Can lead to ambiguity if not handled carefully.

class A {}; class B {};


class C : public A , public B {};

4. Hierarchical Inheritance
• Multiple classes derive from the same base.
• Common features remain in base class.

class A {};
class B : public A {}; class C : public A {};

5. Hybrid Inheritance
• Combines more than one type of inheritance.
• May cause diamond problem.

3. What is Ambiguity in Inheritance? Explain with


example and solutions.
Definition
Ambiguity in inheritance arises when multiple paths exist to reach a base class, leading
to confusion in compiler regarding which function or property to use.

2
C++ Concepts Gurnoor Singh

Example: Diamond Problem


class A { public : void display () { cout << " A " ; } };
class B : public A {};
class C : public A {};
class D : public B , public C {};

Solutions
• Scope Resolution: Specify the class explicitly:
D obj ;
obj . B :: display () ;

• Virtual Base Class: Eliminate ambiguity using virtual inheritance:


class B : virtual public A {};
class C : virtual public A {};
class D : public B , public C {};

4. What is Polymorphism? Explain compile-time


and run-time polymorphism.
Definition
Polymorphism allows the same interface to represent different underlying data types or
function implementations.

Types of Polymorphism
1. Compile-time Polymorphism (Static)

• Achieved via function and operator overloading.

• Resolved during compilation.

void print ( int x ) {}


void print ( double y ) {}

2. Run-time Polymorphism (Dynamic)

• Achieved using virtual functions.

• Function call resolved during runtime.

class Base {
public :
virtual void show () { cout << " Base " ; }
};
class Derived : public Base {

3
C++ Concepts Gurnoor Singh

public :
void show () { cout << " Derived " ; }
};

5. What are Virtual Functions? How do they sup-


port run-time polymorphism?
Definition
A virtual function is a member function in a base class that you expect to be overridden
in derived classes.

Key Points
• Declared using the virtual keyword.

• Enables dynamic dispatch.

• Supports polymorphic behavior using base class pointers.

• Uses vtable mechanism internally.

Example
class Base {
public :
virtual void display () { cout << " Base ␣ class " ; }
};

class Derived : public Base {


public :
void display () override { cout << " Derived ␣ class " ; }
};

Base * ptr = new Derived () ;


ptr - > display () ; // Output : Derived class

6. What is a Pure Virtual Function? Explain with


an example.
Definition
A pure virtual function is a virtual function with no definition in the base class. Used to
create abstract classes.

4
C++ Concepts Gurnoor Singh

Usage
• Declared using = 0.

• Forces derived classes to implement the function.

• Abstract class cannot be instantiated.

Example
class Shape {
public :
virtual void draw () = 0; // Pure virtual
};

class Circle : public Shape {


public :
void draw () { cout << " Drawing ␣ Circle " ; }
};

7. What is Operator Overloading? Explain with a


’+’ operator example.
Definition
Operator overloading allows giving special meaning to operators when used with user-
defined types.

Types
• Unary Operators (++, –)

• Binary Operators (+, -, *, /)

Syntax
return_type operator op ( parameters ) ;

Example: ’+’ Operator


class Complex {
int real , imag ;
public :
Complex ( int r = 0 , int i = 0) : real ( r ) , imag ( i ) {}

Complex operator +( Complex c ) {


return Complex ( real + c . real , imag + c . imag ) ;

5
C++ Concepts Gurnoor Singh

void display () {
cout << real << " ␣ + ␣ " << imag << " i " ;
}
};

Exam Tips
• Always begin with definitions.

• Use neat indentation for code.

• Underline or highlight key terms.

• Add labeled diagrams for inheritance and ambiguity.

• Write examples and outputs clearly.

• Use analogies where applicable.

You might also like