Tag: Access specifiers in C++

  • Inheritance in C++

    Learn Inheritance in C++ with simple examples, syntax, types, and access specifiers. A complete beginner to advanced guide for interviews and exams.

    Inheritance in C++ is an Object-Oriented Programming (OOP) concept where one class (called the derived class) acquires the properties and behavior (variables and functions) of another class (called the base class).

    In simple terms:
    Inheritance allows you to reuse existing code and build new features on top of it.

    Basic Syntax

    
    class BaseClass {
    // data members and member functions
    };
    
    class DerivedClass : access_specifier BaseClass {
    // additional members of derived class
    };

    Example :

    class BaseClass {
    public:
        int a;
    
        void display() {
            cout << "This is base class" << endl;
        }
    };
    
    class DerivedClass : public BaseClass {
    public:
        void show() {
            cout << "This is derived class" << endl;
        }
    };
    

    Example Program

    #include <iostream>
    using namespace std;
    
    class Animal {
    public:
        void eat() {
            cout << "Animal eats" << endl;
        }
    };
    
    class Dog : public Animal {
    public:
        void bark() {
            cout << "Dog barks" << endl;
        }
    };
    
    int main() {
        Dog d;
        d.eat();   // inherited from Animal
        d.bark();  // own function
        return 0;
    }
    

    Output:

    Animal eats
    Dog barks
    

    How Inheritance Works in C++

    1.Base Class is Created

    The base class contains common properties and functions.

    class Animal {
    public:
        void eat() {
            cout << "Animal eats" << endl;
        }
    };
    

    2.Derived Class Inherits Base Class

    The derived class uses : to inherit from the base class.

    class Dog : public Animal {
    public:
        void bark() {
            cout << "Dog barks" << endl;
        }
    };
    

    3.Object of Derived Class is Created

    When you create an object of the derived class, it can access:

    • Its own methods
    • Inherited methods from base class
    int main() {
        Dog d;
        d.eat();   // inherited function
        d.bark();  // own function
    }
    

    Internal Working of Inheritance

    Memory Sharing

    • The derived class object contains base class data + its own data
    • No duplication of code

    Conceptually:

    Dog object = Animal properties + Dog properties
    

    Function Access

    • Derived class can directly use public and protected members
    • Cannot access private members of base class directly

    Constructor Execution Order

    When object is created:

    1. Base class constructor runs first
    2. Derived class constructor runs next
    class A {
    public:
        A() {
            cout << "Base Constructor" << endl;
        }
    };
    
    class B : public A {
    public:
        B() {
            cout << "Derived Constructor" << endl;
        }
    };
    

    Output:

    Base Constructor
    Derived Constructor
    

    Destructor Order (Reverse)

    When object is destroyed:

    1. Derived class destructor runs
    2. Base class destructor runs

    Function Overriding

    Derived class can override base class function

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

    Access Specifiers in C++ Inheritance

    In inheritance, access specifiers define how the base class members are accessible in the derived class.

    Types of Access Specifiers

    public

    • Accessible everywhere
    • Can be used inside and outside the class

    protected

    • Accessible within the class and derived class
    • Not accessible outside

    private

    • Accessible only within the same class
    • Not accessible in derived class directly

    Syntax with Access Specifier

    class DerivedClass : access_specifier BaseClass {
        // code
    };
    

    Example:

    class B : public A
    

    How Access Specifiers Work in Inheritance

    Inheritance TypeBase public →Base protected →Base private →
    publicpublicprotected❌ not accessible
    protectedprotectedprotected❌ not accessible
    privateprivateprivate❌ not accessible

    Example Code

    #include <iostream>
    using namespace std;
    
    class A {
    public:
        int x = 10;
    
    protected:
        int y = 20;
    
    private:
        int z = 30;
    };
    
    class B : public A {
    public:
        void show() {
            cout << x << endl; // ✅ public → accessible
            cout << y << endl; // ✅ protected → accessible
            // cout << z; ❌ private → not accessible
        }
    };
    
    int main() {
        B obj;
        obj.show();
        cout << obj.x; // ✅ public accessible outside
        // cout << obj.y; ❌ not accessible outside
    }
    

    Key Points

    public → accessible everywhere
    protected → accessible in child class only
    private → not accessible in child class

    Default inheritance:

    • class → private inheritance
    • struct → public inheritance

    Real-Life Working Example

    Think of:

    • Parent Class: Vehicle
    • Child Class: Car

    Car automatically gets:

    • speed
    • engine

    But also adds:

    • air conditioning
    • music system

    Why Inheritance is Used in C++

    Benefits

    • Code reusability
    • Reduces duplication
    • Easy maintenance
    • Supports polymorphism
    • Improves readability

    Important Points

    • Inheritance uses : operator
    • Base class constructor executes first
    • Private members are not directly accessible
    • Supports multiple types (single, multiple, multilevel, etc.)
    • Used for “is-a” relationship (Dog is an Animal)

    What is the use of : in C++ Inheritance?

    In C++, the colon : is used to specify inheritance.

    It tells the compiler that:

    “This class is inheriting from another class.”

    Basic Syntax

    class DerivedClass : public BaseClass {
    };
    

    Here:

    • : → means inherit from
    • public → access specifier
    • BaseClass → parent class

    How : Works

    When you write:

    class B : public A {
    };
    

    It means:

    • Class B gets properties and functions of class A
    • B becomes child (derived class)
    • A becomes parent (base class)

    Example

    #include <iostream>
    using namespace std;
    
    class A {
    public:
        void show() {
            cout << "Base class" << endl;
        }
    };
    
    class B : public A {
    };
    
    int main() {
        B obj;
        obj.show();  // inherited using :
    }
    

    Without : → inheritance will NOT happen

    Without : (No Inheritance)

    class B {
    };
    

    Now:

    • B has no relation with A
    • Cannot access A’s functions

    Real Meaning of :

    Think of it like:

    : = “is derived from”
    : = “inherits from”

    Important Uses of :

    1. To inherit a class

    class Child : public Parent

    2. Multiple inheritance

    class C : public A, public B

    3. Access control during inheritance

    class B : private A

    Types of Inheritance in C++

    1. Single Inheritance

    One base class → One derived class

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

    2. Multiple Inheritance

    One derived class inherits from multiple base classes

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

    3. Multilevel Inheritance

    Chain of inheritance

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

    4. Hierarchical Inheritance

    One base class → multiple derived classes

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

    5. Hybrid Inheritance

    Combination of multiple types (complex structure)

    Access Specifiers in Inheritance

    Inheritance TypePublic MembersProtected MembersPrivate Members
    publicpublicprotectednot accessible
    protectedprotectedprotectednot accessible
    privateprivateprivatenot accessible

    Example:

    class A {
    protected:
        int x;
    };
    
    class B : public A {
    public:
        void set() {
            x = 10; // accessible
        }
    };

    Key Concepts

    1. Code Reusability

    Reuse existing class code → reduces redundancy

    2. Method Overriding

    Derived class can redefine base class function

    class A {
    public:
        void show() {
            cout << "A";
        }
    };
    
    class B : public A {
    public:
        void show() {
            cout << "B";
        }
    };
    

    3. Constructor in Inheritance

    Base class constructor runs first

    class A {
    public:
        A() {
            cout << "Base constructor" << endl;
        }
    };
    
    class B : public A {
    public:
        B() {
            cout << "Derived constructor" << endl;
        }
    };
    

    4. Destructor Order

    Destructor runs in reverse order

    5. protected Keyword

    Allows access in derived classes but not outside

    6. super concept in C++

    C++ uses base class name instead of super

    class B : public A {
    public:
        void show() {
            A::show(); // calling base class function
        }
    };
    

    Advantages of Inheritance

    • ✔ Code reuse
    • ✔ Easy maintenance
    • ✔ Extensibility
    • ✔ Improves readability
    • ✔ Supports polymorphism

    Disadvantages

    • ❌ Tight coupling between classes
    • ❌ Can increase complexity
    • ❌ Improper use leads to errors

    Real-Life Example

    Think of:

    • Vehicle (Base Class)
    • Car, Bike (Derived Classes)

    All share common features like speed, engine, but also have unique features.

    Interview Questions

    1. What is inheritance in C++?
    2. Difference between inheritance and polymorphism?
    3. Types of inheritance?
    4. What is multiple inheritance?
    5. What is diamond problem?
    6. Difference between public, private, protected inheritance?
    7. Constructor order in inheritance?
    8. What is virtual inheritance?

    Summary

    Inheritance is a powerful feature in C++ that allows one class to inherit properties and behavior from another class, making code reusable, scalable, and easier to manage.

    Frequently Asked Questions (FAQs) on Inheritance in C++

    1️⃣ What is inheritance in C++?

    Inheritance is an OOP concept where a derived class acquires properties and functions of a base class.

    2️⃣ Why is inheritance used in C++?

    It is used for:

    • Code reusability
    • Reducing duplication
    • Easy maintenance
    • Extending existing functionality

    3️⃣ What is a base class and derived class?

    • Base class → Parent class
    • Derived class → Child class that inherits from base class

    4️⃣ What does : mean in inheritance?

    The : operator is used to define inheritance between classes.

    5️⃣ What are the types of inheritance in C++?

    • Single
    • Multiple
    • Multilevel
    • Hierarchical
    • Hybrid

    6️⃣ What is single inheritance?

    One derived class inherits from one base class.

    7️⃣ What is multiple inheritance?

    A derived class inherits from more than one base class.

    8️⃣ What is multilevel inheritance?

    A class is derived from another derived class (chain structure).

    9️⃣ What is hierarchical inheritance?

    Multiple classes inherit from the same base class.

    🔟 What is hybrid inheritance?

    Combination of multiple inheritance types.

    1️⃣1️⃣ What are access specifiers in inheritance?

    They control visibility of base class members:

    • public
    • protected
    • private

    1️⃣2️⃣ Can private members be inherited?

    Yes, but they are not directly accessible in derived class.

    1️⃣3️⃣ What is function overriding?

    When a derived class provides its own implementation of a base class function.

    1️⃣4️⃣ What is the order of constructor execution?

    • Base class constructor executes first
    • Then derived class constructor

    1️⃣5️⃣ What is the diamond problem?

    It occurs in multiple inheritance when a class inherits from two classes that have a common base class, causing ambiguity.

    Solution: Virtual inheritance

    For detailed understanding of Platform Devices and Drivers on Linux, refer to the Linux documentation on Platform Devices and Drivers .

  • Master Inheritance MCQs in C (2026)

    Practice Inheritance MCQs in C with beginner-friendly questions on abstract classes, polymorphism, and code reusability for students

    It was a rainy evening, and I was revising for my Programming in C . As I flipped through my notes, I realized that inheritance—a concept I had read about many times—felt abstract and hard to grasp. I asked myself, “How can I really understand it instead of just memorizing?”

    The answer came when I started practicing MCQs on inheritance. Each question acted like a mini-challenge, helping me connect theory with real coding scenarios. I discovered how base classes pass properties to derived classes, how abstract classes define interfaces, and how polymorphism makes programs flexible.

    Through this hands-on approach, I transformed a complex topic into something tangible. And now, I want to share this learning path with other students so you can master inheritance in C++ the way I did—step by step, MCQ by MCQ.

    Inheritance MCQs in C

    Introduction of Inheritance MCQs in C

    Inheritance is a fundamental concept in C++ that allows a class (derived class) to acquire properties and behaviors of another class (base class). Understanding inheritance is crucial for code reusability, polymorphism, and creating efficient object-oriented programs.

    If you are a student preparing for the Programming in C++ course, these MCQs on inheritance will help you revise key concepts effectively.

    Key Concepts of Inheritance in C

    Before diving into MCQs, let’s understand the essential topics:

    1. Base Class & Derived Class:
      The class whose properties are inherited is the base class, and the class that inherits is the derived class.
    2. Types of Inheritance:
      • Single Inheritance
      • Multiple Inheritance
      • Multilevel Inheritance
      • Hierarchical Inheritance
      • Hybrid Inheritance
    3. Access Specifiers in C++:
      • public inheritance
      • protected inheritance
      • private inheritance
    4. Abstract Classes & Pure Virtual Functions:
      Abstract classes cannot be instantiated and are used to define a common interface for derived classes.
    5. Polymorphism:
      Enables objects to behave differently based on the class type, often achieved using virtual functions.

    Beginner-Friendly MCQs on Inheritance

    1. What is inheritance in C?

    a) Reusing existing functions
    b) Reusing existing classes
    c) Creating a new object
    d) Overloading operators

    Answer: b) Reusing existing classes

    2. Which type of inheritance allows a derived class to inherit from multiple base classes?

    a) Single Inheritance
    b) Multiple Inheritance
    c) Multilevel Inheritance
    d) Hierarchical Inheritance

    Answer: b) Multiple Inheritance

    3. Which access specifier allows a derived class to inherit all public and protected members of the base class?

    a) Private
    b) Protected
    c) Public
    d) Friend

    Answer: c) Public

    4. Which of the following cannot be instantiated in C?

    a) Base Class
    b) Derived Class
    c) Abstract Class
    d) Friend Class

    Answer: c) Abstract Class

    5. What is polymorphism in C?

    a) Reusing code in multiple classes
    b) Ability of a function or object to take multiple forms
    c) Writing multiple functions with the same name
    d) Using multiple inheritance

    Answer: b) Ability of a function or object to take multiple forms

    6. What is the purpose of a pure virtual function?

    a) To overload a function
    b) To define a function in the base class that must be implemented in derived classes
    c) To hide a function
    d) To define a private function

    Answer: b) To define a function in the base class that must be implemented in derived classes

    7. Which inheritance type can cause the “diamond problem”?

    a) Single Inheritance
    b) Multiple Inheritance
    c) Multilevel Inheritance
    d) Hybrid Inheritance

    Answer: b) Multiple Inheritance

    8. What is code reusability in C?

    a) Using code without understanding
    b) Writing the same code repeatedly
    c) Using existing class properties in a new class
    d) Creating new functions for each task

    Answer: c) Using existing class properties in a new class

    9. Which keyword is used to declare a virtual function?

    a) virtual
    b) override
    c) abstract
    d) polymorphic

    Answer: a) virtual

    10. What is dynamic memory allocation in inheritance?

    a) Memory assigned at compile-time
    b) Memory assigned at runtime
    c) Memory shared by all objects
    d) Memory allocated only for base class

    Answer: b) Memory assigned at runtime

    11. Which of the following statements about base class constructors is correct?

    a) Base class constructors are not called when creating a derived class object
    b) Base class constructors are called automatically before derived class constructors
    c) Base class constructors can only be called manually
    d) Base class constructors are ignored in inheritance

    Answer: b) Base class constructors are called automatically before derived class constructors

    12. Which type of inheritance avoids duplication of base class members in multiple inheritance?

    a) Single Inheritance
    b) Virtual Inheritance
    c) Multilevel Inheritance
    d) Hierarchical Inheritance

    Answer: b) Virtual Inheritance

    13. Which of the following is true about protected members in a base class?

    a) They are not accessible in derived classes
    b) They are accessible only in the same class
    c) They are accessible in derived classes but not outside the class hierarchy
    d) They are always public

    Answer: c) They are accessible in derived classes but not outside the class hierarchy

    14. What is the output of the following code snippet if

    Base has a function display() and Derived overrides it, and you call

    Base* ptr = new Derived(); ptr->display();?

    a) Base class display function
    b) Derived class display function
    c) Compilation error
    d) Runtime error

    Answer: b) Derived class display function

    15. Which keyword is used to prevent a class from being inherited?

    a) virtual
    b) final
    c) sealed
    d) abstract

    Answer: b) final

    16. Which of the following is true about multiple inheritance in C?

    a) It is not supported
    b) Derived class inherits from only one base class
    c) A derived class can inherit features from more than one base class
    d) Multiple inheritance cannot use polymorphism

    Answer: c) A derived class can inherit features from more than one base class

    17. In multilevel inheritance, which class constructor is executed first?

    a) Most derived class constructor
    b) Base class constructor
    c) Middle class constructor
    d) Random constructor

    Answer: b) Base class constructor

    18. What is the main advantage of using inheritance?

    a) It makes code longer
    b) It promotes code reusability and modularity
    c) It increases compilation time
    d) It prevents polymorphism

    Answer: b) It promotes code reusability and modularity

    19. Which of the following statements is true about virtual functions?

    a) They are only used in base classes
    b) They allow derived class functions to override base class functions dynamically
    c) They cannot be overridden
    d) They are automatically inline

    Answer: b) They allow derived class functions to override base class functions dynamically

    20. Which of the following best describes hybrid inheritance ?

    a) Single inheritance only
    b) Combination of two or more types of inheritance
    c) Multilevel inheritance only
    d) Inheritance without base classes

    Answer: b) Combination of two or more types of inheritance

    Conclusion

    Mastering inheritance in C is essential for understanding object-oriented programming and building efficient applications. Practicing these MCQs will help GTU students strengthen concepts like abstract classes, code reusability, polymorphism, and multiple inheritance.

    To boost your preparation further:

    • Review base class and derived class concepts.
    • Practice creating abstract classes and virtual functions.
    • Explore different types of inheritance in small programs.

    Master Inheritance MCQs in C (2025) – Frequently Asked Questions

    What is inheritance in C?

    Inheritance is an object-oriented programming concept that allows a class to acquire properties and behaviors from another class. In C, we simulate inheritance using structures and function pointers.

    How is polymorphism related to inheritance in C?

    Polymorphism allows functions to behave differently based on the object type. In C, this can be achieved using function pointers and structures, often in combination with inheritance-like patterns.

    What are abstract classes in C?

    C doesn’t have built-in abstract classes like C++, but you can mimic them using structures with function pointers that act as “abstract methods” to enforce implementation in derived structures.

    How do MCQs help in learning inheritance in C?

    MCQs reinforce understanding of concepts like polymorphism, abstract classes, and code reusability by testing knowledge in a structured way, making learning more interactive and effective.

    Can we reuse code in C using inheritance?

    Yes, by designing structures and function pointers thoughtfully, you can reuse code across multiple modules, simulating inheritance patterns.

    Are inheritance MCQs only for advanced programmers?

    No. Beginner-friendly MCQs are designed to gradually introduce inheritance, making it easier for learners at all levels.

    What are some common mistakes while learning inheritance in C?

    Mistakes include confusing function pointers, improper structure usage, and misunderstanding the simulation of abstract classes or polymorphism.

    How do I practice inheritance MCQs effectively?

    Start with simple questions, implement concepts in code, review answers, and progressively move to advanced MCQs covering polymorphism and abstract structures.

    Are these MCQs updated for 2025 standards?

    Yes, the questions are aligned with current C programming practices and focus on beginner-friendly, practical understanding for 2025 learners.

    Where can I find free MCQs for practicing inheritance in C?

    You can find curated, beginner-friendly MCQs on educational platforms, coding blogs, and sites like Embedded Prep Master that focus on practical learning.

    Inheritance MCQs in C
    Master Inheritance MCQs in C (2025)