0% found this document useful (0 votes)
48 views32 pages

OOP Exam Model Answer Guide

The question paper "22316-2018-Winter" refers to the MSBTE (Maharashtra State Board of Technical Education) summer 2023 examination for subject code 22316. This subject is part of the curriculum for diploma engineering students under the MSBTE syllabus.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views32 pages

OOP Exam Model Answer Guide

The question paper "22316-2018-Winter" refers to the MSBTE (Maharashtra State Board of Technical Education) summer 2023 examination for subject code 22316. This subject is part of the curriculum for diploma engineering students under the MSBTE syllabus.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION

(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)

WINTER – 2018 EXAMINATION


MODEL ANSWER
Instructions to examiners:
Subject: Object Oriented Programming
22316
with C++ Subject Code: Important

1) The answers should be examined by key words and not as word-to-word as given in the model
answer scheme.
2) The model answer and the answer written by candidate may vary but the examiner may try to
assess the understanding level of the candidate.
3) The language errors such as grammatical, spelling errors should not be given more Importance
(Not applicable for subject English and Communication Skills).
4) While assessing figures, examiner may give credit for principal components indicated in the
figure. The figures drawn by candidate and model answer may vary. The examiner may give
credit for any equivalent figure drawn.
5) Credits may be given step wise for numerical problems. In some cases, the assumed
constant values may vary and there may be some difference in the candidate’s answers and
model answer.
6) In case of some questions credit may be given by judgement on part of examiner of
relevant answer based on candidate’s understanding.
7) For programming language papers, credit may be given to any other program based on
equivalent concept.
Q. Sub Answer Marki
No Q.N. ng
. Schem
e

1. a) Attempt any FIVE of the following: 10


Ans. State any four object oriented languages. 2M
Object oriented programming language:
∙ C++
∙ Smalltalk Any 4
∙ Object pascal languag
∙ java es ½
∙ Simula M
∙ Ada each
∙ Turbo pascal
∙ Eiffel
∙ C#
∙ Python
b) Describe use of protected access specifier used in the class. 2M
Ans. Protected access specifier is use to declare a class member that is Correct
accessible by the member functions within its class and any class use 2M
immediately derived from it.

Page 1 / 25
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)

WINTER – 2018 EXAMINATION


MODEL ANSWER
22316
Subject: Object Oriented Programming
with C++ Subject Code:

c) Differentiate between OOP and POP 2M


Ans
Sr.
PROCEDURE
OBJECT ORIENTED
No. Any
ORIENTED two
PROGRAMMING releva
PROGRAMMING (POP) nt
(OOP) differ
1 Focus is on doing things en
Focus is on data rather than ces
(procedure). 1M each
procedure.
2 Large programs are divided
Programs are divided into
into multiple functions.
multiple objects.
3 Data move openly around
Data is hidden and cannot
the system from function to
be accessed by external
function.
functions.
4 Functions transform data
Objects communicate with
from one form to another
each other through function.
by calling each other.
5 Employs top-down
Employs bottom-up
approach in program
approach in
design.
program design
6 Procedure oriented
Object oriented approach is
approach is used in C
used in
language.
C++ language.

d) Write any two characteristics of destructor. 2M


Ans. Characteristics:
1. It is used to destroy objects created by a
constructor. 2. Name of destructor and name of the Any
class is same. two
3. Its name is preceded with tilde (~) symbol. chara
4. It never takes any argument. cte
5. It does not return any value. ristics
6. It is invoked implicitly by the compiler upon exit from the 1M each
program (or block or function) i.e when scope of object is over.

e) Describe meaning of the following 2M


(i) ios : : in
(ii) ios : : out Meanin
Ans. (i) ios : : in : It is a file mode. It is used to open a file in read only g of ‘in’
mode. 1M
Meanin
(ii) ios : : out : It is a file mode. It is used to open a file in write g of
only mode. ‘out’
1M

Page 2 / 25
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)

WINTER – 2018 EXAMINATION


MODEL ANSWER
22316
Subject: Object Oriented Programming
with C++ Subject Code:
f) Give output for following code: 2M
class student
{
int roll no;
char name [14];
} s[6];
void main()
{
cout<<sizeof(s);
}
Ans Considering roll_no(Single variable) the output is: 96 Correct
OR output
Considering roll, no (Two variables) the output is: 2M
108 OR
Considering roll no the output is: error – space between roll and no

g) Write syntax to define a derived class 2M


