You are on page 1of 46

Chapter 6

Basic Concepts of OOP

1 Mark Questions
1. What is the fundamental idea of object oriented programming?
 Object-oriented programming (OOP) is a programming paradigm based on the concept of “objects”,
which may contain data, in the form of attributes and code in the form of procedures or methods.
 The major four principles those make a language object oriented are encapsulation, data
abstraction, polymorphism and inheritance.

2. What is an Object?
 Object is an instance of a class.
 An object is a collection data members and associated member functions.

3. Define the term Class.


 A class is a blueprint or outline for an object.
 A class is a way of grouping objects having similar characteristics.

4. Define the term Data Abstraction.


Data Abstraction refers to the process of representing essential features without including background
details or explanations.

5. What is Encapsulation?
 Data Abstraction refers to the process of representing essential features without including
background details or explanations.
 Data encapsulation enables data hiding and information hiding.

6. What is meant by function overloading?


Function overloading means two or more function have same, but differ in the number of arguments or data
type of arguments.

7. Define Polymorphism.
The ability of an operator and function to take multiple forms is known as Polymorphism.

8. What is inheritance?
 The process of forming a new class from an existing class is known as Inheritance.
 The objects of one class acquire the properties of another class through inheritance.
9. What is a base class?
It is the class whose properties are inherited by another class. It is also called Super Class.

10. What is a derived class?


It is the class that inherits properties from base class (es).It is also called Sub Class.

11. How are base class and derived class related?


The hierarchical relationship between derived class and base class is known as an “is a” relationship.

12. Define the term data hiding.


Data hiding is a method used in object oriented programming to hide information within computer code.

2 Mark Questions
1. What is the significance of classes in OOP?
 The objects can contain data and code to manipulate the data. The objects can be made user
defined data types with the help of a class. Therefore objects are variables of the type class.
 A class is a way of grouping objects having similar characteristics. Once a class is defined, any
number of objects of that class can be created.

2. What is the difference between program module and an object?


A program module is a self-contained independent program segment only it does not provide security to
data whereas an object is a collection of data members and member functions that operate on data and
data is provided with security.

3. Mention different types of inheritance.


The different types of inheritance are single inheritance, multilevel inheritance, multiple inheritance,
hierarchical inheritance and hybrid inheritance.

4. Mention any two advantages of object oriented programming over earlier programming
methods.
 The programs are modularized based on the principle of classes and objects.
 Linking code & object allows related objects to share common code. This reduces code duplication
and code reusability.
 Data is encapsulated along with functions. Therefore external non- member function cannot access
or modify data, thus providing data security.
 Easier to develop complex software, because complexity can be minimized through inheritance.
 The concept of data abstraction separates object specification and object implementation.
 Creation and implementation of OOP code is easy and reduces software development time.
 OOP can communicate through message passing which makes interface description with outside
system very simple.

3 Mark Questions
1. Briefly discuss the classes and objects.
Object
 Object is an instance of a class.
 An object is a collection data members and associated member functions.
 Objects interact by sending messages to one another. The objects can interact with one another
without having to know the details of data or functions within an object.

Class
 A class is a blueprint or outline for an object.
 A class is a way of grouping objects having similar characteristics.
 The objects can be made user defined data types with the help of a class.

2. Explain Inheritance.
 Inheritance is the capability of one class to inherit properties from another class. Inheritance provides
the idea of reusability
 Base Class is the class whose properties are inherited by another class. It is also called Super Class.
 Derived Class is the class that inherits properties from base class (es).It is also called Sub Class.
 The different types of inheritance are single inheritance, multilevel inheritance, multiple inheritance,
hierarchical inheritance and hybrid inheritance.

3. Write short notes on polymorphism.


 The ability of an operator and function to take multiple forms is known as Polymorphism.
 For example, one function name can be used for different purposes. Similarly, same operator can
be used for different operations.
 There are of two types one is the “compile time polymorphism” and other one is the “run-time
polymorphism”.

4. Mention any 4 high level languages that follow object oriented programming approach.
The four high level languages that follow object oriented programming approach are:

 C++
 C#
 Java
 Python
 Visual Basic

5 Mark Questions
1. Write the difference between procedural programming and object oriented programming.

2. Explain the advantage of OOPs.


(Same answer as Q4 under 2 mark questions)
3. Write the disadvantages of object oriented programming.
 Larger program size: OOP‟s typically involves more lines of code than procedural programs.
 Slower Programs: OOP‟s typically slower than procedure based programs, as they typically require
more instructions to be executed.
 Not suitable for all types of programs.
 To convert a real world problem into an object oriented model is difficult.
 OOP‟s software development, debugging and testing tools are not standardized.
 Polymorphism and dynamic binding also requires processing time, due to overload of function calls
during run time.

