OOP Kit

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 68

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION

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

WINTER – 2019 EXAMINATION


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

Important Instructions to examiners:


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 Marking


No Q.N. Scheme
.
1. Attempt any FIVE of the following: 10
a) State the difference between OOP and POP. 2M
Ans.
Sr. OBJECT ORIENTED PROCEDURE
No. PROGRAMMING ORIENTED
(OOP) PROGRAMMING (POP)
1 Focus is on data rather than Focus is on doing things
procedure. (procedure). Any two
2 Programs are divided into Large programs are divided differen
multiple objects. into multiple functions. ces 1M
3 Data is hidden and cannot Data move openly around each
be accessed by external the system from function to
functions. function.
4 Objects communicate with Functions transform data
each other through from one form to another
function. by calling each other.
5 Employs bottom-up Employs top-down
approach in program approach in program
design design.

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

WINTER – 2019 EXAMINATION


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

6 Object oriented approach is Procedure oriented


used in C++ language. approach is used in C
language.
b) What is a class? Give its example. 2M
Ans. Class is a user defined data type that combines data and functions Class
together. It is a collection of objects of similar type. definitio
n 1M
Example:
class Student
{ Correct
int rollno; example
char name[10]; 1M
public:
void getdata( );
void putdata( );
};

c) What is multilevel inheritance? Draw the diagram to show 2M


multilevel inheritance. using classes with data member and
member function.
Ans. When a class is derived from another derived class then it is called as Define
multilevel inheritance. multilev
el
inherita
nce 1M

Diagram
1M

d) Explain use of scope resolution operator. 2M


Ans. It is used to uncover a hidden variable. Scope resolution operator
allows access to the global version of a variable. The scope resolution Correct
operator is used to refer variable of class anywhere in program. use 2M
:: Variable_name
OR
Scope resolution operator is also used in classes to identify the class

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

WINTER – 2019 EXAMINATION


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

to which a member function belongs. Scope resolution operator is


used to define function outside of class.
Return_type class_name:: function_name( )
{
Function body
}
e) Write two properties of static member function. 2M
Ans. i) A static member function can have access to only other static data Two
members and functions declared in the same class. properti
ii) A static member function can be called using the class name with a es 1M
scope resolution operator instead of object name as follows: each
class_name::function_name;
f) Explain virtual base class with suitable example. 2M
Ans. A virtual base class (Grandparent class) is a class that avoids duplication of Explana
inherited data in derived class (child class) derived from parent classes tion of
(parent1 and parent2) which in turn derived from base class. Virtual
base
Example:
class 1M

Example
1M

g) Give syntax and use of fclose ( ) function. 2M


Ans.
Syntax: Syntax
int fclose(FILE* stream); 1M
Use: This function is used to close a file stream. The data that is Correct
buffered but not written is flushed to the OS and all unread buffered use 1M
data is discarded.
2. Attempt any THREE of the following: 12
a) Describe memory allocation for objects. 4M
Ans. The memory space for object is allocated when they are declared and
not when the class is specified. The member functions are created and
placed in memory space only once when they are defined as a part of

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

WINTER – 2019 EXAMINATION


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

a class definition. Since all the objects belonging to that class use the
same member functions, no separate space is allocated for member Descript
functions. When the objects are created only space for member ion 2M
variable is allocated separately for each object. Separate memory
locations for the objects are essential because the member variables
will hold different data values for different objects.

Diagram
2M

Fig: Memory allocation for objects


b) Write a program to implement single inheritance from the 4M
following Refer Figure No.1

(Note: Any other correct logic shall be considered)


Ans. #include<iostream.h>
#include<conio.h>
class employee
{ Class
protected:
declarati
int emp_id;
char name[10];
on 1M
}; each

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

WINTER – 2019 EXAMINATION


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

class emp_info:public employee


{
int basic_salary;
public:
void getdata()
{
cout<<"Enter emp id";
Functio
cin>>emp_id; n
cout<<"Enter name"; declarati
cin>>name; on 1M
cout<<"Enter basic salary";
cin>>basic_salary;
}
void putdata()
{
cout<<"\nEmp_id="<<emp_id;
cout<<"\nName="<<name;
cout<<"\nBasic Salary="<<basic_salary;
}
};
void main()
{
emp_info e;
clrscr(); Main
e.getdata(); function
e.putdata(); 1M
getch();
}
c) Write any four benefits of OOP. 4M
Ans. Benefits of OOP:
1. We can eliminate redundant code and extend the use of existing
classes.
2. We can build programs from the standard working modules that
communicate with one another, rather than having to start writing Any
the code from scratch. This leads to saving of development time four
and higher productivity. benefits
3. The principle of data hiding helps the programmer to build secure 1M each
programs that cannot be invaded by code in other parts of the
program.
4. It is possible to have multiple instances of an object to co-exist
without any interference.
5. It is possible to map objects in the problem domain to those in the

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

WINTER – 2019 EXAMINATION


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

program.
6. It is easy to partition the work in a project based on objects.
7. The data-centered design approach enables us to capture more
details of a model in implementable form.
8. Object-oriented systems can be easily upgraded from small to
large systems.
9. Message passing techniques for communication between objects
makes the interface descriptions with external systems much
simpler.
10. Software complexity can be easily managed.
d) Describe ‘this’ pointer with an example. 4M
Ans. ‘this’ pointer:
C++ uses a unique keyword called „this‟ to represent an object that
invokes a member function. This unique pointer is automatically
passed to a member function when it is invoked. „this‟ is a pointer
that always point to the object for which the member function was Descript
called. ion 2M
For example, the function call A.max ( ) will set the pointer „this‟ to
the address of the object A. Then suppose we call B.max ( ), the
pointer „this‟ will store address of object B.

Example:
#include<iostream.h>
class sample
{
int a; Correct
public: example
void setdata(int x) 2M
{
this ->a=x;
}
void putdata()
{
cout<<this ->a;
}
};
void main()
{
sample s;

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

WINTER – 2019 EXAMINATION


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

s.setdata(100);
s.putdata( );
}
In the above example, this pointer is used to represent object s when
setdata ( ) and putdata ( ) functions are called.
3. Attempt any THREE of the following: 12
a) Write the applications of object oriented programming. 4M
Ans. Applications of object oriented programming are:
1) Real time systems
2) Simulation and modeling Any
3) Object-oriented databases four
4) Hypertext, hypermedia and expertext correct
5) AI and expert systems applicati
6) Neural networks and parallel programming ons 1M
7) Decision support and office automation systems each
8) CIM/CAM/CAD systems
b) State the rules for writing destructor function. 4M
Ans. Rules for writing destructor function are:
1) A destructor is a special member function which should destroy
the objects that have been created by constructor. Any
2) Name of destructor and name of the class should be same. four
3) Destructor name should be preceded with tilde (~) symbol. correct
4) Destructor should not accept any parameters. rules
5) Destructor should not return any value. 1M each
6) Destructor should not be classified in any types.
7) A class can have at most one destructor.
c) What is inheritance? Give different types of inheritance. 4M
Ans. Inheritance:
The mechanism of deriving new class from an old/existing class is
called inheritance. Correct
OR explanat
Inheritance is the process by which objects of one class acquired the ion of
properties of objects of another classes. inherita
nce 2M
Syntax:

class derived-class-name: visibility-mode base-class-name


{
------//

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

WINTER – 2019 EXAMINATION


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

-----// members of derived class


-----//
};

Types of inheritance:
1) Single inheritance: In single inheritance, a derived class is
derived from only one base class.
Diagram:

Correct
types of
inherita
nce
2) Multiple inheritance: In multiple inheritance, derived class is (any 4)
derived from more than one base classes. 2M
Diagram:

3) Hierarchical inheritance: In hierarchical inheritance, more than


one derived classes are derived from single class.
Diagram:

4) Multilevel inheritance: In multilevel inheritance, a derived class


is derived from a derived class (intermediate base class) which in turn

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

WINTER – 2019 EXAMINATION


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

derived from a single base class.


Diagram:

5) Hybrid inheritance: Hybrid inheritance is a combination of


