Tamilnadu State Board New Syllabus Samacheer Kalvi 11th Computer Science Guide Pdf Chapter 16 Inheritance Text Book Back Questions and Answers, Notes.

Tamilnadu Samacheer Kalvi 11th Computer Science Solutions Chapter 16 Inheritance

11th Computer Science Guide Inheritance Text Book Questions and Answers

Samacheer Kalvi 11th Computer Science Guide Chapter 16 Inheritance

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

Question 2.
Which of the following derives a dass 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

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

Samacheer Kalvi 11th Computer Science Guide Chapter 16 Inheritance

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 dass?
a) Private
b) Public
c) Protected
d) All of these
Answer:
a) Private

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

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

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

Samacheer Kalvi 11th Computer Science Guide Chapter 16 Inheritance

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
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 members of base class 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

Question 9.
Based on the following dass decoration answer the questions (from 9.1 o 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(); >;
};

Samacheer Kalvi 11th Computer Science Guide Chapter 16 Inheritance

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

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

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()

Samacheer Kalvi 11th Computer Science Guide Chapter 16 Inheritance

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

Question 10.
class x
{
int a;
public :
x()
{}
};
class y
{
x x1;
public:
y()
{}
};
class z : public y,x
{
int b;
public:
z()
{}
}z1;

Samacheer Kalvi 11th Computer Science Guide Chapter 16 Inheritance

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

Samacheer Kalvi 11th Computer Science Guide Chapter 16 Inheritance

Part – II

Very Short Answers

Question 1.
What is inheritance?
Answer:
Inheritance is one of the most important features of Object-Oriented Programming. In object-oriented programming, inheritance enables a new class and its objects to take on the properties of the existing classes.

Question 2.
What is a base class?
Answer:
The class to be inherited is called a base class or parent class.

Question 3.
Why derived class is called a power-packed class?
Answer:

  • Multilevel Inheritance: In multilevel inheritance, the constructors will be executed in the order of inheritance.
  • Multiple Inheritance: If there are multiple base classes, then it starts executing from the leftmost base class.

Question 4.
In what multilevel and multiple inheritances differ though both contains many base class?
Answer:
In case of multiple inheritance derived class have more than one base classes (More than one parent). But in multilevel inheritance derived class have only one base class (Only one parent).

Samacheer Kalvi 11th Computer Science Guide Chapter 16 Inheritance

Question 5.
What is the difference between public and private visibility mode?
Answer:
Private visibility mode:
When a base class is inherited with private visibility mode the public and protected members of the base class become ‘private’ members of the derived class.

Public visibility mode:
When a base class is inherited with public 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.

Samacheer Kalvi 11th Computer Science Guide Chapter 16 Inheritance

Part – III

Short Answers

Question 1.
What are the points to be noted while deriving a new class?
Answer:

The following points should be observed for defining the derived class:

  1. The keyword class has to be used.
  2. The name of the derived class is to be given after the keyword class.
  3. A single colon.
  4. The type of derivation (the visibility mode), namely private, public or protected. If no visibility mode is specified, then by default the visibility mode is considered private.
  5. The names of all base classes (parent classes) separated by a comma.

class derivedclass_name :visibility_mode
base_class_name
{
// members of derived class
};

Samacheer Kalvi 11th Computer Science Guide Chapter 16 Inheritance

Question 2.
What is differences between the members present in the private visibility mode and the members present in the public visibility mode?
Answer:
Private visibility mode:
When a base class is inherited with private visibility mode the public and protected members of the base class become ‘private’ members of the derived class.
Samacheer Kalvi 11th Computer Science Guide Chapter 16 Inheritance 1
Private visibility members can not be inherited further. So, it can not be directly accessed by its derived classes.
Public visibility mode:
When a base class is inherited with public 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.
Samacheer Kalvi 11th Computer Science Guide Chapter 16 Inheritance 2
Public visibility members can be inherited by its child and can access in it.

Samacheer Kalvi 11th Computer Science Guide Chapter 16 Inheritance