4. Write the real life applications of object oriented programming.


 Computer graphics applications.
 CAD/CAM software
 Object-oriented database.
 User-Interface design such as windows
 Real-time systems.
 Simulation and Modeling
 Artificial intelligence and expert systems.
 Client-Server Systems.
Chapter 7

Classes and Objects

1 Mark Questions
1. What are the two types of members referenced in a class?
The two types of members referenced in a class are data members and member functions.

2. What are data members?


The variables declared inside the class are called data members.

3. What is a member function?


Member functions are the set of operations that are performed on the objects of the class. There may be
zero or more member functions in a class.

4. Mention the access specifiers used with a class.


The access specifiers used with a class are private, protected and public.

5. Is it possible to access data outside a class?


Yes, the public members can be accessed by member functions of the class and non- member function
(outside the class) of the class.

6. Which type of data members are accessible outside a class?


The public members can be accessed by member functions of the class and non-member function (outside
the class) of the class.

7. Which access specifier is implicitly used in a class?


The private access specifer is implicitly used in a class.

8. Define the term Public access.


The public members can be accessed by member functions of a class and non-member function outside the
class also.

9. Mention the operator used to access members of a class.


The dot (.) is used to access members of a class.

10. What is the significance of scope resolution operator (::)?


The operator :: known as scope resolution operator helps in defining member function outside the class.
11. How are objects of a class declared? Give an Example.
Syntax:
classname objectname1, objectname2, …. ,objectnameN ;
For example:
Largest ob1,ob2; //object declaration

12. What is meant by an array of objects?


An array having class type elements is known as array of objects.

13. Write an example to show how objects can be used as function arguments?
Example:
class class_name
{
…………………..
return_type function_name(class_name para1, class_name para2)
{
…………………………………………….;
}
…………………………..;
}

main()
{
class_name obj1, obj2, obj3;
obj1.function_name(obj2, obj3);
………………
}

2/3 Mark Questions


1. What the difference between class definition and class declaration.
 A class definition is a process of naming a class and data variables, and interface operations of the
class.
 A class declaration specifies the representation of objects of the class and set of operations that can
be applied to such objects.

2. Write the syntax and example for class definition.


The General syntax for defining a class is as follows:
class user_defined_name
{
private:
Member data
Member functions
protected:
Member data
Member functions
public:
Member data
Member functions
};

Example program to show the use of class and object:


#include<iostream.h>
#include<conio.h>
class Test
{
private:
int a,b;
public:
void getnum()
{
cout<<"Enter Numbers for Addition: ";
cin>>a>>b;
}
void show_data()
{
cout<<"Addition of two numbers: "<<(a+b);
}
};

int main()
{
clrscr();
Test a1;
a1.getnum();
a1.show_data();
getch();
return 0;
}

3. Write the syntax and example for class declaration.


Syntax:
class myClass //class header

public:

//fields, constructors
Class declaration
//public method declarations
private:
//private fields
//private method declarations
};

main()
{ Object declaration
myClass obj1, obj2;
}

Example:
(Same example as given in the previous question)

4. What is the significance of using access specifiers? Mention different access specifiers.
The data members and member functions can be accessed using access specifiers. They define the scope of
members. The different access specifiers are private, protected and public.

5. Discuss private access specifiers with an example.


 Private access means a member data can only be accessed by the member function.
 Members declared under private are accessible only within the class.
 If no access specifier is mentioned, then by default, members are private.
 Example:
private:
int x;
float y;
6. Write short notes on public access specifier.
Public access means that members can be accessed by any function outside the class also.

Example:
class box
{
int length;
public : int width;
private : int height;

void set_height ( int i )


{
height = i ;
}

int get_height ( )
{
return height ;
}
};

int main()
{
box object;
object.length = 10;
object.width = 20;
object.set _height ( 30); // private variable can be accessed only through its public method
return 0;
}

7. Explain protected access specifier.


The members which are declared using protected can be accessed only by the member functions, friends of
the class and also by the member functions derived from this class. The members cannot be accessed from
outside.
Example:
private:
int x;
float y;

8. How are class members referenced? Discuss with suitable example.


 The class members are referenced using dot (.) operator. The public data members of a class can
be accessed using dot (.) operator using objects of that class.
Syntax: objectname.datamember;
 The member function is invoked by an object name dot operator and member function name.
For example: obj1.getdata();

9. What is meant by referencing member functions inside class definition and outside class
definition?
Member functions can be defined in two places:
 Inside class definition
 Outside class definition

Inside class definition

 To define member function inside a class the function declaration within the class is replaced by
actual function definition inside the class.
 A function defined in a class is treated as inline function.
 Only small functions are defined inside class definition.
Syntax:
return_type classname(member function)

Outside class definition

 Member functions declared within a class must be defined separately outside the class.
 The definition of member function is similar to normal function. But a member function has an