Ans Syntax:
class derived_class_name : visibility_mode/access_specifier Correct
base_class_name syntax
{ 2M
class body
};

2 a) Attempt any THREE of the following 12


Write a C++ program to accept array of five elements, find and 4M
Ans display smallest number from an array.
#include<iostream.h> Correct
#include<conio.h> logic
void main() 2M
{
int a[5],smallest,i;
clrscr(); Correct
cout<<" Enter array elements:"; syntax
for(i=0;i<5;i++) 2M
cin>>a[i];
smallest=a[0];
for(i=1;i<5;i++)
{
if(a[i]<smallest)

Page 3 / 25
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
WINTER – 2018 EXAMINATION
MODEL ANSWER
22316
Subject: Object Oriented Programming
with C++ Subject Code:

{
smallest=a[i];
}
}
cout<<endl<<"Smallest number="<<smallest;
getch();
}

b) Write a C++ program to declare a class ‘College’ with data 4M


members as name and college code. Derive a new class ‘student’
from the class college with data members as sname and roll no.
Accept and display details of one student with college data.
Ans #include<iostream.h> Decla
#include<conio.h> rat
class college ion
{ and
char name[10]; Defini
int collegecode; tio n
public: of
void getcollege() Base
{ Class
cout<<"Enter college name:"; 1M
cin>>name;
cout<<"Enter college code:"; Decla
cin>>collegecode; rat
} ion
void putcollege() and
{ Defini
cout<<endl<<"College name="<<name; tio n
cout<<endl<<"College code="<<collegecode; of
} Derived
}; Class
class student:public college 2M
{
char sname[10]; Main
int rollno; functio
public: n 1M
void getstudent()
{
cout<<"Enter student name";
Page 4 / 25
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)

WINTER – 2018 EXAMINATION


MODEL ANSWER
22316
Subject: Object Oriented Programming
with C++ Subject Code:

cin>>sname;
cout<<"Enter roll no:";
cin>>rollno;
}
void putstudent()
{
cout<<endl<<"Student name:="<<sname;
cout<<endl<<"Roll no:="<<rollno;
}
};
void main()
{
student s;
clrscr();
s.getcollege();
s.getstudent();
s.putcollege();
s.putstudent();
getch();
}
c) Write a C++ program to declare a class ‘circle’ with data 4M
members as radius and area. Declare a function getdata to
accept radius and putdata to calculate and display area of
Ans circle. #include<iostream.h>
#include<conio.h> Decalar
class circle ation
{ and
float radius,area; Defini
public: tio n
void getdata() of
{ class
cout<<"Enter radius:"; with
cin>>radius; functi
} on s
void putdata() 3M
{
area=3.14*radius*radius;
cout<<"Area of circle="<<area;

Page 5 / 25
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)

WINTER – 2018 EXAMINATION


MODEL ANSWER
22316
Subject: Object Oriented Programming
with C++ Subject Code:

} Main
}; functi
void main() on 1M
{
circle c;
clrscr();
c.getdata();
c.putdata();
getch();
}
d) With suitable example, describe effect of ++ and - - operators 4M
used with pointer in pointer arithmetic.
Ans. ++ Operator: - It is referred as increment operator that increments
the value of variable. If ++ operator is used with pointer variable, Descr
then pointer variable points to next memory address that means ipt
pointer increment with respect to size of the data type used to ion of
declare pointer variable. ++
operat
Example:- or 1M
int a[5]={10,20,30,40,50},*ptr;
ptr=a[0];
for(i=0;i<5;i++) Any
{ relevan
cout<<*ptr; t
ptr++; Examp
} le 1M
In the above example, ptr points to memory location of a[0].
Increment statement ptr++ increments ptr by memory size of int i.e
2 bytes and ptr points to a[1].

- - Operator: - It is referred as decrement operator that decrements


the value of variable. If - - operator is used with pointer variable,
then pointer variable points to previous memory address that means Descri
pointer decrement with respect to size of the data type used to pt ion
declare pointer variable. of - -
operat
or 1M

Page 6 / 25
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)

WINTER – 2018 EXAMINATION


MODEL ANSWER
22316
Subject: Object Oriented Programming
with C++ Subject Code:
Example:- Exam
int a[5]={10,20,30,40,50},*ptr; ple
ptr=a[4]; 1M
for(i=0;i<5;i++)
{
cout<<*ptr;
ptr- -;
}

In the above example, ptr points to memory location of a[4].


Decrement statement ptr- - decrements ptr by memory size of int i.e
2 bytes and ptr points to a[3].

