0% found this document useful (0 votes)
196 views22 pages

XXXXX: Important Instructions To Examiners

The document discusses the model answer key for the winter 2023 examination for the subject of Object Oriented Programming Using C++. It provides instructions to examiners on evaluating answers and the model answer for multiple choice questions covering topics like differences between C and C++, class syntax, applications of OOP, functions like fclose, inline functions, and input operator.

Uploaded by

Sanskar Nimje
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)
196 views22 pages

XXXXX: Important Instructions To Examiners

The document discusses the model answer key for the winter 2023 examination for the subject of Object Oriented Programming Using C++. It provides instructions to examiners on evaluating answers and the model answer for multiple choice questions covering topics like differences between C and C++, class syntax, applications of OOP, functions like fclose, inline functions, and input operator.

Uploaded by

Sanskar Nimje
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/ 22

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION

(Autonomous)
(ISO/IEC - 27001 - 2013 Certified)
__________________________________________________________________________________________________

WINTER – 2023 EXAMINATION


Model Answer – Only for the Use of RAC Assessors

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

1 C supports the procedural style C++ supports both procedural 1M each


programming. and object oriented.

2 Data is less secured in C. In C++, you can use modifiers


for class members to make it
inaccessible for outside users.

3 C follows the top-down approach. C++ follows the bottom-up


approach.

4 C does not support function C++ supports function


overloading. overloading.

5 In C, you can't use functions in In C++, you can use functions in


structure. structure.

6 C does not support reference C++ supports reference


variables. variables.

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.

Page No: 1/22


MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2013 Certified)
__________________________________________________________________________________________________

8 Operator overloading is not Operator overloading is possible


possible in C. in C++.

9 C programs are divided into C++ programs are divided


procedures and modules. into functions and classes.

10 C++ programs are divided into C++ supports the feature of


functions and classes. namespace.

11 C does not support the C++ supports inheritance.


inheritance.

b Give the syntax of class. 2M


Ans Syntax: Correct Syntax
2M
class class_name
{
private:
variable declarations;
function declarations;
public:
variable declarations;
function declarations;
};

c Explain any four applications of OOP. 2M


Ans Applications of object-oriented programming are: Any four
applications
1) Real time systems each ½ marks
2) Simulation and modeling
3) Object-oriented databases
4) Hypertext, hypermedia and expertext
5) AI and expert systems
6) Neural networks and parallel programming
7) Decision support and office automation systems
8) CIM/CAM/CAD systems

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


Ans Syntax: Syntax 1M
Correct use 1M
int fclose(FILE* stream);

Page No: 2/22


MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2013 Certified)
__________________________________________________________________________________________________

Use:

• The fclose() function is used to close a file opened previously using


fopen(). It returns 0 on successful closure and EOF on encountering any
errors.
It performs the following tasks:

• 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.

e Explain inline member function. 2M


Ans An inline function in C++ is a function that the compiler attempts to replace with
its actual code whenever the function is called. This can potentially improve the
performance of your program by reducing the overhead associated with function Explanation
calls. inline 1M

Syntax: Syntax ½ M

inline <return type> function_name(parameter list) Example ½ M


{
// function body
}
Example:
inline int square(int x) {
return x * x;
}

int main() {
int y = square(5);
// ...
}

f Explain the input operator in C++. 2M


Ans The input operator, commonly known as the extraction operator and denoted by Correct
>>, is a powerful tool in C++ used for reading data from input streams. It works explanation 2M
in conjunction with the standard input stream object cin and allows you to easily
retrieve various types of data from the user or any other input source.

Operator: >> (extraction operator)


Purpose: Used to read data from input streams.

Page No: 3/22


MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2013 Certified)
__________________________________________________________________________________________________

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 (*).

1. Reference Operator (&):


• Also known as the "address-of" operator.
• Obtains the memory address of a variable.
• Syntax: & followed by the variable name.

Example:
int x = 10;
int* ptr = &x; // stores the memory address of "x" in "ptr"

2. Dereference Operator (*):

• Also known as the "indirection" operator.


• Retrieves the value stored at the memory address pointed to by a pointer.
• Syntax: * preceding the pointer variable.

Example:
int value = *ptr; // retrieves the value of "x" using the address stored in "ptr"

2 Attempt any THREE of the following: 12 M


a Explain the rules for naming variables in C++. (Any four points) 4M
Ans Any four rules
1 M each
The general rules for naming variables are:

• Variable names can contain letters, digits and underscores.


• Variable names must begin with a letter or an underscore ( _ ).
Page No: 4/22
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2013 Certified)
__________________________________________________________________________________________________

• 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.

b Explain the access specifiers in C++. 4M


Ans There are 3 types of access specifiers: List of access
specifier 1M
1. Public
2. Private Explanation of
3. Protected each term 1M

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.

3. Protected: The protected access modifier is similar to the private access


modifier in the sense that it can’t be accessed outside of its class unless with the
help of a friend class. The difference is that the class members declared as
Protected can be accessed by any subclass (derived class) of that class as well.