„identity label‟ in the header. This label tells the compiler which class the function belongs to.
 The scope of the member function is limited to the class mentioned in the header.
 Scope resolution operator :: is used to define the member function.
Syntax:
return_type classname :: memberfunction(arg1, arg2, …, argn)
{
function body;
}

Outside Program to use classes using member functions inside and outside class
definition
# include<iostream.h>
class item // class declaration
{
int number; // private by default
float cost;
public :
void getdata(int a, float b);

void putdata(void) // function defined here


{
cout <<"Number: "<< number << endl;
cout << "Cost: "<< cost << endl;
}
};

// member function outside class definition


void item :: getdata(int a, float b)
{
number = a;
cost = b;
}

// main program
int main()
{
item x; // create object x
x.getdata(250, 10.5);
x.putdata();
return 0;
}

10. Discuss how objects of a class are referenced with an example.


11. How can arrays be used as class members. Write an example.
Example:
class marks
{
int m[5];
int i ;

public:
void setval(void);
void display(void);
};

The array variable m is a private member of class marks. This can be used
by member function setval( ) and display( ) as follows.

void marks :: setval(void)


{
cout<< "Enter marks: "<<endl;
for (i=0;i<5;i++)
cin>>m[i] ;
}

void marks::display(void)
{
cout<< "The marks are : ";
for(i =0;i<5;i++)
cout<<setw(4)<<m[i]<<endl;
}

12. How are objects passed as arguments to a function? Give an example.


 A function can receive an object as a function argument.
 An object can be passed to a function in two ways:

 Copy of entire object is passed to function (pass-by-value).


 In pass by value, a copy of the object is passed to the function. The function
 creates its own copy of the object and uses it. Therefore changes made to the
 object inside the function do not affect the original object.

 Only address of the object is transferred to the function (pass-by-reference).


 In pass by reference, when an address of an object is passed to the function,
 the function directly works on the original object used in function call. This
 means changes made to the object inside the function will reflect in the original
 object, because the function is making changes in the original object itself.
 Pass-by-reference is more efficient, since it require only to pass the address
 of the object and not the entire object.

(Example to be given)

5 Mark Questions
1. Explain class definition and class declaration with syntax and example.
(Combine answer numbers 1, 2 & 3 under 2/3 mark questions).

2. Describe access specifiers in a class.


(Combine answer numbers 4, 5, 6 & 7 under 2/3 mark questions).
3. Explain member functions.
a) Inside class definition
b) Outside class definition
(Same as answer number 9 under 2/3 mark questions).

4. What are the characteristics of member functions outside a class?


The member functions have following characteristics:
 Type and number of arguments in member function must be same as types and number of data
declared in class definition.
 The symbol :: is known as scope resolution operator. Usage of :: along with class name is the header
of function definition. The scope resolution operator identifies the function as a member of
particular class.
 Several classes can use same function name. Membership label will resolve their scope.
 Member function can access private data of a class. A non-member function cannot.

5. Explain how objects of a class can be defined.


 When a class is defined, it specifies the type information the objects of the class store.
 Once the class is defined an object is created from that class.
 The objects of a class are declared in the same manner like any other variable declaration.
Syntax:
class user_defined_name
{
private: //Members
public: //Methods
};
User_defined_name object1, object2, … ;
Example:
class num
{
private:
int x;
int y;
public:
int sum(int p, int q);
int diff(int p, int q);
};
void main()
{
num s1,s2;
s1.sum(200,300);
s2.diff(600,500);
}

6. Illustrate how an array of objects can be defined.


 An array having class type elements is known as array of objects.
 An array of objects is declared after class definition and is defined in the same way as any other
array.
Example:
class employee
{
char name[10] ;
int age;

public:
void getdata();
void dispdata(void);
};
employee supervisor[3] ;
employee sales_executive[5] ;
employee team_leader[10] ;

In the above example, the array supervisor contains 3 objects namely


supervisor[0], supervisor[1],supervisor[2].
7. Describe how objects can be used as function arguments.
(Same as answer number 12 under 2/3 mark questions)

8. Let product list be a linear array of size N where each element of the array contains following
fields: Itemcode, price and quantity. Declare a class product list with the three data members
and member functions to perform the following:
a) Add values to the product list.
b) Printing the total stock value.
9. A class clock has the following members : a) Hour b) Minute. Create Member Functions:
a) To initialize the data members.
b) Display the time.
c) To convert hours and minutes to minutes.
10. Write a program that receives arrival time, departure time and speed of an automobile in
kilometres/hour as input to a class. Compute the distance travelled in metres/second and
display the result using member functions.
Chapter 9

Constructor and Destructor

1 Mark Questions

1. What is a constructor?
A Constructor is a special member function that is called automatically when an object is
created. It is called constructor, because it constructs or decides the initial values for data members of a
class.