Question 3.
What is the difference between polymorphism and inheritance though are used for the reusability of code?
Answer:
Polymorphism:

  • Reusability of code is implemented through functions (or) methods.
  • Polymorphism is the ability of a function to respond differently to different messages.
  • Polymorphism is achieved through overloading.

Inheritance:

  • Reusability of code is implemented through classes.
  • Inheritance is the process of creating derived classes from the base class or classes.
  • Inheritance is achieved by various types of inheritances namely single, multiple, multilevel, hybrid and hierarchical inheritances.

Samacheer Kalvi 11th Computer Science Guide Chapter 16 Inheritance

Question 4.
What do you mean by overriding?
Answer:
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. This situation is called function overriding and this can be resolved by giving the base class name followed by :: and the member function name.

Question 5.
Write some facts about the execution of constructors and destructors in inheritance.
Answer:

  1. Base class constructors are executed first, before the derived class constructors execution.
  2. Derived class cannot inherit the base class constructor but it can call the base class constructor by using Base_class name: :base_class_constructor() in the derived class definition
  3. If there are multiple base classes, then it starts executing from the leftmost base class
  4. In multilevel inheritance, the constructors will be executed in the order of inheritance The destructors are executed in the reverse order of inheritance.

IV. Explain In Brief (Five Marks)

Question 1.
Explain the different types of inheritance.
Answer:
Types of Inheritance;
There are different types of inheritance viz., Single Inheritance, Multiple inheritance, Multilevel inheritance, hybrid inheritance and hierarchical inheritance.

1. Single Inheritance:
When a derived class inherits only from one base class, it is known as single inheritance.
Samacheer Kalvi 11th Computer Science Guide Chapter 16 Inheritance 3

2. Multiple Inheritance;
When a derived class inherits from multiple base classes it is known as multiple inheritance.
Samacheer Kalvi 11th Computer Science Guide Chapter 16 Inheritance 4

3. Hierarchical inheritance:
When more than one derived classes are created from & single base class known as Hierarchical inheritance.
Samacheer Kalvi 11th Computer Science Guide Chapter 16 Inheritance 5

4. Multilevel Inheritance
The transitive nature of inheritance is itself reflected by this form of inheritance. When a class is derived from a class which is a derived class – then it is referred to as multilevel inheritance.
Samacheer Kalvi 11th Computer Science Guide Chapter 16 Inheritance 6

5. Hybrid inheritance:
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.
Samacheer Kalvi 11th Computer Science Guide Chapter 16 Inheritance 7

Samacheer Kalvi 11th Computer Science Guide Chapter 16 Inheritance

Question 2.
Explain the different visibility modes through pictorial representation.
Answer:
Private visibility mode:
When a base class is inherited with private visibility mode the public and protected members of the base class become ‘private’ members of the derived class.
Samacheer Kalvi 11th Computer Science Guide Chapter 16 Inheritance 8

Protected visibility mode:
When a base class is inherited with protected visibility mode the protected and public members of the base class become ‘protected members’ of the derived class.
Samacheer Kalvi 11th Computer Science Guide Chapter 16 Inheritance 9

Public visibility mode:
When a base class is inherited with public 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.
Samacheer Kalvi 11th Computer Science Guide Chapter 16 Inheritance 10

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();
};

Samacheer Kalvi 11th Computer Science Guide Chapter 16 Inheritance

Answer The Following Questions Based On The Above Given Program:

3.1. Which type of Inheritance is shown in the program?
3.2. Specify the visibility mode of base classes.
3.3 Give the sequence of Constructor/Destructor Invocation when object of class author is created.
3.4. Name the base class(/es) and derived class (/es).
3.5 Give number of bytes to be occupied by the object of the following class:
(a) publisher
(b) branch
(c) author
3.6. Write the names of data members accessible from the object of class author.
3.7. Write the names of all member functions accessible from the object of class author.
3.8 Write the names of all members accessible from member functions of class author.
Answer:
3.1 Multiple Inheritance
3.2 public
3.3 Constructors branch, publisher and author are executed.
Destructors author, publisher and branch will be executed.
3.4 Base classes : branch and publisher Derived class : author
3.5 a) publisher class object requires 93 bytes
b) branch class object requires 64 bytes
c) author class object requires 181 bytes