single, multiple, multilevel and hierarchical inheritance.
Diagram:

d) What are the rules for virtual function? 4M


Ans. Rules for virtual function:
1. The virtual functions must be members of some class.
2. They cannot be static members.
3. They are accessed by using object pointers.
4. A virtual function can be a friend of another class.
5. A virtual function in a base class must be defined, even though it Any
may not be used. four
6. The prototypes of the base class version of a virtual function and rules
all the derived class versions must be identical. 1M each
7. We cannot have virtual constructors, but we can have virtual
destructors.
8. While a base pointer can point to any type of the derived object,
the reverse is not true.
9. When a base pointer points to a derived class, incrementing or

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

WINTER – 2019 EXAMINATION


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

decrementing it will not make it to point to the next object of the


derived class.
10. If a virtual function is defined in the base class, it need not be
necessarily redefined in the derived class.
4. Attempt any THREE of the following: 12
a) What is parameterized constructor? 4M
Ans. A constructor that accepts parameters is called as parameterized
constructor.
In some applications, it may be necessary to initialize the various data
members of different objects with different values when they are
created. Parameterized constructor is used to achieve this by passing
arguments to the constructor function when the objects are created. Correct
descripti
Example: on 4M
class ABC
{
int m;
public:
ABC(int x)
{
m=x;
}
void put()
{
cout<<m;
}
};
void main()
{
ABC obj(10);
obj.put();
}
In the above example, constructor ABC (int x) is a parameterized
constructor function that accepts one parameter. When „obj‟ object is
created for class ABC, parameterized constructor will invoke and
data member m will be initialized with the value 10 which is passed
as an argument. Member function put ( ) displays the value of data
member „m‟.

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

WINTER – 2019 EXAMINATION


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

b) Write a program to sort an 1-d array in ascending order. 4M


(Note: Any other correct logic shall be considered)
Ans. #include<iostream.h>
#include<conio.h>
void main()
{
int arr[20];
int i, j, temp,n;
clrscr(); Correct
cout<<"\n Enter the array size:"; array
cin>>n; input
cout<<"\n Enter array elements:"; 1M
for(i=0;i<n;i++)
{
cin>>arr[i]; Sorting
} of 1D
for(i=0;i<n;i++) array in
{ ascendin
for(j=i+1;j<n;j++) g order
{ 2M
if(arr[i]>arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
cout<<”Sorted Array:”; Display
for(i=0;i<n;i++) of sorted
{ array
cout<<”\n”<<arr[i]; 1M
}
getch();
}
c) Explain the friend function with proper example. 4M
Ans. Friend function:
The private members of a class cannot be accessed from outside the
class but in some situations two classes may need access of each

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

WINTER – 2019 EXAMINATION


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

other‟s private data. So a common function can be declared which


can be made friend of more than one class to access the private data Correct
of more than one class. The common function is made friendly with explanat
all those classes whose private data need to be shared in that function. ion of
This common function is called as friend function. Friend function is friend
not in the scope of the class in which it is declared. It is called function
without any object. The class members are accessed with the object 2M
name and dot membership operator inside the friend function. It
accepts objects as arguments.

Example:
Program to interchange values of two integer numbers using
friend function.
#include<iostream.h>
#include<conio.h>
class B; Correct
class A example
{ 2M
int x;
public:
void accept()
{
cout<<"\n Enter the value for x:";
cin>>x;
}
friend void swap(A,B);
};
class B
{
int y;
public:
void accept()
{
cout<<"\n Enter the value for y:";
cin>>y;
}
friend void swap(A,B);
};
void swap(A a,B b)

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

WINTER – 2019 EXAMINATION


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

{
cout<<"\n Before swapping:";
cout<<"\n Value for x="<<a.x;
cout<<"\n Value for y="<<b.y;
int temp;
temp=a.x;
a.x=b.y;
b.y=temp;
cout<<"\n After swapping:";
cout<<"\n Value for x="<<a.x;
cout<<"\n Value for y="<<b.y;
}
void main()
{
A a;
B b;
clrscr();
a.accept();
b.accept();
swap(a,b);
getch();
}
d) Write a program to count the number of lines in file. 4M
(Note: Any other correct logic shall be considered)
Ans. #include<iostream.h>
#include<fstream.h> Opening
#include<conio.h> of file
void main() 1M
{
ifstream file; Countin
char ch; g
int n=0; number
clrscr(); of lines
file.open("abc.txt"); 2M
while(file) Printing
{ number
file.get(ch); of lines
if(ch=='\n') in a file
n++; 1M

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

WINTER – 2019 EXAMINATION


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

}
cout<<"\n Number of lines in a file are:"<<n;
file.close();
getch();
}
5. Attempt any TWO of the following: 12
a) Write a program to declare a class ‘student’ having data 6M
members as ‘stud_name’ and ‘roll_no’. Accept and display this
data for 5 students.
(Note: Any other correct logic shall be considered)
Ans. #include<iostream.h>
#include<conio.h>
class student
{
int roll_no;
char stud_name[20]; Class
public: declarati
void Accept(); on 2M
void Display();
};
void student::Accept() Accept
{ ( )1M
cout<<"\n Enter student‟s name and roll no\n";
cin>>stud_name>>roll_no;
}
void student::Display()
{ Display
cout<<stud_name<<”\t”<<roll_no<<”\n”; ( ) 1M
}
void main()
{
student S[5];
inti;
clrscr();
for(i=0;i<5;i++)
{ Main ( )
S[i].Accept(); with
} array
cout<<”Student details \n Student‟s Name \t Roll No\n”; 2M

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

WINTER – 2019 EXAMINATION


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

for(i=0;i<5;i++)
{
S[i].Display();
}
getch();
}
b) State and explain the visibility modes used in inheritance. 6M
Ans. Visibility modes:
 private
 protected
 public
Base class Derived class visibility
visibility Private Protected Public
Private Not Not Not
Inherited Inherited Inherited
Protected Private Protected Protected
Public Private Protected Public

 Private:
o When a base class is privately inherited by a derived class,
„public members‟ and „protected members‟ of the base class
become „private members‟ of the derived class.
o Therefore, the public and protected members of the base class
can only be accessed by the member functions of derived class
but, cannot be accessed by the objects of the derived class.
Syntax: Explana
class derived: private base tion 2M
{ for each
//Members of derived class; visibility
}; mode

 Public:
o When a base class is publicly inherited by a derived class then
„protected members‟ of base class becomes „protected
members‟ and ‟public members‟ of the base class become
„public members‟ of the derived class.
o Therefore the public members of the base class can be
accessed by both the member functions of derived class as well

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

WINTER – 2019 EXAMINATION


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

as the objects of the derived class.


Syntax:
class derived: public base
{
//Members of derived class;
};

 Protected:
o When a base class is protectedly inherited by a derived class,
„public and protected members‟ of the base class become
„protected members‟ of the derived class.
o Therefore the public and protected members of the base class
can be accessed by the member functions of derived class as
well as the member functions of immediate derived class of it
but they cannot be accessed by the objects of derived class
Syntax:
class derived: protected base
{
//Members of derived class;
};
c) Write a program to declare a class ‘book’ containing data 6M
members as ‘title’, ‘author-name’, ‘publication’, ‘price’. Accept
and display the information for one object using pointer to that
object.
(Note: Any other correct logic shall be considered)
Ans. #include<iostream.h>
#include<conio.h>
class book
{ Class
char author_name[20]; declarati
char title[20]; on 2M
char publication[20];
float price;
public:
void Accept();
void Display();
};
void book::Accept() Accept
{ ( ) 1M

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

WINTER – 2019 EXAMINATION


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

cout<<"\n Enter book‟s title, author_name, publication and price \n:";


cin>> title >>author_name>> publication >> price;
}
void student::Display() Display
{ ( ) 1M
cout<<title <<”\t”<<author_name<<”\t”<<publication <<”\t”<<
price<<”\n”<<;
}
void main()
{
book b, *p; Main( )
clrscr(); with
p=&b; pointer
p->Accept(); 2M
cout<<”title \t author_name \t publication \t price\n”;
p-> Display();
getch();
}
6. Attempt any TWO of the following: 12
a) Write a program that copies contents of one file into another file. 6M
(Note: Any other correct logic shall be considered)
Ans. Assuming input file to be copied file1.txt contents are “Hello
Friends…” and file where the contents need to copy is file2.txt
already created

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<stdio.h>
#include<stdlib.h>
void main()
{
clrscr();
ifstream fs; File
ofstream ft; open
char ch, fname1[20], fname2[20]; and
cout<<"Enter source file name with extension (like files.txt) : "; close
gets(fname1); 2M
fs.open(fname1);

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

WINTER – 2019 EXAMINATION


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

if(!fs)
{
cout<<"Error in opening source file..!!";
getch();
exit(1); Logic
} for copy
cout<<"Enter target file name with extension (like filet.txt) : "; contents
gets(fname2); 4M
ft.open(fname2);
if(!ft)
{
cout<<"Error in opening target file..!!";
fs.close();
getch();
exit(2);
}
while(fs.eof()==0)
{
fs>>ch;
ft<<ch;
}
cout<<"File copied successfully..!!";
fs.close();
ft.close();
getch();
}
b) Write a program to implement the following hierarchy using 6M
suitable member functions. Refer Figure No.2.

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

