KwickAcademy Cyber Safety and Ethics · 8 min · free
Inheritance: Extending Classes
Inheritance creates a derived class from a base class, so the derived class gets the base members and can add its own.
Follows the syllabus of: NIOS Senior Secondary Computer Science (330)
On screen in this lesson
Base and derived classes
| Inheritance: a new class gets members of an old one |
| Base class: the existing class (parent) |
| Derived class: the new class (child) |
| Derived class can add its own members |
The protected specifier
| private: only the same class |
| protected: the same class and derived classes |
| public: anyone |
| Base private members: not usable in derived class |
Visibility modes
| Base member | public mode | private mode |
|---|---|---|
| public | stays public | becomes private |
| protected | stays protected | becomes private |
| private | not accessible | not accessible |
Protected mode
| public members become protected |
| protected members stay protected |
| private members stay not accessible |
| No mode written in a class: private mode |
Types of inheritance
| Type | Shape | Example |
|---|---|---|
| Single | A to B | Person to Student |
| Multiple | A and B to C | Father, Mother |
| Multilevel | A to B to C | Grandpa to Dad |
| Hierarchical | A to B and C | Vehicle to Car, Bus |
| Hybrid | a mix of types | several levels |
Constructors in inheritance
| Base constructor runs first |
| Then the derived constructor runs |
| Destructors run in reverse order |
| Multiple bases: in the order listed |
Quick answers
In class B : A { } with no mode, can main use b.x?
No. A class uses private mode by default, so x becomes private.
Which constructor runs first?
The base class constructor.
KwickClips from this lesson
Short clips, one idea each. Good for revision the night before.
Which is the parent?42 sec
What mode does a class use if none is written?41 sec
What does multiple mean?40 sec
Which constructor runs first?43 secThe full lesson, in text
Hello students, welcome to Kwickprep. A school has students and teachers, and both have a name and an age. Must we write that code twice? No, inheritance lets us write it once. Today we learn base and derived classes, the three visibility modes, the types of inheritance, and the order constructors run.
Inheritance means creating a new class from an existing class. The new class gets the members of the old class. The existing class is called the base class, or parent class. The new class is called the derived class, or child class. The derived class can also add new members of its own. This relation is called an is a relation, because a student is a person.
Here is the syntax. First we write the base class, Person, as usual. Then we write class Student, a colon, a visibility mode, and the base class name. The colon means derived from. The word public here is the visibility mode, and we will see what it means shortly.
Now a working program. Person has a public data member, age, set to sixteen. Student is derived from Person, and adds roll, set to twelve. In main, s is a Student object. S dot roll is its own member. S dot age also works, even though age was written only in Person. So it prints sixteen and twelve.
Before visibility modes, we need a third access specifier, protected. A private member can be used only inside its own class. A protected member can be used inside its own class and inside its derived classes, but not in main. A public member can be used by anyone. A derived class can never directly use the private members of its base class, although they still exist inside the object.
The visibility mode decides what inherited members become inside the derived class. In public mode, public members stay public in the derived class. Protected members also stay protected. In private mode, both public and protected members become private in the derived class. And private members of the base are not accessible in any mode.
The third mode is protected mode. Here, public members of the base become protected in the derived class. Protected members stay protected. Private members are still not accessible. If you write no mode at all, a class uses private mode by default, which is a common exam trap.
Here Acc is a bank account with a protected balance of nine hundred. Sav, a savings account, is derived from it. Inside Sav, the function get can use bal, because protected members reach derived classes. It adds one hundred rupees of interest. In main, s dot bal would be an error, but s dot get works, and prints one thousand.
There are five types of inheritance. In single inheritance, one base class gives one derived class. In multiple inheritance, one derived class has two or more base classes, like a child with a father and a mother. In multilevel inheritance, a derived class becomes the base for another class, like grandfather, father and son. In hierarchical inheritance, one base class gives many derived classes. Hybrid inheritance mixes two or more of these types.
In multiple inheritance, we list the base classes after the colon, separated by a comma. Here Th holds theory marks, sixty, and Pr holds practical marks, thirty. Res, the result class, is derived from both. The object r gets t from Th and p from Pr. So r dot t plus r dot p prints ninety.
In multilevel inheritance, the classes form a chain. Here we use structs to keep it short, and their members are public. B is derived from A, and C is derived from B. So C gets y from B, and also x from A through B. The object c has x, y and z. One plus two plus three prints six.
When we create a derived object, whose constructor runs first? The base class constructor runs first, because the base part must be ready before the derived part is built. Then the derived class constructor runs. Destructors run in the reverse order, derived first and base last. In multiple inheritance, base constructors run in the order the bases are listed after the colon.
Let us prove it. Here A and B are structs, which are classes with public members, and B is derived from A. Creating b prints A first, then B. At the end of main, the destructor of B runs first, then the destructor of A. So the output is A, B, then tilde B, tilde A.
What if the base constructor needs a value? P, the person, needs an age. The derived S, the student, receives both age and roll. After its brackets, we write a colon and P of a. This passes the age up to the base constructor, which runs first. Then roll is set. So it prints sixteen and twelve.
Pause and predict. Class B is derived from A, but no visibility mode is written. A has a public member called x. Can main use b dot x? No, it cannot. Because B is a class, the default mode is private, so x becomes private inside B.
Let us revise what we learned today. A derived class gets the members of its base class and can add its own. Protected members can be used by the class and its derived classes. The visibility mode can be public, private or protected, and private is the default for a class. The five types are single, multiple, multilevel, hierarchical and hybrid. Base constructors run first, and destructors run in the reverse order.
Courses that teach this
| Course | Unit |
|---|---|
| NIOS Senior Secondary Computer Science (330) | Module 3: Programming in C++ |
| Programming All levels C++ | Object-Oriented Programming |
Voice-over in this lesson is AI-generated. The script is written and checked by Kajal Ma'am. Boards can revise a syllabus mid-year, so confirm anything you plan around against the official board circular. Keep your passwords, OTPs and ID numbers to yourself — we never ask for them. To reach Kajal Ma'am, use the WhatsApp button; sharing your number there is how we call you back.
Free to watch, no sign-up. Live classes with Kajal Ma'am are the paid course; these lessons stay free either way.