3 a) Attempt any THREE of the following 12


Write a C++ program to declare a class addition with data 4M
members as x and y. Initialize values of x and y with
Ans. constructor. Calculate addition and display it using function
‘display’. #include<iostream.h>
#include<conio.h>
class addition
{ Decla
int x,y; rat
public: ion
addition(int,int); and
void display(); defini
}; tio n
addition::addition (int x1,int y1) of
{ class
x=x1; with
y=y1; constr
} uc
void addition::display() tor
{ and
cout<<"\nAddition of two numbers is:"<<(x+y); display
} functio
void main() n 3M
{
addition a(3,4);

Main
functio
n 1M

Page 7 / 25
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)

WINTER – 2018 EXAMINATION


MODEL ANSWER
22316
Subject: Object Oriented Programming
with C++ Subject Code:

a.display();
getch();
}

b) With suitable diagram describe structure of C++ 4M


Ans program. General C++ program has following structure.

INCLUDE HEADER FILES Correct


diagra
CLASS DECLARATION m 2M
MEMBER FUNCTIONS DEFINITIONS
MAIN FUNCTION PROGRAM

Description:-
1. Include header files
In this section a programmer include all header files which are
require to execute given program. The most important file is
iostream.h header file. This file defines most of the C++statements Descr
like cout and cin. Without this file one cannot load C++ program. ipt
2. Class Declaration ion
In this section a programmer declares all classes which are 2M
necessary for given program. The programmer uses general syntax
of creating class.
3. Member Functions Definition
This section allows programmer to design member functions of a
class. The programmer can have inside declaration of a function or
outside declaration of a function.
4. Main Function Program
In this section programmer creates objects and calls various
functions writer within various class.

c) Describe the concept of virtual base class with suitable example. 4M


Note: Program/diagram with syntax shall be considered as an
example.
Ans. Virtual Base Class:
An ancestor class is declared as virtual base class which is used to Descr
avoid duplication of inherited members inside child class due to ipt
multiple path of inheritance. ion
2M

Page 8 / 25
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)

WINTER – 2018 EXAMINATION


MODEL ANSWER
22316
Subject: Object Oriented Programming
with C++ Subject Code:
Examp
le 2M

Consider a hybrid inheritance as shown in the above diagram. The


child class has two direct base classes, Parent1 and Parent2 which
themselves have a common base class as Grandparent. The child
inherits the members of Grandparent via two separate paths. All the
public and protected members of Grandparent are inherited into
Child twice, first via Parent1 and again via Parent2. This leads to
duplicate sets of the inherited members of Grandparent inside Child
class. The duplication of inherited members 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 Grandparent
{
};
class Parent1:virtual public Grandparent
{
};
class Parent2:virtual public Grandparent
{
};
class Child: public Parent1,public Parent2
{
};

Example
#include<iostream.h>
#include<conio.h>
class student
{
int rno;

Page 9 / 25
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)

WINTER – 2018 EXAMINATION


MODEL ANSWER
22316
Subject: Object Oriented Programming
with C++ Subject Code:

public:
void getnumber()
{
cout<<"Enter Roll No:";
cin>>rno;
}
void putnumber()
{
cout<<"\n\n\t Roll No:"<<rno<<"\n";
}
};
class test: virtual public student
{
public:
int part1,part2;
void getmarks()
{
cout<<"Enter Marks\n";
cout<<"Part1:";
cin>>part1; cout<<"Part2:";
cin>>part2;
}
void putmarks()
{
cout<<"\t Marks Obtained\n";
cout<<"\n\t Part1:"<<part1;
cout<<"\n\tPart2:"<<part2;
}
};
class sports: public virtual student
{
public:
int score;
void getscore()
{
cout<<"Enter Sports Score:";
cin>>score;
}
void putscore()

Page 10 / 25
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)

WINTER – 2018 EXAMINATION


MODEL ANSWER
22316
Subject: Object Oriented Programming
with C++ Subject Code:

{
cout<<"\n\t Sports Score is:"<<score;
}
};
class result: public test, public sports
{
int total;
public:
void display()
{
total=part1+part2+score;
putnumber();
putmarks();
putscore();
cout<<"\n\t Total Score:"<<total;
}
};
void main()
{
result obj;
clrscr();
obj.getnumber();
obj.getmarks();
obj.getscore();
obj.display();
getch();
}
d) Describe use of static data member in C++ with 4M
Ans example. Use of static data member:
1. Static data member is used to maintain values common to the entire Use of
class. static
2. It is initialized to zero when the first object of its class is created. data
3. Only one copy of that member is created for the entire class and is memb
shared by all the objects of that class. er
2M
Example:
#include<iostream.h>
#include<conio.h> Releva
class test nt
{ exampl
e 2M

Page 11 / 25
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)

