WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

C++ · Basic · question 8 of 100

What are the different types of inheritance in C++?

📕 Buy this interview preparation book: 100 C++ questions & answers — PDF + EPUB for $5

Inheritance is a key feature of object-oriented programming that allows one class to derive properties and behavior from another class. In C++, there are four different types of inheritance:

Public Inheritance: In public inheritance, the public members of the base class become public members of the derived class, and the protected members of the base class become protected members of the derived class. Private members of the base class are not accessible from the derived class. Public inheritance is the most common type of inheritance and is used to model an "is-a" relationship between two classes. For example:

    class Animal {
        public:
        void eat() {
            std::cout << "Animal is eating" << std::endl;
        }
    };
    
    class Dog : public Animal {
        public:
        void bark() {
            std::cout << "Dog is barking" << std::endl;
        }
    };
    
    int main() {
        Dog myDog;
        myDog.eat(); // calls Animal::eat()
        myDog.bark(); // calls Dog::bark()
        return 0;
    }

In this example, the Dog class inherits publicly from the Animal class. This means that the Dog class has access to the public member function eat() of the Animal class, and it can also define its own member function bark().

Protected Inheritance: In protected inheritance, the public and protected members of the base class become protected members of the derived class. Private members of the base class are not accessible from the derived class. Protected inheritance is used to model a "is-implemented-in-terms-of" relationship between two classes. For example:

    class Base {
        protected:
        int myValue;
    };
    
    class Derived : protected Base {
        public:
        void setValue(int value) {
            myValue = value; // protected member of base class is accessible in derived class
        }
    };
    
    int main() {
        Derived myDerived;
        myDerived.setValue(42); // sets myValue to 42
        return 0;
    }

In this example, the Derived class inherits protectedly from the Base class. This means that the myValue member variable of the Base class is protected in the Derived class, and can be accessed and modified only by member functions of the Derived class.

Private Inheritance: In private inheritance, the public and protected members of the base class become private members of the derived class. Private members of the base class are not accessible from the derived class. Private inheritance is used to model a "is-implemented-in-terms-of" relationship between two classes, but with stronger encapsulation. For example:

    class Base {
        private:
        int myValue;
    };
    
    class Derived : private Base {
        public:
        void setValue(int value) {
            myValue = value; // private member of base class is accessible in derived class
        }
    };
    
    int main() {
        Derived myDerived;
        myDerived.setValue(42); // sets myValue to 42
        return 0;
    }

In this example, the Derived class inherits privately from the Base class. This means that the myValue member variable of the Base class is private in the Derived class, and can be accessed and modified only by member functions of the Derived class.

Multiple Inheritance: Multiple inheritance allows a derived class to inherit from multiple base classes. In this case, the members of each base class are combined into the derived class. Multiple inheritance is used to model complex relationships between classes, but it can also lead to ambiguity and complexity.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic C++ interview — then scores it.
📞 Practice C++ — free 15 min
📕 Buy this interview preparation book: 100 C++ questions & answers — PDF + EPUB for $5

All 100 C++ questions · All topics