Question Bank & Solutions-Module-1
Question Bank & Solutions-Module-1
Module-1
OP OOP
The programming task is divided The programming task is divided into objects
into a collection of data structures (consisting of data members and associated
and methods or functions. member functions)
A piece of code uses the data to The data uses the piece of code to perform the
perform the specific task specific task
Data is moved freely from one Data is hidden (using private and protected
function to another function using access specifiers) and can be accessed only by
function parameters. member functions, not by an external function.
Basic concepts and features of object oriented programming language like C++.
2. Data abstraction
3. Data encapsulation
4. Inheritance
5. Polymorphism
6. Binding
7. Message passing
Classes are user-defined data types on which objects are created. Objects with similar
properties and methods are grouped together to form a class. So class is a collection of
objects. The object is an instance of a class.
2. Data abstraction
Abstraction refers to the act of representing essential features without including the
background details or explanation.
Example: Let’s take one real life example of a TV, which you can turn on and off, change
the channel, adjust the volume, and add external components such as speakers, VCRs,
and DVD players, BUT you do not know its internal details, that is, you do not know how
it receives signals over the air or through a cable, how it translates them, and finally
displays them on the screen.
#include <iostream>
int main( )
return 0;
Here, you don’t need to understand how cout displays the text on the user’s screen. You
need to only know the public interface and the underlying implementation of cout is free
to change.
3. Data encapsulation
Information hiding, Wrapping (combining) of data and functions into a single unit (class)
is known as data encapsulation.
Data is not accessible to the outside world, only those functions which are wrapped in
the class can access it.
4. Inheritance
Inheritance is the process of deriving a new class from an existing class. Existing class
is known as base, parent or super class. The new class that is formed is called derived
class, child or sub class. Derived class has all the features of the base class plus it has
some extra features also.
Writing reusable code. Objects can inherit characteristics from other objects.
5. Polymorphism
A single name can have multiple meanings depending on its context. It includes
function overloading, operator overloading.
6. Binding
Binding means connecting the function call to the function code to be executed in
response to the call.
Static binding means that the code associated with the function call is linked at compile
time. Also known as early binding or compile time polymorphism.
Dynamic binding means that the code associated with the function call is linked at
runtime. Also known as late binding or runtime polymorphism.
7. Message passing
4. List and explain any four features of an object-oriented program. Jan.-19, Marks 5
7. What is a reference variable? Explain. Write a C++ program to swap two integer
values and display the values before and after swapping. Jul.-17, Marks 5
Write a C++ program to swap two integers values and display the values before and
after swapping using call by reference.
int a =10;
int &x=a;
Here variable x is a reference variable for variable a. Both a and x are pointing at the
same memory location. Hence if the value of a is changed then the value of x also
changes automatically.
Use of Reference
The reference variables are used to pass the parameter to the functions pass by
reference.
Points to Remember
1. Reference variables should not be initilized with a constant value. For example int
&a=100 is not allowed.
The following program demonstrates the C++ program to swap two integers values
and display the values before and after swapping using call by reference.
#include<iostream>
int temp;
temp = x;
x = y;
y = temp;
int main()
int a, b;
cin>>a;
cin>>b;
swap(a, b);
OUTPUT:
How to write a C++ program to find the areas of the circle (PI * r * r), rectangle (l * b),
and square (x * x) by getting the r, l, b, and x through keyboard and printing the areas on
the console using the method area() by applying the concept of function overloading.
Function overloading means, two or more functions have the same names but different
argument lists.
The arguments may differ in the type of arguments or number of arguments, or both.
However, the return types of overloaded methods can be the same or different is called
function overloading.
Function overloading conceptshelps us to use the same function names multiple time in
the same program.
Following program demonstrates the overloaded function to find the area of the circle,
rectangle, and square.
#include<iostream>
return x*x;
return l*b;
double area(double r)
return 3.142*r*r;
int main()
int x, l, b;
double r;
cin>>x;
cin>>l;
cin>>b;
cin>>r;
OUTPUT:
9. What is polymorphism? Write a C++ program using the overloaded function area to
find the area of a square, circle, and rectangle. Jul.-17, Marks 6
10. Define function overloading and Write a C++ program to find the areas of the circle
(PI * r * r), rectangle (l * b), and square (x * x) by getting the r, l, b, and x through the
keyboard and printing the areas on the console using the method area() by applying the
concept of function overloading. July 18, Marks 8.
11. What is Function Overloading? Write a C++ program to define three overloaded
functions to find the sum of two integers, the sum of two floating-point numbers, and
the sum of three integers. Jan.-19, Marks 7
Function overloading means, two or more functions have the same names but different
argument lists.
The arguments may differ in the type of arguments or number of arguments, or both.
However, the return types of overloaded methods can be the same or different is called
function overloading.
Function overloading conceptshelps us to use the same function names multiple time in
the same program.
Following program demonstrates the overloaded function to find the sum of two
integers, sum of two floating-point numbers, and the sum of three integers.
#include<iostream>
return x+y;
return x+y;
return x+y+z;
int main()
OUTPUT:
14. Explain how one can bridge two classes using the friend function. Write a C++
program to find the sum of two numbers using the bridge friend function add().Jul.-17,
Marks 6
Explain how one can bridge two classes using the friend function. Write a C++ program
to find the sum of two numbers using bridge function add()
A class can have global non-member functions and member functions of other classes
as friends.
The friend function is a function that is not a member function of the class but it can
access the private and protected members of the class.
These are special functions which are declared anywhere in the class but have given
special permission to access the private members of the class.
Following program demonstrates the C++ program to find the sum of two numbers
using bridge function add().
#include<iostream>
class addtwonumbers
int x, y;
public:
addtwonumbers()
x = 0;
y = 0;
addtwonumbers(int a, int b)
x = a;
y = b;
}
friend int add(addtwonumbers &obj);
};
int main()
addtwonumbers a1;
OUTPUT:
Sum is: 0
Sum is: 30
15. What are static members of a class? Write a C++ program to count the number of
objects created. Jul.-17, Marks 5
A data member prefixed with a static keyword becomes the static member of a class.
Irrespective of the number of objects of the class are created, there exists only one copy
of the static member.
Static data members hold global data that is common to all objects of the class.
Examples of such global data are
count of objects currently present, common data accessed by all objects, etc.
Only one copy of the data member is created for the entire class and is shared by all the
objects of class, no matter how many objects are created.
Static variables are normally used to maintain values common to entire class objects.
The following program demonstrates the C++ program to count the number of objects
created.
#include<iostream>
class counter
{
private:
public:
counter()
count++;
void display()
};
int counter::count=0;
int main()
counter c1;
counter c2;
counter c3;
c2.display();
}
OUTPUT:
17. What is the inline function? Write a C++ program to find the maximum of two
numbers using the inline function. July-19, Marks 8
Write a C++ program to find the maximum of two numbers using inline function.
In normal function, First control will move from calling to called function.
Then arguments will be pushed on to the stack, then control will move back to the
calling from called function.
This process takes extra time in executing. To avoid this, we use inline function.
That is, when we call the inline function the compiler replaces the call with the
corresponding function definition, thus saving time of context switching between the
calling and called function.
The following program demonstrates the C++ program to find the maximum of two
numbers using the inline function.
#include<iostream>
if (x>y)
return x;
else
return y;
int main()
int a, b;
cin>>a;
cin>>b;
OUTPUT:
Program should read 3 student information and print the 3 student information.
#include<iostream>
class Student
private:
int rno;
char name[20];
float avgmarks;
public:
void read_data();
void print_data();
};
void Student::read_data()
cin>>rno;
cin>>name;
cin>>avgmarks;
void Student::print_data()
cout<<rno<<"\t"<<name<<"\t"<<avgmarks<<endl;
int main()
Student std[5];
{
std[i].read_data();
cout<<"RNo\tName\tAvg Marks"<<endl;
std[i].print_data();
return 0;
Output:
Student Information is
1 Mahesh 95.98
2 Rahul 99.99
3 Prabhas 100
4 Sachin 97
5 Virat 96
19. How does namespace help in preventing pollution of the global namespace?
July-17, Jan.-18, Marks 4
In the above syntax “std” is the namespace where ANSI C++ standard class libraries are
defined.
If one wants to use functions like cout, cin, endl etc, he has to include std namespace in
his program. Otherwise compiler flag an compile time error.
Syntax:
namespace namespace_name
#include<iostream>
namespace ns1
int a = 10;
namespace ns2
float a = 20.5;
}
int main()
cout<<"Namespace example"<<endl;
return 0;
Output:
Namespace example
In the above program we can use cout, endl as we have used the standard namespace
that is std in our program.
The ns1 and ns2 are the programmer generated namespaces. To access the value of a
from ns1 and ns2 we need to use scope resolution operator.
How to write a C++ program to get the employee details (emp no, emp name, basic
salary (initialized to 1000 by the constructor) and allowance) of the Employee class
through the keyboard using the method GetData() and display them using the method
DispData() on the console in the format emp no, emp name, basic salary, allowance.
Program Description:
3. Next, we add a default constructor to initialize the basic salary of all employees to
1000. That is, whenever an object of class employee is created, the basic salary of the
employee is initialized to 1000.
4. GetData() and DispData() are the two-member functions. Which are declared in class
employee and are defined outside the class using scope resolution operator.
6. Finally, GetData() is called three times to store the 3 employee information and
DispDat() is called 3 times to display the employee information.
The following program demonstrates the C++ program to get and display employee
details using an array of objects concept
#include<iostream>
class Employee
private:
int empid;
char empname[20];
int bsalary;
int allowance;
public:
Employee()
bsalary = 1000;
void GetData();
void DispData();
};
void Employee::GetData()
cin>>empid;
cin>>empname;
cin>>allowance;
void Employee::DispData()
{
cout<<endl<<empid<<"\t"<<empname<<"\t"<<bsalary<<"\t"<<allowance
;
int main()
Employee e[3];
for(int i=0;i<3;i++)
e[i].GetData();
e[i].DispData();
OUTPUT:
Enter the employee information:
21. Write a C++ program to get the employee details (empno, empname, bsalary
(initialized to 1000 by constructor) and allowance) of the Employee class through the
keyboard using the method GetData() and display them using the method DispData() on
the console in the format empno, empname, bsalary,allowance. VTU : July-18, Marks 8
22. What is a constructor? List the different types of constructors and explain the
default constructor with an example. Jan.-18, Marks 6
Constructors are special class functions which performs initialization of every object.
Constructor’s initialize values to data members after storage is allocated to the object.
While defining a constructor you must remember that the name of constructor will be
same as the name of the class, and constructors never have return type.
Constructors can be defined either inside the class definition or outside class definition
using class name and scope resolution :: operator.
1. Default Constructor
2. Parameterized Constructor
3. Copy Constructor
4. Explicit Constructor
Default constructor is the constructor which doesn’t take any argument. It has no
parameters in default constructors
Syntax :
class_name ()
Constructor Definition
#include<iostream>
class areaRect
private:
int h, w;
public:
areaRect()
h = 0;
w = 0;
int area()
{
cout<<"Enter the height of rectangle: ";
cin>>h;
cin>>w;
return h*w;
};
int main()
int result;
areaRect a1;
result = a1.area();
Output:
These are the constructors with parameters. Using this Constructor you can provide
different values to data members of different objects, by passing the appropriate values
as an argument.
Syntax :
class_name ()
Constructor Definition
#include<iostream>
class areaRect
private: int h, w;
public:
areaRect()
h = 0;
w = 0;
areaRect(int x, int y)
{
h = x;
w = y;
int area()
return h*w;
};
int main()
int result;
areaRect a1;
result = a1.area();
result = a2.area();
Output:
Area of Rectangle is: 0
The copy constructor is called whenever a new variable is created from an object.
Syntax :
Constructor Definition
#include<iostream>
class areaRect
private: int h, w;
public:
areaRect()
{
h = 10;
w = 10;
areaRect(areaRect &obj)
h = obj.h;
w = obj.w;
int area()
return h*w;
};
int main()
int result;
areaRect a1;
result = a1.area();
cout <<endl<<"Area of Rectangle is: "<<result;
areaRect a2(a1);
result = a2.area();
Output:
In C++, compiler is allowed to make one time type conversion to resolve the parameters
to functions.
Prefixing explicit keyword prevents the constructor from using the implicit conversion.
Syntax :
Constructor Definition
}
Following program demonstrates the explicit constructor:
#include<iostream>
class test
private:
int val;
val = x;
void display()
};
int main()
test t1=10;
t1.display();
Output:
23. What is a constructor? Mention its types. Explain parameterized constructor with an
example. Jan.-19, Marks 6
24. What is a constructor? Mention its type. Explain the parameterized constructor with
a suitable code. July-19, Marks 8
25. Write a C++ program to define an Employee class with the following Members: Data
Members: empid, emp salary Member Functions: to read a data and to print the data.
The program should use an array within the class to read 3 employee information and
print the 3 employee information.
Write a C++ program to define a Employee class with the following Members:
Program should use array within class to read 3 employee information and print the 3
employee information.
Source Code for C++ program to read and print Employee information
#include<iostream>
using namespace std;
class Employee
{
private:
int empid[3];
int empsal[3];
public:
void read_data();
void print_data();
};
void Employee::read_data()
{
for (int i=0; i<3;i++)
{
cout<<"Enter the Employee ID. ";
cin>>empid[i];
cout<<"Enter the Employee salary. ";
cin>>empsal[i];
}
}
void Employee::print_data()
{
for (int i=0; i<3;i++)
{
cout<<empid[i]<<"\t"<<empsal[i]<<endl;
}
}
int main()
{
Employee emp;
emp.read_data();
cout<<endl<<"Employee Information is: "<<endl;
cout<<"EmpID\tEMP Salary"<<endl;
emp.print_data();
}
Output:
123 10000
234 20000
345 30000