WINTER – 2018 EXAMINATION


MODEL ANSWER
22316
Subject: Object Oriented Programming
with C++ Subject Code:
static int count;
int obj_no;
public:
void getdata()
{
obj_no=++count;
cout<<"\n Object number="<<obj_no;
}
static void showcount()
{
cout<<"\n total number of objects="<<count;
}
};
int test::count;
void main()
{
test t1,t2;
clrscr();
t1.getdata();
t2.getdata();
test::showcount();
test t3;
t3.getdata();
test::showcount();
getch();
}

4 a) Attempt any THREE of the following 12


Write a C++ program to implement inheritance shown in 4M
following figure:

Accept and display data of one teacher and one student using
object of class ‘Info’
Note: Any other correct logic of multiple inheritance in
program shall be considered.

Page 12 / 25
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
WINTER – 2018 EXAMINATION
MODEL ANSWER
22316
Subject: Object Oriented Programming
with C++ Subject Code:

Ans #include<iostream.h> Correct


#include<conio.h> defini
class Teacher tio n
{ of
protected: class -
char Name[20]; Teach
int empid; er
}; 1M
class Student
{ Correct
protected: defini
char sname[20]; tio n
int rollno; of
}; class
class Info:public Teacher,public Student Student
{ 1M
public:
void acceptT() Correct
{ defini
cout<<"\nEnter data for teacher:"; tio n
cout<<"\nName:"; of
cin>>Name; class
cout<<"\nEmployee id:"; Info
cin>>empid; 1M
}

void displayT()
{
cout<<"\nTeacher's data is:";
cout<<"\nName:"<<Name;
cout<<"\nEmployee id:"<<empid;
}
void acceptS()
{
cout<<"\nEnter student's data:";
cout<<"\nName:";
cin>>sname;

Page 13 / 25
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)

WINTER – 2018 EXAMINATION


MODEL ANSWER
22316
Subject: Object Oriented Programming
with C++ Subject Code:

cout<<"\nRoll no:"; Correct


cin>>rollno; defini
} tio n
void displayS() of
{ main
cout<<"\nStudent's data is:"; functio
cout<<"\nName:"<<sname; n 1M
cout<<"\nRoll no:"<<rollno;
}
};
void main()
{
Info I;
clrscr();
I.acceptT();
I.displayT();
I.acceptS();
I.displayS();
getch();
}

b) Write a C++ program to print multiplication table of 4M


7. (example: 7 x 1 ….7 x 10 = 70)
Ans #include<iostream.h> Correct
#include<conio.h> logic
void main() 2M
{
int num;
clrscr();
cout<<"Multiplication table for 7 is:"<<endl; Correct
for(num=1;num<=10;num++) syntax
{ 2M
cout<<"7 *"<<num<<"="<<7*num<<endl;
}
getch();
}

c) Write a C++ program to swap two integer numbers and swap 4M


two float numbers using function overloading.

Page 14 / 25
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)

WINTER – 2018 EXAMINATION


MODEL ANSWER
22316
Subject: Object Oriented Programming
with C++ Subject Code:

Ans (Hint: overload swap function) Correct


Note: Any other relevant logic shall be considered. logic
2M

