XXXXX: Important Instructions To Examiners
XXXXX: Important Instructions To Examiners
(Autonomous)
(ISO/IEC - 27001 - 2013 Certified)
__________________________________________________________________________________________________
Subject Name: Object Oriented Programming Using C++ Subject Code: 22316
Important Instructions to examiners: XXXXX
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.
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 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 Marking
Answer
No. Q. N. Scheme
1 Attempt any FIVE of the following: 10 M
a Differentiate between C and C++. (Any two points) 2M
Ans Any two
Sr. relevant
C C++
No. differences
7 In C, scanf() and printf() are C++ mainly uses stream cin and
mainly used for input/output. cout to perform input and output
operations.
Use:
• Flushing the buffer: If the file was opened for writing, fclose() flushes
the remaining contents of the internal buffer to the file ensuring all data is
written.
• Closing the file: It releases the system resources associated with the open
file descriptor. This includes the internal buffer and the file descriptor
itself.
Syntax: Syntax ½ M
int main() {
int y = square(5);
// ...
}
Example:
int number;
cin >> number;
g Explain reference and dereference operators w.r.t. pointer. 2M
Ans Pointers are powerful tools that allow you to access and manipulate memory Reference
locations directly. operator 1M
Interacting with pointers requires understanding two crucial operators: the Dereference
operator 1M
reference operator (&) and the dereference operator (*).
Example:
int x = 10;
int* ptr = &x; // stores the memory address of "x" in "ptr"
Example:
int value = *ptr; // retrieves the value of "x" using the address stored in "ptr"
• Variable names are case sensitive (myVar and myvar are different
variables).
• Variable names cannot contain whitespaces or special characters like !, #,
%, etc.
• Reserved words (like C++ keywords, such as int) cannot be used as
variable names.
Choose variable names that are descriptive and reflect the purpose or meaning of
the variable. This helps improve code readability and makes it easier for others
(including yourself) to understand the code later.
1.Public: All the class members declared under the public specifier will be
available to everyone. The data members and member functions declared as
public can be accessed by other classes and functions too. The public members
of a class can be accessed from anywhere in the program using the direct
member access operator (.) with the object of that class.
2. Private: The class members declared as private can be accessed only by the
member functions inside the class. They are not allowed to be accessed directly
by any object or function outside the class. Only the member functions or
the friend functions are allowed to access the private data members of the class.
Write a C++ program to find out whether the given number is even or odd
d 4M
(taking input from keyboard).
Ans (Note: Any relevant logic should be considered) Accepting
#include<iostream.h> Number 1M
#include<conio.h> Condition to
void main() check number
{ 2M
int num;
Display result
clrscr();
1M
cout<<"\nEnter a Number ";
cin>>num;
if(num%2==0)
{
Explanation
1M,
Correct
example
program 2M
As shown in the diagram class B inherits property from class A and class C
inherits property from class B.
#include<iostream>
#include<conio.h>
using namespace std;
class base //single base class
{
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;
}
};
class derive2 : public derive1 // derived from class derive1
{
private:
int z;
public:
void indata()
{
d Write a C++ program for write into a file using file operations. 4M
Ans #include<iostream.h> Object creation
#include<fstream.h> 1M,
#include <conio.h>
int main( ) Open() 1M,
{
File writing
fstream newfile;
1M,
newfile.open(“Test.txt”,ios::out);
Page No: 10/22
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2013 Certified)
__________________________________________________________________________________________________
if(!newfile) Close() 1M
{
cout<<”\nFile cannot be created”;
}
else
{
cout<<”\nFile created successfully”;
newfile<<” Writing this to a file.”;
newfile.close( );
}
getch( );
return 0;
}
OR
#include <iostream.h>
#include <fstream.h>
#include <conio.h>
int main( )
{
ofstream newfile;
newfile.open ("Test.txt");
newfile.close( );
getch( );
return 0;
3) It can be invoked like a normal function without the help of any object.
4) Unlike member functions, it cannot access the member names directly and has
to use an object name and dot membership operator with each member name.
OR
#include<iostream>
#include<conio.h>
Page No: 13/22
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2013 Certified)
__________________________________________________________________________________________________
protected:
float Marks1,Marks2;
public:
void inputTestMarks() {
cout << "Enter test marks1: ";
cin >> Marks1;
cout << "Enter test marks2: ";
cin >> Marks2;
}
void displayTestMarks() {
cout << "Test Marks1: "<<Marks1<<endl;
r1.displaySportMarks();
r1.TotalMarks();
return 0;
}
b Describe ‘this’ pointer in C++ with an example. 6M
Ans In C++, the "this" pointer is a keyword that refers to the object for which a member 2 marks for
explaination,2
function is being called. It's a pointer that holds the memory address of the current
marks for
object instance. It's implicitly available within non-static member functions of a syntax,2 marks
for example.
class.
this->variable_name=value;
#include<iostream>
/* local variable is same as a member's name */
class Test
{
private:
int x;
public:
void setX (int x)
{
// The 'this' pointer is used to retrieve the object's x
// hidden by the local variable 'x'
this->x = x;
}
void print() { cout << "x = " << x << endl; }
};
int main()
{
Test obj;
int x = 20;
obj.setX(x);
obj.print();
return 0;
}
cout << "Enter details for Employee " << i + 1 << std::endl;
cout << "Enter Employee ID: ";
cin >> empId;
cout << "Enter Salary: ";
cin >> salary;
employees[i].setData(empId, salary);
}
// Displaying data for 10 employees
cout << "\nDetails of Employees:" <<endl;
for (int i = 0; i <10; ++i) {
std::cout << "Employee " << i + 1 << ": ";
employees[i].displayData();
}
return 0;
}
6 Attempt any TWO of the following: 12 M
a Explain the rules of virtual function. 6M
Ans 1. The functions cannot be static 1 mark for each
2. You derive them using the “virtual” keyword rule any 6
points
3. Virtual functions in C++ needs to be a member of some other class (base
class)
4. They can be a friend function of another class
5. The prototype of these functions should be the same for both the base and
derived class
6. Virtual functions are accessible using object pointers
7. Redefining the virtual function in the derived class is optional, but it
needs to be defined in the base class
8. The function call resolving is done at run-time
9. You can create a virtual destructor but not a constructor
b Explain virtual base class with an example. 6M
Ans Virtual Base Class: An ancestor class is declared as virtual base class to avoid Explanation
duplication of inherited members inside child class due to multiple paths of
inheritance. 2 marks,
Syntax 1 mark,
3 marks for
correct
program
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.
Syntax:
class Grandparent
{
};
class Parent1:virtual public Grandparent
{
};
class Parent2: public virtual Grandparent
{
};
class Child: public Parent1,public Parent2
Page No: 19/22
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2013 Certified)
__________________________________________________________________________________________________
{
};
Example:
#include<iostream>
#include<conio.h>
using namespace std;
class student
{
int roll_no;
public:
void getnumber()
{
cout<<"Enter Roll No:";
cin>>roll_no;
}
void putnumber()
{
cout<<"\n\n\t Roll No:"<<roll_no<<"\n";
}
};
class test: virtual public student
{
public:
int test1,test2;
void getmarks()
{
cout<<"Enter Marks\n";
cout<<"Test 1:";
cin>>test1; cout<<"Test 2:";
cin>>test2;
}
void putmarks()
{
cout<<"\t Marks Obtained\n";
cout<<"\n\t Test 1 Marks :"<<test1;
cout<<"\n\t Test 1 Marks:"<<test2;
}
};
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;
Page No: 20/22
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2013 Certified)
__________________________________________________________________________________________________
}
};
class result: public test, public sports
{
int total;
public:
void display()
{
total=test1+test2+score;
putnumber();
putmarks();
putscore();
cout<<"\n\t Total Score:"<<total;
}
};
int main()
{
result obj;
obj.getnumber();
obj.getmarks();
obj.getscore();
obj.display();
return 0;
}
c Write a program to declare a class ‘item’ containing data members as
‘item_name’, ‘code’, ‘price’. Accept and display the information for one 6M
object using pointer to that object.
Ans #include<iostream> Declaration of
using namespace std; class 2 marks,
class item
{ accept and
public: display
char item_name[20]; function 2
int code,price; marks,
2 marks for
public:
main function
// accept function to get the information about the item
with pointer to
void accept()
object.
{
cout<<"Enter the item name :"<<endl;
cin>>item_name;
cout<<"Enter the item code :"<<endl;
cin>>code;
cout<<"Enter the item price :"<<endl;
cin>>price;
cout<<"-------------------------------------------"<<endl;
}
// display function to show the information about the item
void display()
{
cout<<"ITEM NAME:"<<item_name<<endl;
cout<<"CODE:"<<code<<endl;
Page No: 21/22
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2013 Certified)
__________________________________________________________________________________________________
cout<<"PRICE:"<<price<<endl;
}
};
int main()
{
// Create an object of the Item class i and pointer variable i1
item i, *i1;
// Pointer to the object
i1=&i;
// accepting and displaying information for the object i using pointer variable i1
i1->accept();
i1->display();
}