WINTER – 2019 EXAMINATION


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

(Note: Any other correct logic shall be considered)


Ans. # include <iostream.h>
#include<conio.h>
class Student
{
int roll_no;
char name[10];
public: Class
void read_studentData() student
{ declarati
cout<<”Enter student‟s roll no and name \n”; on 1M
cin>>roll_no>> name;
}
void display_studentData ()
{
cout<<”\n roll_no\t name\n”;
cout<<roll_no<<”\t”<<name<<”\n”;
}
};
class test: public Student
{
protected:
int marks1,marks2;
public: Class
void read_test() test
{ declarati
cout<<”\n Enter test marks\n”; on 1M
cin>>marks1>>marks2;
}

void display_test()
{
cout<<”\n test Marks \n Marks1 \t Marks2 \n”;
cout<<marks1<<”\t”<<marks2;
}
};
class sports
{
int score;

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

WINTER – 2019 EXAMINATION


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

public:
void read_sportsData()
{
cout<<”\n Enter sport score\n”; Class
cin>> score; sports
} declarati
void display_sportsData() on 1M
{
cout<<”\n sport score:”<<score;
}
};
class result: public test, public sports
{
int total;
public:
void read_result()
{
read_ studentData () ;
read_test(); Class
read_sportsData(); result
total=marks1+marks2; declarati
} on 2M
void display_result()
{
display_ studentData () ;
display_test();
display_sportsData();
cout<<”\n Total=”<<total;
}
};
void main()
{
result r;
clrscr();
r.read_result(); Main ()
r.display_result(); 1M
getch();
}

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

WINTER – 2019 EXAMINATION


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

c) Write a program to overload the ‘—’ unary operator to negate 6M


the values.
(Note: Any other correct logic shall be considered)
Ans. #include<iostream.h>
#include<conio.h>
#include<string.h>
class Number
{
int x,y;
public:
Number (int a, int b)
{ Correct
a =x; Program
b =y; with
} output
void display() 6M
{
cout<<"value of x=”<<x<<”\n Value of y= ”<<y;
}
void operator - ( )
{
x = - x;
y = - y;
}
};
void main ()
{
Number N1(5,6);
clrscr ();
N1. display ();
-N1;
cout<<"\n After negation:";
N1. display ();
getch ();
}

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

WINTER – 2022 EXAMINATION


MODEL ANSWER
Subject: Object Oriented Programming with C++ Subject Code: 22316
Important Instructions to examiners:
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 anyequivalent 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.
8) As per the policy decision of Maharashtra State Government, teaching in
English/Marathi and Bilingual (English + Marathi) medium is introduced at first year
of AICTE diploma Programme from academic year 2021-2022. Hence if the
students in first year (first and second semesters) write answers in Marathi or
bilingual language (English +Marathi), the Examiner shall consider the same and
assess the answer based on matching of concepts with model answer.

Q. Sub Answer Marking


No Q.N. Scheme
1. Attempt any FIVE of the following: 10
a) State any four applications of OOP 2M
Ans. Following are the application of OOP: Any four
• Most popular application of OOP is in the area of user interface applications
½M each
design such as windows. Hundreds of windowing systems have
been developed using OOP techniques.
• C++ is suitable for any programming task including development
of editors, compilers
• Developing databases
• Developing communication systems and complex real-time
applications.
• Simulation and modeling.
• AI and Expert system

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

WINTER – 2022 EXAMINATION


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

b) Explain user defined data types with example 2M


Ans. The data types that are defined by the user are called user-defined Definition
data type. 1M
Two
Examples: examples
1. Class 1M
2. Structure
3. Union
4. Enumeration
c) Describe use of scope resolution operator with example. 2M
Ans. • Scope of a variable extends from the point of declaration to the Explanation
end of the block. 1M
• A variable declared inside a block is ‘local’ variable and a Example
variable declared outside block is called as global variable. 1M
• When we want to use a global variable but also has a local
variable with same name.
• To access the global version of the variable, C++ provides scope
resolution operator.
#include <iostream>
int n = 12; // A global variable
int main()
{
int n = 13; // A local variable
cout<< ::n<<endl;
// Print the global variable: 12
cout<< n <<endl;
// Print the local variable: 13
}
d) Define constructor’s and its type 2M
Ans. A constructor is a special member function that is executed Definition
automatically whenever the object of a class is created. 1M any two
types 1M
Following are the types of constructors:
1. Default constructor
2. Parameterized constructor
3. Copy constructor
4. Constructor with default argument
e) Explain concept of pointer with example 2M
Ans. A pointer is a variable that holds an address of another variable. They Concept /
have data type just like variables, for example an integer type pointer Definition
1M
can hold the address of an integer variable and a character type

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

WINTER – 2022 EXAMINATION


MODEL ANSWER
Subject: Object Oriented Programming with C++ Subject Code: 22316
pointer can hold the address of char variable. Example
Example : 1M
int *ptr; // ptr is a pointer
int a = 10; //’a’ is ordinary variable holding an integer
ptr = &a; //initializing pointer with the address of ‘a’

f) Explain file modes used to perform file operations. 2M


Ans. Following are the file modes used to perform file operations Any two file
modes 1M
Parameter Meaning each
ios :: app Append to end-of-file
ios :: ate Go to end-of-file on opening
ios :: binary Binary file
ios :: in Open file for read only
ios :: nocreate Open fails if file does not exist
ios :: noreplace Open fails if file does already exist
ios :: out Open file for writing only
ios :: trunc Delete the contents of the file if it exists

g) List all stream classes used in stream operation 2M


Ans. Following are the stream classes used in stream operation Listing of
 ios Any four
stream
 istream classes ½M
 ostream each
 iostream
 streambuf
 filebuf
 fstreambase
 ifstream
 ofstream
 fstream

2. Attempt any THREE of the following: 12


a) Develop a C++ program to print Fibonacci series 4M
Ans. #include <iostream> Correct
using namespace std; logic 2M
int main()

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

WINTER – 2022 EXAMINATION