#include<iostream.h> Correct
#include<conio.h> syntax
void swap(int a,int b) 2M
{
int temp;
temp=a;
a=b;
b=temp;
cout<<"\nInteger values after swapping are:"<<a<<"
"<<b; }
void swap(float x,float y)
{
float temp1=x;
x=y;
y=temp1;
cout<<"\nFloat values after swapping are:"<<x<<"
"<<y; }
void main()
{
clrscr();
swap(10,20);
swap(10.15f,20.25f);
getch();
}
d) Write a C++ program to count number of spaces present in 4M
contents of file.
Note: Any other relevant logic shall be considered
Ans #include<iostream.h> Correct
#include<fstream.h> logic
#include<conio.h> 2M
void main()
{
ifstream file;
charch; Correct

Page 15 / 25
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)

WINTER – 2018 EXAMINATION


MODEL ANSWER
22316
Subject: Object Oriented Programming
with C++ Subject Code:

int s=0; syntax


clrscr(); 2M
file.open("abc.txt");
while(file)
{
file.get(ch);
if(ch==' ')
{
s++;
}
}
cout<<"\nNumber of spaces present in the content of the given
file are:"<<s;
getch();
}
e) Write a C++ program to find greatest number among two 4M
numbers from two different classes using friend function.
Ans. #include<iostream.h>
#include<conio.h>
class second;
class first
{
int x;
public: Correct
void getx() defini
{ tio n
cout<<"\nEnter the value of x:"; of
cin>>x; class
} first
friend void max(first,second); 1M
};
class second
{
int y;
public: Correct
void gety() defini
{ tio n
cout<<"\nEnter the value of y:"; of
class
second

Page 16 / 25
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)

WINTER – 2018 EXAMINATION


MODEL ANSWER
22316
Subject: Object Oriented Programming
with C++ Subject Code:
cin>>y; 1M
}
friend void max(first,second);
};
void max(first a,second b) Correct
{ defini
if(a.x>b.y) tio n
{ of
cout<<"\Greater value is:"<<a.x; friend
} functi
else on 1M
{ ,
cout<<"\nGreater value is:"<<b.y;
}
}
void main()
{
first a; Correct
second b; defini
clrscr(); tio n
a.getx(); of
b.gety(); main
max(a,b); functio
getch(); n 1M
}

5 a) Attempt any TWO of the following 12


Write a C++ program to overload binary operator ‘+’ to 6M
concatenate two strings.
Ans
#include<iostream.h>
#include<conio.h> Creati
#include<string.h> ng
class opov Class
{ 2M
char str1[10];
public:
void getdata() Operato
{ r
Functio

Page 17 / 25
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)
WINTER – 2018 EXAMINATION
MODEL ANSWER
22316
Subject: Object Oriented Programming
with C++ Subject Code:

cout<<"\nEnter a strings"; n
cin>>str1; 2M
}
void operator +(opov o)
{
cout<<strcat(str1,o.str1);
}
};
void main() Main
{ Functio
opov o1,o2; n
clrscr(); 2M
o1.getdata();
o2.getdata();
o1+o2;
getch();
}

b) Write a C++ program to write ‘Welcome to poly’ in a file. Then 6M


read the data from file and display it on screen.
Note: Any other relevant logic shall be considered
Ans #include<iostream.h>
#include<conio.h> Writing
#include<fstream.h> data in
void main() file
{ 3M
char str[25] = "Welcome to poly",ch;
clrscr(); Readi
ofstream fout; ng
fout.open("output.txt"); data
fout<<str; from
fout.close(); file
ifstream fin; and
fin.open("output.txt"); display
while (!fin.eof()) on
{ screen
fin.getline(str, 25); 3M
cout<<str<<endl;
}
Page 18 / 25
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)

WINTER – 2018 EXAMINATION


MODEL ANSWER
22316
Subject: Object Oriented Programming
with C++ Subject Code:

fin.close();
getch();
}
c) Write a C++ program to declare a class ‘Account’ with data 6M
members as accno, name and bal. Accept data for eight
accounts and display details of accounts having balance less
Ans than 10,000. #include<iostream.h>
#include<conio.h> Creati
class Account ng
{ Class
long int accno, bal; 2M
char name[10];
public: Logic
void getdata() to
{ Displa
cout<<"\nEnter account number, balance and name "; y
cin>>accno>>bal>>name; object
} with
void putdata() given
{ conditio
if(bal>10000) n
{ 1M
cout<<"\nThe Account Number is "<<accno;
cout<<"\nThe Balance is "<<bal; Creati
cout<<"\nThe Name is "<<name; ng 8
} objects
} 1M
};
void main()
{ Calling
Account a[8]; functi
int i; on s
clrscr(); 2M
for(i=0;i<8;i++)
{
a[i].getdata();
}
for(i=0;i<8;i++)
{

Page 19 / 25
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)

WINTER – 2018 EXAMINATION


MODEL ANSWER
with C++ Subject Code:
Subject: Object Oriented Programming 22316
a[i].putdata();
}
getch();
}

6 a) Attempt any TWO of the following 12


(i) Write a C++ program to find whether the entered number is 6M
even or odd.
(ii) Write a C++ program to declare a structure employee with
members as empid and empname. Accept and display data for
one employee using structure variable.
Ans
(i) Write a C++ program to find whether the entered number is
even or odd.
Accep
#include<iostream.h> tin g
#include<conio.h> Numb
void main() er 1M
{
int num; Condi
clrscr(); tio n
cout<<"\nEnter a Number "; to
cin>>num; check
if(num%2==0) numb
{ er
cout<<"\nEntered number is even"; 1M
}
else Display
{ result
cout<<"\nEntered number is odd"; 1M
}
getch();
}