c What is inheritance? Give different types of inheritance. 4M


Ans Inheritance: Correct
explanation
The mechanism of deriving new class from an old/existing class is called of inheritance
inheritance. 2M
OR
Correct
Inheritance is the process by which objects of one class acquired the properties types of
of objects of another classes. inheritance
(any 4) 2M
Syntax:
class derived-class-name: visibility-mode base-class-name
{
------//

Page No: 5/22


MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2013 Certified)
__________________________________________________________________________________________________

-----// members of derived class


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

2) Multiple inheritance: In multiple inheritance, derived class is derived from


more than one base classes.
Diagram:

3) Hierarchical inheritance: In hierarchical inheritance, more than one derived


classes are derived from single class.
Diagram:

Page No: 6/22


MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2013 Certified)
__________________________________________________________________________________________________

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


from a derived class (intermediate base class) which in turn derived from a single
base class.
Diagram:

5) Hybrid inheritance: Hybrid inheritance is a combination of single, multiple,


multilevel and hierarchical inheritance.
Diagram:

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

Page No: 7/22


MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2013 Certified)
__________________________________________________________________________________________________

cout<<"\nEntered number is even";


}
else
{
cout<<"\nEntered number is odd";
}
getch();
}

3 Attempt any THREE of the following: 12 M


Write a C++ program to find the area of rectangle using class rectangle
which has following details-
i) Accept length and breadth from user.
a 4M
ii) Calculate the area
iii) Display the result.

Ans #include<iostream> Correct class


#include<conio.h> definition 1M,
using namespace std;
class rectangle Data member
{ accepting 1M,
private:
Area
int length;
calculation and
int breadth;
display 1M,
public:
void accept() Main function
{ 1M
cout<<"Enter length & breadth: \n";
cin>>length;
cin>>breadth;
}
void area()
{
int area;
area=length*breadth;
cout<<"\nArea of rectangle:"<<area;
}
};
int main()
{
rectangle r;
r.accept();
r.area();
getch();
}
b Explain multilevel inheritance with an example. 4M
Ans When a class is derived from another derived class then it is called as multilevel Correct
inheritance. diagram 1M,

Explanation
1M,

Page No: 8/22


MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2013 Certified)
__________________________________________________________________________________________________

Correct
example
program 2M

As shown in the diagram class B inherits property from class A and class C
inherits property from class B.

(Note: Any relevant Program should be considered)

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

Page No: 9/22


MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2013 Certified)
__________________________________________________________________________________________________

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();
getch();
}
c Write a C++ program to define structure student having data members 4M
name, roll no., age. Accept and display data for one student.

Ans #include<iostream> Correct


#include<conio.h> structure
using namespace std; definition 1M,
struct student
{ Variable
char name[50]; creation 1M,
int rollno; Data accepting
int age; and display 2M
};
int main()
{
student s;
cout<<"\nEnter name, rollno & age of student: \n";
cin>>s.name>>s.rollno>>s.age;
cout<<"\nName is "<<s.name;
cout<<"\nrollno "<<s.rollno;
cout<<"\nage "<<s.age;
getch();
}

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 << "Writing this to a file.\n";

newfile.close( );
getch( );
return 0;

4 Attempt any THREE of the following: 12 M


Write a C++ program to copy data from one file to another.
a 4M
Ans #include <iostream> Two object
#include <fstream> creation for
using namespace std; two file 2M,
int main()
{ Data reading
string line; from and
ifstream srcfile("source.txt"); writing to file
ofstream destifile("destination.txt"); 1M,
if(srcfile && destifile) File closing 1M
{
while(getline(srcfile,line))
{
destifile <<line << "\n";
}

Page No: 11/22


MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2013 Certified)
__________________________________________________________________________________________________

cout << "Copy Finished \n";


}
else
{
cout<<"Cannot read File";
}
srcfile.close();
destifile.close();
return 0;
}

Differentiate between compile time polymorphism and run time


b polymorphism. (Any four points) 4M

Ans Any 4 correct


Compile Time Polymorphism Run Time Polymorphism comparison
each have 1M
In compile-time polymorphism, the In run-time polymorphism, the
method call is decided by the method call is decided during the
compiler. execution of the program.

Compile-time polymorphism is Run-time polymorphism is


implemented through method implemented through method
overloading, which allows multiple overriding, where different classes
methods with the same name but can have the same method with the
different parameters and return types. same parameters.

Compile-time polymorphism is also Run-time polymorphism is also


known as early binding, overloading, referred to as late binding, dynamic
and static binding. binding, and overriding.

Compile-time polymorphism is Run-time polymorphism is achieved


achieved through operator through pointers and virtual functions.
overloading and function overloading.

Compile-time polymorphism is less Run-time polymorphism is more


flexible as it operates at compile time. adaptable as it operates at run time.

Faster execution Comparatively slower execution

c Explain the characteristics of Friend function. 4M