MODEL ANSWER
Subject: Object Oriented Programming with C++ Subject Code: 22316
{ Correct
int t1 = 0, t2 = 1, nextTerm = 0, n,count; syntax 2M
cout<< "Enter a positive number: ";
cin>> n;
// displays the first two terms which is always 0 and 1
cout<< "Fibonacci Series: " << t1 << " " << t2 <<" ";
count=2;
while(count!= n)
{
nextTerm = t1 + t2;
cout<<nextTerm<<" ";
t1 = t2;
t2 = nextTerm;
count++;
}
return 0;
}
b) Develop a c++ program for multilevel inheritance. 4M
Ans. (Any program with correct logic of multilevel inheritance shall be
considered) Correct
#include <iostream> logic 2M
using namespace std; Correct
class base //single base class syntax 2M
{
public:
int x;
void getdata()
{
cout<< "Enter value of x= "; cin>> x;
}
};
class derive1 : public base // derived class from base class
{
public:
int y;
void readdata()
{
cout<< "\nEnter value of y= "; cin>> y;
}

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

WINTER – 2022 EXAMINATION


MODEL ANSWER
Subject: Object Oriented Programming with C++ Subject Code: 22316
};
class derive2 : public derive1 // derived from class derive1
{
private:
int z;
public:
void indata()
{
cout<< "\nEnter value of z= "; cin>> z;
}
void product()
{
cout<< "\nProduct= " << x * y * z;
}
};
int main()
{
derive2 a; //object of derived class
a.getdata();
a.readdata();
a.indata();
a.product();
return 0;
}
c) Develop a c++ program for accept data from user to calculate 4M
percentage for 5 subjects and display grade according to
percentage.
Ans. #include<iostream> Correct
using namespace std; logic 2M
int main()
{ Correct
int phy,chem,bio,maths,comp; syntax 2M
int total;
float percent;
cout<<"Enter marks of computer";
cin>>phy;
cout<<"enter marks of English";
cin>>chem;
cout<<"enter marks of bio";

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

WINTER – 2022 EXAMINATION


MODEL ANSWER
Subject: Object Oriented Programming with C++ Subject Code: 22316
cin>>bio;
cout<<"enter marks of maths";
cin>>maths;
cout<<"enter marks of physics"; cin>>comp;
total= phy+chem+bio+maths+comp;
percent=total/5.0;
if(percent>=90)
{
cout<<"Grade=A+";
}
else if(percent>=80)
{
cout<<"Grade=A";
}
else if(percent>=70)
{
cout<<"Grade=B+";
}
else if(percent>=60)
{
cout<<"Grade=B";
}
else if(percent>=40)
{
cout<<"Grade=C";
}
else
{
cout<<"Grade= Fail";
}
}

d) Develop c++ program to open and read content of file also write 4M
"object oriented" string in fi1e and close it'
Ans. #include<iostream> Correct
logic 2M
#include<fstream>
using namespace std;
int main() Correct
{ syntax 2M

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

WINTER – 2022 EXAMINATION


MODEL ANSWER
Subject: Object Oriented Programming with C++ Subject Code: 22316
ofstreamfout;
fout.open("abc.txt",ios::out);
fout<<"Object Oriented";
fout.close();
ifstream fin;
char line[100];
fin.open("abc.txt",ios::in);
cout<<"Contents of file are:\n";
while(fin)
{
fin.getline(line,100);
cout<<line<<"\n";
}
fin.close();
return 0;
}
3. Attempt any THREE of the following: 12
a) Describe structure of C++ program 4M
Ans. General C++ program has following structure.
Correct
diagram 2M

Description
2M

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
like cout and cin. Without this file one cannot load C++ program.
2. Class Declaration
In this section a programmer declares all classes which are 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

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

WINTER – 2022 EXAMINATION


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

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.
b) Explain with suitable example Friend Function. 4M
Ans. Friend function: Correct
The private members of a class cannot be accessed from outside the explanation
2M
class but in some situations two classes may need access of each
other’s private data. So a common function can be declared which Any
can be made friend of more than one class to access the private data Correct
of more than one class. The common function is made friendly with example 2M
all those classes whose private data need to be shared in that function.
This common function is called as friend function. Friend function is
not in the scope of the class in which it is declared. It is called
without any object. The class members are accessed with the object
name and dot membership operator inside the friend function. It
accepts objects as arguments.
Example:
Program to interchange values of two integer numbers using friend
function.
#include<iostream.h>
#include<conio.h>
class B;
class A
{
int x;
public:
void accept()
{
cout<<"\n Enter the value for x:";
cin>>x;
}
friend void swap(A,B);
};
class B
{
int y;
public:

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

WINTER – 2022 EXAMINATION


MODEL ANSWER
Subject: Object Oriented Programming with C++ Subject Code: 22316
void accept()
{
cout<<"\n Enter the value for y:";
cin>>y;
}
friend void swap(A,B);
};
void swap(A a,B b)
{
cout<<"\n Before swapping:";
cout<<"\n Value for x="<<a.x;
cout<<"\n Value for y="<<b.y;
int temp;
temp=a.x;
a.x=b.y;
b.y=temp;
cout<<"\n After swapping:";
cout<<"\n Value for x="<<a.x;
cout<<"\n Value for y="<<b.y;
}
void main()
{
A a;
B b;
clrscr();
a.accept();
b.accept();
swap(a,b);
getch();
}
c) Describe all visibility modes and effects with example. 4M
Ans. Visibility modes used in inheritance are: List of
 Private Visibility
modes 1M
 Protected
 Public Explanation
of each
mode 2M,

Example
1M each

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

WINTER – 2022 EXAMINATION


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

Fig: Visibility modes in inheritance


 Private:
 When a base class is privately inherited by a derived class, public
members and protected members of the base class become private
members of the derived class.
 Therefore, the public and protected members of the base class can
only be accessed by the member functions of derived class but,
cannot be accessed by the objects of the derived class.
Example:
class Result: private Student
{
float percentage;
};

 Public:
 When a base class is publicly inherited by a derived class then
protected members of base class becomes protected members and
public members of the base class become public members of the
derived class.
 Therefore, the public members of the base class can be accessed by
both the member functions of derived class as well as the objects of
the derived class.

Example:
class Result: public Student
{
float percentage;
};

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

WINTER – 2022 EXAMINATION


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

 Protected:
 When a base class is inherited by derived class in protected visibility
mode, public and protected members of the base class become
protected members of the derived class.
 Therefore the public and protected members of the base class can be
accessed by the member functions of derived class as well as the
member functions of immediate derived class of it but they cannot be
accessed by the objects of derived class.

Example:
class Result: protected Student
{
float percentage;
};
d) Differentiate between Compile time polymorphism and Runtime 4M
polymorphism
Ans. Sr. Compile time Runtime polymorphism Any four
No. polymorphism differences
1M each
1 The linking of function call The linking of function call
to function definition is done to function definition is done
at compile time. at run time.
2 Functions to be executed are Function to be executed is
known well before(at unknown until appropriate
compile time) selection is made at run time
3 This does not require use of This requires use of pointers
pointers to objects to object
4 Function calls execution are Function calls execution are
faster slower
5 It is implemented with It is implemented with
operator overloading or virtual function.
function overloading

4. Attempt any THREE of the following: 12


a) Develop a c++ program to implement inheritance shown in 4M
following fig

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

WINTER – 2022 EXAMINATION


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