2. Write one reason which defines the need to use a constructor. Simplify.
The purpose of a constructor is to mainly initialize the member variables of a class.
or
It is used for initialization of objects.

3. What should be the access parameters for constructor declaration?


The constructors should be declared in public section i.e., public is the access parameter for constructor
declaration.

4. Can a constructor return a value to a calling function?


No, constructors cannot return value to a calling function

5. How many types of constructors are there?


There are three types of constructors. Namely.
 Default Constructors.
 Parameterized Constructors
 Copy Constructors.

6. What is a default constructor?


 A constructor which does not accept any arguments is called as a default constructor.
 It is also called as zero argument constructors.

7. What is the drawback of default constructor?


 When many objects of the same class are created, all objects are initialized to same set of values by
default constructor.
 It is not possible to initialize different objects with different initial values using default constructor.
8. Is it possible to overload a default constructor?
Since no arguments are there, the default constructor cannot be overloaded.

9. What is a parameterized constructor?


 A constructor that takes one or more arguments is called parameterized constructor.
 It is also called as regular constructors.

10. Write any one feature of parameterized constructor.


 The parameterized constructors can be overloaded.
 For an object created with one argument, constructor with only one argument is invoked and
executed.
 The parameterized constructor can have default arguments and default values.

11. Name two methods through which constructors can be invoked.


The two methods through which constructors can be invoked are implicit call and explicit call.

12. What is an explicit call?


In explicit call, declaration of an object is followed by assignment operator, constructor name and argument
list enclosed in paranthesis.

13. What is an implicit call with reference to constructors?


An Implicit call means the declaration of the object is followed by argument list enclosed in parenthesis.

14. When is = used with constructors?


The = is used for the parameterized constructor with exactly one argument.

15. What is a copy constructor?


Copy constructor is a parameterized constructor using which one objectcan be copied to another object.

16. Write the syntax for declaration of copy constructor.


class Class_Name
{ public:
Class_Name(Class_Name &ptr)
{
……..
}
};
17. Can a copy constructor be invoked explicitly?
No, a copy constructor cannot be invoked explicitly.

18. What is meant by constructor overloading?


The concept of having more than one constructor in a class with same name, as long as each has a
different list of arguments is known as Constructor Overloading

19. What is a destructor?


A destructor is special member function that is executed when an object of that class is destroyed.

20. Which operator is used with destructor?


The operator tilde sign (~) is used with destructor.

2 Mark Questions

1. What is a constructor? Give an example.


A Constructor is a special member function that is called automatically when an object is
created. It is called constructor, because it constructs or decides the initial values for data members of a
class.

Example Program to demonstrate the use of Constructor:


#include <iostream.h>
#include <iomanip.h>
class counter
{
private:
int a;

public:
counter()
{
a = 0;
}

void inc_count()
{
a++;
}

int get_count()
{
return a;
}
};

int main()
{
counter c1, c2;
cout<<“c1 = “<<c1.get_count();
cout<<“c2 = “<<c2.get_count();
c1.inc_count();
c2.inc_count();
cout<<“c1 = “<<c1.get_count()<<endl;
cout<<“c2 = “<<c2.get_count()<<endl;
}

2. Why are constructors needed in a program? Justify.


 Constructors are named as constructors because they are getting called when an object is
constructed.
 The use of a constructor can be cleverly done especially in those problems where it is necessary to
initialize certain data members compulsorily.
 Instead of having separate member functions for initializing we can perform those operations inside
the constructor itself.

3. Write the syntax and example for default constructor.


Syntax and Example
Program Example

4. Mention the features of parameterized constructors.


 The parameterized constructors can be overloaded.
 For an object created with one argument, constructor with only one argument is invoked and
executed.
 The parameterized constructor can have default arguments and default values.

5. Which are the different methods through which constructors are invoked?
Invoking Parameterized Constructors can be done in three different ways

1) Implicit Call
2) Explicit Call
3) Initialization at the time of declaration with “=” operator

6. Write an example to show the use of parameterized constructor through explicit call.
Program to use parameterized constructor thorough explicit call:
#include<iostream.h>
class num
{
private:
int a,b;
public:
num(int p, int q)
{
a = p;
b = q;
}

void display()
{
cout<<"a = "<<a<<" and b = "<<b<<endl;
}
};

void main()
{
num obj1=num(10,20);
num obj2=num(40,50);
cout<<"First construction: ";obj1.display();
cout<<"Second construction: ";obj2.display();
}

Output:
First construction: a = 10 and b = 20
Second construction: a = 40 and b = 50

7. When is copy constructor used in a program?


Copy Constructors are used in the following situations
 To initialize an object with the values of already existing objects.
 When objects must be returned as function values
 To state objects as by value parameters of a function.