Samacheer Kalvi 11th Computer Science Guide Chapter 16 Inheritance

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 pentry();
void Pdisplay();
};
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?
4.2. Specify the visibility mode of base classes.
4.3 Give the sequence of Constructor/Destructor Invocation when object of class Result is created.
4.4. Name the base class(/es) and derived class (/es).
4.5 Give number of bytes to be occupied by the object of the following class:
(a) Personal
(b) Marks
(c) Result
4.6. Write the names of data members accessible from die object of class Result.
4.7. Write the names of all member functions accessible from the object of class Result.
4.8 Write the names of all members accessible from member functions of class Result.
Answer:
4.1 Multilevel Inheritance
4.2 For Marks class – private visibility
For Result class – public visibility
4.3 Constructors Personal, Marks and Result be executed.
Destructors Result, Marks and Personal will be executed.
4.4 Base classes : Personal and Marks
Derived classes : Marks and Result

4.5 a) Personal class object requires 28 bytes (using Dev C++)
b) Marks class object requires 53 bytes (using Dev C++)
c) Result class requires 82 bytes (using Dev C++)

4.6 Data members FinalGrade, Commence(Own class members) alone can be accessed.
No members inherited under public, so base class members can not be accessed.

4.7 Member functions
Rcalculate( ), Rdisplay (own class member functions)
Mentry, Mdisplay (derived from Marks class) alone can be accessed.
Personal class public member functions can not be accessed because Marks class inherited under private visibility mode.

4.8 1) Data members

  • Total, Agg, Final Grade and Commence of its own class
  • M, Grade from Marks class can be accessed.

Personal class data members can not be accessed because Marks class inherited under private visibility mode.

Samacheer Kalvi 11th Computer Science Guide Chapter 16 Inheritance

2) Member functions
Mentry and Mdisplay from Marks class can be invoked from Result class member
Personal class member-functions can not be accessed because Marks class inherited under private visibility mode.

Question 5.
Write the output of the following program.
#include<iostream>
using namespace std;
class A
{
protected:
int x;
public:
void show()
{
cout<<“x = “<<x<<endl;
}
A()
{
cout<<endl<<” I am class A “<<endl;
}
~A()
{
cout<<endl<<” Bye”;
}
};
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<<endl<<“I am class B”<<endl;
}
~B()
{
cout<<endl<<” Bye”;
}
void show()
{
cout<<“x = “<<x<<endl;
cout<<“y = “<<y<<endl;
}
};
int main()
{
A objA;
B objB(30, 20);
objB.show();
return 0;
}
Output:
Samacheer Kalvi 11th Computer Science Guide Chapter 16 Inheritance 11

Samacheer Kalvi 11th Computer Science Guide Chapter 16 Inheritance

Question 6.
Debug the following program.
Output:
—————
15
14
13

Program :
%include(iostream.h)
#include<conio.h>
Class A
{
public;
int al,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<<b1<<‘\t'<<b2<<‘t\'<<b3;
}
void main()
{
clrscr()
B der;
derl:func();
getch();
}
Answer:
Modified Error Free Program :
using namespace std;
#include<iostream>
#include<conio.h>
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;
A::getdata();
b1=a1;
b2=a2;
b3=a3;
cout<<b1<<‘\n'<<b2<<‘\n'<<b3;
}
};
int main()
{
B der;
der.func();
getch();
return 0;
}
Samacheer Kalvi 11th Computer Science Guide Chapter 16 Inheritance 12

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 ………………
(a) multiple inheritances
(b) multilevel inheritance
(c) hierarchical inheritance
(d) single inheritance
Answer:
(d) single inheritance

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 these
Answer:
a) Inheritance

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

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

