2014 Winter Model Answer Paper
2014 Winter Model Answer Paper
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
WINTER – 14 EXAMINATION
Subject Code: 17432 Model Answer Subject Name: Object Oriented
Programming
Page 1 of 38
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
WINTER – 14 EXAMINATION
Subject Code: 17432 Model Answer Subject Name: Object Oriented
Programming
Page 2 of 38
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
WINTER – 14 EXAMINATION
Subject Code: 17432 Model Answer Subject Name: Object Oriented
Programming
Page 3 of 38
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
WINTER – 14 EXAMINATION
Subject Code: 17432 Model Answer Subject Name: Object Oriented
Programming
Ans: Structure is a collection of different data types written under a common name. It is a user defined
data type.
Syntax:-
structstructure_name
{
Data_type variable 1;
Data_type variable 2;
Data_type variable n;
};
(k) List any four object oriented languages.
(Each language – ½ Mark)
Ans: object oriented languages:
1. Simula
2. Smalltalk
3. C++
4. Ada
5. Java
6. C#
Page 4 of 38
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
WINTER – 14 EXAMINATION
Subject Code: 17432 Model Answer Subject Name: Object Oriented
Programming
1. A static member function can have access to only other static members declared in the same
class.
2. A static member function can be called using the class name:
Class_name::function_name;
Page 5 of 38
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
WINTER – 14 EXAMINATION
Subject Code: 17432 Model Answer Subject Name: Object Oriented
Programming
Page 6 of 38
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
WINTER – 14 EXAMINATION
Subject Code: 17432 Model Answer Subject Name: Object Oriented
Programming
Page 7 of 38
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
WINTER – 14 EXAMINATION
Subject Code: 17432 Model Answer Subject Name: Object Oriented
Programming
Ans:
3.Data move openly around the system 3. Data is hidden and cannot be accessed by
from function to function. external functions.
4.Functions transform data from one form 4. Objects communicate with each other
to another by calling each other. through function.
Page 8 of 38
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
WINTER – 14 EXAMINATION
Subject Code: 17432 Model Answer Subject Name: Object Oriented
Programming
Page 9 of 38
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
WINTER – 14 EXAMINATION
Subject Code: 17432 Model Answer Subject Name: Object Oriented
Programming
Page 10 of 38
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
WINTER – 14 EXAMINATION
Subject Code: 17432 Model Answer Subject Name: Object Oriented
Programming
cout<<"enter y";
cin>>y;
}
};
class C:public A,public B
{
public:
void display()
{
cout<<"x="<<x;
cout<<"y="<<y;
}
};
void main()
{
C z;
clrscr();
z.getA();
z.getB();
z.display();
getch();
}
Ans: Consider a situation where all three kinds of inheritance, namely, multilevel, multiple,
hierarchical inheritance, are involved. This illustrated in fig a. the child has two direct base classes
„parent1 & parent2 which themselves have a common base class “grandparent”. The child inherits
the traits of “grandparent” via two separate path .it can also inherit directly as shown by broken
line. The “grandparent” sometimes referred to as indirect base class.
Page 11 of 38
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
WINTER – 14 EXAMINATION
Subject Code: 17432 Model Answer Subject Name: Object Oriented
Programming
inheritance by the “child as shown in fig a might pose some problems. All the public & protected
members of “grandparent are inherited into “child” twice, first via “parent1& again via “parent
2.This means, “child would have duplicate sets of the members inherited from “grandparent. This
introduces ambiguity & should be avoided. The duplication of inherited members due to these
multiple paths can be avoided by making the common base class as virtual base class while
declaring the direct or intermediate base classes as shown below.
Class A //grandparent
{
//body of class
};
//body of class
};
Page 12 of 38
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
WINTER – 14 EXAMINATION
Subject Code: 17432 Model Answer Subject Name: Object Oriented
Programming
//body of class
};
};
Ans:
(c) Write program to define a class student having data members name and roll no. Accept and
display data for one object.
(Class definition - 2 Marks, creating object t - 1 Mark, Function call - 1 Mark)
Ans: #include<iostream.h>
#include<conio.h>
Page 13 of 38
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
WINTER – 14 EXAMINATION
Subject Code: 17432 Model Answer Subject Name: Object Oriented
Programming
class student
{
private:
introll_no;
char name[20];
public :
void get()
{
cout<<"\nenterrollno& name";
cin>>roll_no>>name;
}
void put()
{
cout<<"\nroll_no\t" <<roll_no;
cout<<"\nname\t"<<name;
}
};
void main()
{
student s1;
s1.get();
s1.put();
getch();
}
Page 14 of 38
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
WINTER – 14 EXAMINATION
Subject Code: 17432 Model Answer Subject Name: Object Oriented
Programming
(d) Write a program to calculate area of circle and area of rectangle using function overloading.
(Function for Calculating area of circle - 2 Marks, Function for Calculating area of rectangle
- 2 Marks)
Ans: #include<iostream.h>
#include<conio.h>
float area(float a)
{
return (3.14*a*a);
}
int area(int p,int q)
{
return(p*q);
}
void main()
{
clrscr();
cout<<"Area of circle:"<<area(6);
cout<<"Area of Rectangle:"<<area(5,6);
getch();
}
Page 15 of 38
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
WINTER – 14 EXAMINATION
Subject Code: 17432 Model Answer Subject Name: Object Oriented
Programming
Example:
#include<iostream.h>
#include<conio.h>
void main()
{
int value;
int*ptr;
Page 16 of 38
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
WINTER – 14 EXAMINATION
Subject Code: 17432 Model Answer Subject Name: Object Oriented
Programming
ptr= &value;
cout<<”memory address before incrementation=”;
cout<<ptr<<endl;
ptr++;
cout<<” memory address after incrementation=”;
cout<<ptr<<endl; }
memory address before incrementation=0X24c8ff4
memory address after incrementation=0X24c8ff5
Ans: Destructor is used to destroy then objects that have been created by a constructor. Destructor is special
member function whose name is same name as that of the class name but is preceded by tilde (~).
Syntax:
~Class_name();
e.g:
class integer
{
int m, n;
public:
~integer();
};
Only one destructor because we can‟t overload the destructor
Page 17 of 38
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
WINTER – 14 EXAMINATION
Subject Code: 17432 Model Answer Subject Name: Object Oriented
Programming
2. Single inheritance:
It includes single base class which can allow only one derived class to inherit its properties.
3. Multi-level inheritance: A single class can be derived from a single base class. We can
derive a new class from as already derived class. It includes different levels for defining class.
A child class can share properties of its base class (parent class) as well as grandparent class.
Page 18 of 38
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
WINTER – 14 EXAMINATION
Subject Code: 17432 Model Answer Subject Name: Object Oriented
Programming
4. Hierarchical inheritance: In this inheritance, multiple classes can be derived from one single
base class. All derived classes inherit properties of single base class.
Page 19 of 38
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
WINTER – 14 EXAMINATION
Subject Code: 17432 Model Answer Subject Name: Object Oriented
Programming
(c) Write a program to declare a class staff having data members as name and post. Accept and
display data for five staff members. (Using array of object)
(Class definition - 2 Marks, Creation and working of array - 2 Marks)
Ans: #include<iostream.h>
#include<conio.h>
class staff
private:
public:
void get()
cin>>name>>endl>>post;
void put()
cout<<"Name of staff="<<name<<endl;
cout<<"Name of post="<<post;
};
Page 20 of 38
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
WINTER – 14 EXAMINATION
Subject Code: 17432 Model Answer Subject Name: Object Oriented
Programming
void main()
staff s[5];
int i;
clrscr();
for(i=0;i<=4;i++)
s[i].get();
for(i=0;i<=4;i++)
s[i].put();
getch();
Ans:
1. C++ uses a unique keyword called “this” to represent an object that invokes a member
function.
2. This is a pointer that points to the object for which this function was called.
3. This is a unique pointer is automatically passed to a member function when it is called.
Page 21 of 38
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
WINTER – 14 EXAMINATION
Subject Code: 17432 Model Answer Subject Name: Object Oriented
Programming
4. The pointer this acts as an implicit argument to all the member function.
5. its an in-built pointer so programmer doesn‟t need to explicitly create this pointer.
6. one class can have exactly one this pointer.
Example:
class ABC
{
int a;
…
voidgetdata()
{
}
…};
The private variable „a‟ can be used directly inside a member function, like
a=123;
We can also use the following statement to do the same job:
this->a=123;
this->getdata();
Page 22 of 38
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
WINTER – 14 EXAMINATION
Subject Code: 17432 Model Answer Subject Name: Object Oriented
Programming
6. Unary operators overloaded by means of a member function, take no explicit arguments and
return no explicit values, but those overloaded by means of a friend function, take one
reference argument.
7. Binary operators overloaded through a member function take one explicit argument and those
which are overloaded through a friend function take two explicit arguments.
Ans: In object as function argument we send an object of a class to member function as an argument
which allows programmer to share the data and functions associated with that object. This
facilitates communication between two different classes. There are two types of having object as
function arguments. As follows
Object as function argument: - where actual object is sent as an argument
Friend function: - where the reference of an object is sent as an argument to a function of another
class.
Example:-
#include <iostream.h>
class rational
{
private:
intnum;
intdnum;
public:
void get ()
{
cout<<"enter numerator";
cin>>num;
cout<<"enter denominator";
cin>>dnum;
}
Page 23 of 38
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
WINTER – 14 EXAMINATION
Subject Code: 17432 Model Answer Subject Name: Object Oriented
Programming
void print ()
{
cout<<num<<"/"<<dnum<<endl;
}
void multi(rational r1,rational r2) // receiving object as argument
{
num=r1.num*r2.num;
dnum=r1.dnum*r2.dnum;
}
};
void main ()
{
rational r1,r2,r3;
r1.get();
r2.get();
r3.multi(r1,r2); // sending object as an argument
r3.print();
}
Page 24 of 38
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
WINTER – 14 EXAMINATION
Subject Code: 17432 Model Answer Subject Name: Object Oriented
Programming
Difference between member function & normal function is that member function
incorporates membership „identity label‟ in header
General form of member function definition is as:
classclass_name
{
…
…
…
public:
return_typefunction_name(argument(s));
};
return-type class-name:: function-name(argument(s))
{
function body
}
Membership label class-name:: tell complier that function-name belongs to class class-
name. This is scope of function restricted to class-name specified in header line. Scope
resolution operator (::) is used.
For example member functions getdata() &putdata() of class item can be coded as follows:
void item :: getdata(int a, float b)
{
number =a;
cost= b;
}
o Inside Class Definition:
This is method of defining member function is that member function declaration are
replaced by actual function definition inside the class
For example item class can be defined as:
class item
{
int number; //variable declaration
Page 25 of 38
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
WINTER – 14 EXAMINATION
Subject Code: 17432 Model Answer Subject Name: Object Oriented
Programming
Ans:
Page 26 of 38
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
WINTER – 14 EXAMINATION
Subject Code: 17432 Model Answer Subject Name: Object Oriented
Programming
Ans:
Polymorphism refers to property by which objects belonging to different classes are classes are
able to respond to same message, but in different forms.
Therefore an essential requirement of polymorphism is ability to refer to object without any
regard of their classes. This requires use of single pointer variable to refer to objects of
different classes.
Here we use pointer to base class to refer to all derived objects. Base pointer even when it is
made to contain address of derived class, always executes function in base class.
Compiler simply ignores content of pointer & chooses member function that matches type of
pointer. In this case polymorphism is achieved by using virtual functions.
When we use same function name in both base & derived classes, function in base class is
declared as virtual using keyword virtual preceding its normal declaration.
When function is made virtual C++ determines which function to runtime based on type of
object pointed to by base pointer rather than type of pointer.
Thus by making base pointer to point to different objects different version of virtual function
can be executed.
Runtime polymorphism is achieved only when virtual function accessed through pointer to
base class.
Example:
//Virtual function
#include<iostream.h>
#include<conio.h>
class base
{
public:
void display()
{ cout<<"\n Display Base"; }
Page 27 of 38
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
WINTER – 14 EXAMINATION
Subject Code: 17432 Model Answer Subject Name: Object Oriented
Programming
Ans: CODE:
#include<iostream.h>
Page 28 of 38
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
WINTER – 14 EXAMINATION
Subject Code: 17432 Model Answer Subject Name: Object Oriented
Programming
#include<conio.h>
void main()
{
int a[5], i,*a1, no, flag=0;
clrscr();
a1=&a[0];
cout<<"\n Enter array elements: \n";
for(i=0; i<5; i++)
{
cout<<"Enter "<<i<<" elements: \n";
cin>>*a1;
a1++;
}
cout<<"Enter element to be searched:\n";
cin>>no;
a1=&a[0];
for(i=0; i<5; i++)
{
if(*a1==no)
{
cout<<"Number is present at "<<i+1<<" Position.\n";
flag++;
a1++;
}
else
{
a1++;
}
}
if(flag == 0)
{ cout<<" Number is not present.\n"; }
Page 29 of 38
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
WINTER – 14 EXAMINATION
Subject Code: 17432 Model Answer Subject Name: Object Oriented
Programming
getch();
}
(e) Explain single inheritance with program.
(Description with diagram - 2 Marks; Program - 2 Marks; Any Relevant Program Shall Be
Considered)
Ans: Derived class with only one base class is called single inheritance.
#include<iostream.h>
#include<conio.h>
class dimension
{
intlength,width, height;
public:
voidgetdata()
{
cout<<”Enter length, width ,Heigth\n”;
cin>>length>>width>>height;
}
void display()
{
cout<<”Length:”<<length<<endl<<”Width:”<<width<<endl<<”Height:”<<height;
}
Page 30 of 38
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
WINTER – 14 EXAMINATION
Subject Code: 17432 Model Answer Subject Name: Object Oriented
Programming
};
classbookshelf: public dimension
{
intno_of_shelves;
public:
void accept()
{
cout<<”Enter No_Of_Shelves:”;
cin>>no_of_shelves;
}
void show()
{
cout<<” No_Of_Shelves:”<<no_of_shelves;
}
};
void main()
{
bookshelf b;
b.getdata();
b.display();
b.accept();
b.show();
getch();
}
Ans:
A constructor is special member function whose task is to initialize objects of it‟s class. It is
special because its name is same as that of class name.
Page 31 of 38
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
WINTER – 14 EXAMINATION
Subject Code: 17432 Model Answer Subject Name: Object Oriented
Programming
Complex C (5.0);
Assigns value 5.0 to real variable & 0.0 to imag (by default)
Statement
Complex C (2.0,3.0);
Actual parameter when specified overrides default value. Missing argument must be trailing
arguments
“Default constructor” A :: A() is totally different than “Constructor with default argument” A
:: A (int = 0)
Default argument constructor can be called with either one or no argument. When called with
no argument it becomes default constructor
When both these forms are used in class it causes ambiguity for statement such as
A a;
Ambiguity is whether to call A :: A() or A :: A(int = 0)
Page 32 of 38
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
WINTER – 14 EXAMINATION
Subject Code: 17432 Model Answer Subject Name: Object Oriented
Programming
CODE:
#include<iostream.h>
#include<conio.h>
class student
{
protected:
introll_no;
char name[15];
public:
void get()
{
cout<<"\n Enter Roll no & name";
cin>>roll_no>>name;
Page 33 of 38
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
WINTER – 14 EXAMINATION
Subject Code: 17432 Model Answer Subject Name: Object Oriented
Programming
}
void show()
{
cout<<"\n Roll_no:"<<roll_no;
cout<<"\n Name:"<<name;
}
};
classtest:public student
{
protected:
int mark1,mark2;
public:
void get1()
{
cout<<"\n Enter marks of 2 subjects";
cin>>mark1>>mark2;
}
void show1()
{
cout<<"\n Marks Are: \n1)"<<mark1<<"\n2)"<<mark2;
}
};
classresult:public test
{
protected:
int total;
public:
voidcal()
{
total=mark1+mark2;
cout<<"\n Total is:"<<total;
Page 34 of 38
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
WINTER – 14 EXAMINATION
Subject Code: 17432 Model Answer Subject Name: Object Oriented
Programming
}
};
void main()
{
result R;
R.get();
R.show();
R.get1();
R.show1();
R.cal();
getch();
}
(b) Write a program to declare a class birthday having data members day, month, year. Accept
this information for five objects using pointer to the array of objects.
(Implementation of Class with Appropriate Functions - 4 Marks, Function Call - 4 Marks)
Ans: #include<iostream.h>
#include<conio.h>
class birthday
{
intday,month, year;
public:
voidgetdata()
{
cout<<" Enter birthdate(day month year):\n";
cin>>day>>month>>year;
}
voidputdata()
{
cout<<" "<<day<<"/ "<<month<<"/ "<<year;
Page 35 of 38
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
WINTER – 14 EXAMINATION
Subject Code: 17432 Model Answer Subject Name: Object Oriented
Programming
}
};
void main()
{
int i;
birthday B[5], *ptr;
clrscr();
ptr= &B[0];
for(i=0; i<5;i++)
{
cout<<"Enter birthdate for object;"<<i+1;
ptr->getdata();
ptr++;
}
ptr= &B[0];
for(i=0; i<5;i++)
{
cout<<"\nBirthdate for object: "<<i+1;
ptr->putdata();
ptr++;
}
getch();
}
(Description of Overloaded Constructor - 4 Marks, Example - 4 Marks, any relevant example shall
be considered)
Ans:
There are no argument constructor, one argument constructor & even parameterized
constructor
Page 36 of 38
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
WINTER – 14 EXAMINATION
Subject Code: 17432 Model Answer Subject Name: Object Oriented
Programming
Page 37 of 38
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
WINTER – 14 EXAMINATION
Subject Code: 17432 Model Answer Subject Name: Object Oriented
Programming
When more than one constructor function is defined in class then we say that constructor
is overloaded.
Program:
#include<iostream.h>
#include<conio.h>
classoop
{
inta,b;
public:
oop()
{
a=100;
b=50;
}
oop(int x,int y)
{
a=x;
b=y;
}
voidputdata()
{
cout<<"\nValue 1"<<a;
cout<<"\nValue 2"<<b;
}
};
void main()
{
intp,q;
oop o1;
oop o2(50,100);
clrscr();
o1.putdata();
o2.putdata();
getch();
}
Page 38 of 38