(ii) Write a C++ program to declare a structure employee with


members as empid and empname. Accept and display data for
one employee using structure variable.

#include<iostream.h>
#include<conio.h>
Creati
ng
struct
ur

Page 20 / 25
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)

WINTER – 2018 EXAMINATION


MODEL ANSWER
22316
Subject: Object Oriented Programming
with C++ Subject Code:

struct employee e with


{ specifi
int empid; ed
char empname[10]; membe
}; r
void main() 1M
{
employee e;
clrscr(); Accep
cout<<"\nEnter employee id and Employee Name "; tin g
cin>>e.empid>>e.empname; and
cout<<"\mThe Employee Id is "<<e.empid; displayi
cout<<"\nThe Employee Name is "<<e.empname; ng
getch(); values
} 2M
b) Write a C++ program to implement following 6M
inheritance.

Accept and display data for one programmer and one manager.
Make display function virtual.
Ans.
#include<iostream.h>
#include<conio.h>
class Employee
{ Creati
int empid,empcode; ng all
public: classes
void emp() 3M
{
cout<<"\nEnter an employee id ";
cin>>empid;
cout<<"\nEnter an employee code ";
cin>>empcode;

Page 21 / 25
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)

WINTER – 2018 EXAMINATION


MODEL ANSWER
22316
Subject: Object Oriented Programming
with C++ Subject Code:
} Main
void virtual display() Functio
{ n
cout<<"\nEmployee id "<<empid; 3M
cout<<"\nEmployee code"<<empcode;
}
};
class Programmer : public Employee
{
char Skill[10];
public:
void getskill()
{
cout<<"\nEnter a Skill for Programmer ";
cin>>Skill;
}
void display()
{
cout<<"\nThe Programmer Skill is "<<Skill;
}
};
class Manager : public Employee
{
char department[10];
public:
void getdept()
{
cout<<"\nEnter a Department for Manager ";
cin>>department;
}
void display()
{
cout<<"\nThe Department of Manager is
"<<department; }
};
void main()
{
Employee e, *eptr;
Programmer p;

Page 22 / 25
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)

WINTER – 2018 EXAMINATION


MODEL ANSWER
22316
Subject: Object Oriented Programming
with C++ Subject Code:

Manager m;
clrscr();
cout<<"\nFor Programmer Class ";
eptr = &e;
eptr->emp();
p.getskill();
eptr->display();
eptr= &p;
eptr->display();

cout<<"\nFor Manager Class ";


eptr = &e;
eptr->emp();
m.getdept();
eptr->display();
eptr= &m;
eptr->display();

getch();
}

c) Write a C++ program for following multilevel 6M

inheritance.

Accept and display data for one car with all details.

Ans #include<iostream.h>
#include<conio.h>
class Carmanufacturer
{ Declarat
char Name[10];

Page 23 / 25
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)

WINTER – 2018 EXAMINATION


MODEL ANSWER
22316
Subject: Object Oriented Programming
with C++ Subject Code:

public: ion &


void getcarm() Defini
{ tio n
cout<<"\nEnter Car Name "; of all
cin>>Name; classes
} 3M

void putcarm()
{
cout<<"\nThe Car Name is "<<Name;
}
};
class Carmodel : public Carmanufacturer
{
char Modelname[10];
int Modelno;
public:
void getcarmodel()
{
cout<<"\nEnter Car Model Name and Model No. ";
cin>>Modelname>>Modelno;
}
void putcarmodel()
{
cout<<"\nEnter Car Model Name and Model No.
"<<Modelname<<" "<<Modelno;
}
};
class Car: public Carmodel
{
char colour[10], Carno[10];
public:
void getcar()
{
cout<<"\nEnter Car colour and car number";
cin>>colour>>Carno;
}
void putcar()
{

Page 24 / 25
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)

WINTER – 2018 EXAMINATION


MODEL ANSWER
22316
Subject: Object Oriented Programming
with C++ Subject Code:

cout<<"\nEnter Car colour and car number Main


"<<colour<<" "<<Carno; functio
} n 3M
};
void main()
{
Car c;
clrscr();
c.getcarm();
c.getcarmodel();
c.getcar();
c.putcarm();
c.putcarmodel();
c.putcar();
getch();
}

Page 25 / 25

You might also like