8. Write syntax and example for copy constructor.


Syntax
Program to demonstrate copy constructor
3 Mark Questions

1. Mention three types of constructors.


There are three types of constructors. Namely.
 Default Constructors.
 Parameterized Constructors
 Copy Constructors.

2. What are the features of default constructors?


 For every object created, this constructor is automatically called.
 All objects of a class are initialized to same set of values by the default constructor.
 If different objects are to be initialized with different values, it cannot be done using default
constructor.

3. What are the disadvantages of default constructor?


 When many objects of the same class are created, all objects are initialized to same set of values by
default constructor.
 It is not possible to initialize different objects with different initial values using default constructor.

4. Write short notes for constructor overloading.


 Overloading a constructor means specifying additional operation by passing the required arguments.
 Depending on the requirement one can define any number of overloaded constructors in a single
class.
 Depending on the type and number of arguments passed, the compiler decides which version of the
constructor to invoke during object creation.

5 Mark Questions

1. Write the rules for writing a constructor function.


 A Constructor always has name that is same as the class name of which they are the members.
This will help the compiler to identify that they are the constructors.
 There is no return type for constructors (not even void). Since, the constructor is called automatically
by the system, there is no program for it to return anything to; a return value would not make
sense.
 A constructor should be declared in public section.
 A constructor is invoked automatically when objects are created. Constructors can have default
arguments.
 A class can have more than one constructor. However all constructor of a class should have the
same name.
 It is not possible to refer to the address of the constructors.
 The constructors make implicit calls to the operators new and delete when memory allocation is
required.

2. Explain default constructor with syntax and example.


(Refer Q3 under 2 marker – definition, syntax & example)

3. Explain parameterized constructor with syntax and example.


A constructor that takes one or more arguments is called parameterized constructor.

Syntax and Example

(Write same example given under Q6 in 2 mark questions).

4. Explain with an example to show how a constructor is invoked explicitly.


(Refer Q6 under 2 marker – example and explanation)

5. Explain the features of copy constructor.


 Copy constructor is not invoked explicitly.
 Copy constructor is invoked automatically when a new object is created and equated to an already
existing object in the declaration statement itself.
 When a new object is declared and existing object is passed as a parameter to it in the declaration,
then also copy constructor is invoked.
 When an object is passed to a function using pass-by-value, copy constructor is automatically called.
 Copy constructor is invoked when an object returns a value.

6. Explain destructors with syntax and example.


A destructor is special member function that is executed when an object of that class is destroyed.

Syntax

Program to show the use of destructor


#include<iostream.h>
#include<conio.h>
#include <iomanip.h>
class num
{
private:
int x;
public:
num();
void display();
~num();
};

num::num()
{
cout<<“ In constructor: \n”;
x=100;
}
num::~num()
{
cout<<“in destructor”<<endl;
}
void num::display()
{
cout<<“Value of x = ”<<x<<endl;
}

int main()
{
clrscr();
num a;
a.display();
getch();
return 0;
}

Output:
In constructor
Value of x = 100
In destructor
Chapter 10

Inheritance

1 Mark Questions

1. What is inheritance?
Inheritance is the capability of one class to inherit properties from another class.

2. How to implement inheritance?


Inheritance is implemented using the following syntax:
class derived_classname : access specifier base classname

3. What is base class?


It is the class whose properties are inherited by another class. It is also called Super Class.

4. What is derived class?


It is the class that inherits properties from base class (es).It is also called Sub Class.

5. What is public access specifier?


It means “public parts of base class remain public and protected parts of base class remain protected for
derived class”.

6. What is private access specifier?


The private access specifier means “Public and Protected Parts of base class remain Private in derived
class”.

7. Mention any one advantage of inheritance.


1. Reusing existing code
2. Faster development time
3. Easy to maintain
4. Easy to extend
5. Memory utilization

8. Is inheritance possible in C?
No, because it doesn‟t support object oriented concept.

9. What is the use of protected access specifier?


The private member of a base class cannot be inherited to the derived class and protected member of a
base class remains protected in a derived class.

10. What is the use of private access specifier?


The private members of a base class cannot be inherited and protected members of a base class become
private members to derived class.

11. What is the use of public access specifier?


The private members of a base class cannot be inherited and protected members remain protected in a
derived class.

12. What is single inheritance?


If a class is derived from a single base class, it is called as single inheritance.

13. What is multilevel inheritance?


The classes can also be derived from the classes that are already derived. This type of inheritance is called
multilevel inheritance.

14. What is hierarchical inheritance?


If a number of classes are derived from a single base class, it is called as hierarchical inheritance.

15. What is hybrid inheritance?


Hybrid Inheritance is combination of Hierarchical and Multilevel Inheritance.

16. What is multiple inheritance?


If a class is derived from more than one base class, it is known as multiple inheritance.