Correct
definition of
class
Ans. #include <iostream> Employee
1M,
using namespace std;
class Employee Correct
{ definition of
protected: class
char Name[20]; Customer
1M,
int Id;
long contact_noE; Correct
}; definition of
class Customer class
{ Company
1M,
protected:
char Name[20]; Correct
char Address[50]; definition of
long contact_noC; main() 1M
};
class Company:publicEmployee,public Customer
{
char Name[20];
char location[20];
public:
void getE()
{
cout<<"\nEnter employee's data:";
cout<<"\nName:";
cin>>Name;
cout<<"\nId:";
cin>>Id;
cout<<"\nContact no:";
cin>>contact_noE;
cout<<"\nCompany name:";
cin>>Name;

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

WINTER – 2022 EXAMINATION


MODEL ANSWER
Subject: Object Oriented Programming with C++ Subject Code: 22316
cout<<"\nLocation:";
cin>>location;
}
void getC()
{
cout<<"\nEnter customer's data:";
cout<<"\nName:";
cin>>Name;
cout<<"\nAddress:";
cin>>Address;
cout<<"\nContact no:";
cin>>contact_noC;
cout<<"\nCompany name:";
cin>>Name;
cout<<"\nLocation:";
cin>>location;
}
void putE()
{
cout<<"\nEmployee's data is:";
cout<<"\nName:"<<Name;
cout<<"Id:"<<Id;
cout<<"\nContact no:"<<contact_noE;
cout<<"\nCompany name:"<<Name;
cout<<"\nLocation:"<<location;
}
void putC()
{
cout<<"\nCustomer's data is:";
cout<<"\nName:"<<Name;
cout<<"Address:"<<Address;
cout<<"\nContact no:"<<contact_noC;
cout<<"\nCompany name:"<<Name;
cout<<"\nLocation:"<<location;
}
};
intmain()
{
Company C;

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

WINTER – 2022 EXAMINATION


MODEL ANSWER
Subject: Object Oriented Programming with C++ Subject Code: 22316
C.getE();
C.getC();
C.putE();
C.putC();
return 0;
}
b) Develop a c++ program to print sum of 10 nos. using array. 4M
Ans. #include<iostream.h>
#include<conio.h> Initializatio
void main() n of array
2M,
{
int arr[20],i,n,sum=0; Logic of the
clrscr(); program2M
cout<<"\nEnter size of an array:";
cin>>n;
cout<<"\nEnter the elements of an array:";
for(i=0;i<n;i++)
{
cin>>arr[i];
}
for(i=0;i<n;i++)
{
sum=sum+arr[i];
}
cout<<"\nArray elements are:";
for(i=0;i<n;i++)
{
cout<<arr[i]<<" ";
}
cout<<"\nSum of array elements is:"<<sum;
getch();
}
c) Develop a c++ program to perform arithmetic operation using 4M
pointer.
Ans. (Any other program with correct logic can be considered) Correct
#include<iostream.h> logic 2M
#include<conio.h> Correct
void main() syntax 2M
{

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

WINTER – 2022 EXAMINATION


MODEL ANSWER
Subject: Object Oriented Programming with C++ Subject Code: 22316
int a=5,*ptr;
clrscr();
ptr=&a;
cout<<a<<endl;
cout<<*ptr<<endl;
*ptr=*ptr+1;
cout<<*ptr<<endl;
*ptr=*ptr-1;
cout<<*ptr<<endl;
getch();
}
d) Develop a c++ program to create structure student with data 4M
member Name, Roll No., percentage accept and display data for 5
student using structure.
Ans. #include<iostream.h> Correct
#include<conio.h> syntax for
struct student structure
{ definition
char Name[20]; 2M
int Roll_No; Correct
float percentage; syntax for
}s[5]; main
void main() function 2M
{
int i;
clrscr();

for(i=0;i<5;i++)
{
cout<<"Enter name of student "<<i+1;
cin>>s[i].Name;
cout<<"Enter Roll Number of student "<<i+1;
cin>>s[i].Roll_No;
cout<<"Enter percentage of student "<<i+1;
cin>>s[i].percentage;
}
cout<<"\nStudents data is: ";
for(i=0;i<5;i++)
{

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

WINTER – 2022 EXAMINATION


MODEL ANSWER
Subject: Object Oriented Programming with C++ Subject Code: 22316
cout<<"Student "<<i+1<<" Name: ";
cout<<s[i].Name<<endl;
cout<<"Student "<<i+1<<" Roll Number";
cout<<s[i].Roll_No<<endl;
cout<<"Student "<<i+1<<" Percentage: ";
cout<<s[i].percentage<<endl;
}
getch();
}
e) Develop c++ program to check Detection of end of file. 4M
Ans. (Any other relevant example shall be considered)
#include<iostream> Correct
#include<fstream> logic 2M
using namespace std; Correct
int main() syntax 2M
{
ofstreamfout;
fout.open("abc.txt",ios::out);
fout<<"Object Oriented";
fout.close();
ifstream fin;
char line[100];
fin.open("abc.txt",ios::in);
cout<<"Contents of file are:\n";
char c;
while(fin.get(c))
cout<<c;

if(fin.eof())
cout<<"[EoFreached]\n";
else
cout<<"[error reading]\n";
return 0;
}
5. Attempt any TWO of the following: 12
a) Define class book with following data member and member 6M
functions for 10 book.

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

WINTER – 2022 EXAMINATION


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

Declare
class with
data
members
Ans. #include <iostream.h> and member
class book functions-
{ 4M
char name[20],author[20];
int price;
public: Main
void getdata() function
{ with
cout<<"Enter name"; function
call in loop -
cin>>name; 2M
cout<<"Enter author";
cin>>author;
cout<<"Enter price";
cin>>price;
}
void putdata()
{
cout<<endl<<"Name = "<<name;
cout<<endl<<"Author = "<<author;
cout<<endl<<"Price = "<<price;
}
};
void main()
{
book b[10];
int i;
for(i=0;i<10;i++)
b[i].getdata();
for(i=0;i<10;i++)
b[i].putdata();
getch();
}

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

WINTER – 2022 EXAMINATION


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

b) Develop a c++ program to implement virtual Base class. 6M


Ans. (Any correct program with virtual base class shall be considered.)
#include<iostream.h> Program
with
#include<conio.h> grandparent
class student class
{ (virtual base
int rno; class)-
public: 1M,
void getnumber() Parent
{ class1-1M,
cout<<"Enter Roll No:";
cin>>rno; Parent
} class2-1M,
void putnumber() Child class-
{ 2M,
cout<<"\n\n\t Roll No:"<<rno<<"\n";
} main
}; function-
1M
class test: virtual public student
{
protected:
int part1,part2;

public:
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

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

WINTER – 2022 EXAMINATION


MODEL ANSWER
Subject: Object Oriented Programming with C++ Subject Code: 22316
{
protected:
int score;

public:
void getscore()
{
cout<<"Enter Sports Score:";
cin>>score;
}
void putscore()
{
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();
}

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

WINTER – 2022 EXAMINATION


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

c) Explain rules of operator overloading and overload ‘+’ operator 6M


to concatenate two strings. Any four
Ans. Rules for overloading operators: rules of
operator
1. Only existing operators can be overloaded. New operators cannot overloading
be created. - ½ M each,
2. The overloaded operator must have at least one operand that is of
user defined data type.
3. We can’t change the basic meaning of an operator. That is to say, program
logic for
we can’t redefine the plus (+) operator to subtract one value from overloading
other. -2M,
4. Overloaded operators follow the syntax rules of the original
operators. They can’t be overridden.
5. There are some operators that can’t be overloaded.
6. We can’t use friend functions to overload certain operators.
7. Unary operators overloaded by means of member function take no
explicit arguments and return no explicit values, but those overloaded calling
by means of the friend function, take one reference argument (the overloaded
object of the relevant class). function 2M
8. Binary operators overloaded through a member function, take one
explicit argument and those which are overloaded through a friend
function take two explicit arguments.
9. When using binary operators overloaded through a member
function, the left-hand operand must be an object of the relevant
class.
10. Binary arithmetic operators such as +, -, * and / must explicitly
return a value. They must not attempt to change their own arguments.

Program:
#include<string.h>
class operatorplus
{
char str1[10];
public:
void getdata()
{
cout<<"\nEnter a string";
cin>>str1;
}
void operator + (operatorplus s)

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

WINTER – 2022 EXAMINATION


MODEL ANSWER
Subject: Object Oriented Programming with C++ Subject Code: 22316
{
cout<<strcat(str1, s.str1);
}
};
void main ()
{
operatorplus op1, op2;
clrscr();
op1.getdata();
op2.getdata();
op1+op2;
getch();
}
6. Attempt any TWO of the following: 12
a) Develop a c++ program for constructor with default argument 6M
and use of destructor program for
Ans. (One program with both or two different programs shall be constructor
with default
considered.) argument-
#include<iostream.h> 3M
#include<conio.h>
#include<string.h>
class student use of
destructor-
{ 3M
int rno;
char name[10];
int year;
public:
student(int r,char n[],char y=1)
{
rno=r;
strcpy(name,n);
year=y;
}
void putdata()
{
cout<<rno;
cout<<name;
cout<<year;
}

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

WINTER – 2022 EXAMINATION


MODEL ANSWER
Subject: Object Oriented Programming with C++ Subject Code: 22316
~student()
{
cout<<"Destructor is executed:";
}
};
void main()
{
student s1(2,"abc");
student s2(3,"xyz",3);
clrscr();
s1.putdata();
s2.putdata();
getch();
}

OR

Program for use of destructor:


