2014 Summer Model Answer Paper
2014 Summer Model Answer Paper
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
SUMMER – 14 EXAMINATION
Subject Code: 17432 Model Answer Subject Name: Object Oriented
Programming
____________________________________________________________________________________________________
Objects are run-time entities used to represent or model a particular piece of the system.
OR
An object is the instance of the class.
OR
An object is like a compound variable of the user defined type.
Page 1 of 44
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
SUMMER – 14 EXAMINATION
Subject Code: 17432 Model Answer Subject Name: Object Oriented
Programming
____________________________________________________________________________________________________
Class:
A class is the collection of related data and function under a single name.
OR
A class is collection of object of similar type.
Ans: Definition:
The mechanism of deriving new classes from an old one is called inheritance.
OR
Inheritance is the process by which objects of one class acquired the properties of objects
of another classes.
Types of Inheritance:
1. Single Inheritance
2. Multiple Inheritance
Page 2 of 44
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
SUMMER – 14 EXAMINATION
Subject Code: 17432 Model Answer Subject Name: Object Oriented
Programming
____________________________________________________________________________________________________
3. Multilevel Inheritance
4. Hierarchical Inheritance
5. Hybrid Inheritance
vi) What is a pointer? Write down the general syntax of its declaration.
(Definition – 1 Mark, Syntax – 1 Mark)
Ans: Definition:
Syntax:
data_type *pointer_variable_name;
Ans:
Size of Size of Operator
. Membership operator
.* Pointer-to-member operator
?: Conditional operator
Page 3 of 44
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
SUMMER – 14 EXAMINATION
Subject Code: 17432 Model Answer Subject Name: Object Oriented
Programming
____________________________________________________________________________________________________
Ans:
POP OOP
(1) In pop, more emphasis is given in (1) In oop, more comphasis is given on data
doing things. rather than procedures.
(2) Large program are divided into small (2) Large program are divided into what are
modules class functions. known as objects.
(3) Most of functions show global data. (3) Data is hidden and cannot be accessed by
external functions.
(4) Does not supports Abstraction, Inheritance (4) Supports Abstraction, Inheritance,
, Encapsulation and polymorphism . Encapsulation and polymorphism.
(6)Data moves openly around stem to system (6) Functions that operate on data of an object
from one function to another. are tied together in data structure.
(8) New data and functions cannot be easily (8)New data and functions can be easily
added whenever required. added whenever required .
Page 4 of 44
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
SUMMER – 14 EXAMINATION
Subject Code: 17432 Model Answer Subject Name: Object Oriented
Programming
____________________________________________________________________________________________________
ii) Write a program to define a structure ‘Tender’ having data members tender-no., cost and
company-name. Accept & display this data two variable of this structure.
(Structure Declaration: 2 Marks, Accept data for 2 variables: 1 Mark, Display data for 2
variables: 1 Mark)
#include<iostream.h>
struct tender
int tender_no;
float cost;
char company_name[20];
} t1,t2;
void main()
clrscr();
cout <<"Enter values for tender_number, cost and company for first tender";
cin>>t1.tender_no>>t1.cost>>t1.company_name;
cout <<"Enter values for tender_no,cost and company for second tender";
cin>>t2.tender_no>>t2.cost>>t2.company_name;
Page 5 of 44
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
SUMMER – 14 EXAMINATION
Subject Code: 17432 Model Answer Subject Name: Object Oriented
Programming
____________________________________________________________________________________________________
cout<<"\n company name="<<t1.company_name;
getch();
Ans: A derived class with multiple base classes is called as multiple inheritance .
Diagram:
Derived class
Syntax :-
{ ____ } ;
{ ____ } ;
Page 6 of 44
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
SUMMER – 14 EXAMINATION
Subject Code: 17432 Model Answer Subject Name: Object Oriented
Programming
____________________________________________________________________________________________________
class derived_class_name :visibility_mode base_class_name_1 ,…, visibility mode
{ ____ } ;
#include<conio.h>
#include<iostream.h>
class base1
public:
int b1;
void get()
cin>>b1;
void put()
cout<<"\n b=="<<b1;
};
class base2
public:
int b2;
Page 7 of 44
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
SUMMER – 14 EXAMINATION
Subject Code: 17432 Model Answer Subject Name: Object Oriented
Programming
____________________________________________________________________________________________________
void get1()
cin>>b2;
void put1()
cout<<"\n b=="<<b2;
};
public:
void get2()
get();
get1();
void put2()
put();
put1();
};
Page 8 of 44
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
SUMMER – 14 EXAMINATION
Subject Code: 17432 Model Answer Subject Name: Object Oriented
Programming
____________________________________________________________________________________________________
void main()
clrscr();
derived d;
d.get2();
d.put2();
getch();
Page 9 of 44
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
SUMMER – 14 EXAMINATION
Subject Code: 17432 Model Answer Subject Name: Object Oriented
Programming
____________________________________________________________________________________________________
hiding or information hiding. Abstraction refers to the act of representing essential features
without including the background details or explanation. Classes use the concept of abstraction
,they encapsulate all the essential properties of the object that are to be created.
4. Inheritance
Inheritance is the process by which objects of one class acquired the properties of objects of
another classes. In OOP, the concept of inheritance provides the idea of reusability. This means
that we can add additional features to an existing class without modifying it. This is possible by
deriving a new class from the existing one. The new class will have the combined feature of both
the classes.
5. Polymorphism
Polymorphism means the ability to take more than on form. An operation may exhibit different
behavior is different instances. For example, consider the operation of addition. For two numbers,
the operation will generate a sum. If the operands are strings, then the operation would produce a
third string by concatenation.
6. Dynamic Binding
Binding refers to the linking of a procedure call to the code to be executed in response to the call.
7. Message Passing
An object-oriented program consists of a set of objects that communicate with each other.
Objects communicate with one another by sending and receiving information.
Example:
Employee.Salary (name);
Page 10 of 44
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
SUMMER – 14 EXAMINATION
Subject Code: 17432 Model Answer Subject Name: Object Oriented
Programming
____________________________________________________________________________________________________
only space for member variables is allocated separately for each object as member variables will
hold different values.
As in diagramfuncti
only one copy of each
functifunction is created and shared by all objects and separate
functi
copy for eachon
variable is created.on on
declar declar decla
ations ations ratio
c) Write a program
; to declare a ;class ‘Journal’ having ns
data
; members as journal-name, price
& no-of-pages. Accept this data
Data Datafor two objects & Data
display the name of journal having
greater price.variab variab varia
le
(Class Declaration le
- 1 Mark, Creating ble Accept and display data - 2 Marks)
two objects – 1 Mark,
declar declar decla
Ans: ations
#include<conio.h> ations ratio
; ; ns ;
#include<iostream.h>
functi functi functi
class Journalon on on
declar declar decla
{ ations ations ratio
public: ; ; ns ;
int no_of_pages;
float price;
void get()
cout<<"\n Enter value of journal name and no. of pages and price";
cin>>journal_name>>no_of_pages>>price;
void put()
cout<<"\n Price="<<price;
};
void main()
Journal J1,J2;
J1.get();
J2.get();
J1.put();
else
Page 12 of 44
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
SUMMER – 14 EXAMINATION
Subject Code: 17432 Model Answer Subject Name: Object Oriented
Programming
____________________________________________________________________________________________________
J2.put();
getch();
Ans: Definition: constructor which accepts one or more arguments are called as parameterized
constructor.
Syntax:
Constructor_name( list of parameters)
{
Constructor body
}
Example:
class Number
public:
int m,n;
n=x;
m=y;
};
void main()
clrscr();
Page 13 of 44
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
SUMMER – 14 EXAMINATION
Subject Code: 17432 Model Answer Subject Name: Object Oriented
Programming
____________________________________________________________________________________________________
Number N2(10,20);
getch();
Ans: When address of an object of a class is stored into the pointer variable of the same class type then
it is pointer to object.
This pointer can be used to access the data member and member functions of same class.
#include<iostream.h>
class product
private:
int code;
float price;
public:
void getdata(void)
cin>>code;
cin>>price;
void display(void)
Page 14 of 44
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
SUMMER – 14 EXAMINATION
Subject Code: 17432 Model Answer Subject Name: Object Oriented
Programming
____________________________________________________________________________________________________
{
};
void main()
Page 15 of 44
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
SUMMER – 14 EXAMINATION
Subject Code: 17432 Model Answer Subject Name: Object Oriented
Programming
____________________________________________________________________________________________________
3. Attempt any FOUR of the following: Marks 16
a) State any four features of OOP.
(List of features (Any four) – 1 Mark each)
b) Write a program to implement single inheritance from following Figure No.1 accepts &
display the data for one table.
Class-furniture
Price
Class-Table
Surface area
Fig. No. 1
Page 16 of 44
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
SUMMER – 14 EXAMINATION
Subject Code: 17432 Model Answer Subject Name: Object Oriented
Programming
____________________________________________________________________________________________________
Ans: #include<stdlib.h>
#include<iostream.h>
class furniture
public:
char matrial[20];
int price;
};
public:
int height ;
float sur_area;
};
void main()
table t1;
cout<<"enter material";
cin>>t1.matrial;
cout<<"enter price";
cin>>t1.matrial;
cout<<"enter height";
cin>>t1.height;
Page 17 of 44
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
SUMMER – 14 EXAMINATION
Subject Code: 17432 Model Answer Subject Name: Object Oriented
Programming
____________________________________________________________________________________________________
cout<<"enter surface area";
cin>>t1.sur_area;
cout<<"price is"<<t1.matrial<<endl;
Page 18 of 44
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
SUMMER – 14 EXAMINATION
Subject Code: 17432 Model Answer Subject Name: Object Oriented
Programming
____________________________________________________________________________________________________
when base class is inherited with private visibility mode by derived class, public and protected
ember of base class becomes private members of the derived class and Private data of base
class will not inherited.
When base class is publicly Inherited then Public members of base class becomes public members
of derived class, Private data member will not be inherited and protected data of base class
become protected in derived class.
When base class inherited protected, then public and protected members of base class becomes
protected in derived class, Private data member will not be inherited
Ans: #include<iostream.h>
#include<conio.h>
void main()
{
char s[10];
char *ptr;
int count=0;
cout<<"enter string"<<endl;
cin>>s;
ptr=&s;
while(*ptr!=‟\0‟)
{
count++;
ptr++;
}
cout<<"length of string is"<<count;
getch();
}
Page 19 of 44
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
SUMMER – 14 EXAMINATION
Subject Code: 17432 Model Answer Subject Name: Object Oriented
Programming
____________________________________________________________________________________________________
e) State any four rules for operator overloading.
(For each rule - 1 Mark, any four rules are expected)
1. Only existing operators can be overloaded. New operators cannot be created.
2. The overloaded operator must have at least one operand that is of user-defined type.
3. We cannot change the basic meaning of an operator. That is to say, we cannot redefine the
plus(+) operator to subtract one value from the other.
4. Overloaded operators follows the syntax rules of the original operators. They cannot be
overridden.
5. There are operators that cannot be overloaded.
. Membership operator
.* Pointer-to-member operator
?: Conditional operator
Page 20 of 44
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
SUMMER – 14 EXAMINATION
Subject Code: 17432 Model Answer Subject Name: Object Oriented
Programming
____________________________________________________________________________________________________
f) Write a program using function overloading to swap 2 integer numbers & swap 2 float
numbers.
(Function for swapping two integers – 2 Marks, Function for swapping two Float numbers – 2
Marks)
Ans: #include<conio.h>
#include<iostream.h>
void swap(int a, int b)
{
int temp;
temp=a;
a=b;
b=temp;
cout<<"\n After integer swapping";
cout<<"\n A=="<<a<<"\nB=="<<b;
}
void swap(float x, float y)
{
float temp1;
temp1=x;
x=y;
y=temp1;
cout<<"\n After float swapping";
cout<<"\n X=="<<x<<"\nY=="<<y;
}
void main()
{
clrscr();
swap(10,20);
Page 21 of 44
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
SUMMER – 14 EXAMINATION
Subject Code: 17432 Model Answer Subject Name: Object Oriented
Programming
____________________________________________________________________________________________________
swap(10.10,20.10);
getch();
}
1. Real-time system
8. CIM/CAM/CAD systems
b) Write a program to define a class having data members principle, duration & rate-of-
interest. Declare rate-of- interest as static member variable. Calculate the simple interest &
display it for one object.
(Class declaration – 2 Marks, Calculation Function – 1 Mark, Display data function – 1 Mark)
Ans: #include<iostream.h>
#include<conio.h>
class SI
float principle;
int duration;
Page 22 of 44
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
SUMMER – 14 EXAMINATION
Subject Code: 17432 Model Answer Subject Name: Object Oriented
Programming
____________________________________________________________________________________________________
static int rate_of_interest;
public:
void get()
cin>> principle;
cin>>duration;
cout<<"\n...........................\n";
void put()
cout<<"\n________________________________\n";
};
void main()
SI s;
clrscr();
s.get();
Page 23 of 44
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
SUMMER – 14 EXAMINATION
Subject Code: 17432 Model Answer Subject Name: Object Oriented
Programming
____________________________________________________________________________________________________
cout<<"\n The simple interest is @10% \n";
s.put();
getch();
c) Illustrate the concept of constructor with default argument with suitable example.
class integer
int m,n ;
public:
void display ()
cout<< “\n”<<m;
cout<<”\t”<<n;
Page 24 of 44
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
SUMMER – 14 EXAMINATION
Subject Code: 17432 Model Answer Subject Name: Object Oriented
Programming
____________________________________________________________________________________________________
m=x;
n=y;
};
void main ()
integer i1 (200);
i1.display ();
getch ();
Page 25 of 44
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
SUMMER – 14 EXAMINATION
Subject Code: 17432 Model Answer Subject Name: Object Oriented
Programming
____________________________________________________________________________________________________
Base class
B C
#include<iostream.h>
#include<conio.h>
class emp
int id;
char name[20];
int sal;
public:
void accept()
cin>>id>>name>>sal;
void dis()
Page 26 of 44
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
SUMMER – 14 EXAMINATION
Subject Code: 17432 Model Answer Subject Name: Object Oriented
Programming
____________________________________________________________________________________________________
cout<<"id"<<id;
cout<<"name--"<<name<<endl;
cout<<"salary"<<sal;
}};
{ public:
acc1()
accept();
dis();
}} ;
{public:
acc2()
{accept();
dis() ;
}};
void main()
manager m;
clerk c;
m.acc1();
c.acc2();
getch();
Page 27 of 44
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
SUMMER – 14 EXAMINATION
Subject Code: 17432 Model Answer Subject Name: Object Oriented
Programming
____________________________________________________________________________________________________
3. „this „ is a pointer that always point to the object for which the member function was called .
4. For example, the function call A.max () will set the pointer „this‟ to the address of the object A.
Next time suppose we call B.max(), the pointer „this‟ will store address of object B.
#include<conio.h>
#include<iostream>
class sample
int a;
public:
void setdata(int x)
this ->a=x;
void putdata()
cout<<this ->a;
};
Page 28 of 44
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
SUMMER – 14 EXAMINATION
Subject Code: 17432 Model Answer Subject Name: Object Oriented
Programming
____________________________________________________________________________________________________
void main()
clrscr();
sample s;
s.setdata(100);
s.putdata();
getch();
#include <iostream>
class myclass {
int num;
public:
myclass(int x) {
num = x;
}
Page 29 of 44
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
SUMMER – 14 EXAMINATION
Subject Code: 17432 Model Answer Subject Name: Object Oriented
Programming
____________________________________________________________________________________________________
friend int isneg(myclassob);
};
int main()
{
myclass a(-1), b(2);
return 0;
}
b) Write a program to accept string from user & count number of vowels in the string using
pointer to string.
(Accepting String - 1 Mark, positioning pointer variable - 1 Mark, Counting vowels - 2 Marks)
Ans: #include<iostream.h>
#include<conio.h>
void main()
{
char string[20], *p;
int count=0;
clrscr();
cout<<"\nEnter a string:-\t";
cin>>string;
Page 30 of 44
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
SUMMER – 14 EXAMINATION
Subject Code: 17432 Model Answer Subject Name: Object Oriented
Programming
____________________________________________________________________________________________________
p=&string[0];
while(*p!='\0')
{
if(*p=='a'||*p=='e'||*p=='i'||*p=='o'||*p=='u')
{
count++;
}
else if(*p=='A'||*p=='E'||*p=='I'||*p=='O'||*p=='U')
{
count++;
}
p++;
}
cout<<"\nNumbers of vowels characters in given string are "<<count;
getch();
}
Page 31 of 44
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
SUMMER – 14 EXAMINATION
Subject Code: 17432 Model Answer Subject Name: Object Oriented
Programming
____________________________________________________________________________________________________
{
a=0;
b=0;
}
ovldcnst(int x,int y)
{
a=x;
b=y;
}
void display()
{
cout<<"\n"<<"\t"<<a<<"\t"<<b<<"\n";
}
};
void main()
{
ovldcnst o,o1(10,20);
clrscr();
o.display();
o1.display();
ovldcnst o2 = o1;
o2.display();
getch();
}
Page 32 of 44
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
SUMMER – 14 EXAMINATION
Subject Code: 17432 Model Answer Subject Name: Object Oriented
Programming
____________________________________________________________________________________________________
d) Identify the type of inheritance and implement it by writing a program for the following
figure no. 2. Assume suitable member functions.
Customer
Name,
Phone - number
Depositor
Acc-no,
Balance
Borrower
Loan-no
Loan – amount
Fig. No. 2
Page 33 of 44
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
SUMMER – 14 EXAMINATION
Subject Code: 17432 Model Answer Subject Name: Object Oriented
Programming
____________________________________________________________________________________________________
{
cout<<"\nEnter Name and Phone number:-\t";
cin>>name>>phone_number;
}
void display_c()
{
cout<<"\nThe Name is:-\t"<<name;
cout<<"\nPhone Number is:-\t"<<phone_number;
}
};
class Depositor:public Customer
{
int acc_no, balance;
public:
void accept_d()
{
cout<<"\nEnter Account Number and Balance:-\t";
cin>>acc_no>>balance;
}
void display_d()
{
cout<<"\nAccount Number is:-\t"<<acc_no;
cout<<"\nBalance is:-\t"<<balance;
}
};
class Borrower : public Depositor
{
int Loan_no, Loan_amount;
public:
void accept_b()
{
Page 34 of 44
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
SUMMER – 14 EXAMINATION
Subject Code: 17432 Model Answer Subject Name: Object Oriented
Programming
____________________________________________________________________________________________________
cout<<"\nEnter Loan Number and Loan Amount:-\t";
cin>>Loan_no>>Loan_amount;
}
void display_b()
{
cout<<"\nLoan Number is:-\t"<<Loan_no;
cout<<"\nLoan Amount is:-\t"<<Loan_amount;
}
};
void main()
{
Borrower b1;
clrscr();
b1.accept_c();
b1.accept_d();
b1.accept_b();
b1.display_c();
b1.display_d();
b1.display_b();
}
Page 35 of 44
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
SUMMER – 14 EXAMINATION
Subject Code: 17432 Model Answer Subject Name: Object Oriented
Programming
____________________________________________________________________________________________________
While accessing these functions the developer will first creates the pointer variable of class type
i.e. a pointer of object for the class which holds the virtual function in it. Then while run time one
can call required function by adjusting the pointer location.
This is also known as run time polymorphism or late binding or dynamic binding as selection of
the functions takes place at run time.
#include<iostream.h>
#include<conio.h>
class student
{
public:
int roll;
float per;
char name[10];
virtual void getdata()
{
cout<<"\n Enter the Rollno, Percentage, Name:";
cin>>roll>>per>>name;
}
virtual void putdata()
{
cout<<"Rollno"<<roll<<"has percentage"<<per<<"And Name is:"<<name;
}
};
class student1:public student
{
char branch[10];
public:
void getdata()
{
Page 36 of 44
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
SUMMER – 14 EXAMINATION
Subject Code: 17432 Model Answer Subject Name: Object Oriented
Programming
____________________________________________________________________________________________________
cout<<"\n Enter the branch:";
cin>>branch;
}
void putdata()
{
cout<<"The Branch is:"<<branch;
}
};
void main()
{
student s,*sptr;
student1 s1;
clrscr();
sptr=&s;
sptr->getdata();
sptr->putdata();
sptr=&s1;
sptr->getdata();
sptr->putdata();
getch();
}
f) Write a program to declare a class distance having data members feet & inches. Overload
unary ‘_’ operator so that when it is used with object of this class, it will decrement values of
inches by 1.
(Class declaration - 1 Mark, Operator function - 2 Marks, Accept & display value -1 Mark)
Ans: #include<iostream.h>
#include<conio.h>
class distance
{
Page 37 of 44
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
SUMMER – 14 EXAMINATION
Subject Code: 17432 Model Answer Subject Name: Object Oriented
Programming
____________________________________________________________________________________________________
int feet,inches;
public:
void accept()
{
cout<<"\nEnter distance in feet and inches";
cin>>feet>>inches;
}
void display()
{
cout<<"\nDistance is:-";
cout<<"\nFeet = "<<feet;
cout<<"\nInches = "<<inches;
}
void operator -()
{
feet--;
inches--;
}
};
void main()
{
distance d1;
clrscr();
d1.accept();
cout<<"\nDistance before Operator overloading";
d1.display();
-d1;
cout<<"\nDistance after Operator Overloading";
d1.display();
getch();
}
Page 38 of 44
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
SUMMER – 14 EXAMINATION
Subject Code: 17432 Model Answer Subject Name: Object Oriented
Programming
____________________________________________________________________________________________________
a) Write a program to declare a class ‘staff ‘having data member as name & department.
Accept this data for 10 staffs & display names of staff that are in cm department.
(Declaring class with proper functions and data members - 3 Marks, creating ten objects - 1
Mark, accepting values- 1 Mark, displaying requiring data - 3 Marks)
Ans: #include<iostream.h>
#include<conio.h>
#include<string.h>
class staff
{
char name[20],department[5];
public:
void accept()
{
cout<<"\nEnter Name and Department";
cin>>name>>department;
}
void display()
{
if(strcmpi(department,"cm")==0)
{
cout<<"\nStaff "<<name<<" works in cm department";
}
}
};
void main()
{
staff s[10];
int i;
Page 39 of 44
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
SUMMER – 14 EXAMINATION
Subject Code: 17432 Model Answer Subject Name: Object Oriented
Programming
____________________________________________________________________________________________________
clrscr();
for(i=0;i<10;i++)
{
s[i].accept();
}
for(i=0;i<10;i++)
{
s[i].display();
}
getch();
}
b) Explain the concept of virtual base class with its general syntax & suitable example.
(Explanation - 2 Marks, Diagram- 1 Mark, Syntax -2 Marks, Program -3 Marks)
Ans: Suppose you have two derived classes B and C that have a common base class A, and you also
have another class D that inherits from B and C. You can declare the base class A as virtual to
ensure that B and C share the same subobject of A.
In the following example, an object of class D has two distinct subobjects of class L, one through
class B1 and another through class B2. You can use the keyword virtual in front of the base class
specifiers in the base lists of classes B1 and B2 to indicate that only one subobject of type L,
shared by class B1 and class B2, exists.
B1 B
Page 40 of 44
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
SUMMER – 14 EXAMINATION
Subject Code: 17432 Model Answer Subject Name: Object Oriented
Programming
____________________________________________________________________________________________________
Using the keyword virtual in this example ensures that an object of class D inherits only one
subobject of class L.
#include<iostream.h>
#include<conio.h>
class student
{
intrno;
public:
void getnumber()
{
cout<<"Enter Roll No:";
cin>>rno;
}
void putnumber()
{
cout<<"\n\n\tRoll No:"<<rno<<"\n";
}
};
Page 41 of 44
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
SUMMER – 14 EXAMINATION
Subject Code: 17432 Model Answer Subject Name: Object Oriented
Programming
____________________________________________________________________________________________________
int part1,part2;
void getmarks()
{
cout<<"Enter Marks\n";
cout<<"Part1:";
cin>>part1;
cout<<"Part2:";
cin>>part2;
}
void putmarks()
{
cout<<"\tMarks Obtained\n";
cout<<"\n\tPart1:"<<part1;
cout<<"\n\tPart2:"<<part2;
}
};
Page 42 of 44
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
SUMMER – 14 EXAMINATION
Subject Code: 17432 Model Answer Subject Name: Object Oriented
Programming
____________________________________________________________________________________________________
void main()
{
result obj;
clrscr();
obj.getnumber();
obj.getmarks();
obj.getscore();
obj.display();
getch();
}
Page 43 of 44
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
SUMMER – 14 EXAMINATION
Subject Code: 17432 Model Answer Subject Name: Object Oriented
Programming
____________________________________________________________________________________________________
Page 44 of 44