17. What is virtual base class?


When two or more objects are derived from a common base class, we can prevent multiple copies of the
base class being present in an object derived from those objects by declaring the base class
as virtual when it is being inherited. Such a base class is known as virtual base class.

18. What is an abstract class?


An abstract class is one that is not used to create objects.

19. When is it necessary to use inheritance?


When a newly created class need to acquire properties of existing class and with its own properties then it
is necessary to use inheritance.

20. What is visibility mode?


The visibility mode basically controls the access specifier to be for inheritable member of base class in the
derived class.

2 Mark Questions

1. What is the difference between public and private access specifier?


In public access mode, protected members of base class remain protected in a derived class. In private
access mode, protected members of a base class become private members in a derived class. And the
private members of a base class cannot be inherited in both the access mode.

2. Mention any two advantages of inheritance?


 Reusability- Inheritance helps the code to be reused in many situations.
 Saves Time and Effort- Since the main code written can be reused in various situations as needed.

3. Mention any two types of inheritance?


Single Inheritance, Multilevel Inheritance, Multiple Inheritance, Hierarchical inheritance and Hybrid
inheritance.

4. What is the difference between inheritance and polymorphism?


5. What is single inheritance? Give an example.
 If a class is derived from a single base class, it is called as single inheritance.
 Example:
#include<iostream.h>
#include<conio.h>
class base
{
private:
int rollno;
char name[20];
public:
void read()
{
cout<<“Enter Roll No and name “<<endl;
cin>> rollno >>name;
}
void display()
{
cout<<“roll no: “<< rollno <<endl;
cout<<“name : “<<name <<endl;
}
};

class derive: public base


{
private:
int m1;
int m2;
int t;
public:
void read1()
{
cout<<“enter first marks and second marks: ”<<endl;
cin>>m1>>m2;
t = m1+m2;
}
void display1()
{
cout<<"First marks = "<<m1<<endl;
cout<<"Second marks = "<<m2 <<endl;
cout<<"Total marks = "<<t<<endl;
}
};

void main()
{
derive ob;
clrscr();
ob.read();
ob.read1();
ob.display();
ob.display1();
getch();
}

Output:

6. What is multilevel inheritance? Give an example.


 The classes can also be derived from the classes that are already derived. This type of inheritance
is called multilevel inheritance.
 Example:
#include<iostream.h>
#include<conio.h>
class A
{
public:
void displayA( )
{
cout << “ Base class A”<<endl;
}
};

class AB : public A
{
public:
void displayAB( )
{
cout << “ Intermediate Base class AB”<<endl;
cout << “ Derived from A” << endl;
}
};

class ABC : public AB


{
public:
void displayABC( )
{
cout << “ Derived Class ABC”<<endl;
cout << “ Derived from AB” << endl;
}
void output( )
{
displayA( );
displayAB( );
displayABC( );
}
};
void main( )
{
ABC obj;
clrscr( );
obj.output( );
getch( );
}
7. What is hierarchical inheritance? Give an example.
 If a number of classes are derived from a single base class, it is called as hierarchical inheritance.
 Example:
#include <iostream>
using namespace std;

class A //single base class


{
public:
int x, y;
void getdata()
{
cout << "\nEnter value of x and y:\n"; cin >> x >> y;
}
};
class B : public A //B is derived from class base
{
public:
void product()
{
cout << "\nProduct= " << x * y;
}
};
class C : public A //C is also derived from class base
{
public:
void sum()
{
cout << "\nSum= " << x + y;
}
};
int main()
{
B obj1; //object of derived class B
C obj2; //object of derived class C
obj1.getdata();
obj1.product();
obj2.getdata();
obj2.sum();
return 0;
}

8. What is hybrid inheritance? Give an example.


 Hybrid Inheritance is combination of Hierarchical and Multilevel Inheritance.
 Example:

#include <iostream>

using namespace std;

class A

public:

int x;

};

class B : public A

public:

B() //constructor to initialize x in base class A

x = 10;

}
};

class C

public:

int y;

C() //constructor to initialize y

{ y = 4; }

};

class D : public B, public C //D is derived from class B and class C

public:

void sum()

cout << "Sum= " << x + y;

};

int main()

D obj1; //object of derived class D

obj1.sum();

return 0;

}
9. What is multiple inheritance? Give an example.
 If a class is derived from more than one base class, it is known as multiple inheritance.
 Example:
#include<iostream>
using namespace std;
class Person {
// Data members of person
public:
Person(int x) { cout << "Person::Person(int ) called" << endl; }
};

class Faculty : public Person {


// data members of Faculty
public:
Faculty(int x):Person(x) {
cout<<"Faculty::Faculty(int ) called"<< endl;
}
};