#include<iostream.h>
#include<conio.h>
int count=0;
class Test
{
public:
Test()
{
count++;
cout<<"\n No. of Object created:\t"<<count;
}

~Test()
{
cout<<"\n No. of Object destroyed:\t"<<count;
--count;
}
};

void main()
{

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

WINTER – 2022 EXAMINATION


MODEL ANSWER
Subject: Object Oriented Programming with C++ Subject Code: 22316
Test t,t1,t2,t3;
getch();
}
b) Describe Function overloading with suitable program 6M
Ans. Function overloading refers to the use of the same function name to Description
create functions that perform a variety of different tasks. Each 3M,
function has same name but different number of arguments or Any
different types of arguments. program
The function is executed depending on the argument list in the with
function call. function
Program: overloading
3M
#include<iostream.h>
#include<conio.h>
void swap(int a,int b)
{
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();
}

In the above program, swap( ) is overloaded by defining two different


data type parameters in argument list. first swap( ) definition contains
two integer parameters and second contains two float parameters.

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

WINTER – 2022 EXAMINATION


MODEL ANSWER
Subject: Object Oriented Programming with C++ Subject Code: 22316
when first function is called , compiler checks number of parameters
and data type of each parameter with first function definition. If both
are same then the definition executes and swapping of integer
numbers is displayed. If the function call argument list does not
match with first definition then second definition argument list is
compared and if matches then swapping of float numbers is
displayed.
c) Develop a c++ program to implement inheritance shown in fig 6M

Ans.
(Any other correct implementation shall be considered.) Each class
#include<iostream.h> implementat
#include<conio.h> ion-1M
class HOD
{
protected:
char name[10];
char deptName[20];
};
class Faculty:public HOD
{
char fname[10];
public:
void getfaculty()
{
cout<<"Enter department name:";
cin>>deptName;
cout<<"Enter HOD name:";
cin>>name;

cout<<"Enter Faculty name:";


cin>>fname;

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

WINTER – 2022 EXAMINATION


MODEL ANSWER
Subject: Object Oriented Programming with C++ Subject Code: 22316
}
void putfaculty()
{
cout<<"\nDepartment name= "<<deptName;
cout<<"\nHOD name= "<<name;
cout<<"\nFaculty name= "<<fname;
}
};
class LabIncharge:public HOD
{
protected:
char lname[10];
};
class Technical:publicLabIncharge
{
char q[10];
public:
void getlabT()
{
cout<<"Enter department name:";
cin>>deptName;
cout<<"Enter HOD name:";
cin>>name;
cout<<"Enter Lab Incharge name:";
cin>>lname;
cout<<"Enter Qualification:";
cin>>q;
}
void putlabT()
{
cout<<"\nDepartment name= "<<deptName;
cout<<"HOD name= "<<name;
cout<<"Lab Incharge name: "<<lname;
cout<<"Qualification: "<<q;
}
};
class NonTechnical:publicLabIncharge
{
char q[10];

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

WINTER – 2022 EXAMINATION


MODEL ANSWER
Subject: Object Oriented Programming with C++ Subject Code: 22316
public:
void getlabN()
{
cout<<"Enter department name:";
cin>>deptName;
cout<<"Enter HOD name:";
cin>>name;
cout<<"Enter Lab Incharge name:";
cin>>lname;
cout<<"Enter Qualification:";
cin>>q;
}
void putlabN()
{
cout<<"\nDepartment name= "<<deptName;
cout<<"HOD name= "<<name;
cout<<"Lab Incharge name: "<<lname;
cout<<"Qualification: "<<q;
}};
class Student:public HOD
{
char sname[10];
public:
void getstudent()
{
cout<<"Enter department name:";
cin>>deptName;
cout<<"Enter HOD name:";
cin>>name;
cout<<"Enter Student name:";
cin>>sname;
}
void putstudent()
{
cout<<"\nDepartment name= "<<deptName;
cout<<"HOD= "<<name;
cout<<"Student name= "<<sname;
}};
void main(){

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

WINTER – 2022 EXAMINATION


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

Faculty f;
Student s;
Technical t;
NonTechnical n;
clrscr();
f.getfaculty();
s.getstudent();
t.getlabT();
n.getlabN();
f.putfaculty();
s.putstudent();
t.putlabT();
n.putlabN();
getch();
}

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

SUMMER – 2023 EXAMINATION


MODEL ANSWER-Only for the Use of RAC Assessors

Subject: Object Oriented Programming Using C++ Subject Code: 22316

Important Instructions to examiners:


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 anyequivalent 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.
8) As per the policy decision of Maharashtra State Government, teaching in English/Marathi
and Bilingual (English + Marathi) medium is introduced at first year of AICTE diploma
Programme from academic year 2021-2022. Hence if the students in first year (first and
second semesters) write answers in Marathi or bilingual language (English +Marathi), the
Examiner shall consider the same and assess the answer based on matching of concepts
with model answer.

Q. Sub Answer Marking


No Q.N. Scheme
1. Attempt any FIVE of the following: 10M
a) Demonstrate the static and dynamic initialization of variables 2M
Ans. Static initialization Static
Syntax- initialization
datatype variable_name=value; -1M,
dynamic
Example -
initialization
int a=5; -1M
or
int a;
a=5;

Dynamic initialization
Syntax –
int a;

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

SUMMER – 2023 EXAMINATION


MODEL ANSWER-Only for the Use of RAC Assessors

Subject: Object Oriented Programming Using C++ Subject Code: 22316

cout<<”Enter value of variable ”;


cin>>variable_name;

Example-
int a;
cout<<”Enter value of variable a”;
cin>>a;

Note: Syntax / Example shall be considered


b) Write the syntax for declaration of a class 2M
Ans. class class_name Correct
{ syntax 2M
private:
variable declarations;
function declarations;
public:
variable declarations;
function declarations;
};
c) State the characteristics of static member function 2M
Ans.  A static function can have access to only other static members Any two
characteristi
declared in the class. cs 1M each
 A static member function can be called using the class name
instead of objects as follows: class_name::function_name;
d) State different types of visibility modes in inheritance 2M
Ans.  private Any two
types 1M
 public each
 protected

e) Give the syntax for constructor in derived classes 2M


Ans. Derived_constructor (arglist1, arglist2,…, arglistN,): Correct
base1(arglist2), . . . ,baseN(arglistN) syntax 2M
{
Body of derived class constructor
}
f) Define polymorphism with its types 2M
Ans. Polymorphism is the ability to take more than one form. Definition
1M

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

SUMMER – 2023 EXAMINATION


MODEL ANSWER-Only for the Use of RAC Assessors

Subject: Object Oriented Programming Using C++ Subject Code: 22316

Types:
 1. Compile time polymorphism Types 1M

 2. Run time polymorphism


g) Define file with its operations 2M
Ans. A file is a collection of related data stored in a particular area on Definition
the disk. 1M
File Operations: Any two
 Open file operations
 Close file 1M
 Read file
 Write file
 Update file
2. Attempt any THREE of the following: 12M
a) State the features of object oriented programming. 4M
Ans. Features of Object Oriented Programming are:
 Follows bottom-up approach in program design. Any four
features 1M
 Emphasis is on data rather than procedure. each
 Programs are divided into what are known as objects.
 Data structures are designed such that they characterize the
objects
 Functions that operate on the data of an object are tied together
in the data structures.
 Data is hidden and cannot be accessed by external functions.
 Objects may communicate with each other through functions.
 New data and functions can be easily added whenever necessary

b) Explain overloaded constructor with suitable example. 4M


Ans.  Constructor overloading is a process of defining more than one
constructor function in a class.
Explanation
 A class can have more than one constructor function 2M
 Type of constructor: default constructor, parameterized
constructor, constructor with default value and copy constructor.
 An appropriate constructor function executes when number of
parameters and its data type matches with constructor function
prototype.

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

SUMMER – 2023 EXAMINATION


MODEL ANSWER-Only for the Use of RAC Assessors

Subject: Object Oriented Programming Using C++ Subject Code: 22316

