Get the most accurate TN Board Solutions for Class 11 Computer Science Chapter 16 Inheritance here. Updated for the 2026-27 academic session, these solutions are based on the latest TN Board textbooks for Class 11 Computer Science. Our expert-created answers for Class 11 Computer Science are available for free download in PDF format.
Detailed Chapter 16 Inheritance TN Board Solutions for Class 11 Computer Science
For Class 11 students, solving TN Board textbook questions is the most effective way to build a strong conceptual foundation. Our Class 11 Computer Science solutions follow a detailed, step-by-step approach to ensure you understand the logic behind every answer. Practicing these Chapter 16 Inheritance solutions will improve your exam performance.
Class 11 Computer Science Chapter 16 Inheritance TN Board Solutions PDF
Part I
Choose The Correct Answer:
Question 1. Which of the following is the process of creating new classes from an existing class?
(a) Polymorphism
(b) Inheritance
(c) Encapsulation
(d) superclass
Answer: (b) Inheritance
In simple words: Inheritance is the method where a new class is made from an already existing class. This helps in reusing code and building more complex software easily.
๐ฏ Exam Tip: Remember that inheritance allows new classes (derived classes) to reuse and extend functionalities of existing classes (base classes), which saves development time.
Question 2. Which of the following derives a class student from the base class school?
(a) school: student
(b) class student: public school
(c) student: public school
(d) class school: public student
Answer: (b) class student: public school
In simple words: To make a 'student' class inherit from a 'school' class, you write 'class student: public school'. This makes 'student' a child of 'school'.
๐ฏ Exam Tip: Pay attention to the syntax for class derivation: `class DerivedClass : visibility_mode BaseClass`. The visibility mode (`public`, `private`, `protected`) determines how base class members are accessed in the derived class.
Question 3. The type of inheritance that reflects the transitive nature is
(a) Single Inheritance
(b) Multiple Inheritance
(c) Multilevel Inheritance
(d) Hybrid Inheritance
Answer: (c) Multilevel Inheritance
In simple words: Multilevel inheritance is like a family tree where a child inherits from a parent, and that parent also inherited from their own parent. It's a chain of inheritance.
๐ฏ Exam Tip: Visualize multilevel inheritance as a chain: Class C inherits from B, and B inherits from A. C gets properties from both B and A.
Question 4. Which visibility mode should be used when you want the features of the base class to be available to the derived class but not to the classes that are derived from the derived class?
(a) Private
(b) Public
(c) Protected
(d) All of the options
Answer: (a) Private
In simple words: Use 'private' visibility mode. This lets the first child class use the parent's features, but stops any grandchildren classes from using them.
๐ฏ Exam Tip: Understand the impact of each visibility mode: `public` maintains original access, `protected` makes public members protected, and `private` makes all inherited members private in the derived class.
Question 5. Inheritance is the process of creating new class from
(a) Base class
(b) abstract
(c) derived class
(d) Function
Answer: (a) Base class
In simple words: Inheritance is when you make a new class using an existing 'base class' as a starting point.
๐ฏ Exam Tip: Remember that a base class is the original class, and a derived class is the new class that gets features from the base class.
Question 6. A class is derived from a class which is a derived class itself, then this is referred to as
(a) multiple inheritances
(b) multilevel inheritance
(c) single inheritance
(d) double inheritance
Answer: (b) multilevel inheritance
In simple words: If Class C comes from Class B, and Class B comes from Class A, that's called multilevel inheritance.
๐ฏ Exam Tip: Multilevel inheritance creates a 'grandparent-parent-child' relationship among classes, allowing features to be passed down through generations.
Question 7. Which amongst the following is executed in the order of inheritance?
(a) Destructor
(b) Member function
(c) Constructor
(d) Object
Answer: (b) Member function
In simple words: Member functions run when you call them. In inherited classes, they follow how you set up your code.
๐ฏ Exam Tip: While constructors execute from base to derived, and destructors from derived to base, member functions are called based on the specific object and context, not a fixed inheritance order.
Question 8. Which of the following is true with respect to inheritance?
(a) Private members of base class are inherited to the derived class with private private accessibility
(b) Private members of base class are not inherited to the derived class with private accessibility
(c) Public members of base class are inherited but not visible to the derived class
(d) Protected are inherited but not visible to the outside class
Answer: (b) Private members of base class are not inherited to the derived class with private accessibility
In simple words: Private things in a parent class do not get passed on to the child class. Only the parent class can use them.
๐ฏ Exam Tip: Private members of a base class are never inherited; they are completely hidden from derived classes, promoting strong encapsulation.
Question 9. Based on the following class decoration answer the questions (from 9.1 to 9.5)class vehicle
{
int wheels;
public:
void input_data(float,float);
void output_data();
protected:
int passenger;
};
class heavy_vehicle: protected vehicle
{
int diesel_petrol;
protected:
int load;
protected:
int load;
public:
void read_data(float,float)
void write_data(); };
class bus: private heavy_vehicle
{
char Ticket[20];
public:
void fetch_data(char).
void display_data(); >;
}
Question 9.1. Which is the base class of the class heavy, vehicle?
(a) Bus
(b) heavy_vehicle
(c) vehicle
(d) both (a) and (c)
Answer: (c) vehicle
In simple words: The 'vehicle' class is the original class that 'heavy_vehicle' builds upon.
๐ฏ Exam Tip: In `class Derived : Visibility Base`, the `Base` class listed after the colon is always the direct base class.
Question 9.2. The data member that can be accessed from the function displaydata()
(a) passenger
(b) load
(c) Ticket
(d) All of these
Answer: (d) All of these
In simple words: The `display_data()` function in the `bus` class can use `Ticket`, `passenger`, and `load` because they are all accessible to it, either directly or through the way classes were inherited.
๐ฏ Exam Tip: Carefully trace the visibility of members through each level of inheritance. Private inheritance makes public/protected base members private in the derived class, limiting further access.
Question 9.3. The member function that can be accessed by an objects of bus Class is
(a) input_data()
(b) ,output_data()write_data(
(c) fetch_data(),display_data()
(d) All of these
Answer: (c) fetch_data(),display_data()
In simple words: A `bus` object can only use `fetch_data()` and `display_data()` because these are the only functions made public in the `bus` class. Other functions are hidden.
๐ฏ Exam Tip: Public member functions are the only ones directly callable by an object of that class. Private and protected members/functions can only be accessed by other member functions of the same or derived classes.
Question 9.4. The member function that is inherited as public by Class Bus
(a) input_data()
(b) read_data(),output_data(),write_data()
(c) fetch_data(), display_data()
(d) None of these
Answer: (d) None of these
In simple words: No function is inherited as public by the `bus` class. This is because `bus` uses `private` inheritance, making all inherited parts private inside `bus`.
๐ฏ Exam Tip: Understand the key difference: `public` members of a base class inherited via `private` mode become `private` in the derived class, not `public`.
Question 10.class x
{
int a;
public:
x()
{}
};
class y
{
x x1;
public:
y()
{}
};
class z: public y,x
{
int b;
public:
z()
{}
};
What is the order of constructor for object z to be invoked?
(a) z,y,x,x
(b) x,y,z,x
(c) y,x,x,z
(d) x,y,z
(e) x,y,x,z
Answer: (e) x,y,x,z
In simple words: When an object of class `z` is made, the constructors are called in a specific order: first for `x` (because it's a member inside `y`), then for `y` (as the first base class), then for `x` (as the second base class), and finally for `z` itself.
๐ฏ Exam Tip: In multiple inheritance, base class constructors are called in the order they are listed in the derived class definition, and member objects are constructed before the class's own constructor body.
Part - II
Very Short Answers
Question 1. What is inheritance?
Answer: Inheritance is a key concept in Object-Oriented Programming (OOP) where a new class, called a derived class, is created from an existing class, known as a base class. This process allows the new class and its objects to automatically acquire the characteristics and behaviors (properties and methods) of the existing class, promoting code reuse. For example, a 'Car' class can inherit from a 'Vehicle' class, gaining common vehicle features.
In simple words: Inheritance is a way for new classes to get features from older classes. It's like a child inheriting traits from a parent, helping to reuse code and build new things faster.
๐ฏ Exam Tip: Define inheritance clearly as a mechanism for code reusability and establishing 'is-a' relationships between classes.
Question 2. What is a base class?
Answer: A base class, also known as a parent class, is the original or existing class from which other new classes are created. It provides a foundational set of properties and methods that derived classes can inherit and extend. It acts as a blueprint for specialized classes.
In simple words: A base class is the main class that gives its features to other new classes. It's like the parent in a family tree.
๐ฏ Exam Tip: Emphasize that the base class is the source of inherited features, and derived classes extend its functionality.
Question 3. Why derived class is called a power-packed class?
Answer: A derived class is called 'power-packed' because it combines its own unique features with all the inherited features from its base class(es). This means it has more capabilities and functionalities than any single base class alone. It can perform tasks specific to itself while also using the established behaviors of its ancestors, making it very versatile. For instance, a 'SportsCar' derived from a 'Car' class would have all car features plus its own speed enhancements.
In simple words: A derived class is 'power-packed' because it gets all the good features from its parent classes and also has its own new, special abilities. It's like getting all your parents' strengths and adding your own.
๐ฏ Exam Tip: Focus on the concepts of feature accumulation and extension to explain why derived classes are considered 'power-packed'.
Question 4. In what multilevel and multiple inheritances differ though both contains many base class?
Answer: Multilevel and multiple inheritances are different even though both involve multiple classes. In multiple inheritance, a single derived class inherits directly from several different base classes, like a child having two separate parents. In contrast, multilevel inheritance involves a chain where one class inherits from another, which then inherits from a third, forming a clear hierarchy with only one direct parent at each level, similar to a grandchild inheriting from a child, who inherited from a parent.
In simple words: Multiple inheritance is when one class gets features from many different parent classes at the same time. Multilevel inheritance is like a family tree where a class inherits from its parent, and that parent inherited from its own parent.
๐ฏ Exam Tip: Distinguish multiple (horizontal inheritance from several direct parents) from multilevel (vertical inheritance in a chain).
Question 5. What is the difference between public and private visibility mode?
Answer: The difference lies in how base class members are accessed in the derived class. In `private` visibility mode, all public and protected members of the base class become private members of the derived class, meaning they can only be accessed by the derived class's own methods. In `public` visibility mode, public members of the base class remain public in the derived class, and protected members remain protected, allowing them to be accessed by public interfaces or further derived classes, respectively. Choosing the correct mode ensures proper encapsulation and access control.
In simple words: With `private` mode, everything from the parent class becomes private in the child class. With `public` mode, public things stay public, and protected things stay protected in the child class.
๐ฏ Exam Tip: Remember that private inheritance makes all inherited members private, effectively cutting off further direct inheritance of those members, while public inheritance maintains the original access levels of public and protected members.
Part - III
Short Answers
Question 1. What are the points to be noted while deriving a new class?
Answer: When creating a new derived class, several key points must be followed. First, use the `class` keyword, followed by the chosen name for your new derived class. Next, include a single colon (`:`). After the colon, specify the `visibility mode` (like `public`, `private`, or `protected`) for the inheritance; if not specified, it defaults to `private`. Finally, list the name(s) of the `base class(es)` from which the new class will inherit, separating multiple base classes with commas. The syntax usually looks like this: `class DerivedClass : visibility_mode BaseClass { // members };` This structured approach helps in building a clear inheritance hierarchy.
In simple words: To make a new class from an old one, you need to use the 'class' word, give the new class a name, add a colon, choose how it will inherit (like 'public' or 'private'), and then write the old class's name. This tells the computer how to link them.
๐ฏ Exam Tip: Always remember the syntax for class derivation: `class DerivedClass : visibility_mode BaseClass`. The visibility mode is crucial for controlling member access.
Question 2. What is differences between the members present in the private visibility mode and the members present in the public visibility mode?
Answer: The distinction between private and public visibility modes in inheritance impacts how base class members are accessed within the derived class. In `private` visibility mode, all public and protected members from the base class become private within the derived class, making them accessible only by the derived class's own member functions and not by any further derived classes. Conversely, in `public` visibility mode, the public members of the base class remain public in the derived class, and protected members remain protected. This allows public members to be accessed externally and protected members to be inherited by subsequent derived classes. This difference is key for managing access control and code reusability across class hierarchies.
| Base Class Member Type | When Inherited with Private Visibility | Derived Class Access |
|---|---|---|
| Private members | Not inherited | Inaccessible |
| Protected members | Become | Private members |
| Public members | Become | Private members |
| Base Class Member Type | When Inherited with Public Visibility | Derived Class Access |
|---|---|---|
| Private members | Not inherited | Inaccessible |
| Protected members | Become | Protected members |
| Public members | Become | Public members |
In simple words: When you link classes, 'private' mode makes all parent features hidden in the child class. But 'public' mode keeps public parent features public and protected ones protected in the child class. This changes what can be seen and used later.
๐ฏ Exam Tip: Always remember that private inheritance drastically restricts access, while public inheritance maintains the original access levels of non-private base members, making them available further down the hierarchy.
Question 3. What is the difference between polymorphism and inheritance though are used for the reusability of code?
Answer: Both polymorphism and inheritance are powerful concepts in object-oriented programming that help reuse code, but they do so in different ways. Polymorphism focuses on reusing functions or methods; it is the ability of a single function or method to behave differently based on the type of object it is called upon, often achieved through method overloading or overriding. For example, a `draw()` function can behave differently for a `Circle` object versus a `Square` object. Inheritance, on the other hand, reuses code at the class level; it allows a new class (derived class) to acquire the properties and methods of an existing class (base class), thereby creating a hierarchical relationship. This is implemented through various types like single, multiple, or multilevel inheritance, allowing classes to build upon existing structures. Ultimately, polymorphism allows 'many forms' for actions, while inheritance allows 'building upon' existing structures.
In simple words: Polymorphism means one function can do many things depending on the object. Inheritance means new classes can get features from old classes. Both help reuse code, but polymorphism is about functions doing different jobs, and inheritance is about classes sharing features.
๐ฏ Exam Tip: Think of polymorphism as 'one interface, many implementations' (behavioral reusability), and inheritance as 'is-a' relationship (structural reusability).
Question 4. What do you mean by overriding?
Answer: Function overriding occurs when a derived class creates its own version of a member function that already exists in its base class with the same name and signature. When this happens, the derived class's function 'shadows' or 'hides' the base class's version. This allows the derived class to provide a specific implementation for a function that is already defined in its parent, tailored to its own needs. To explicitly call the hidden base class function, one can use the scope resolution operator, like `BaseClassName::functionName()`. For example, a `Dog` class might override the `makeSound()` function from a `Animal` base class to specifically output 'Woof!'.
In simple words: Overriding is when a child class changes how a function from its parent class works. It keeps the same name but has a different job. You can still use the parent's original function by adding its name before the function call.
๐ฏ Exam Tip: Distinguish overriding (same signature, different implementation in derived class) from overloading (same name, different signatures in the same class).
Question 5. Write some facts about the execution of constructors and destructors in inheritance.
Answer: In an inheritance hierarchy, constructors and destructors follow specific execution orders.
- Constructors: Base class constructors are always executed first, before the derived class constructors. This ensures that the foundational parts of an object are properly set up before its specialized parts.
- Calling Base Constructors: A derived class cannot directly inherit a base class constructor, but it must explicitly call it (or the compiler calls the default base constructor) in its own constructor's initialization list to ensure the base part of the object is built correctly.
- Multiple Inheritance: When a class inherits from multiple base classes, their constructors are called in the order they are listed in the derived class's inheritance declaration, from left to right.
- Multilevel Inheritance: In multilevel inheritance, constructors are executed sequentially from the topmost base class down to the most derived class.
- Destructors: Destructors, conversely, are executed in the reverse order of constructors; that is, the derived class destructor is called first, followed by the base class destructors. This ensures that specialized resources are released before general ones. For example, in a `Vehicle -> Car -> SportsCar` hierarchy, `Vehicle`'s constructor runs first, then `Car`'s, then `SportsCar`'s. But destructors run `SportsCar` first, then `Car`, then `Vehicle`.
In simple words: Constructors in parent classes always run first, then the child class constructor. If a class has many parents, their constructors run from left to right in the list. Destructors, which clean up, run in the opposite order: child first, then parents.
๐ฏ Exam Tip: Remember: 'Base before Derived' for constructors, and 'Derived before Base' for destructors. This is a fundamental concept for object lifecycle management in C++ inheritance.
IV. Explain In Brief (Five Marks)
Question 1. Explain the different types of inheritance.
Answer: Inheritance is a fundamental concept in Object-Oriented Programming (OOP) that allows a new class to derive properties and behaviors from an existing class, promoting code reuse. There are several types of inheritance, each structuring the relationship between classes differently:
1. Single Inheritance: In this type, a derived class inherits features from only one base class. It's the simplest form, creating a direct one-to-one parent-child relationship.
2. Multiple Inheritance: Here, a derived class inherits from more than one base class. This allows the derived class to combine functionalities from several distinct sources.
3. Hierarchical Inheritance: In this structure, a single base class is inherited by multiple derived classes. It creates a tree-like structure where one parent has many children.
4. Multilevel Inheritance: This involves a chain of inheritance where a class inherits from another class, which in turn inherits from a third class. It reflects a transitive relationship.
In simple words: There are different ways classes can share features: Single (one parent), Multiple (many parents), Hierarchical (one parent, many children), and Multilevel (grandparent to parent to child). Each way creates a different family tree for classes.
๐ฏ Exam Tip: For descriptive questions, clearly define each type of inheritance and ideally provide a simple diagram or real-world analogy to illustrate the concept. Hybrid inheritance is a combination of these types.
5. Hybrid inheritance: When there is a combination of more than one type of inheritance, it is known as hybrid inheritance. This can be a mix of Multilevel and Multiple inheritances, or Hierarchical and Multilevel inheritance, or even Hierarchical, Multilevel, and Multiple inheritances all together.
Question 2. Explain the different visibility modes through pictorial representation.
Answer:
There are different ways to set how parts of a base class can be seen and used by a derived class. These are called visibility modes. They control what is inherited and with what level of access.
Private visibility mode:
When a base class is inherited with 'private' visibility, all its public and protected members become private in the derived class. This means they cannot be accessed directly by objects of the derived class, and they cannot be inherited further by classes derived from this derived class.
Private visibility members cannot be inherited further. So, they cannot be directly accessed by the derived classes.
Protected visibility mode:
When a base class is inherited with 'protected' visibility, its protected and public members become protected in the derived class. This means they can be accessed by the derived class and any class that further inherits from it, but not directly by outside objects.
Public visibility mode:
When a base class is inherited with 'public' visibility, its protected members stay protected in the derived class, and its public members stay public. This allows the derived class to use them, and they can also be inherited by future child classes. Public visibility members can be inherited by their child classes and accessed directly.
In simple words: Visibility modes control how much of the parent class a child class can see and use. Private makes everything super hidden, protected lets children and grandchildren see, and public keeps everything open.
๐ฏ Exam Tip: Remember that private members of a base class are never directly inherited, regardless of the visibility mode. They can only be accessed indirectly through public or protected member functions of the base class.
Question 3. What is the difference between polymorphism and inheritance though are used for the reusability of code?
Answer: Both polymorphism and inheritance help reuse code in programming, but they do it in different ways.
Polymorphism:
- Code reusability is done using functions or methods.
- It means a function can act in different ways based on the object it is called on.
- This is often achieved through a method called 'overloading', where functions have the same name but do different things based on the inputs.
- Code reusability is done using classes.
- It is the process of creating new classes (derived classes) from existing ones (base classes). The new classes get features from the old ones.
- There are different kinds of inheritance, like single, multiple, multilevel, hybrid, and hierarchical, all of which help share code among classes.
๐ฏ Exam Tip: Focus on "what" is being reused. Polymorphism reuses function names with different actions (behavior), while inheritance reuses class definitions and attributes (structure).
Question 4. What do you mean by overriding?
Answer: Overriding happens when a function in a derived class has the exact same name and inputs as a function in its base class. When this occurs, the function in the derived class "shadows" or "hides" the function from the base class. To use the base class function in such a case, you need to specify the base class name followed by `::` and then the function name. This ensures you call the correct version. This concept is important for specialized behaviors in derived classes.
In simple words: Overriding means a child class has its own version of a function that also exists in its parent class. The child's version takes priority, but you can still call the parent's version if you need to.
๐ฏ Exam Tip: Overriding allows you to provide a specific implementation of a function that is already provided by its base class, which is key for achieving runtime polymorphism.
Question 5. Write some facts about the execution of constructors and destructors in inheritance.
Answer: Here are some facts about how constructors and destructors work in inheritance:
1. Base class constructors are always run first, before the constructors of the derived class. This ensures the base part of an object is properly set up before the derived part.
2. A derived class cannot directly inherit a base class constructor, but it can call the base class constructor using `Base_class name::base_class_constructor()` in its own definition to initialize the base part.
3. If a class has multiple base classes (like in multiple inheritance), their constructors are executed starting from the leftmost base class in the inheritance list.
4. In multilevel inheritance, constructors run in the order of inheritance (from the top-most base class down to the final derived class). Destructors, however, run in the exact reverse order of constructor execution, ensuring resources are freed safely from derived to base.
In simple words: Parent parts of a class are built first, then child parts. When deleting, child parts are destroyed first, then parent parts. If there are many parents, they are built from left to right.
๐ฏ Exam Tip: Always remember the "build from base up, destroy from derived down" rule for constructors and destructors. This order is crucial for correct object lifecycle management and preventing resource leaks.
IV. Explain In Brief (Five Marks)
Question 1. Explain the different types of inheritance.
Answer: Inheritance is a key feature in object-oriented programming that allows a new class to use properties and behaviors from an existing class. There are several types of inheritance, which define how classes can share features:
1. Single Inheritance: This is when a derived class inherits features from only one base class. It is the simplest form of inheritance.
When a derived class inherits only from one base class, it is known as single inheritance.
2. Multiple Inheritance: In this type, a derived class inherits from multiple base classes, meaning it gets features from more than one parent class. This allows a class to combine behaviors from different sources.
When a derived class inherits from multiple base classes, it is known as multiple inheritance.
3. Hierarchical inheritance: This happens when more than one derived class is created from a single base class. It's like a single parent having multiple children, each with its own unique traits but sharing common features from the parent.
When more than one derived class is created from a single base class, it is known as Hierarchical inheritance.
4. Multilevel Inheritance: This type shows a "transitive" nature, where a class is derived from another class, which itself is derived from yet another class. It's like a grandparent-parent-child relationship, forming a chain of inheritance.
When a class is derived from a class which is a derived class, then it is referred to as multilevel inheritance.
5. Hybrid inheritance: This is a mix of two or more types of inheritance. For example, it could be a combination of multilevel and multiple inheritances, or hierarchical and multilevel inheritance, or even more complex combinations. This allows for very flexible class structures.
When there is a combination of more than one type of inheritance, it is known as hybrid inheritance. Hence, it may be a combination of Multilevel and Multiple inheritances or Hierarchical and Multilevel inheritance or Hierarchical, Multilevel and Multiple inheritances.
๐ฏ Exam Tip: When describing inheritance types, always provide a clear definition and a simple example or a diagram to illustrate the class relationship.
Question 2. Explain the different visibility modes through pictorial representation.
Answer:
Visibility modes define how the members (data and functions) of a base class are inherited and accessed by a derived class. They are crucial for controlling data encapsulation and access control. There are three main visibility modes:
Private visibility mode:
When a base class is inherited privately, all its public and protected members become private within the derived class. This means they cannot be accessed directly by objects of the derived class, and they cannot be inherited further by any classes that derive from this class. Private members of the base class remain private and are not directly accessible by the derived class, but can be accessed indirectly through public or protected base class functions.
Protected visibility mode:
When a base class is inherited as protected, its public and protected members become protected in the derived class. This allows the derived class members to access them, and they can also be inherited by further derived classes, but they are not directly accessible from outside the derived class via its objects.
Public visibility mode:
When a base class is inherited publicly, its protected members stay protected in the derived class, and its public members stay public. This allows the derived class to use them, and they can also be inherited by future child classes. Public visibility members can be inherited by their child classes and accessed directly. This is the most open form of inheritance.
In simple words: Visibility modes decide what parts of a parent class a child class can use and how. 'Private' means child can't share it, 'protected' means child can share with its children, and 'public' means anyone can share.
๐ฏ Exam Tip: When drawing diagrams for visibility modes, ensure the arrows correctly indicate the transformation of access specifiers (e.g., public to private) in the derived class.
Question 3. Based on the following class decoration answer the questions (from 9.1 to 9.5)
class vehicle
{
int wheels;
public:
void input_data(float,float);
void output_data();
protected:
int passenger;
};
class heavy_vehicle: protected vehicle
{
int diesel_petrol;
protected:
int load;
protected:
int load;
public:
void read_data(float,float)
void write_data(); };
class bus: private heavy_vehicle
{
char Ticket[20];
public:
void fetch_data(char);
void display_data();
};
Question 9.1. Which is the base class of the class heavy_vehicle?
Answer: (c) vehicle
In simple words: The 'vehicle' class is the original class from which 'heavy_vehicle' gets its features.
๐ฏ Exam Tip: In C++, the class mentioned after the colon in a class definition (e.g., `class Derived : Base`) is always the base class.
Question 9.2. The data member that can be accessed from the function displaydata()
Answer: (d) All of these
In simple words: The `display_data()` function can reach and use 'passenger', 'load', and 'Ticket'.
๐ฏ Exam Tip: When determining accessibility, trace the inheritance chain and apply visibility rules. `display_data()` is a public member of `bus`, so it can access all members of `bus` and inherited members that are not private to `bus` or its direct base `heavy_vehicle`.
Question 9.3. The member function that can be accessed by an objects of bus Class is
(a) input_data()
(b) read_data(),output_data(),write_data()
(c) fetch_data(),display_data()
(d) All of these
Answer: (c) fetch_data(),display_data()
In simple words: An object of the 'bus' class can directly call its own `fetch_data()` and `display_data()` functions.
๐ฏ Exam Tip: Only public member functions of a class can be accessed directly by objects of that class. Trace the inheritance to find which base class public members become public in the derived class.
Question 9.4. The member function that is inherited as public by Class Bus
(a) input_data()
(b) read_data(),output_data(),write_data()
(c) fetch_data(), display_data()
(d) None of these
Answer: (d) None of these
In simple words: None of the functions from 'vehicle' or 'heavy_vehicle' are public in 'bus'.
๐ฏ Exam Tip: Pay close attention to the visibility modes in each inheritance step. `heavy_vehicle` inherits `protected vehicle`, making `vehicle`'s public members `protected` in `heavy_vehicle`. `bus` inherits `private heavy_vehicle`, making `heavy_vehicle`'s `protected` members `private` in `bus`. This means no functions are inherited as `public` by `bus` from its base classes.
Question 10.
class x
{
int a;
public:
x()
{}
};
class y
{
x x1;
public:
y()
{}
};
class z: public y,x
{
int b;
public:
z()
{}
}z1;
What is the order of constructor for object z to be invoked?
(a) z,y,x,x
(b) x,y,z,x
(c) y,x,x,z
(d) x,y,z
(e) x,y,x,z
Answer: (e) x,y,x,z
In simple words: When a `z` object is created, the constructors are called in a specific order: first, `x` (from the `x x1` member in `y`), then `y`, then `x` (from the direct inheritance `public x`), and finally `z`.
๐ฏ Exam Tip: In multiple inheritance, base class constructors are called in the order they are listed after the colon. Also, if a class contains objects of other classes, those member objects' constructors are called before the containing class's constructor.
Part - II
Question 1. What is inheritance?
Answer: Inheritance is a very important part of Object-Oriented Programming (OOP). It allows a new class, called the derived or child class, and its objects to automatically take on the characteristics and behaviors (properties) of an existing class, known as the base or parent class. This helps in reusing code and building relationships between classes.
In simple words: Inheritance lets new classes use properties from older classes, making code easier to reuse.
๐ฏ Exam Tip: Always define inheritance as a mechanism for code reusability and establishing "is-a" relationships between classes, clearly stating base and derived classes.
Question 2. What is a base class?
Answer: A base class is the original class from which other classes (derived classes) inherit properties and methods. It acts as the blueprint that provides common features to its child classes. It is also often called a parent class.
In simple words: A base class is the main class that shares its features with other classes.
๐ฏ Exam Tip: Understand that a base class provides a foundation; it defines the common interface and functionality that its derived classes will share and potentially extend.
Question 3. Why derived class is called a power-packed class?
Answer: A derived class is called a "power-packed" class because it combines features and functionalities from its base class(es) with its own unique additions. This means it has more capabilities than a simple class that doesn't inherit. It can inherit members and behaviors and also add new ones or modify inherited ones.
For example:
- In Multilevel Inheritance, constructors are run in the order of inheritance, so a derived class builds upon all its parent classes' setups.
- In Multiple Inheritance, if there are many base classes, the derived class collects features from all of them, making it very powerful by merging different functionalities. The constructors start running from the leftmost base class.
In simple words: A derived class is "power-packed" because it gets all the good stuff from its parent class(es) and also has its own new abilities.
๐ฏ Exam Tip: Explain that a derived class is "power-packed" because it combines inherited functionality (reusability) with its own new or overridden features (specialization), making it more capable.
Question 4. In what multilevel and multiple inheritances differ though both contains many base class?
Answer: Even though both multilevel and multiple inheritance involve more than one class, they are different in how the classes are linked.
In multiple inheritance, a derived class directly inherits from several base classes. It has many direct parents. For example, a "Child" class inheriting directly from "Mother" and "Father" classes.
In multilevel inheritance, a derived class inherits from only one base class, but that base class itself inherited from another class, and so on. It forms a chain, like a "Grandchild" class inheriting from a "Child" class, which inherited from a "Parent" class. So, it has only one direct parent at each step of the chain.
In simple words: Multiple inheritance means one child has many direct parents. Multilevel inheritance means a child has one parent, who also had one parent, creating a family tree chain.
๐ฏ Exam Tip: Clearly differentiate between the "structure" of inheritance: multiple means "many parents to one child" (lateral), while multilevel means "one parent to child, and that child is a parent to another" (vertical chain).
Question 5. What is the difference between public and private visibility mode?
Answer: The difference between public and private visibility modes lies in how the members of the base class are accessed within the derived class and by outside objects.
Private visibility mode:
When a base class is inherited with 'private' visibility, its public and protected members become private within the derived class. This means they cannot be directly accessed by objects of the derived class. They are also not inherited further by any classes that derive from this (second-level) derived class. Private members of the base class remain private and are never directly inherited.
Public visibility mode:
When a base class is inherited with 'public' visibility, its protected members stay protected in the derived class, and its public members stay public. This allows the derived class to access these members directly. These members can also be inherited by any further classes derived from this class.
In simple words: Private visibility means inherited things become hidden inside the new class. Public visibility means inherited things stay as visible as they were in the original class.
๐ฏ Exam Tip: Emphasize that visibility modes affect the access level *within* the derived class and its subsequent child classes, not the original access level within the base class itself.
Part - III
Question 1. What are the points to be noted while deriving a new class?
Answer: When you create a new class (derived class) from an existing one (base class), you need to follow these rules:
1. You must use the keyword `class` to define the new derived class.
2. The name of the derived class should be provided right after the `class` keyword.
3. A single colon (`:`) must be placed after the derived class name.
4. You need to specify the type of derivation, also known as the visibility mode, which can be `private`, `public`, or `protected`. If you don't mention a visibility mode, it is set to `private` by default.
5. The names of all base classes (parent classes) from which the new class inherits should be listed after the visibility mode, separated by commas if there are multiple.
Here is a common syntax:
`class derivedclass_name : visibility_mode base_class_name`
`{`
` // members of derived class`
`};`
In simple words: When making a new class from an old one, you need to say "class", give it a name, add a colon, pick how visible the old class's parts will be (public, private, or protected), and then name the old class.
๐ฏ Exam Tip: Remember that specifying the visibility mode is crucial for controlling access to inherited members. The default is `private` if not specified.
Question 2. What is differences between the members present in the private visibility mode and the members present in the public visibility mode?
Answer: The difference between private and public visibility modes impacts how the inherited members from the base class behave in the derived class and how they can be accessed.
Private visibility mode:
When a base class is inherited using the `private` visibility mode, both the public and protected members of the base class become private members in the derived class. This means they cannot be directly accessed by objects of the derived class, and they cannot be further inherited by any classes that derive from this (second-level) derived class. Private members of the base class are never directly inherited.
Public visibility mode:
When a base class is inherited using the `public` visibility mode, the protected members of the base class remain protected in the derived class. The public members of the base class remain public in the derived class. This allows the derived class to access these members directly, and they can also be inherited by subsequent child classes. Public visibility members can be inherited by their child classes and accessed directly.
In simple words: In 'private' inheritance, inherited parts become hidden in the new class, like they are private. In 'public' inheritance, inherited parts keep their original visibility, so public stays public and protected stays protected.
๐ฏ Exam Tip: A key point is that `private` inheritance effectively "stops" the public interface of the base class from being public in the derived class, while `public` inheritance maintains the public interface.
Question 3.
#include<iostream>
#include<string.h>
#include<stdio.h>
using name spacestd;
class publisher
{
char pname[15];
char hoffice[15];
char address[25];
double turnover;
protected:
char phone[3][10];
void register();
public:
publisher();
publisher();
void enter_data();
void disp_data();
};
class branch
{
char bcity[15];
char baddress[25];
protected:
int no_of_emp;
public:
char bphone[2][10];
branch();
~branch();
void havedata();
void givedata();
};
class author: public branch, publisher
{
int aut_code;
charaname[20];
float income;
public:
author();
~author();
void getdata();
void putdata();
};
Answer The Following Questions Based On The Above Given Program:
3.1. Which type of Inheritance is shown in the program?
Answer:
3.1 Multiple Inheritance
In simple words: The program uses multiple inheritance because the `author` class gets features from both the `branch` and `publisher` classes.
๐ฏ Exam Tip: Recognize multiple inheritance by seeing a derived class listing more than one base class after the colon in its definition.
3.2. Specify the visibility mode of base classes.
Answer:
3.2 public
In simple words: Both `branch` and `publisher` are inherited publicly by `author`.
๐ฏ Exam Tip: The visibility mode is explicitly stated as `public` in the `author` class definition: `class author: public branch, public publisher`.
3.3 Give the sequence of Constructor/Destructor Invocation when object of class author is created.
Answer:
3.3 When an `author` object is created, the constructors are called in this order: `branch`, then `publisher`, and finally `author`. For destructors, the order is reversed: `author`, then `publisher`, and finally `branch`.
In simple words: Constructors run: `branch` then `publisher` then `author`. Destructors run: `author` then `publisher` then `branch`.
๐ฏ Exam Tip: In multiple inheritance, constructors are called in the order of base classes listed (left to right), and destructors are called in reverse order.
3.4. Name the base class(/es) and derived class (/es).
Answer:
3.4 The base classes are `branch` and `publisher`. The derived class is `author`.
In simple words: `branch` and `publisher` are the older classes, and `author` is the new class that gets features from them.
๐ฏ Exam Tip: A base class is the parent, and a derived class is the child. In multiple inheritance, there are multiple parents and one child.
3.5 Give number of bytes to be occupied by the object of the following class:
(a) publisher
(b) branch
(c) author
Answer:
3.5 The approximate memory required for objects of these classes is:
a) A `publisher` class object requires 93 bytes.
b) A `branch` class object requires 64 bytes.
c) An `author` class object requires 181 bytes. This is because it includes the memory for its own members plus the combined memory of its base classes, `branch` and `publisher` (64 + 93 + its own members (4+20+4)).
In simple words: `publisher` takes 93 bytes, `branch` takes 64 bytes. `author` takes 181 bytes because it includes all the data from `publisher` and `branch`, plus its own data.
๐ฏ Exam Tip: The size of an object is the sum of its own data members plus the sizes of all its base class parts and any member objects. The exact size might vary slightly based on compiler and padding.
3.6. Write the names of data members accessible from the object of class author.
Answer:
3.6 Data members accessible directly from an `author` class object are `aut_code`, `charaname[20]`, and `income`. These are the `public` members of the `author` class itself. The data members inherited from `publisher` and `branch` (like `pname`, `hoffice`, `address`, `turnover`, `bcity`, `baddress`, `no_of_emp`) are `protected` or `private` in their original classes and thus not directly accessible by an `author` object.
In simple words: An `author` object can directly see and use `aut_code`, `charaname`, and `income`.
๐ฏ Exam Tip: Remember that only public members are directly accessible via an object. Protected members are accessible within derived classes but not through objects outside the class hierarchy, and private members are only accessible within their own class.
3.7. Write the names of all member functions accessible from the object of class author.
Answer:
3.7 The member functions accessible directly from an `author` class object are `getdata()` and `putdata()`. These are the `public` functions defined within the `author` class itself. The public functions inherited from `publisher` (`enter_data()`, `disp_data()`) and `branch` (`havedata()`, `givedata()`) become `public` in `author` due to public inheritance, so they are also accessible directly by an `author` object. Thus, the accessible functions are `getdata()`, `putdata()`, `enter_data()`, `disp_data()`, `havedata()`, and `givedata()`.
In simple words: An `author` object can use `getdata()`, `putdata()`, `enter_data()`, `disp_data()`, `havedata()`, and `givedata()`.
๐ฏ Exam Tip: When inheritance is `public`, public members of the base class remain public in the derived class, making them accessible through derived class objects.
3.8 Write the names of all members accessible from member functions of class author.
Answer:
3.8 From within member functions of the `author` class, the following members are accessible:
1) Data members:
- `aut_code`, `charaname[20]`, `income` (from its own class, `author`).
- `pname[15]`, `hoffice[15]`, `address[25]`, `turnover` (public members from `publisher` are `public` in `author` after public inheritance).
- `phone[3][10]` (protected members from `publisher` are `public` in `author` after public inheritance).
- `bcity[15]`, `baddress[25]` (public members from `branch` are `public` in `author` after public inheritance).
- `no_of_emp` (protected member from `branch` is `public` in `author` after public inheritance).
- `getdata()`, `putdata()` (from its own class, `author`).
- `publisher()`, `enter_data()`, `disp_data()` (from `publisher`).
- `branch()`, `~branch()`, `havedata()`, `givedata()` (from `branch`).
*Note: Private members of base classes (`a` from `publisher` and internal `int wheels` from `vehicle` via `heavy_vehicle`) are never directly accessible to derived class member functions.*
In simple words: Inside an `author`'s own functions, it can use all its own data and functions. It can also use all public and protected data and functions inherited from `branch` and `publisher`. It cannot use private members of `branch` or `publisher`.
๐ฏ Exam Tip: When evaluating accessibility from within a member function, consider its own class members, and then trace the inheritance path, applying the inherited visibility mode at each step (public means public/protected members stay public/protected, protected means they become protected, private means they become private). Private members of base classes are never accessible.
Question 4. Consider the following C++ code and answer the questions.
class Personal
{
int Class, Rno;
char Section;
protected:
char Name[20];
public:
personal();
void p_entry();
void p_display();
};
class Marks:private Personal
{
float M[5];
protected:
char Grade[5];
public:
Marks();
void Mentry();
void Mdisplay();
};
class Result:public Marks
{
float Total, Agg;
public:
char FinalGrade, Commence[20];
Result();
void Rcalculate();
void Rdisplay();
};
4.1. Which type of Inheritance is shown in the program?
Answer:
4.1 Multilevel Inheritance
In simple words: This program uses multilevel inheritance because `Result` inherits from `Marks`, and `Marks` inherits from `Personal`, creating a chain.
๐ฏ Exam Tip: Multilevel inheritance is characterized by a "chain" of inheritance where a derived class serves as the base class for another class.
4.2. Specify the visibility mode of base classes.
Answer:
4.2 The visibility modes are:
- For the `Marks` class, inheriting from `Personal`, the visibility mode is `private`.
- For the `Result` class, inheriting from `Marks`, the visibility mode is `public`.
In simple words: `Marks` privately inherits `Personal`, and `Result` publicly inherits `Marks`.
๐ฏ Exam Tip: Always note the `public`, `protected`, or `private` keyword specified after the colon when a class is derived, as it defines the inheritance visibility mode.
4.3 Give the sequence of Constructor/Destructor Invocation when object of class Result is created.
Answer:
4.3 When an object of the `Result` class is created, the constructors are invoked in the following order: `Personal`, then `Marks`, and finally `Result`. Destructors will be invoked in the reverse order: `Result`, then `Marks`, and finally `Personal`.
In simple words: First `Personal` is built, then `Marks`, then `Result`. To destroy, `Result` goes first, then `Marks`, then `Personal`.
๐ฏ Exam Tip: In multilevel inheritance, constructors are called from the top-most base class down to the final derived class. Destructors are called in the opposite direction.
4.4. Name the base class(/es) and derived class (/es).
Answer:
4.4 In this program:
- The base classes are `Personal` and `Marks`.
- The derived classes are `Marks` and `Result`. (`Marks` is derived from `Personal`, and `Result` is derived from `Marks`).
In simple words: `Personal` is a base class. `Marks` is a derived class of `Personal` and also a base class for `Result`. `Result` is a derived class of `Marks`.
๐ฏ Exam Tip: In a multilevel hierarchy, a class can act as both a derived class (from its parent) and a base class (for its child).
4.5 Give number of bytes to be occupied by the object of the following class:
(a) Personal
(b) Marks
(c) Result
Answer:
4.5 The approximate memory occupied by objects of these classes (using Dev C++) is:
a) A `Personal` class object requires 28 bytes.
b) A `Marks` class object requires 53 bytes.
c) A `Result` class object requires 82 bytes.
In simple words: `Personal` takes 28 bytes. `Marks` takes 53 bytes (its own plus `Personal`'s part). `Result` takes 82 bytes (its own plus `Marks`'s part).
๐ฏ Exam Tip: Class sizes are cumulative in inheritance. A derived class object includes the memory footprint of all its base class components, plus its own data members, often with some padding for alignment.
4.6. Write the names of data members accessible from the object of class Result.
Answer:
4.6 From an object of the `Result` class, only its own data members are directly accessible: `FinalGrade` and `Commence[20]`. No members inherited from `Personal` or `Marks` are accessible via a `Result` object directly because `Marks` privately inherits `Personal`, and `Result` publicly inherits `Marks`. This makes all `Personal` members private in `Marks`, and all `Marks` members (including those from `Personal`) either private or protected in `Result`.
In simple words: An object of `Result` can only access `FinalGrade` and `Commence`. Other inherited data is hidden.
๐ฏ Exam Tip: Trace the visibility modes through the entire inheritance chain. A `private` inheritance effectively blocks external access to inherited members, even if the next inheritance step is `public`.
4.7. Write the names of all member functions accessible from the object of class Result.
Answer:
4.7 The member functions accessible from an object of the `Result` class are:
- `Rcalculate()` and `Rdisplay()` (own class member functions).
- `Mentry()` and `Mdisplay()` (derived from `Marks` class).
Functions from the `Personal` class (`p_entry()`, `p_display()`) are not accessible because `Marks` class inherited `Personal` with `private` visibility mode, making `Personal`'s public and protected members private within `Marks`. Even though `Result` inherits `Marks` publicly, it cannot regain the original public accessibility of `Personal`'s members.
In simple words: A `Result` object can use `Rcalculate()`, `Rdisplay()`, `Mentry()`, and `Mdisplay()`. It cannot use any functions from the `Personal` class because of how `Marks` inherited from `Personal`.
๐ฏ Exam Tip: The visibility of inherited members is determined by the *most restrictive* inheritance path. Once a member becomes private due to a private inheritance, it generally cannot become public again further down the chain.
4.8 Write the names of all members accessible from member functions of class Result.
Answer:
4.8 From within member functions of the `Result` class, the following members are accessible:
1) Data members:
- `Total`, `Agg`, `FinalGrade`, and `Commence` (from its own `Result` class).
- `M` and `Grade` (from the `Marks` class, as `Marks`'s protected members are inherited as protected in `Result` due to public inheritance).
2) Member functions:
- `Mentry()` and `Mdisplay()` from the `Marks` class can be invoked from `Result` class member functions.
In simple words: `Result`'s functions can use all its own data (`Total`, `Agg`, `FinalGrade`, `Commence`) and `Marks`'s data (`M`, `Grade`). They can also use `Marks`'s functions (`Mentry()`, `Mdisplay()`). They cannot use any data or functions from `Personal` because `Personal` was inherited privately by `Marks`.
๐ฏ Exam Tip: When internal member functions are accessing members, remember that `private` inheritance blocks access to *all* inherited base class members (even if they were public/protected in the base) at that level of derivation and any subsequent levels.
Question 5. Write the output of the following program.
#include<iostream>
using namespace std;
class A
{
protected:
int x;
public:
void show()
{
cout<<"x = "<
A()
{
cout<
~A()
{
cout<
};
class B : public A
{
protected:
int y;
public:
B(int x, int y)
{
//this -> is used to denote the objects datamember this->x = x;
//this -> is used to denote the objects datamember this->y = y;
}
B()
{
cout<
~B()
{
cout<
void show()
{
cout<<"x = "<
};
int main()
{
A objA;
B objB(30, 20);
objB.show();
return 0;
}
Answer:
The program output would be:
I am class A
I am class B
x = 30
y = 20
Bye
Bye
Bye
In simple words: The program first creates an 'A' object, then a 'B' object (which also creates an 'A' part inside it). It prints messages from their constructors, then shows the values of x and y from the 'B' object. Finally, it prints "Bye" messages as the objects are destroyed.
๐ฏ Exam Tip: When predicting output for inheritance and constructors/destructors, remember that base class constructors run before derived class constructors, and destructors run in reverse order (derived before base).
Question 6. Debug the following program.
Output:
15
14
13
Program :
%include(iostream.h)
#include<conio.h>
Class A
{
public;
int a1,a2:a3;
void getdata[]
{
a1=15;
a2=13;a3=13;
}
}
Class B:: public A()
{
PUBLIC
voidfunc()
{
int b1:b2:b3;
A::getdata[];
b1=a1;
b2=a2;
a3=a3;
cout<
}
void main()
{
clrscr()
B der;
derl:func();
getch();
}
Answer:
The program has several syntax errors. Here is the corrected (modified error-free) program:
Modified Error Free Program:
#include<iostream>
#include<conio.h>
using namespace std;
class A
{
public:
int a1,a2,a3;
void getdata()
{
a1=15;
a2=14;
a3=13;
}
};
class B : public A
{
public:
void func()
{
int b1,b2,b3;
getdata(); // Calls base class getdata
b1=a1;
b2=a2;
b3=a3;
cout<
};
int main()
{
B der;
der.func();
getch();
return 0;
}
The output of this corrected program will be:
15
14
13
In simple words: The original code had many small mistakes in how it was written. The fixed code now correctly defines the classes and functions. When it runs, it will print the numbers 15, 14, and 13, each on a new line.
๐ฏ Exam Tip: When debugging C++ code, carefully check for common errors like missing semicolons, incorrect header syntax (`.h` vs no `.h` with `using namespace std`), invalid variable declarations (`a2:a3` instead of `a2, a3`), and proper access specifiers (`public:` instead of `public;`).
11th Computer Science Guide Inheritance Additional Questions and Answers
Choose The Correct Answer:
Question 1. When a derived class inherits only from one base class, it is known as __________ inheritance.
(a) multiple inheritances
(b) multilevel inheritance
(c) hierarchical inheritance
(d) single inheritance
Answer: (d) single inheritance
In simple words: When a new class only gets features from one parent class, it's called single inheritance.
๐ฏ Exam Tip: Single inheritance is the simplest form, where a derived class has exactly one direct base class.
Question 2. __________ enables new class and its objects to take on the properties of the existing classes.
(a) Inheritance
(b) Encapsulation
(c) Overriding
(d) None of the options
Answer: (a) Inheritance
In simple words: Inheritance is the way new classes can get features from old ones.
๐ฏ Exam Tip: Inheritance is the fundamental OOP concept that allows code reusability by creating a hierarchy of classes.
Question 3. When more than one derived classes are created from a single base class, it is called __________ .
(a) inheritance
(b) hybrid inheritance
(c) hierarchical inheritance
(d) multiple inheritances
Answer: (c) hierarchical inheritance
In simple words: When one main class has many different child classes, it is called hierarchical inheritance.
๐ฏ Exam Tip: Hierarchical inheritance represents a one-to-many relationship from a single base class to multiple derived classes.
Question 4. A class that inherits from a superclass is called a __________ class.
(a) Sub
(b) Base class
(c) Derived
(d) Sub or Derived
Answer: (d) Sub or Derived
In simple words: A class that gets features from an older, main class is called a sub-class or derived class.
๐ฏ Exam Tip: The terms "sub-class" and "derived class" are synonymous, both referring to a class that inherits from another.
Question 5. The __________ are invoked in reverse order.
(a) constructor
(b) destructor
(c) pointer
(d) operator
Answer: (b) destructor
In simple words: Destructors are functions that are called in the opposite order of how objects were created.
๐ฏ Exam Tip: Constructors are called from base to derived, while destructors are called from derived to base, which is the reverse order.
Question 6. There are __________ types of inheritance,
(a) Two
(b) Three
(c) Four
(d) Five
Answer: (d) Five
In simple words: There are five main ways classes can inherit features from each other in object-oriented programming. Each type offers a different structure for how classes relate.
๐ฏ Exam Tip: Remember the five main types of inheritance: Single, Multiple, Multilevel, Hybrid, and Hierarchical. Knowing these categories is key.
Question 7. __________ inheritance is a type of inheritance.
(a) Single or Hybrid
(b) Multilevel / Hierarchical
(c) Multiple
(d) All of the options
Answer: (d) All of the options
In simple words: Single, Hybrid, Multilevel, Hierarchical, and Multiple are all valid types of inheritance that describe how classes can be structured. Each one defines a specific relationship pattern.
๐ฏ Exam Tip: When faced with "All of the options", check if each listed choice is individually correct. If all are, then "All of the options" is the right answer.
Question 8. When a derived class inherits only from one base class, it is known as __________ inheritance.
(a) Single
(b) Multilevel / Hierarchical
(c) Multiple
(d) Hybrid
Answer: (a) Single
In simple words: If a new class gets features from just one existing class, we call this single inheritance. It's the simplest way for classes to pass on traits.
๐ฏ Exam Tip: Single inheritance is fundamental; understand that it means a clear one-to-one parent-child relationship between classes.
Question 9. When a derived class inherits from multiple base classes it is known as __________ inheritance.
(a) Single
(b) Multilevel / Hierarchical
(c) Multiple
(d) Hybrid
Answer: (c) Multiple
In simple words: When a class is created from more than one parent class, it is known as multiple inheritance. This allows the derived class to combine features from several sources.
๐ฏ Exam Tip: Distinguish multiple inheritance (many parents, one child) from multilevel inheritance (a chain of parents and children). They are distinct concepts.
Question 10. When more than one derived classes are created from a single base class, it is known as __________ inheritance.
(a) Single
(b) Hierarchical
(c) Multiple
(d) Hybrid
Answer: (b) Hierarchical
In simple words: If one main class has many different child classes that branch off from it, this structure is called hierarchical inheritance. It's like one family head with many children.
๐ฏ Exam Tip: Hierarchical inheritance creates a tree-like structure, with a single base at the top and multiple derived classes below it.
Question 11. The transitive nature of inheritance is itself reflected by __________ form of inheritance.
(a) Single
(b) Multilevel
(c) Multiple
(d) Hybrid
Answer: (b) Multilevel
In simple words: Multilevel inheritance shows how features can be passed down through several steps, like a grandparent to a parent to a child. This stepping-down process is what we call transitive.
๐ฏ Exam Tip: Transitivity means that if A passes to B, and B passes to C, then A effectively passes to C. This chain is the hallmark of multilevel inheritance.
Question 12. When a class is derived from a class which is a derived class โ then it is referred to as __________ inheritance.
(a) Single
(b) Multilevel
(c) Multiple
(d) Hybrid
Answer: (b) Multilevel
In simple words: When a class inherits from another class that has already inherited from a different class, it creates a chain, which is called multilevel inheritance. It's like generations of a family.
๐ฏ Exam Tip: Multilevel inheritance involves a hierarchy of classes where each class acts as both a derived class and a base class for another class.
Question 13. When there is a combination of more than one type of inheritance, it is known as __________ inheritance.
(a) Single
(b) Multilevel
(c) Multiple
(d) Hybrid
Answer: (d) Hybrid
In simple words: If a program uses a mix of different inheritance types, like combining single and multiple inheritance, we call it hybrid inheritance. It's a combination of different patterns.
๐ฏ Exam Tip: Hybrid inheritance is a flexible way to model complex real-world relationships by using multiple inheritance patterns together.
Question 14. Hybrid inheritance may be a combination of __________ inheritance.
(a) Multilevel and Multiple
(b) Hierarchical and Multilevel
(c) Hierarchical, Multilevel and Multiple
(d) All of the options
Answer: (d) All of the options
In simple words: Hybrid inheritance means you can mix any of the basic inheritance types. It can be a combination of many different inheritance structures, making it very flexible.
๐ฏ Exam Tip: Hybrid inheritance is not a new type but a combination of existing types, offering design flexibility for various scenarios.
Question 15. The order of inheritance by derived class, to inherit the base class is __________
(a) Left to Right
(b) Right to Left
(c) Top to Bottom
(d) None of these
Answer: (a) Left to Right
In simple words: When a class inherits from several base classes, it processes them in the order they are listed, usually from left to right. This order affects how constructors are called.
๐ฏ Exam Tip: The order of inheritance (left to right) is important in languages like C++ as it determines the order of constructor calls and how ambiguities are resolved.
Question 16. In __________ inheritance the base classes do not have any relationship between them,
(a) Single
(b) Multilevel
(c) Hybrid
(d) Multiple
Answer: (d) Multiple
In simple words: In multiple inheritance, a class can have many parent classes, and these parent classes themselves do not need to be related to each other. They are independent sources of features.
๐ฏ Exam Tip: Multiple inheritance allows a derived class to combine unrelated functionalities, potentially leading to a "diamond problem" in some languages.
Question 17. In __________ inheritance a derived class itself acts as a base class to derive another class.
(a) Single
(b) Multilevel
(c) Multiple
(d) Multiple
Answer: (b) Multilevel
In simple words: In multilevel inheritance, a class that has inherited from one class then becomes a parent class itself, allowing another class to inherit from it. It's like a chain of inheritance.
๐ฏ Exam Tip: This "middle" class in multilevel inheritance demonstrates how inheritance can extend over several levels, passing features down the chain.
Question 18. __________ inheritance is similar to relation between grandfather, father and child,
(a) Single
(b) Multilevel
(c) Multiple
(d) Multiple
Answer: (b) Multilevel
In simple words: Multilevel inheritance is like a family tree where a child inherits from a father, and the father inherited from a grandfather. Each generation passes traits to the next.
๐ฏ Exam Tip: The grandfather-father-child analogy perfectly illustrates the concept of multilevel inheritance, where traits are passed down through a chain.
Question 19. A class without any declaration will have __________ byte size.
(a) 1
(b) 0
(b) 2
(d) 10
Answer: (a) 1
In simple words: Even an empty class needs a small amount of memory, usually 1 byte, to give each object a unique address. This helps identify different objects in memory.
๐ฏ Exam Tip: This minimum size ensures that every object, even one with no data members, can be uniquely identified and has a distinct memory location.
Question 20. class x{}; x occupies __________
(a) 1
(b) 0
(b) 2
(d) 10
Answer: (a) 1
In simple words: An object of an empty class, like 'x' from 'class x{};', still takes up 1 byte of memory. This is to make sure that each object has its own unique address.
๐ฏ Exam Tip: In C++, an empty class typically has a size of 1 byte to ensure that objects of the class have distinct memory addresses.
Question 21. In inheritance, which member of the base class will be acquired by the derived class is done by using __________
(a) Visibility modes
(b) Data members
(c) Member functions
(d) None of these
Answer: (a) Visibility modes
In simple words: Visibility modes, like public, private, or protected, control which parts of the base class can be accessed by the derived class. They decide what features are inherited.
๐ฏ Exam Tip: Visibility modes are crucial for encapsulation, defining the access rights of inherited members within the derived class.
Question 22. The accessibility of base class by the derived class is controlled by __________
(a) Visibility modes
(b) Data members
(c) Member functions
(d) None of these
Answer: (a) Visibility modes
In simple words: How much a child class can use or see from its parent class is decided by visibility modes (public, private, protected). These modes act like permissions.
๐ฏ Exam Tip: Understanding visibility modes is essential for controlling data encapsulation and defining the relationship between base and derived classes.
Question 23. __________ is a visibility modes.
(a) private
(b) public
(c) protected
(d) All of the options
Answer: (d) All of the options
In simple words: Private, public, and protected are all ways to control who can see or use parts of a class. They are the three main visibility modes in object-oriented programming.
๐ฏ Exam Tip: Always remember the three access specifiers: public, private, and protected, as they define the access levels for class members.
Question 24. The default visibility mode is __________
(a) private
(b) public
(c) protected
(d) All of the options
Answer: (a) private
In simple words: If you don't say whether members are public, private, or protected, they are automatically treated as private. This keeps data safe by default.
๐ฏ Exam Tip: Always specify the visibility mode explicitly to avoid relying on defaults, which can sometimes lead to unexpected behavior.
Question 25. When a base class is inherited with __________ visibility mode the public and protected members of the base class become 'private' members of the derived class.
(a) private
(b) public
(c) protected
(d) All of the options
Answer: (a) private
In simple words: If you inherit a class in private mode, all the public and protected things from the parent class become private in your new class. This means they cannot be accessed from outside the new class.
๐ฏ Exam Tip: Private inheritance means that all public and protected members of the base class become private in the derived class, limiting their external access.
Question 26. When a base class is inherited with __________ visibility mode the protected and public members of the base class become 'protected members' of the derived class,
(a) private
(b) public
(c) protected
(d) All of the options
Answer: (c) protected
In simple words: When a class inherits in protected mode, its parent's public and protected features become protected in the new class. This allows child classes to access them, but not outside code.
๐ฏ Exam Tip: Protected inheritance makes base class public and protected members accessible to derived classes, but they remain protected from external access.
Question 27. When a base class is inherited with __________ visibility mode, the protected members of the base class will be inherited as protected members of the derived class and the public members of the base class will be inherited as public members of the derived class.
(a) private
(b) public
(c) protected
(d) All of the options
Answer: (b) public
In simple words: If a class is inherited in public mode, its public features stay public, and its protected features stay protected in the new class. This keeps their original access levels.
๐ฏ Exam Tip: Public inheritance maintains the access levels of base class members in the derived class, providing a direct "is-a" relationship.
Question 28. When classes are inherited with __________ the private members of the base class are not inherited they are only visible.
(a) private
(b) public
(c) protected
(d) Either A or B or C
Answer: (d) Either A or B or C
In simple words: Private parts of a base class are never passed on to a child class, no matter which inheritance mode is used. They stay hidden within the base class.
๐ฏ Exam Tip: Private members are strictly encapsulated within their own class and cannot be directly inherited or accessed by derived classes.
Question 29. When classes are inherited with __________ the private members of the base class are continue to exist in derived classes, and cannot be accessed.
(a) private
(b) public
(c) protected
(d) Either A or B or C
Answer: (d) Either A or B or C
In simple words: Private data from a parent class still exists in memory when a child class is made, but the child class cannot directly see or use it. This applies to all inheritance types.
๐ฏ Exam Tip: Private members are part of the base class sub-object within the derived class, but their direct accessibility is blocked due to encapsulation.
Question 30. __________ inheritance should be used when you want the features of the base class to be available to the derived class but not to the classes that are derived from the derived class.
(a) private
(b) public
(c) protected
(d) Either A or B or C
Answer: (a) private
In simple words: Use private inheritance when you want a child class to use its parent's features, but you don't want those features to be passed down further to grandchildren classes. It stops the inheritance chain at the first child.
๐ฏ Exam Tip: Private inheritance implies an "implemented-in-terms-of" relationship rather than a direct "is-a" relationship, limiting further inheritance of public/protected members.
Question 31. __________ inheritance should be used when features of base class to be available only to the derived class members but not to the outside world.
(a) private
(b) public
(c) protected
(d) Either A or B or C
Answer: (c) protected
In simple words: If you want features of a parent class to be used only by its direct child classes, but not by any other part of the program, you should use protected inheritance. This keeps the features somewhat private but available for family members.
๐ฏ Exam Tip: Protected members are accessible within the class itself and by its derived classes, ensuring controlled access while allowing specialized behavior.
Question 32. __________ inheritance can be used when features of base class to be available the derived class members and also to the outside world.
(a) private
(b) public
(c) protected
(d) Either A or B or C
Answer: (b) public
In simple words: If you want the features of a base class to be openly available to both its child classes and to other parts of the program, you should use public inheritance. It makes everything accessible.
๐ฏ Exam Tip: Public inheritance is used when the derived class represents a specialized version of the base class and all public interfaces should remain public.
Question 33. When an object of the derived class is created, the compiler first call the __________ class constructor.
(a) Base
(b) Derived
(c) Either Base or Derived
(d) None of these
Answer: (a) Base
In simple words: When you make a new object of a child class, the computer always builds the parent class part first. So, the parent's constructor runs before the child's constructor.
๐ฏ Exam Tip: Constructors are called in the order of inheritance, from the most base class to the most derived class, ensuring proper initialization of all sub-objects.
Question 34. When the object of a derived class expires first the __________ class destructor is invoked.
(a) Base
(b) Derived
(c) Either Base or Derived
(d) None of these
Answer: (b) Derived
In simple words: When a child class object is no longer needed, the computer first cleans up the child part of the object. So, the child's destructor runs before the parent's destructor.
๐ฏ Exam Tip: Destructors are called in the reverse order of constructor invocation, from the most derived class to the most base class, to ensure proper cleanup.
Question 35. The __________ are executed in the order of inherited class.
(a) Constructors
(b) Destructors
(c) Either A or B
(d) None of these
Answer: (a) Constructors
In simple words: Constructors are special functions that set up classes. When you have inherited classes, the constructors always run starting from the oldest parent class, then down to the child classes.
๐ฏ Exam Tip: This "bottom-up" execution for constructors ensures that the foundational parts of an object are built before its more specific parts.
Question 36. The __________ are executed in the reverse order.
(a) Constructors
(b) Destructors
(c) Either A or B
(d) None of these
Answer: (b) Destructors
In simple words: Destructors are special functions that clean up classes. When classes are inherited, the destructors run from the youngest child class first, then move up to the oldest parent class.
๐ฏ Exam Tip: The "top-down" execution for destructors ensures that the specific resources of the derived class are released before the general resources of the base class.
Question 37. If there are multiple base classes, then it starts executing from the __________ base class.
(a) Leftmost
(b) Rightmost
(c) Compiler decided
(d) None of these
Answer: (a) Leftmost
In simple words: When a class inherits from many parents, the program starts creating parts of the class by looking at the first parent listed, and then moves to the next ones. This happens in order from left to right.
๐ฏ Exam Tip: The order of constructors for multiple base classes follows the order they are listed in the derived class's inheritance list, from left to right.
Question 38. __________ members of the base class can be indirectly accessed by the derived class using the public or protected member function of the base class.
(a) private
(b) public
(c) protected
(d) Either A or B or C
Answer: (a) private
In simple words: Private parts of a parent class cannot be directly seen by a child class. But the child class can still use public or protected functions from the parent class, and those functions might use the private data internally.
๐ฏ Exam Tip: This concept highlights encapsulation; private members are protected but can be manipulated through public or protected interfaces of the same class.
Question 39. __________ member function has the access privilege for the private members of the base class.
(a) public
(b) protected
(c) Both A and B
(d) None of these
Answer: (c) Both A and B
In simple words: Both public and protected functions within the parent class itself can touch its own private data. This means child classes can use these functions to indirectly work with the parent's private data.
๐ฏ Exam Tip: Only member functions (public or protected) and friend functions of a class can directly access its private members, upholding data hiding principles.
Question 40. __________ functions can access the private members.
(a) Member
(b) Non-member
(c) Destructor
(d) None of these
Answer: (a) Member
In simple words: Only functions that are part of a class (its "member functions") can directly see and use that class's private data. This keeps the private data safe from outside changes.
๐ฏ Exam Tip: This is a core concept of encapsulation: data is kept private, and access is granted only through defined member functions.
Question 41. In case of inheritance there are situations where the member function of the base class and derived classes have the same name. The __________ operator resolves this problem.
(a) Conditional
(b) Membership
(c) Scope resolution
(d) None of these
Answer: (c) Scope resolution
In simple words: When a child class has a function with the same name as its parent class, the scope resolution operator (::) helps you choose which one to use. It tells the computer exactly which function you mean.
๐ฏ Exam Tip: The scope resolution operator (::) allows you to specify which class's member you are referring to, preventing ambiguity when names overlap.
Question 42. When a derived class member function has the same name as that of its base class member function, the derived class member function __________ the base class's inherited function.
(a) Shadows
(b) Hides
(c) Either A or B
(d) None of these
Answer: (c) Either A or B
In simple words: If a child class creates a function with the same name as one in its parent class, the child's function "covers up" or "hides" the parent's function. This is known as shadowing or hiding.
๐ฏ Exam Tip: Function hiding occurs when a derived class defines a function with the same name as a base class function, even if the signatures are different.
Question 43. When a derived class member function has the same name as that of its base class member function, the derived class member function shadows/hides the base class's inherited function is called function __________
(a) Overriding
(b) Overloading
(c) Shadowing
(d) Either A or C
Answer: (d) Either A or C
In simple words: When a child class makes a function with the exact same name and inputs as a parent class function, it's called "overriding." If the names are the same but inputs are different, it's sometimes called "shadowing" or "hiding" the parent's function.
๐ฏ Exam Tip: Function overriding applies when the derived class redefines a virtual function with the same signature as a base class function. Function hiding (shadowing) applies when a non-virtual function is redefined or a function with a different signature is defined.
Question 44. __________ pointer is a constant pointer that holds the memory address of the current object.
(a) this
(b) void
(c) new
(d) None of these
Answer: (a) this
In simple words: The 'this' pointer is a special pointer inside every object that always points to that object itself. It helps the object refer to its own parts, even if there are other variables with the same name.
๐ฏ Exam Tip: The 'this' pointer is an implicit parameter to all non-static member functions, providing a way for an object to refer to itself.
Question 45. __________ pointer is useful when the argument variable name in the member function and the data member name are same.
(a) this
(b) void
(c) new
(d) None of these
Answer: (a) this
In simple words: When a function's input name is the same as an object's internal data name, the 'this' pointer helps avoid confusion. It tells the computer if you mean the input or the object's own data.
๐ฏ Exam Tip: Using 'this->dataMember' is a clear way to differentiate between a member variable and a local parameter with the same name, ensuring the correct variable is modified.
Very Short Answers (2 Marks)
Question 1. Write a short note on hierarchical inheritance.
Answer: Hierarchical inheritance happens when several child classes are created from just one parent class. It's like one main family tree that branches out into many separate children, all sharing the same root. For example, a "Vehicle" base class could have "Car", "Bike", and "Truck" as derived classes.
In simple words: When one main class has many child classes that all come from it, this is called hierarchical inheritance.
๐ฏ Exam Tip: Visualizing hierarchical inheritance as a single-parent, multiple-child relationship (like a tree branching out) helps in understanding its structure.
Question 2. What are the types of inheritance?
Answer: There are several types of inheritance that define how classes can share features with each other. These include Single Inheritance (one parent, one child), Multiple Inheritance (many parents, one child), Multilevel Inheritance (a chain of parent-child relationships), Hybrid Inheritance (a mix of different types), and Hierarchical Inheritance (one parent, many children). Each type helps to organize code in a specific way for reusability.
In simple words: The main types of inheritance are Single, Multiple, Multilevel, Hybrid, and Hierarchical.
๐ฏ Exam Tip: Listing all five main types is crucial for a complete answer; briefly describe each type to show full understanding.
Question 3. Give the syntax of deriving a class.
Answer: To create a new class (derived class) from an existing class (base class), you follow a specific syntax: class derived_class_name : visibility_mode base_class_name
Here, derived_class_name is the name of your new class, visibility_mode tells how the base class features will be accessed (like public, private, or protected), and base_class_name is the name of the class you are inheriting from. Inside the curly braces, you define the new members for the derived class. This structure clearly shows the relationship between the new and existing classes.
In simple words: The basic way to write a derived class is: class new_class : mode old_class { ... };.
๐ฏ Exam Tip: Include the colon (:) and the visibility mode (e.g., public, private, protected) as they are essential parts of the derivation syntax.
Question 4. Write note on this pointer.
Answer: The 'this' pointer is a special constant pointer in C++ that automatically points to the object on which a member function is currently being called. It helps an object to refer to itself. For example, if you have a variable named 'x' inside a class, and a function also takes an input 'x', you can use 'this->x' to clearly mean the class's own 'x' variable. This helps in telling the difference between a class's data member and a local variable or parameter that has the same name.
In simple words: The 'this' pointer is a hidden pointer that helps an object refer to its own data and functions, especially when names are similar.
๐ฏ Exam Tip: Emphasize that 'this' is a constant pointer to the current object and is implicitly used in member functions.
Short Answers (3 Marks)
Question 1. What are inheritance and access control?
Answer: Inheritance is a key feature in object-oriented programming that allows a new class (derived class) to take on properties and behaviors from an existing class (base class). This helps in code reuse and building new classes upon established ones.
Access control, using visibility modes like public, private, and protected, determines how members of the base class are exposed in the derived class. For instance, private members of a base class are never directly inherited, while public members can be inherited with various levels of access depending on the chosen visibility mode. This control ensures data security and proper object interaction.
In simple words: Inheritance lets new classes use features from old classes, and access control rules (like public or private) decide which features can be used and by whom.
๐ฏ Exam Tip: Clearly define both inheritance and access control, then explain how access specifiers (public, private, protected) work together to regulate inherited member visibility.
Question 1. What are inheritance and access control?
Answer: When you create a new class from an existing one (derived class), you can specify how the derived class accesses the original class's (base class) members. This is called a visibility mode. This mode does not change how individual members of the base class work internally, but it limits how the derived class can use them.
Classes can be created using three main visibility modes:
1. In a public base class, public and protected members stay public and protected in the derived class.
2. In a protected base class, public and protected members become protected in the derived class.
3. In a private base class, public and protected members become private in the derived class.
4. In all these cases, private members of the base class always stay private and cannot be used by the derived class directly. However, they can sometimes be accessed indirectly through public or protected member functions of the base class, which have the right access.
In simple words: Inheritance lets new classes use features of older classes. Access control decides *how* these features are used by the new class, like if they remain public, become protected, or turn private. Private members are never directly inherited.
๐ฏ Exam Tip: Remember that private members of a base class are never directly inherited, only public and protected members are, and their visibility changes based on the inheritance mode.
Question 2. Write a program for the working of constructors and destructors under inheritance.
Answer:
#include<iostream>
using namespace std;
class base
{
public:
base()
{
cout<<"\nConstructor of base class...";
}
~base()
{
cout<<"\nDestructor of base class....";
}
};
class derived:public base
{
public:
derived()
{
cout << "\nConstructor of derived ...";
}
~derived()
{
cout << "\nDestructor of derived...";
}
};
class derived1 : public derived
{
public:
derived1()
{
cout << "\nConstructor of derived! //, ...";
}
~derived1()
{
cout << "\nDestructor of derived ...";
}
};
int main()
{
derived1 x;
return 0;
}
Output:
Constructor of base class...
Constructor of derived ...
Constructor of derived ...
Destructor of derived ...
Destructor of derived ...
Destructor of base class....
In simple words: This program shows that when a derived class object is created, the base class constructors are called first, followed by derived class constructors in order. When the object is destroyed, destructors are called in the reverse order.
๐ฏ Exam Tip: Always observe the order of constructor and destructor calls in inheritance: constructors run from base to derived, while destructors run from derived to base.
Question 3. What about access control in a publicly derived class?
Answer: In a publicly derived class, the public and protected members of the base class will remain public and protected respectively in the derived class. This means the public members of the derived class can be accessed by an object of the derived class just like its own members.
In simple words: When a class inherits publicly, the base class's public parts stay public, and its protected parts stay protected in the new class. This lets you access them easily through the new class.
๐ฏ Exam Tip: Public inheritance maintains the same access level for public and protected members from the base class in the derived class, which is crucial for maintaining functionality.
Question 4. What about access control in the privately derived class?
Answer: In a privately derived class, both the public and protected members of the base class become private members of the derived class. This makes it impossible to directly access these inherited members using an object of the derived class. Instead, you need to call them through other publicly defined member functions within the derived class.
In simple words: When a class inherits privately, all public and protected parts from the base class become private in the new class. You cannot directly use them from outside; you must use other public functions within the new class to reach them.
๐ฏ Exam Tip: Private inheritance is useful for implementing "has-a" relationships where the derived class reuses base class functionality internally without exposing it externally.
Question 1. Write a program to implement single Inheritance.
Answer:
#include <iostream>
using namespace std;
class student //base class
{
private:
char name[20];
int rno;
public:
void acceptname()
{
cout<<"\n Enter roll no and name ..";
cin>>rno>>name;
}
void displayname()
{
cout<<"\n Roll no :-" <<rno;
cout<<"\n Name :-"<<name<<endl;
}
};
class exam : public student
//derived class with single base class
{
public:
int mark1, mark2,mark3,mark4,mark5, mark6, total;
void acceptmark()
{
cout<<"\n Enter lang, eng, phy, che, esc, mat marks..";
cin>>mark1>>mark2>>mark3>>
mark4>>mark5>>mark6;
}
void displaymark()
{
cout<<"\n\t\t, Marks Obtained
cout<<"\n Language.. "<<mark1;
cout<<"\n English .. "<<mark2;
cout<<"\n Physics .. "<<mark3;
cout<<"\n Chemistry.. "<<mark4;
cout<<"\n Comp.sci.. "<<mark5;
cout<<"\n Maths .. "<<mark6;
}
};
int main()
{
exam e1;
//calling base class function using derived
//class object
e1.acceptname();
e1.acceptmark();
e1.displayname();
//calling base class function using derived
//class object
e1.displaymark();
return 0;
}
Output:
Enter roll no and name .. 1201
KANNAN
Enter lang,eng,phy,che,esc,mat
marks.. 100 100 100 100 100 100
Roll no:-1201
Name:-KANNAN
Marks Obtained
Language.. 100
English .. 100
Physics .. 100
Chemistry.. 100
Comp.sci.. 100
Maths .. 100
In simple words: This program demonstrates single inheritance, where the `exam` class gets all the features of the `student` class. This allows `exam` objects to use functions like `acceptname` and `displayname` from `student`, along with its own mark-related functions.
๐ฏ Exam Tip: For single inheritance, clearly define one base class and one derived class, showing how the derived class reuses functions from its single parent.
Question 2. Write a program to implement multiple inheritance.
Answer:
#include <iostream>
using namespace std;
class student //base class
{
private:
char name[20];
int rno;
public:
void acceptname()
{
cout<<"\n Enter roll no and name.. ";
cin>>rno>>name;
}
void displayname()
{
cout<<"\n Roll no :-"<<rno;
cout<<"\n Name:-" << name << endl;
}
};
class detail //Base class
{
public:
int dd,mm,yy;
char cl[4];
void acceptdob()
{
cout<<"\n Enter date,month,year in digits and class ..";
cin>>dd>>mm>>yy>>cl;
}
void displaydob()
{
cout<<"\n class:-"<<cl;
cout<<"\t\t DOB : "<<dd<<" - "<<mm<<"-"<<yy<<endl;
}
};
//derived class with multiple base class
class exam: public student, public detail
{
public:
int mark1, mark2,mark3,mark4,mark5, mark6, total;
void acceptmark()
{
cout<<"\n Enter lang, eng, phy, che, esc, mat marks..";
cin>>mark1>>mark2>>mark3>> mark4>>mark5>>mark6;
}
void displaymark()
{
cout<<"\n\t\t Marks Obtained
cout<<"\n Language.. "<<mark1;
cout<<"\n English .. "<<mark2;
cout<<"\n Physics .. "<<mark3;
cout<<"\n Chemistry.. "<<mark4;
cout<<"\n Comp.sci.. "<<mark5;
cout<<"\n Maths .. "<<mark6;
}
};
int main()
{
exam e1;
//calling base class function using derived
//class object
e1.acceptname();
//calling base class function using derived
//class object
e1.acceptdob();
e1.acceptmark();
//calling base class function using derived
//class object
e1.displayname();
//calling base class function using derived
//class object
e1.displaydob();
e1.displaymark();
return 0;
}
Output:
Enter roll no and name .. 1201 MEENA
Enter date, month, year in digits and
class .. 7 12 2001 XII
Enter lang, eng, phy, che, esc, mat
marks.. 96 98 100 100 100 100
Roll no:-1201
Name MEENA
class:-XII
DOB : 7-12-2001
Marks Obtained
Language..96
English .. 98
Physics .. 100
Chemistry.. 100
Comp.sci.. 100
Maths .. 100
In simple words: This program shows multiple inheritance. The `exam` class inherits features from two parent classes, `student` and `detail`. This means an `exam` object can use functions from both `student` (like name and roll number) and `detail` (like date of birth and class details), plus its own mark-related functions.
๐ฏ Exam Tip: When implementing multiple inheritance, ensure that there are no name clashes between members of different base classes, or resolve them using scope resolution operator if necessary.
Question 3. Write a program to implement multilevel inheritance.
Answer:
#include <iostream>
using namespace std;
class student //base class
{
private:
char name[20];
int rno;
public:
void acceptname()
{
cout<<"\n Enter roll no and name.. ";
cin>>rno>>name;
}
void displayname()
{
cout<<"\n Roll no :-"<<rno;
cout<<"\n Name << name <<
endl;
}
};
//derived class with single base class class
class exam: public student
{
public:
int mark1, mark2, mark3, mark4, mark5, mark6;
void acceptmark()
{
cout<<"\n Enter lang,eng,phy,che,esc,mat marks..";
cin>>mark1>>mark2>>mark3>>
mark4>>mark5>>mark6;
}
void displaymark()
{
cout<<"\n\t\t Marks Obtained
cout<<"\n Language... "<<mark1;
cout<<"\n English... "<<mark2;
cout<<"\n Physics... "<<mark3;
cout<<"\n Chemistry... "<<mark4;
cout<<"\n Comp.sci... "<<mark5;
cout<<"\n Maths... "<<mark6;
}
};
class result: public exam
{
public:
int total;
void showresult()
{
total=mark1+mark2+mark3+mark4+mark5+mark6;
cout<<"\nTOTAL MARK SCORED : "<<total;
}
};
int main()
{
result r1;
//calling base class function using derived
//class object
r1.acceptname();
//calling base class function which itself is a derived
r1.acceptmark();
// class function using its derived class object
r1.displayname();
//calling base class function
//using derived class //object
//calling base class function which itself is a derived
r1.displaymark();
//class function using its derived class object
r1.showresult();
//calling the child class function
return 0;
}
Output:
Enter roll no and name .. SARATHI
Enter lang,eng,phy,che,csc,mat
marks.. 96 98 100 100 100 100
Roll no:-1201
Name:-SARATHI
Marks Obtained
Language... 96
English... 98
Physics... 100
Chemistry... 100
Comp.sci... 100
Maths... 100
TOTAL MARK SCORED: 594
In simple words: This program illustrates multilevel inheritance, where `result` inherits from `exam`, and `exam` in turn inherits from `student`. This creates a chain of inheritance, allowing the `result` class to access functions and data members from both `exam` and `student` classes.
๐ฏ Exam Tip: In multilevel inheritance, the grandchild class inherits traits from both its direct parent and its grandparent, creating a clear hierarchy.
Question 4. Write a program to implement hierarchical inheritance.
Answer:
#include <iostream>
using namespace std;
class student //base class
{
private:
char name[20];
int rno;
public:
void acceptname()
{
cout<<"\n Enter roll no and name..";
cin>>rno>>name;
}
void displayname()
{
cout<<"\n Roll no :-"<<rno;
cout<<"\n Name :-"<<name<<
endl;
}
};
//derived class with single base class
class qexam: public student
{
public:
int mark1, mark2, mark3, mark4, mark5, mark6;
void acceptmark()
{
cout<<"\n Enter lang, eng, phy, che, esc, mat marks for quarterly exam";
cin>>mark1>>mark2>>mark3>>mark4>>mark5>>mark6;
}
void displaymark()
{
cout<<"\n\t\t Marks Obtained in quarterly";
cout<<"\n Language.. "<<mark1;
cout<<"\n English .. "<<mark2;
cout<<"\n Physics .. "<<mark3;
cout<<"\n Chemistry.. "<<mark4;
cout<<"\n Comp.sci.. "<<mark5;
cout<<"\n Maths .. "<<mark6;
}
};
//derived class with single base class
class hexam : public student
{
public:
int mark1, mark2, mark3, mark4, mark5, mark6;
void acceptmark()
{
cout<<"\n Enter lang, eng, phy, che, esc, mat marks for half/early exam..";
cin>>mark1>>mark2>>mark3>>mark4>>mark5>>mark6;
}
void displaymark()
{
cout<<"\n\t\t Marks Obtained in Halfyearly";
cout<<"\n Language., "<< mark1;
cout<<"\n English .. "<< mark2;
cout<<"\n Physics .. "<< mark3;
cout<<"\n Chemistry.. "<< mark4;
cout<<"\n Comp.sci.. "<< mark5;
cout<<"\n Maths .. "<< mark6;
}
};
int main()
{
qexam q1;
hexam h1;
//calling base class function using derived class object
q1.acceptname();
//calling base class function
q1.acceptmark();
//calling base class function using derived class object
h1.displayname();
h1.acceptmark();
//calling base class- function using its // derived class object
h1.displaymark();
return 0;
}
Output:
Enter roll no and name .. 1201
KANNAN
Enter lang,eng,phy,che,esc,mat
marks for quarterly exam. .
95 96 100 98 100 99
Roll no :-1201
Name :-KANNAN
Marks Obtained in quarterly
Language.. 95
English .. 96
Physics .. 100
Chemistry.. 98
Comp.sci.. 100
Maths .. 99
Enter roll no and name .. 1201
KANNAN
Enter lang,eng,phy,che,esc, mat marks for the half-yearly exam.
96 98 100 100 100 100
In simple words: This program shows hierarchical inheritance, where a single base class (`student`) is inherited by multiple derived classes (`qexam` and `hexam`). This allows `qexam` and `hexam` to share common features from `student` while having their own unique functionalities.
๐ฏ Exam Tip: In hierarchical inheritance, one parent class is shared by many child classes, making it efficient for common features that diverge into different specializations.
Question 5. Write a program to implement hybrid inheritance.
Answer:
#include <iostream>
using namespace std;
class student //base class
{
private:
char name[20];
int rno;
public:
void acceptname()
{
cout<<"\n Enter roll no and name.. ";
cin>>rno>>name;
}
void displayname()
{
cout<<"\n Roll no :-"<<rno;
cout<<"\n Name << name <<
endl;
}
};
//derived class with the single base class
class exam: public student
{
public:
int mark1, mark2, mark3, mark4, mark5, mark6;
void acceptmark()
{
cout<<"\n Enter larig, eng, phy, che, esc,mat marks..";
cin>>mark1>>mark2>>mark3>> mark4>>mark5>>mark6;
}
void displaymark()
{
cout<<"\n\t\t Marks Obtained ";
cout<<"\n Language.. "<<mark1;
cout<<"\n English .. "<<mark2;
cout<<"\n Physics .. "<<mark3;
cout<<"\n Chemistry.. "<<mark4;
cout<<"\n Comp.sci.. "<<mark5;
cout<<"\n Maths .. "<<mark6;
}
};
class detail //base classs 2
{
public:
int dd,mm,yy;
char cl[4];
void acceptdob()
{
cout<<"\n Enter date,month,year in digits and class ..";
cin>>dd>>mm>>yy>>cl;
}
void displaydob()
{
cout<<"\n class :-"<<cl;
cout<<"\t\t DOB : "<<dd<<" - "<<mm<<"-"<<yy<<endl;
}
};
//inherits from the exam, which itself is a //derived
//class and also from class detail
class result: public exam, public detail
{
public:
int total;
void showresuit()
{
total=mark1+mark2+mark3+mark4+mark5+mark6;
cout<<"\nTOTAL MARK SCORED: "<<total;
}
};
int main()
{
result r1;
//calling base class function using derived class object
r1.acceptname();
//calling base class which itsel is a derived class function using its derived class
//object
r1.acceptmark();
r1.acceptdob();
cout<<"\n\n\t\t MARKS STATEMENT"; //calling base class function using derived
//class object
r1.displayname();
r1.displaydob();
//calling the child class function
r1.displaymark();
r1.showresuit();
return 0;
}
Output:
Enter roll no and name .. 1201 RAGU
Enter lang,eng,phy,che,esc,mat
marks.. 96 98 100 100 100 100
Enter date,month, year in digits and class .. 7 12 2001 XII
MARKS STATEMENT
Roll no :-1201
Name :-RAGU
class : -XII
DOB : 7 - 12 -2001
Marks Obtained
Language..96
English .. 98
Physics .. 100
Chemistry.. 100
Comp.sci.. 100
Maths .. 100
TOTAL MARK SCORED: 594
In simple words: This program shows hybrid inheritance, combining different types of inheritance. Here, `exam` is derived from `student` (single inheritance), and then `result` inherits from both `exam` and `detail` (multiple inheritance). This allows the `result` class to combine traits from various parent classes, creating a flexible class structure.
๐ฏ Exam Tip: Hybrid inheritance often combines single, multiple, or multilevel inheritance to achieve complex class relationships, requiring careful design to avoid ambiguity and ensure correct functionality.
Question 5. Write a program to implement hybrid inheritance.
Answer: This program demonstrates hybrid inheritance in C++. It sets up classes for student details, exam marks, and other personal information. These are then combined into a 'result' class, showcasing a blend of multilevel and multiple inheritance to manage diverse data. This approach allows developers to reuse code efficiently and create complex class relationships for flexible system design.
Program:
#include <iostream>
using namespace std;
class student //base class
{
private:
char name[20];
int rno;
public:
void acceptname()
{
cout<<"\n Enter roll no and name ..";
cin>>rno>>name;
}
void displayname()
{
cout<<"\n Roll no :-"<<rno;
cout<<"\n Name :-"<<name<<endl;
}
};
//derived class with the single base class
class exam: public student
{
public:
int mark1, mark2, mark3, mark4, marks, mark6, total;
void acceptmark()
{
cout<<"\n Enter lang, eng, phy, che, esc,mat marks..";
cin>>mark1>>mark2>>mark3>>mark4>>mark5>>mark6;
}
void displaymark()
{
cout<<"\n\t\t Marks Obtained ";
cout<<"\n Language.. "<<mark1;
cout<<"\n English .. "<<mark2;
cout<<"\n Physics .. "<<mark3;
cout<<"\n Chemistry.. "<<mark4;
cout<<"\n Comp.sci.. "<<mark5;
cout<< "\n Maths .. "<<mark6;
}
};
class detail //base classs 2
{
public:
int dd,mm,yy;
char cl[4];
void acceptdob()
{
cout<<"\n Enter date,month,year in digits and class ..";
cin>>dd>>mm>>yy>>cl;
}
void displaydob()
{
cout<<"\n class :-"<<cl;
cout<<"\t\t DOB : "<<dd<<" - "<<mm<<"-"<<yy<<endl;
}
};
//inherits from the exam, which itself is a //derived
//class and also from class detail
class result: public exam, public detail
{
public:
void showresuit()
{
total=mark1+mark2+mark3+mark4+mark5+mark6;
cout<<"\nTOTAL MARK SCORED: "<<total;
}
};
int main()
{
result r1;
//calling base class function using derived class object
r1.acceptname();
//calling base class which itself is a derived class function using its derived class
//object
r1.acceptmark();
r1.acceptdob();
cout<<"\n\n\t\t MARKS STATEMENT"; //calling base class function using derived
//class object
r1.displayname();
r1.displaydob();
//calling base class which itself is a derived class function using its derived class
//object
r1.displaymark();
//calling the child class function
r1.showresuit();
return 0;
}
Output:
Enter roll no and name .. 1201 RAGU
Enter lang,eng,phy,che,esc,mat marks.. 96 98 100 100 100 100
Enter date,month, year in digits and class .. 7 12 2001 XII
MARKS STATEMENT
Roll no :-1201
Name :-RAGU
class : -XII
DOB : 7-12-2001
Marks Obtained
Language..96
English .. 98
Physics .. 100
Chemistry.. 100
Comp.sci.. 100
Maths .. 100
TOTAL MARK SCORED: 594
In simple words: This program shows how to mix different types of class inheritance. It creates a main program that takes in student details, exam marks, and other information, then uses these details to show a final mark sheet.
๐ฏ Exam Tip: When writing programs for inheritance, clearly define base and derived classes, and pay attention to visibility modes to control access to members.
Free study material for Computer Science
TN Board Solutions Class 11 Computer Science Chapter 16 Inheritance
Students can now access the TN Board Solutions for Chapter 16 Inheritance prepared by teachers on our website. These solutions cover all questions in exercise in your Class 11 Computer Science textbook. Each answer is updated based on the current academic session as per the latest TN Board syllabus.
Detailed Explanations for Chapter 16 Inheritance
Our expert teachers have provided step-by-step explanations for all the difficult questions in the Class 11 Computer Science chapter. Along with the final answers, we have also explained the concept behind it to help you build stronger understanding of each topic. This will be really helpful for Class 11 students who want to understand both theoretical and practical questions. By studying these TN Board Questions and Answers your basic concepts will improve a lot.
Benefits of using Computer Science Class 11 Solved Papers
Using our Computer Science solutions regularly students will be able to improve their logical thinking and problem-solving speed. These Class 11 solutions are a guide for self-study and homework assistance. Along with the chapter-wise solutions, you should also refer to our Revision Notes and Sample Papers for Chapter 16 Inheritance to get a complete preparation experience.
FAQs
The complete and updated Samacheer Kalvi Class 11 Computer Science Solutions Chapter 16 Inheritance is available for free on StudiesToday.com. These solutions for Class 11 Computer Science are as per latest TN Board curriculum.
Yes, our experts have revised the Samacheer Kalvi Class 11 Computer Science Solutions Chapter 16 Inheritance as per 2026 exam pattern. All textbook exercises have been solved and have added explanation about how the Computer Science concepts are applied in case-study and assertion-reasoning questions.
Toppers recommend using TN Board language because TN Board marking schemes are strictly based on textbook definitions. Our Samacheer Kalvi Class 11 Computer Science Solutions Chapter 16 Inheritance will help students to get full marks in the theory paper.
Yes, we provide bilingual support for Class 11 Computer Science. You can access Samacheer Kalvi Class 11 Computer Science Solutions Chapter 16 Inheritance in both English and Hindi medium.
Yes, you can download the entire Samacheer Kalvi Class 11 Computer Science Solutions Chapter 16 Inheritance in printable PDF format for offline study on any device.