Question 5.
The ……………… are invoked in reverse order.
(a) constructor
(b) destructor
(c) pointer
(d) operator
Answer:
(b) destructor

Question 6.
There are ________ types of inheritance,
a) Two
b) Three
c) Four
d) Five
Answer:
d) Five

Question 7.
_________ inheritance is a type of inheritance.
a) Single or Hybrid
b) Multilevel / Hierarchical
c) Multiple
d) All the above
Answer:
d) All the above

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

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

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

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

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

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

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 the above
Answer:
d) All the above

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

Question 16.
In ________ inheritance the base classes dc not have any relationship between them,
a) Single
b) Multilevel
c) Hybrid
d) Multiple
Answer:
d) Multiple

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

Question 18.
_________ inheritance is similar to relation between grandfather, father and child,
a) Single
b) Multilevel
c) Multiple
d) Multiple
Answer:
b) Multilevel

Question 19.
A class without any declaration will have ________byte size.
a) 1
b) 0
b) 2
d) 10
Answer:
a) 1

Question 20.
class x{}; x occupies
a) 1
b) 0
b) 2
d) 10
Answer:
a) 1

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

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

Question 23.
_______ is a visibility modes.
a) private
b) public
c) protected
d) All the above
Answer:
d) All the above

Question 24.
The default visibility mode is ________
a) private
b) public
c) protected
d) All the above
Answer:
a) private

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 the above
Answer:
a) private

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 the above
Answer:
c) protected

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 the above
Answer:
b) public

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

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

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

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

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

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

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

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

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

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

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

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

Question 40.
________ functions can access the private members.
a) Member
b) Non-member
c) Destructor
d) None of these
Answer:
a) Member

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

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

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

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

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

Very Short Answers (2 Marks)

Question 1.
Write a short note on hierarchical inheritance.
Answer:
When more than one derived class is created from a single base class, it is known as Hierarchical inheritance.

Question 2.
What are the types of inheritance?
Answer:
There are different types of inheritance viz., Single Inheritance, Multiple inheritance, Multilevel inheritance, hybrid inheritance and hierarchical inheritance.

Question 3.
Give the syntax of deriving a class.
Answer:
Syntax:
class derived_dass_name :visibility_mode base_dass_name
{
// members of derivedclass
};

Question 4.
Write note on this pointer.
Answer:
‘this’ pointer is a constant pointer that holds the memory address of the current object. It identifies the currently calling object. It is useful when the argument variable name in the member function and the data member name are same. To identify the data member it will be given as this->data member name.

Short Answers (3 Marks)

Question 1.
What are inheritance and access control?
Answer:
When you declare a derived class, a visibility mode can precede each base class in the base list of the derived class. This does not alter the access attributes of the individual members of a base class but allows the derived class to access the members of a base class with restriction.

Classes can be derived using any of the three visibility modes:

  1. In a public base class, public and protected members of the base class remain public and protected members of the derived class.
  2. In a protected base class, public and protected members of the base class are protected members of the derived class.
  3. In a private base class, public and protected members of the base class become private members of the derived class.
  4. In all these cases, private members of the base class remain private and cannot be used by the derived class.
  5. However, it can be indirectly accessed by the derived class using the public or protected member function of the base class since they have the access privilege for the private members of the base class.

Question 2.
Write a program for the working of constructors and destructors under inheritance.
Program
#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 derived 1 :public derived
{
public :
derived 1()
{
cout << “\nConstructor of derived! //, …”;
}
~derived1()
{
cout << “\nDestructor of derived …”;
}
};
int main()
{
derivedl x;
return 0;
}

Output:
Constructor of base class…
Constructor of derived …
Constructor of derived …
Destructor of derived …
Destructor of derived …
Destructor of base class….

Question 3.
What about access control in a publicly derived class?
Answer:
From a publicly derived class, public and protected members of the base class remain public and protected members of the derived class. The public members can be accessed by the object of the derived class similar to its own members in public.