Example: Example
2M
class example
{
int a,b;
public:
example()
{
a=0;
b=0;
}
example(int x)
{
a=x;
}
example(int x,int y)
{
a=x;
b=y;
}
};
void main()
{
example I1;
example I2(10);
example I3(10,20);
}

In the above code, example is the name of the class.


First constructor is a default constructor that receives no arguments.
When object I1 is created without values, first constructor executes
and assigns 0 value to variables a and b.
Second constructor receives one argument. When object I2 is
created with one value, second constructor executes and assigns
value 10 to variable a.
Third constructor receives two arguments. When object I3 is created
with two values, third constructor executes and assigns value 10 to
variable a and value 20 to variable b.

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

SUMMER – 2023 EXAMINATION


MODEL ANSWER-Only for the Use of RAC Assessors

Subject: Object Oriented Programming Using C++ Subject Code: 22316

c) Write a program on single inheritance 4M


Ans. #include<iostream.h>
#include<conio.h> Correct
class employee logic 2M
{
protected:
Correct
int emp_id; syntax 2M
char name[10];
};
class emp_info:public employee
{
int basic_salary;
public:
void getdata()
{
cout<<"Enter emp id";
cin>>emp_id;
cout<<"Enter name";
cin>>name;
cout<<"Enter basic salary";
cin>>basic_salary;
}
void putdata()
{
cout<<"\nEmp_id="<<emp_id;
cout<<"\nName="<<name;
cout<<"\nBasic Salary="<<basic_salary;
}
};
void main()
{
emp_info e;
clrscr();
e.getdata();
e.putdata();
getch();
}

Note: Any other relevant program shall be considered.


d) Illustrate this pointer with example. 4M
Ans.  C++ uses a unique keyword called ‘this’ to represent an object
that invokes a member function.

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

SUMMER – 2023 EXAMINATION


MODEL ANSWER-Only for the Use of RAC Assessors

Subject: Object Oriented Programming Using C++ Subject Code: 22316

 ‘this’ unique pointer is automatically passed to a member


function when it is invoked. Explanation
2M
 ‘this’ is a pointer that always point to the object for which the
member function is called.
 For example, the function call A.max ( ) will set the pointer
‘this’ to the address of the object A. Then suppose we call
B.max ( ), the pointer ‘this’ will store address of object B.

Example: Example
#include<iostream.h> 2M
class sample
{
int a;
public:
void setdata(int x)
{
this ->a=x;
}
void putdata()
{
cout<<this ->a;
}
};
void main()
{
sample s;
s.setdata(100);
s.putdata( );
}
In the above example, this pointer is used to represent object s when
setdata() and putdata ( ) functions are called.

Note: Any other relevant example shall be considered.

3. Attempt any THREE of the following: 12M


a) Write a program to print first n natural numbers and their sum 4M
using for loop.
Ans. #include<iostream.h>
#include<conio.h>

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

SUMMER – 2023 EXAMINATION


MODEL ANSWER-Only for the Use of RAC Assessors

Subject: Object Oriented Programming Using C++ Subject Code: 22316

void main()
{ Declaration
1M, Display
int n, sum = 0; of natural
clrscr(); numbers
cout<< "Enter a positive integer:"; 1M,
cin>> n;
cout<<"\nNatural numbers till "<<n<<" numbers are:"; Calculate
and display
for(inti = 1; i<= n; i++) sum 2M
{
cout<<i<<" ";
}
cout<<"\n";
for (i = 1; i<= n; i++)
{
sum=sum+i;
}
cout<< "Sum of numbers is = " << sum;
getch();
}

b) Compare static and non static data members (any four points) 4M
Ans.
Static data member Non static data member Any four
correct
1.It is declared using keyword 1.It is declared without using points 1M
'static'. keyword 'static'. each
2. Static data member is 2. Non-static data member
automatically initialized with can be initialized with zero or
zero. non-zero value.
3.All objects of a class share 3.Each object of the class gets
the same copy of static data its own copy of non-static
members. data members.
4.It can be accessed through 4.Itcan be accessed only
static member function as well through non-static member
as non-static member function function of the class
of the class
5. Declaration Syntax: 5.Declaration Syntax:
static data_type variable; data_type variable;
e.g. static int count; e.g. int no;

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

SUMMER – 2023 EXAMINATION


MODEL ANSWER-Only for the Use of RAC Assessors

Subject: Object Oriented Programming Using C++ Subject Code: 22316

c) State any four rules for virtual functions 4M


Ans. Rules for virtual function are:
1. A virtual function must be a member of some class.
2. Virtual functions cannot be static members.
3. Virtual functions can be a friend of another class. Any four
4. Virtual functions should be accessed using pointers. correct rules
1M each
5. A virtual function in based class must be defined, even though
it may not be used.
6. The prototype of virtual functions should be same in base as
well as in derived class.
7. If it is defined in base class, it need not be necessarily redefined
in derived class.
8. A class may have virtual destructor, but it cannot have a virtual
constructor.
9. While a base pointer can point to any type of derived object, the
reverse is not true.
10. When a base pointer points to a derived class, incrementing or
decrementing it will not make it to point to the next object of
derived class.
d) Write a program for get and put functions 4M
Ans. Program code:
#include <iostream.h> Correct
syntax of
int main() get(): 2M,
{
int count = 0;
char c;
cout<< "INPUT TEXT\n"; Correct
syntax of
cin.get(c); put(): 2M
while (c != '\n')
{
cout.put(c);
count++;
cin.get(c);
}
cout<< "\nNumber of characters = " << count << "\n";
return 0;
}
Note: Any other relevant program shall be considered.

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

SUMMER – 2023 EXAMINATION


MODEL ANSWER-Only for the Use of RAC Assessors

Subject: Object Oriented Programming Using C++ Subject Code: 22316

4. Attempt any THREE of the following: 12M


a) Explain the structure of C++ program 4M
Ans. General C++ program has following structure:
Correct
diagram 2M

Description: -
1. Include header files Correct
In this section a programmer includes all header files which are explanation
required to execute a given program. The most important file is 2M
iostream.h header file. This file defines most of the C++statements
like cout and cin. Without this file one cannot load the C++
program.

2. Class Declaration
In this section a programmer declares all classes which are
necessary for the given program. The programmer uses general
syntax of creating class.

3. Member Functions Definition


This section allows programmers to design member functions of a
class. The programmer can have an inside declaration of a function
or outside declaration of a function.

4. Main Function Program


In this section programmers create objects and call various
functions writer within various class.

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

SUMMER – 2023 EXAMINATION


MODEL ANSWER-Only for the Use of RAC Assessors

Subject: Object Oriented Programming Using C++ Subject Code: 22316

b) List any four properties of constructor function 4M


Ans. Properties of constructor function are:
1. A constructor name must be same as that of its class name. Any four
properties
2. Constructors are called automatically when the objects are 1M each
created.
3. Constructors should be declared in the public section to be
available to all the functions.
4. Constructors do not have return type, not even void and
therefore they cannot return value.
5. Constructors can have default arguments as other C++ functions.
6. Constructors cannot be inherited.
7. Constructors cannot be static.
8. Constructors cannot be virtual.
9. The address of a constructor cannot be referred.
10. Constructors are member functions so they can be overloaded.

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


Ans. Virtual Base Class: An ancestor class is declared as virtual base
class to avoid duplication of inherited members inside child class
due to multiple paths of inheritance.

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 Description
inherits the members of Grandparent via two separate paths. All the 2M
public and protected members of Grandparent are inherited into
Child twice, first via Parent1 and again via Parent2. This leads to Example
duplicate sets of the inherited members of Grandparent inside Child 2M
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.

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

SUMMER – 2023 EXAMINATION


MODEL ANSWER-Only for the Use of RAC Assessors

Subject: Object Oriented Programming Using C++ Subject Code: 22316

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
{
intrno;
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:";

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

SUMMER – 2023 EXAMINATION


MODEL ANSWER-Only for the Use of RAC Assessors

Subject: Object Oriented Programming Using C++ Subject Code: 22316

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()
{
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;

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

SUMMER – 2023 EXAMINATION


MODEL ANSWER-Only for the Use of RAC Assessors

Subject: Object Oriented Programming Using C++ Subject Code: 22316

clrscr();
obj.getnumber();
obj.getmarks();
obj.getscore();
obj.display();
getch();
}

d) State the advantages of pointer 4M