class Student : public Person {


// data members of Student
public:
Student(int x):Person(x) {
cout<<"Student::Student(int ) called"<< endl;
}
};

class TA : public Faculty, public Student {


public:
TA(int x):Student(x), Faculty(x) {
cout<<"TA::TA(int ) called"<< endl;
}
};

int main() {
TA ta1(30);
}
10. What is virtual base class? Give example.
 When two or more objects are derived from a common base class, we can prevent multiple copies
of the base class being present in an object derived from those objects by declaring the base class
as virtual when it is being inherited. Such a base class is known as virtual base class.
 Example:
#include<iostream>
using namespace std;
class Person {
public:
Person(int x) { cout << "Person::Person(int ) called" << endl; }
Person() { cout << "Person::Person() called" << endl; }
};

class Faculty : virtual public Person {


public:
Faculty(int x):Person(x) {
cout<<"Faculty::Faculty(int ) called"<< endl;
}
};

class Student : virtual public Person {


public:
Student(int x):Person(x) {
cout<<"Student::Student(int ) called"<< endl;
}
};

class TA : public Faculty, public Student {


public:
TA(int x):Student(x), Faculty(x) {
cout<<"TA::TA(int ) called"<< endl;
}
};

int main() {
TA ta1(30);
}
11. What is an abstract class?
 An abstract class is one that is not used to create objects.
 An abstract class is designed only to act as a base class (to be inherited by other classes).

12. Which are the components which cannot be inherited?


A derived class inherits every member of a base class except:

 its constructor and its destructor


 its operator=() members
 its friends

13. Explain single inheritance with a suitable C++ program.


(Refer Q5 under 2 mark questions).

14. How does inheritance influence the working of constructors?


 The inheritance influence the working of constructor, if base class contains parameterized
constructor, then it is compulsory to pass the arguments using derived class. Normally derived class
is used to declare objects therefore derived class is used to pass arguments to base class
constructor.
 If both base class and derive class has constructors, then base class constructor is executed first and
then derived class constructor is executed i.e., pass the value using derived class to base class
constructor and then to derived class constructor. The derived class is responsible to pass arguments
to both base class as well as derived class.

15. How does inheritance influence the working of destructors?


 Constructors, destructors and overloaded assignment operators are not inherited.
 C++ requires that a derived-class constructor call its base-class constructor to initialize the base-class
data members that are inherited into the derived class.
 If the derived-class‟s constructor did not invoke the base-class‟s constructor explicitly, C++ would
attempt to invoke the base class‟s default constructor.
 When an object of a derived class is built(constructed):
 the base-class‟s constructor is called immediately to initialize the base-class data members in the
derived-class object and then
 the derived-class constructor will initialize the additional derived-class data members.
 When a derived-class object is destroyed, the destructors are called in the reverse order of the
constructors:
 First the derived-class destructor is called, then the base-class destructor.
 When deriving a class, the base class may be declared as either public, protected, or private.

3 Mark Questions

1. What is the difference between public and private access specifier with respect to inheritance?
(write definition for both : refer Q10 & Q11 under 1 mark questions).

2. What are the advantages of inheritance?


(Refer Q7 under 1 mark question – write atleast one line explanation for each).

3. What are the types of inheritance?


Single, Multilevel, Multiple, Hybrid & Hierarchical inheritance.

5 Mark Questions

1. What is the difference between public, private and protected access specifier?
(Refer Q9,Q10 & Q11 under 1 mark question for definition and Q1 under 3 mark question for example).

2. What are the advantages of inheritance?


(Refer Q7 under 1 mark question – write atleast one line explanation for each).

3. What are the types of inheritance? Explain any 2.


(Refer Q5, Q6, Q7, Q8 & Q9 under 2 mark question).
4. What is virtual base class? Give example.
(Refer Q10 under 2 mark question).

5. Explain the requirements of a virtual base class.


 When two or more objects are derived from a common base class, we can prevent multiple copies
of the base class being present in an object derived from those objects by declaring the base class
as virtual when it is being inherited. Such a base class is known as virtual base class.
 This can be achieved by preceding the base class name with the word virtual.
 Example:
Class A
{
---------------------;
---------------------;
};
class B : virtual public A
{
---------------------;
---------------------;
};
class C : virtual public A
{
---------------------;
---------------------;
};
class D : public B, public C
{
---------------------;
---------------------;
};

(Refer Q10 under 2 mark question for example).

6. How does inheritance influence the working of constructors and destructors?


(Refer Q14 & Q15 under 2 mark questions).
Chapter 11

Pointers

1 Mark Questions

1. Write a definition for a variable of type pointer to float.


Syntax: float *pointervariable;
Example: float *ptr;

2/3/5 Mark Questions

1. What do you mean by pointer? Explain with example.


 A pointer is a variable that holds a memory address, usually the location of another variable in
memory.
 Example:

In the above example, variable „a‟ is allocated a memory location of 5000 and it stores the value 10.
Similarly, pointer „ptr‟ is allocated memory location 6000 and it stores the address of variable „a‟ i.e. 5000
as its value.

2. Mention advantages of pointer?


 It is possible to write efficient programs.
 Memory is utilized properly.
 Dynamically allocate & deallocate memory.
 Easy to deal with hardware components.
 Establishes communication between program and data.

3. What is address operator? Give example.


 & is a unary operator that returns the memory address of its operand. This operator has the same
precedence and right-to-left associativity as the other unary operators.
 Example:
int num = 25;
int *iptr;
iptr = &num; // The Address of Operator &
In this example, „num‟ is the variable and „&num‟ is its address which is stored in pointer „iptr‟.

4. What is pointer operator? Give example.


 The second operator is Indirection Operator *, and it is the complement of &. It is a unary operator
that returns the value of the variable located at the address specified by its operand.
 Example:
int num = 25;
int *iptr;
iptr = &num; // The Address of Operator &
int val = *iptr; //The pointer or indirection operator
In the above example, „iptr‟ stores the address of variable „a‟ using „&num‟, whereas variable „val‟
gets the value stored in „num‟ using „*iptr‟ i.e. the pointer/indirection operator.

5. How to declare pointer? Give example.


 The general form is, data-type *variable_name;
 Data-type is any valid data type supported by C++ or any user defined type and variable_name is
the name of pointer variable. The presence of * indicates that it
is a pointer variable.
Example:
int *iptr; // iptr is declared to be pointer variable of int type.
float *fptr; //fptr is declared to be pointer variable of float type.
char *cptr; //cptr is declared to be pointer variable of character type.

6. How to initialize pointer? Give example.


 The general syntax is, pointer_variable_name = &variable_name
 We can assign the address of a variable to a pointer variable as follows:
int num = 25;
int *iptr;
iptr = &num;
In the above example, the variable num (=25) is assigned to pointer variable iptr.

7. What is static memory?


 The fixed size of memory allocation for data during program compilation is called static memory
allocation.
 All the variables declared normally, are allocated memory statically.
 Example:
int a; //Allocates 2 bytes of memory space during the compilation time.

8. What is dynamic memory?


 In the dynamic memory allocation, the amount of memory to be allocated is not known. This
memory is allocated during run-time as and when required.
 C++ supports dynamic allocation and deallocation of objects using the new and delete operators.
These operators allocate memory for objects from a pool called the free store. The new operator
calls the special function operator new and the delete operator calls the special function operator
delete.
 New operator
 We can allocate storage for a variable while program is running by using new operator.
 Dynamic allocation is perhaps the key to pointers.
 It is used to allocate memory without having to define variables and then make pointers
point to them.
 Example:
To allocate memory of type integer, int *iptr = new int;
int *pNumber;
pNumber = new int;
The first line declares the pointer, pNumber. The second line then allocates
memory for an integer and then makes pNumber point to this new memory.
 Delete operator
 Memory that is dynamically allocated using the NEW operator can be freed using
 delete operator.
 The delete operator calls the operator delete functio, which frees memory back to the
available pool.
 Use delete function to free dynamic memory as: delete iptr;

9. What is free store?


 Free store is a pool of unallocated memory heap given to a program that is used by the program for
dynamic allocation during execution.
 It is also called as heap memory.

10. Illustrate the use of “self referential structures” with the help of example.
 A self-referential structure is used to create data structures like linked lists, stacks, etc.
 Following is an example of this kind of structure where pointer variable *next is of datatype „node‟
which is the structure type itself:
struct node
{
int data;
node *next;
};

11. What is new operator in C++? Give example.


(Refer Q8)

12. What is delete operator in C++? Give example.


(Refer Q8)

13. What is array of pointers? Give example.


 Since we know that pointer is a variable which stores address of another variable, an array of
pointers means that it is a collection of addresses.
 The example below shows the array of pointers.
int *iptr[5];
int i=10, j=20, k=30, l=40, m=50;

iptr[0] = &i; *iptr[0] = 10;


iptr[1] = &j; *iptr[1] = 20;
iptr[2] = &k; *iptr[2] = 30;
iptr[3] = &l; *iptr[3] = 40;
iptr[4] = &m; *iptr[4] = 50;
14. How dynamic memory allocation is different from static memory allocation.
(Refer Q7 & Q8)

15. Show the general form of new and delete operator in C++?
(Refer Q7 & Q8)

16. What is the relationship between array and pointers? Give example.
17. What is the relationship between string and pointers? Give example.
18. What is the relationship between structures and pointers? Give example.
19. What is the relationship between object and pointers? Give example.
20. Explain with example by passing the reference.
21. Explain with example by passing the pointers.

You might also like