Question 4.
What about access control in the privately derived class?
Answer:
From a privately derived class, public and protected members of the base class become private members of the derived class. Hence it is not possible to access the derived members using the object of the derived class. The Derived members are invoked by calling it from the publicly defined members.

Explain in Detail

Question 1.
Write a program to implement single Inheritance.
Program
# include <iostream>
using namespace std;
class student //base class
{
private :
char name[20];
int rno;
public:
void acceptnameO
{
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 markl, 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;
}
};
int main()
{
exam e1;
el.acceptname();
//calling base class function using derived
class object
e1.acceptmark();
e1.displayname();
//calling base class function using derived
class object
e1l.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

Question 2.
Write a program to implement multiple inheritance.
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;
}
};
class detail //Base class
{
int dd,mm,yy;
char cl[4];
public:
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.. “<<markl;
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

Question 3.
Write a program to implement multilevel inheritance.
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 single base class class
exam: public student
{
public:
int mark1, mark2, mark3, mark4, mark5, mark6;
void accept mark()
{
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… “<<markl;
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
{
int total;
public:
void showresult()
{
total=markl+mark2+mark3+mark 4+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

Question 4.
Write a program to implement hierarchical inheritance.
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 single base class
class qexam: public student
{
public:
int mark1, mark2, mark3, mark4, marks, mark6;
void accent mark()
{
cout<<“\n Enter lang, eng, phy, che, esc, mat marks for quarterly exam”;
cin>>markl>>mark2>>mark3>>mark4>>mark5>>mark6;
}
void displaymark()
cout< <“\n\t\t Marks Obtained in quarterly”;
cout< <“\n Language.. “<<marki;
cout<<”\n English .. “<<mark2;
cout< <“\n Physics .. “<<mark3;
cout< <“\fl 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, marks, mark6;
void acceptmark()
{
cout<<“\n Enter lang, eng, phy, che, esc, mat marks for half/early exam..”;
cin>>markl>>mark2>>mark3>>mark4>>mark5>>mark6;
}
void displaymark()
{
cout<<“\n\t\t Marks Obtained in Halfyearly”;
cout<<“\n Language., “<< mark1;
coutcc”\n English .. “<< mark2;
coutcc”\n Physics .. “<< mark3;
coutcc”\n Chemistry.. “<< mark4;
coutcc”\n Comp.sci.. “<< mark5;
coutcc”\n Maths .. “<< mark6;
}
};
int main()
{
qexam q1;
hexam hi;
//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.acceptname();
//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
Roll no:-1201
Name:-KANNAN
Marks Obtained in Halfyearly
Language.. 96
English .. 98
Physics .. 100
Chemistry.. 100
Comp.sci.. 100
Maths .. 100

Question 5.
Write a program to implement hybrid inheritance.
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;
cou<<“\n Name name <<
endl;
}
};
//derived class with the single base class
class exam: public student
{
public:
int mark1, mark2, mark3, mark4, marks, mark6;
void accept mark()
{
cout<<“\n Enter larig, eng, phy, che, esc,mat marks..”;
cin>>markl>>mark2>>mark3>> mark4>>mark5>>mark6;
}
void displaymark()
{
cout<<“\n\t\t Marks Obtained “;
cout<<“\n Language.. “<<markl;
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
{
int dd,mm,yy;
char cl[4];
public:
void acceptdob()
{
cout<<“\n Enter date,month,year in digits and class ..”;
cin>>dd>>mm>>yy>>cl;
}
void displaydob()
{
cou<<“\n class :-“<<cl;
cou<<“\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
{
int total;
public:
void showresuit()
{
total=markl+mark2+mark3+ mark4+mark5+mark6;
cout<<“\nTOTAL MARK SCORED: ” <<total;
}
};
class detail //base classs 2
{
int dd,mm,yy;
char cl[4];
public:
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
{
int total;
public:
void showresuit()
{
total = markl + mark2-i-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 base class which itsel 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