Ans. 1. Pointers save memory space.
2. Pointers reduce the length and complexity of a program. Any four
correct
3. Pointers allow the passing of arrays and strings to function more advantages
efficiently. 1M each
4. Pointers make it possible to return more than one value from the
function.
5. Execution time with pointers is faster because data are
manipulated with the address, that is, direct access to memory
location.
6. Memory is accessed efficiently with the pointers. The pointer
assigns and releases the memory as well. Hence it can be said
the Memory of pointers is dynamically allocated.
7. Pointers are used with data structures. They are useful for
representing two-dimensional and multi-dimensional arrays.
8. An array, of any type, can be accessed with the help of pointers,
without considering its subscript range.
9. Pointers are used for file handling.
10. Pointers are used to allocate memory dynamically.
11. In C++, a pointer declared to a base class could access the object
of a derived class. However, a pointer to a derived class cannot
access the object of a base class.

e) Write a program for closing a file 4M


Ans. Program code:
#include <iostream>
#include <fstream>
int main()
{

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

SUMMER – 2023 EXAMINATION


MODEL ANSWER-Only for the Use of RAC Assessors

Subject: Object Oriented Programming Using C++ Subject Code: 22316

fstreamFileName;
FileName.open("FileName.txt", ios::in); File object
creation 1M,
if (!FileName) opening of
{ file 1M,
cout<<"File doesn’t exist"; closing of
} file 2M
else
{
cout<<"File opened successfully";
}
FileName.close();
return 0;
}
5. Attempt any TWO of the following: 12M
a) State the use of scope resolution operator and explain it with 6M
example.
Ans.  Scope of a variable extends from the point of declaration to the Explanation
3M
end of the block.
 A variable declared inside a block is 'local' variable and a
variable declared outside block is called as global variable.
 When we want to use a global variable but also has a local
variable with same name.
 To access the global version of the variable, C++ provides scope
resolution operator.

Example:-
#include <iostream> Example
3M
using namespace std;
char a = 'm';
static int b = 50;
int main() {
char a = 's';
cout<< "The value static variable b is : "<< ::b;
cout<< "\nThe value of local variable a is : " << a;
cout<< "\nThe value of global variable a is : " << ::a;
return 0;
}
Note: Any other relevant example shall be considered.

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

SUMMER – 2023 EXAMINATION


MODEL ANSWER-Only for the Use of RAC Assessors

Subject: Object Oriented Programming Using C++ Subject Code: 22316

b) Write a program to show object as function argument. 6M


Ans. #include <iostream>
class Decimal Correct
logic 3M
{
private:
int a; Correct
public: syntax 3M
void set(int x)
{
a = x;
}
void sum(Decimal ob1, Decimal ob2)
{
a = ob1.a + ob2.a;
}
void print()
{
cout<< "Value of A: " << a <<endl;
}
};
int main()
{
Decimal d1;
Decimal d2;
Decimal d3;
d1.set(10);
d2.set(20);
d3.sum(d1, d2);
cout<<"First Decimal value\n";
d1.print();
cout<<"Second Decimal value\n";
d2.print();
cout<<"Sum of Decimal object1 and object2 =\n";
d3.print();
return 0;
}

Note: Any other relevant program shall be considered.

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

SUMMER – 2023 EXAMINATION


MODEL ANSWER-Only for the Use of RAC Assessors

Subject: Object Oriented Programming Using C++ Subject Code: 22316

c) Write a program on hybrid inheritance 6M


Ans. #include <iostream>
class A Correct
logic 3M
{
public: Correct
syntax 3M
int x;
};
class B : public A
{
public:
B() //constructor to initialize x in base class A
{
x = 10;
}
};
class C
{
public:
int y;
C() //constructor to initialize y
{
y = 4;
}
};
class D : public B, public C //D is derived from class B and class
C
{
public:
void sum()
{
cout<< "Sum= " << x + y;
}
};
int main()

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

SUMMER – 2023 EXAMINATION


MODEL ANSWER-Only for the Use of RAC Assessors

Subject: Object Oriented Programming Using C++ Subject Code: 22316

{
D obj1; //object of derived class D
obj1.sum();
return 0;
}

Note: Any other relevant program shall be considered.


6. Attempt any TWO of the following 12M
a) State the difference between constructor and destructor (any six 6M
points)
Ans. Constructor Destructor Any six
points 1M
1. A constructor is a special 1. A destructor is a special each
member function whose task s member function
is to initialize the objects of whose task is to destroy
its class. the objects that have
been created by
constructor.
2. Constructors are classified in 2. Destructors are not
various types such as: classified in any types.
Overloaded constructor
Default constructor
Parameterized constructor
Copy constructor
3. It is invoked automatically 3. It is invoked implicitly
when the objects are created. by the compiler upon
exit of program / block /
function.
4. Constructor accepts 4. Destructor never
parameters. Also it can have accepts any parameter.
default value for its
parameter.
5. A class can have more than 5. A class can have at the
one constructor. most one constructor.
6. It constructs the values of 6. It does not construct the
data members of the class. values for the data
members of the class.
7. Syntax- 7. Syntax-
classname() destructor name is

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

SUMMER – 2023 EXAMINATION


MODEL ANSWER-Only for the Use of RAC Assessors

Subject: Object Oriented Programming Using C++ Subject Code: 22316

{… preceded with tilde.


… ~classname()
} {….
….
}
8. Example- 8. Example-
ABC() ~ABC()
{ {
… ….
} }
b) Explain abstract class with suitable example. 6M
Ans.  By definition, a C++ abstract class must include at least one pure
virtual function. Correct
explanation
 Alternatively, put a function without a definition. 3M
 Because the subclass would otherwise turn into an abstract class
Correct
in and of itself, the abstract class's descendants must specify the example 3M
pure virtual function.
 Although the Abstract class type cannot be created from scratch,
it can have pointers and references made to it.
 A pure virtual function can exist in an abstract class in addition
to regular functions and variables.
 The main objective of abstract base class is to provide some
traits to the derived classes and to create a base pointer required
for achieving run time polymorphism
 Example:
#include<iostream>
class Figure
{
public:
virtual int Area() = 0;
void setBreadth(int br)
{
breadth = br;
}
void setHeight(int ht)

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

SUMMER – 2023 EXAMINATION


MODEL ANSWER-Only for the Use of RAC Assessors

Subject: Object Oriented Programming Using C++ Subject Code: 22316

{
height = ht;
}
protected:
int breadth;
int height;
};
class Rectangle: public Figure
{
public:
int Area()
{
return (breadth * height);
}
};

class Triangle: public Figure


{
public:
int Area()
{
return (breadth * height)/2;
}
};
int main()
{
Rectangle R1;
Triangle T1;
R1.setBreadth(6);
R1.setHeight(12);
T1.setBreadth(40);
T1.setHeight(4);
cout<< "The area of the rectangle is: " << R1.Area() <<endl;
cout<< "The area of the triangle is: " << T1.Area() <<endl;
}

Note: Any other relevant example shall be considered.

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

SUMMER – 2023 EXAMINATION


MODEL ANSWER-Only for the Use of RAC Assessors

Subject: Object Oriented Programming Using C++ Subject Code: 22316

c) Write C++ program to overload binary operator ‘+’ to 6M


concatenate two strings.
Ans. #include<iostream> Correct
#include<string.h> logic 3M
class String
{
public: Correct
char str[20]; syntax 3M
public:
void accept_string()
{
cout<<"\n Enter String : ";
cin>>str;
}
void display_string()
{
cout<<str;
}
String operator+(String x) //Concatenating String
{
String s;
strcat(str,x.str);
strcpy(s.str,str);
return s;
}
};
int main()
{
String str1, str2, str3;
str1.accept_string();
str2.accept_string();
str3=str1+str2;
cout<<"\n\n Concatenated String is : ";
str3.display_string();
return 0;
}

Page 20 / 20

You might also like