Ans 1) It is not in the scope of the class to which it has been declared as friend. Any 4 correct
characteristics
2) Since it is not in the scope of a class, it cannot be called using the object of each have 1M
that class.

3) It can be invoked like a normal function without the help of any object.

Page No: 12/22


MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2013 Certified)
__________________________________________________________________________________________________

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.

5) It can be declared either in public or the private part of a class without


affecting its meaning.

6) It has the objects as arguments.

7) The friend function should not be defined inside the class.

d Write a program to declare a class measure having data members add1,


add2, add3. Initialize the data members using constructor and store their 4M
addition in third data member using function and display the addition.

Ans //Using Default Constructor Correct class


definition
#include<iostream> 1M,Constructor
#include<conio.h>
definition 1M,
using namespace std;
class measure Addition and
{
result display
private:
int add1,add2,add3; 1M,
public:
Main function
measure()
{ 1M
add1=10;
add2=20;
}
void sum()
{
add3=add1+add2;
}
void display()
{
cout<<"\nSum = "<<add3;
}
};
int main()
{
measure m();
measure m1;
m1.sum();
m1.display();
getch();
return 0;
}

OR

//Using Parameterized Constructor

#include<iostream>
#include<conio.h>
Page No: 13/22
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2013 Certified)
__________________________________________________________________________________________________

using namespace std;


class measure
{
private:
int add1, add2, add3;
public:
measure(int a,int b)
{
add1=a;
add2=b;
}
void sum()
{
add3=add1+add2;
}
void display()
{
cout<<"\nSum = "<<add3;
}
};
int main()
{
measure m(10,20);//if parameterized constructor is used
m.sum();
m.display();
getch();
return 0;
}

Differentiate between constructor and destructor in C++. (Any four points)


e 4M
Ans Any 4 correct
Constructor Destructor comparison
each have 1M
Constructor helps to initialize the Whereas destructor is used to destroy
object of a class and allots the the instances.
memory to an object.

It is declared as Whereas it is declared as


className( arguments if any) ~ className( no arguments )
{Constructor’s Body }. { }.
A constructor is automatically A destructor is automatically invoked
invoked when an object is created. when the program ends or the object
is no longer in use.

Constructors can take arguments. Destructors do not take any


arguments.

A constructor enables an object to A destructor enables an object to


initialize some of its values before it perform some actions or execute
is used. some code at the time of its
destruction.

Page No: 14/22


MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2013 Certified)
__________________________________________________________________________________________________

Constructors can be overloaded. Destructors cannot be overloaded.

In the context of constructors, a class In the context of destructors, a class


can have multiple constructors. always has only one destructor.

Constructors are typically invoked in Destructors are typically invoked in


sequential order. the reverse order of the constructor
invocation.

5 Attempt any TWO of the following: 12 M


a Write a C++ program to implement multiple inheritance as shown in Figure 6M
No. 1. Accept and display data of test marks and sport’s marks using object
of class ‘result’.

Ans #include <iostream> 2 marks for


logic,2 marks
using namespace std; for syntax,2
marks for
// Base class for test marks
correctness of
class Test { program

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;

Page No: 15/22


MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2013 Certified)
__________________________________________________________________________________________________

cout << "Test Marks2: "<<Marks2<<endl;


}
};
// Base class for sports marks
class Sports {
protected:
float sportMarks;
public:
void inputSportsMarks() {
cout << "Enter sports marks: ";
cin >> sportMarks;
}
void displaySportMarks() {
cout << "Sports Marks: " << sportMarks << endl;
}
};
// Derived class Result inheriting TestMarks and SportsMarks
class Result : public Test, public Sports {
private:
int total;
public:
void TotalMarks() {
total=Marks1+Marks2+sportMarks;
cout<<"Total:"<<total;
}
};
int main() {
Result r1;
r1.inputTestMarks();
r1.inputSportsMarks();
r1.displayTestMarks() ;

Page No: 16/22


MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2013 Certified)
__________________________________________________________________________________________________

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.

Syntax for this pointer is:

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;
}

Page No: 17/22


MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2013 Certified)
__________________________________________________________________________________________________

write a program to declare a class ‘employee’ containing data members


c 6M
‘emp-id’ and ‘salary’. Accept and display this data for 10 employees.
Ans (Note: Any relevant logic should be considered) 2 marks for
#include <iostream> class
declaration,
using namespace std;
2 marks for
class employee { logic,
private: 2 marks for
correct
int emp_id; implementation
double salary;
public:
void setData(int id, double sal) {
emp_id = id;
salary = sal;
}
void displayData() {
cout << "Employee ID: " << emp_id << ", Salary: " << salary << endl;
}
};
int main()
{
Employee employees[10];
// Accepting data for 10 employees
for (int i = 0; i <10; ++i) {
int empId;
double salary;

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;

Page No: 18/22


MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2013 Certified)
__________________________________________________________________________________________________

}
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)
__________________________________________________________________________________________________

{
};

(Note: Any relevant Program should be considered)

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

Page No: 22/22

You might also like