0% found this document useful (0 votes)
7 views

OOP EPA

The document provides an analysis of examination papers across multiple semesters, detailing the topics covered, question formats, and marks distribution for various subjects related to object-oriented programming and C++. It includes a breakdown of questions by semester and unit, highlighting the evolution of topics and question types over time. The analysis serves as a reference for understanding the assessment structure and content focus in programming courses.

Uploaded by

neilshinde67
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

OOP EPA

The document provides an analysis of examination papers across multiple semesters, detailing the topics covered, question formats, and marks distribution for various subjects related to object-oriented programming and C++. It includes a breakdown of questions by semester and unit, highlighting the evolution of topics and question types over time. The analysis serves as a reference for understanding the assessment structure and content focus in programming courses.

Uploaded by

neilshinde67
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 137

Examination Paper Analysis

Winter -23 Summer-24


Winter-22 Summer-22 Summer-23
Topic
Name of Topic
No. Marks
Question Marks Question Marks Question Marks Question Marks Question
1.(a) 2 1.(a) 2 1.(a) 2 1.(a) 2 Q.1 (a) 2
1.(b) 2 1.(c) 2 2.(a) 4 1.(c) 2 Q.1 (b) 2
1.(c) 2 2.(a) 4 3.(a) 4 1.(f) 2 Q.1 (d) 2
2.(a) 4 2.(d) 4 3.(d) 4 2.(a) 4 Q.2 (a) 4
Principal of object 2.(c) 4 4.(c) 4 4.(a) 4 2.(d) 4 Q.4 (e) 4
1
oriented programming
3.(a) 4 4.(d) 4 5.(a) 6 3.(c) 4 Q.5 (a) 6
4.(b) 4
4.(d) 4
Total: 26 Total: 20 Total: 24 Total: 18 Total: 14
1.(d) 2 1.(b) 2 1.(e) 2 2.(b) 4
1.(d) 2
3.(b) 4 1.(c) 2 1.(b) 2 3.(a) 4
1.(f) 2
5.(a) 6 2.(b) 4 2.(b) 4 4.(b) 4
2.(b) 4
6.(a) 6 3.(b) 4 3.(a) 4 6.(a) 6
2.(b) 4
4.(b) 4 4.(b) 4 6.(b) 6
3.(a) 4
2 Classes and objects 5.(b) 6 4.(d) 4
3.(c) 4
6.(a) 6 4.(e) 4
4.(e) 4
5.(c) 6
5.(c) 6
6.(a) 6

Total: 18 Total: 36 Total: 28 Total: 30 Total: 24


2.(b) 4 4.(a) 4 1.(d) 2 2.(c) 4 1.(e) 2
Extending classes using 3.(c) 4 5.(b) 4 1.(e) 2 3.(b) 4 2.(c) 4
3
inheritance 4.(a) 4 6.(c) 6 2.(c) 4 5.(b) 6 3.(b) 4
5.(b) 6 4.(c) 4 6.(b) 6 3.(c) 4
Examination Paper Analysis

6.(c) 6 5.(c) 6 4.(a) 4


6.(b) 6 5. (a) 6

Total: 24 Total: 14 Total: 24 Total: 20 Total: 24


1.(e) 2 1.(g) 2 1.(f) 2 1.(g) 2 1.(C) 2
3.(d) 4 3.(d) 4 2.(d) 4 4.(b) 4 2.(d) 4
4.(c) 4 4.(b) 4 3.(c) 4 5.(a) 6 4.(c) 4
Pointers and 5.(c) 6 5.(a)(i) 3 4.(d) 4 6.(c) 6 5.(b) 6
4
polymorphism in c++ 6(b) 6 5.(b)(ii) 3 6.(c) 6 6.(c) 6
6(b) 6
Total: 22 Total: 22 Total: 20 Total: 18 Total: 22
1.(f) 2 1.(b) 2 1.(g) 2 1.(d) 2 1.(f) 2
1.(g) 2 1.(e) 2 4.(e) 4 3.(b) 4 1.(g) 2
2.(d) 4 3.(b) 4 4.(a) 4 3.(d) 4
5 File operations 4.(e) 4 4.(d) 4

Total: 12 Total: 8 Total: 6 Total: 10 Total: 12


Examination Paper Analysis

Exam Unit No and Question no as per MSBTE paper Marks


Year Name
Summer Unit:1 Q1 a) List two memory management operators available in C++. Also state its use 2
2022 Principal of
Q1 c) Write any four applications of OOP 2
object Q2 a) State and describe characteristics of object-oriented programming. 4
oriented Q2 d) Describe concept of type casting using suitable example. 4
programming Q4 c) Write a C++ program to find and display the smallest number of an array. 4
Q4 d) Write a C++ program to declare a structure book with members as book id and name. Accept and 4
display data of five books using array of structure.
Winter Unit:1 Q1 a) State any Four application of oop. 2
2022 Principal of Q1 b) Explain user defined datatype with example. 2
object Q1 c) Describe use of scop resolution operator with example 2
oriented Q2 a) Develop a c++ program to print Fibonacci series. 4
programming Q2 c) Develop a c++ program for accept data from user to calculate percentage for 5 subject and display 4
grade according to percentage.
Q3 a) Describe structure of c++ program. 4
Q4 d) Develop a c++ program to create structure student with data member Name, Roll No., percentage 4
accept and display data for 5 student using structure.
Q4 b) Develop a c++ program to print sum of 10 no. using array. 4

Summer Unit:1 1.(a) Demonstrate the static and dynamic initialization of variable. 2
2023 Principal of 2.(a) State the features of object oriented programming. 4
object 3.(a) Write a program to print first n natural numbers and their sum using for loop. 4
oriented
3.(d) Write a program for get and put functions. 4
programming
4.(a) Explain the structure of C++ program. 4
5.(a) State the use of scope resolution operator and explain it with example. 6

Winter Unit:1 1.(a) Differentiate between C and C++. (Any two points) 2
2023 Principal of 1.(c) Explain any four applications of OOP. 2
object 1.(f) Explain the input operator in C++. 2
oriented
2.(a) Explain the rules for naming variables in C++. (Any four points) 4
programming
Examination Paper Analysis

2.(d) Write a C++ program to find out whether the given number is even or odd (taking input from 4
keyboard).
3.(c) Write a C++ program to define structure student having data members name, roll no., age. Accept 4
and display data for one student.
Summer Unit:1 Q.1 (a) State any two features of object-oriented programming. 2
2024 Principal of Q.1 (b) Define class and object. 2
object Q.1 (d) State the use of Memory Management Operator and explain it with example. 2
oriented Q.4 (e) With suitable example describe structure of C++ program. 4
programming Q.2 (a) Develop a program to find factorial of a given number using for loop. 4
Q.5 (c) Develop a C++ program to add two 3x3 matrices and display addition. 6
Summer Unit 2 Q1 d) Define constructor. List types of constructor. 2
2022 Classes and Q 1 f) Write any two characteristics of friend function. 2
objects Q2 b) Write a C++ program to declare a class college with name and code. Derive a new class as student 4
with members as name. Accept and display details of one student along with college data.
Q2 c) Write a C++ program to declare a class student with data members as roll no and name. Declare a 4
constructor to initialize data members of class. Display the data.
Q3 a) Write a C++ program to declare a class mobile having data members as price and model number. 4
Accept and display the data for Ten objects.
Q3 c) Describe visibility modes and their effects used in inheritance. 4
Q4 e) Describe constructor with default arguments with an example. 4
Q5 c) (Hint : class 1 contains m1 and class 2 contains m2) Write a C++ program to declare two classes 6
with data members as m1 and m2 respectively. Use friend function to calculate average of two (m1, m2)
marks and display it. 6
Q6 a) Write any two characteristics of static data member. Write C++ program to count number of objects
created with the help of static data member
Winter Unit 2 Q 1 d) Define constructors and it's type. 2
2022 Classes and Q 3 b) Explain with suitable example Friend Function. 4
objects Q 6 a) Develop a c++ program for constructor with default argument and use of destructor. 6
Summer Unit 2 1.(b) Write the syntax for declaration of class. 2
2023 Classes and 1.(c) State the characteristics of static member function. 2
objects 2.(b) Explain overloaded constructor with suitable example. 4
3.(b) Compare static and non static data members. 4
Examination Paper Analysis

4.(b) List any four properties of constructor function. 4


5.(b) Write a program to show object as function argument. 6
6.(a) State the difference between constructor and destructor (any six points) 6
Winter Unit 2 1.(e) Explain inline member function. 2
2023 Classes and 1.(b) Give the syntax of class. 2
objects 2.(b) Explain the access specifiers in C++. 4
3.(a) Write a C++ program to find the area of rectangle using class rectangle which has following 4
details – i) Accept length and breadth from the user. ii) Calculate the area iii) Display the result.
4.(c) Explain the characteristics of Friend function 4
4.(d) Write a program to declare a class measure having data members add1, add2 and add3. Initialize 4
the data members using constructor and store their addition in third data member using function and
display the addition.
4.(e) Differentiate between constructor and destructor in C++. (Any four points) 4
5.(c) Write a program to declare a class ‘employee’ containing data members ‘emp-id’ and ‘salary’. 6
Accept and display this data for 10 employees.
Summer Unit 2 2.(b) Explain friend function with suitable example. 4
2024 Classes and 3.(a) Develop a program to declare a class student the data members are rollno, name and marks accept 4
objects and display data for one object of class student.
4.(b) Differentiate between constructor and destructor (any 4 points) 4
6.(a) Develop a C++ program to create structure book with data members name, cost and author. 6
Accept and display data for 5 books using structure
6.(b) Develop a C++ program using parameterized constructor 6

Summer Unit 3 Q4 a) Describe the concept of virtual base class with example. 4
2022 Extending Q5 b) Write a C++ program to implement following inheritance: Refer Fig. No. 1 6
classes using
inheritance
Examination Paper Analysis

Accept and display total of one object of result.


Q6 c) Write a program to implement inheritance as shown in figure No. 2. Assume suitable member

function
Accept and display data of one Teacher and one Officer.
Winter Unit 3 Q 2 b) Develop a c++ program for multilevel inheritance. 4
2022 Extending Q 3 c) Describe all visibility modes and effects with example 4
classes using
inheritance

.
Q 5 b) Develop a c++ program to implement virtual Base class. 6
Q 6 c) Develop c++ program to implement inheritance shown in fig. 6
Examination Paper Analysis

Summer Unit 3 1.(d) State different types of visibility mode in inheritance. 2


2023 Extending 1.(e) Give the syntax for constructor in derived classes. 2
classes using 2.(c) Write a program on single inheritance. 4
inheritance
4.(c) Illustrate the concept of virtual base class with suitable class. 4
5.(c) Write a program on hybrid inheritance. 6
6.(b) Explain abstract class with suitable example. 6
Winter Unit 3 2.(c) What is inheritance? Give different types of inheritance. 4
2023 Extending 3.(b) Explain multilevel inheritance with an example 4
classes using 6
inheritance
Examination Paper Analysis

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

Q.6.(b) Explain virtual base class with an example.


Summer Unit 3 Q.5 a) Write a C++ program to implement inheritance as shown in fig: 6
2024 Extending
classes using
inheritance

Q4.a) Describe all visibility modes and effects with example. 4


Q3.c) Write a C++ program to declare a class college with name and code. Derive new class as student 4
with members as name. Accept and display details of one student along with college data. 4
Q3.b) Describe the concept of virtual base class with suitable example. 4
Q2.c) What is multilevel inheritance? Develop a C++ program for multilevel inheritance. 4
Q1.e) List different types of inheritance. 2
Examination Paper Analysis

Summer Unit 4 Q1.g) State the need of virtual function in C++. 2


2022 Pointers and Q3.d) Differentiate between compile time and run time polymorphism. 4
polymorphism Q4.b) Write a C++ program to overload add function to add two integer numbers and two float numbers. 4
in c++ Q5 a) i) Define pointer operator and address operator with example. 3
Q5 b) ii) Write a C++ program to declare a class train with members as train no and name. Accept and 3
display data for one object of train. Use pointer to object to call functions of class.
Q6 b) Write a C++ program to overload “+” operator so that it will perform concatenation of two strings. 6
(Use class get data function to accept two strings)
Winter Unit 4 Q 1 e) Explain concept of pointer with example. 2
2022 Pointers and Q 4 c) Develop a c++ program to perform arithmetic operation using pointer. 4
polymorphism Q 5 c) Explain rules of operator overloading and overload ‘+’ operator to concatenate two strings. 6
in c++ Q 6 b) Describe Function overloading with suitable program. 6
Q 3 d) Differentiate between compile time polymorphism and Runtime polymorphism. 4
Q 5 a) Define class book with following Data member and member function for 10 book. 6
Data Member Member Function
1. B - name → getdata ( )
2. B - author → put data ( )
3. B - price
Summer Unit 4 1.(f) Define polymorphism with its types. 2
2023 Pointers and 2.(d) Illustrate this pointer with example. 4
polymorphism 3.(c) State any four rules for virtual functions. 4
in c++
4.(d) State the advantages of pointer. 4
6.(c) Write C++ program to overload binary operator ‘+’ to concatenate two strings. 6
Winter Unit 4 1.(g) Explain reference and dereference operators w.r.t. pointer 2
2023 Pointers and 4.(b) Differentiate between compile time polymorphism and run time polymorphism. (Any four points) 4
polymorphism 5.(b) Describe ‘this’ pointer in C++ with an example. 6
in c++
6.(c) Write a program to declare a class ‘item’ containing data members as ‘item_name’, ‘code’, ‘price’. 6
Accept and display the information for one object using pointer to that object.
Summer Unit 4 1. (c) What are the rules for virtual function (write any two) 2
2024 Pointers and 2.(d) Explain virtual function with example. Give the rules for virtual functions. 4
polymorphism 4. (c) Describe function overloading and function overriding with suitable example. 4
in c++
5.(b) Develop a program to declare class book containing data members as title 6
Examination Paper Analysis

,authorname ,publication, price accept and display the information for one object using pointer to that
objects.
6.(c) Write a C++ program to overload “+” operator so that it will perform concatenation of two strings. 6
(Use class get data function to accept two strings.)
Summer Unit 5 Q1 b) List c++ stream classes along with their function. (any two classes). 2
2022 File Q1 e) Explain ios :: app and ios :: in flags 2
operations Q3 b) Write a C++ program to copy the contents of a source file student 1.txt to a destination file student 4
2.txt using file operations.
Winter Unit 5 Q 1 f) Explain file modes used to perform file operations. 2
2022 File Q 1 g) List all stream classes used in stream operation. 2
operations Q.2 d)Develop c++ program to open and read content of file also write “object oriented” string in file 4
and close it. 4
Q 4 e) Develop c++ program to check Detection of end of file.
Summer Unit 5 1.(g) Define file with it’s operations. 2
2023 File 4.(e) Write a program for closing a file. 4
operations
Winter Unit 5 1.(d) Give the syntax and use of fclose( ) function. 2
2023 File 3.(d) Write a C++ program for write into a file using file operations. 4
operations 4.(a) Write a C++ program to copy data from one file to another. 4
Unit 5 4.(d) Develop a C++ program to read content of file abc.txt 4
Summer File 3.(d) Write a program to count number of lines in a file. 4
2024 operations 1.(g) Give the syntax of fclose ( ) method. 2
1.(f) List C++ stream classes along with their function (any two classes) 2
Examination Paper Analysis

Exam Unit No Question no as per MSBTE paper Marking


Year and Name Scheme
Summer Unit:1 Q1 a) List two memory management operators available in C++. Also state its use. 2
2022 Principal of Solution: (Any 2 )
object new operator:
oriented The new operator in C++ is used for dynamic storage allocation. This operator can be used to create object
programmi of any type.
ng The general syntax of new operator in C++ is as follows:
Pointer-variable = new datatype;
In the above statement, new is a keyword and the pointer variable is a variable of type datatype.
For example:
int *a=new int;
delete operator:
The delete operator in C++ is used for releasing memory space when the object is no longer needed. Once
a new operator is used, it is efficient to use the corresponding delete operator for release of memory.
The general syntax of delete operator in C++ is as follows:
delete pointer-variable;
Q1 c) Write any four applications of OOP. 2
Solution: (Any 4 )
Development of editors, compilers.
• Developing data bases.
• Developing communication systems and complex real-time applications.
• Simulation and Modeling.
• AI and Expert system
Q2 a) State and describe characteristics of object oriented programming. 2
Solution: (Any 4 )
• Emphasis in data rather than procedures.
• Programs are divided into objects.
• Data structures are design such that they characterize object.
• Functions that operate on the data are tied together in the data structure called object.
• Data is hidden and cannot be accessed by external functions.
• Object may communicate with each other through functions.
• New data and functions can be easily added whenever necessary.
Examination Paper Analysis

• It follows Bottom-Up approach.


Q2 d) Describe concept of type casting using suitable example.
Solution: Type-casting* is the conversion of one data type into another.
There are two ways to perform type-casting in C++:
• Implicit Casting
• Explicit Casting

• Implicit Casting:
In implicit casting, the compiler will convert the values automatically. The conversion takes place in the
order of precedence, i.e., the data type is converted into a data type of higher precedence. It also ensures a
minimum loss of data. Below is the precedence order of the data types.
For example, if we take an expression where we are performing some operation on an int and float data
type, the int value will automatically be converted to a float data type due to its higher precedence.
Example:
#include <iostream.h>
using namespace std;
int main() {
int a = 18;
char b = 'S';
a = a * b;
float c = a / 5.0;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
cout << "c = " << c << endl;
return 0;
}
• Explicit Casting:
In explicit casting, the user needs to physically convert one data type into another. The syntax for using
this casting is:
(data_type) variable_name;
Example:
#include <iostream>
using namespace std;
Examination Paper Analysis

int main()
{
float x = 28.5;
float a = (int) x - 12;
cout << "a = " << a;
return 0;
}
Q4 c) Write a C++ program to find and display the smallest number of an array. 4(correct
Solution: logic 4
#include<iostream.h> marks)
#include <conio.h>
int main()
{
int a[100], count, i, min;
cout << "Enter Number of Elements in Array\n";
cin >> count;
cout << "Enter " << count << " numbers \n";
// Read array elements
for(i = 0; i < count; i++){
cin >> a[i];
}
min = a[0];
// search num in inputArray from index 0 to elementCount-1
for(i = 0; i < count; i++){
if(a[i] < min){
min = a[i];
}
}
cout << "Minimum Element\n" << min;
return 0;
}
Examination Paper Analysis

Q4 d) Write a C++ program to declare a structure book with members as book id and name. Accept and 4(correct
display data of five books using array of structure. logic 4
Solution: marks)
# include <iostream.h>
struct Book
{
char name[40];
int bookid;
};
void main()
{
Book b[5];
int i;
for ( i=0; i<5 ; i++)
{
cout << “Enter Book name “;
cin >> b[i].name;
cout<< ”Enter Book ID”;
cin>> b[i].bookid;
}
cout << “DETAILS OF BOOK: “ <<endl;
for ( i=0; i<5 ; i++)
{ cout<< ”NAME : “ << b[i].name <<endl
cout<< ” BOOK ID : “<< b[i].bookid <<endl;
}
}
Winter Unit:1 Q1 a) State any Four application of oop. 2
2022 Principal of Solution : Four Applications of OOP: (Any 4)
object • Development of editors, compilers
oriented • Developing data bases
programmi • Developing communication systems and complex real-time applications.
ng • Simulation and modeling.
• AI and Expert system
Examination Paper Analysis

Q1 b) Explain user defined datatype with example. 2


Solution : (1/2 marks
User-Defined DataTypes:The data types that are defined by the user are called the derived datatype or each)
user-defined derived data type.These types include:
• Class – A User-defined data type that holds data members and functions whose access can be
specified as private, public, or protected. It uses the ‘class’ keyword for defining the data structure.
• Structure – A structured data type is used to group data items of different types into a single type.
For Eg. Structure can be Address, in which it further contains information such as Flat number,
Building name, street, city, state, pin code, etc. It is defined by using the ‘struct’
• Union – Union is a type of data structure where all the members of that union share the same
memory location. If any changes made in the Union, it will be visible to others as well. The
‘union’ keyword is used to define this type of User-defined data type.
• Enumeration – It helps assign names to integer constants in the program. The keyword ‘enum’ is
used. It is used to increase the readability of the code.
• Typedef – Typedef defines a new name for an existing data type. It does not create a new data
class. It makes code readability easy and gives more clarity to the user.
Q1 c) Describe use of scope resolution operator with example 2
Solution : (Use 1 m and
The scope resolution operator ( :: ) is used for several reasons. For example: If the global variable name is correct
same as local variable name, the scope resolution operator will be used to call the global variable. It is also example 1
used to define a function outside the class and used to access the static variables of class. m)
#include <iostream>
using namespace std;
char a = 'm';
static int b = 50;
int main() {
char a = 's';
cout << "The static variable : "<< ::b;
cout << "\nThe local variable : " << a;
cout << "\nThe global variable : " << ::a;
return 0;
}
Q2 a) Develop a c++ program to print Fibonacci series.
Examination Paper Analysis

Solution :Fibonacci Series without recursion: 4(correct


#include <iostream> logic 4
using namespace std; marks)
int main()
{
int n1=0,n2=1,n3,i,number;
cout<<"Enter the number of elements: ";
cin>>number;
cout<<n1<<" "<<n2<<" "; //printing 0 and 1
for(i=2;i<number;++i) //loop starts from 2 because 0 and 1 are already printed
{
n3=n1+n2;
cout<<n3<<" ";
n1=n2;
n2=n3;
}
return 0;
}
Fibonnaci series using recursion in C++
#include<iostream>
using namespace std;
void printFibonacci(int n){
static int n1=0, n2=1, n3;
if(n>0){
n3 = n1 + n2;
n1 = n2;
n2 = n3;
cout<<n3<<" ";
printFibonacci(n-1);
}
}
int main(){
int n;
Examination Paper Analysis

cout<<"Enter the number of elements: ";


cin>>n;
cout<<"Fibonacci Series: ";
cout<<"0 "<<"1 ";
printFibonacci(n-2); //n-2 because 2 numbers are already printed
return 0;
}
Q2 c) Develop a c++ program for accept data from user to calculate percentage for 5 subject and display 4
grade according to percentage. (Correct
Solution : C++ Logic 4
#include<iostream> Marks)
using namespace std;
int main()
{
int phy,chem,bio,maths,comp;
int total;
float percent;
cout<<"Enter marks of computer";
cin>>phy;
cout<<"enter marks of English";
cin>>chem;
cout<<"enter marks of bio";
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)
Examination Paper Analysis

{
cout<<"Grade=B";
}
else if(percent>=70)
{
cout<<"Grade=C";
}
else if(percent>=60)
{
cout<<"Grade=D";
}
else if(percent>=40)
{
cout<<"Grade=E";
}
else
{
cout<<"Gread F";
}
}
Q3 a) Describe structure of c++ program. 4
Description: - (diagram 1 m
and
explanation
marks)

1. Include header files


In this section a programmer includes all header files which are required to execute a given program. The
most important file is iostream.h header file. This file defines most of the C++statements
Examination Paper Analysis

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 written within various classes.
Q4 d) Develop a c++ program to create structure student with data member Name, Roll No., percentage
accept and display data for 5 student using structure. 4(correct
#include <iostream> logic 4
using namespace std; marks)
struct student
{
char name[50];
int roll;
float marks;
};
int main()
{
student s[5];
int I;
for(i=0;i<5;i++)
{
cout << "Enter information," << endl;
cout << "Enter name: ";
cin >> s[i].name;
cout << "Enter roll number: ";
cin >> s[i].roll;
cout << "Enter marks: ";
cin >> s[i].marks;
}
for(i=0;i<5;i++)
Examination Paper Analysis

cout << "\nDisplaying Information," << endl;


cout << "Name: " << s[i].name << endl;
cout << "Roll: " << s[i].roll << endl;
cout << "Marks: " << s[i].marks << endl;
return 0;
}
}
Q4 b) Develop a c++ program to print sum of 10 no. using array. 4(correct
Solution: logic 4
#include <iostream> marks)
using namespace std;
int main()
{
int arr[10],;
int sum=0;
for(int i=0; i<10; i++)
{
cout<<"Enter number-"<<i+1<<": ";
cin>>arr[i];
}
for(int i=0; i<10; i++)
{

sum =sum+ arr[i];


}

cout<<"Sum of 10 numbers is "<<sum;


}
Summer Unit:1 1.(a) Demonstrate the static and dynamic initialization of variable. 2M
2023 Principal of Ans.
object Syntax
oriented datatype variable_name=value;
Examination Paper Analysis

programmi Example - Static


ng int a=5; initialization
Or -1M,
int a; a=5;
dynamic
Dynamic initialization initialization
Syntax – -1M
int a;
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
2.(a) State the features of object-oriented programming. 4M
Ans.
Features of Object Oriented Programming are: Any four
features 1M
• Follows bottom-up approach in program design.
each
• Emphasis is on data rather than procedure.
• 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.

3.(a) Write a program to print first n natural numbers and their sum using for loop. 4M
Ans.
#include<iostream.h>
#include<conio.h> Declaration
1M, Display
Examination Paper Analysis

void main() of natural


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

3.(d) Write a program for get and put functions. 4M Correct


Ans. syntax of
#include <iostream.h> get(): 2M,
int main() Correct
{ syntax of
int count = 0; put(): 2M
char c;
cout<< "INPUT TEXT\n";
cin.get(c);
while (c != '\n')
{
cout.put(c);
count++;
Examination Paper Analysis

cin.get(c);
}
cout<< "\nNumber of characters = " << count << "\n";
return 0;
}
Note: Any other relevant program shall be considered.
4.(a) Explain the structure of C++ program. 4M
Ans.
General C++ program has following structure:
Correct
diagram 2M
Correct
explanation
2M

Description: -
1. Include header files
In this section a programmer includes all header files which are required to execute a 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 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 written within various classes.
5.(a) State the use of scope resolution operator and explain it with example. 6M
Ans. Explanation
Scope of a variable extends from the point of declaration to the end of the block. 3M Example
3M
Examination Paper Analysis

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.h>
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;
}
Winter Unit:1 1.(a) Differentiate between C and C++. (Any two points)
2024 Principal of Solution:
object Sr.
oriented C C++
No.
programmi 1 C supports the C++ supports both
ng procedural style procedural and object
programming. oriented.
2 Data is less secured in In C++, you can use
C. modifiers for class
members to make it
inaccessible for outside
users.
3 C follows the top-down C++ follows the
approach. bottom-up approach.
4 C does not support C++ supports function
function overloading. overloading.
5 In C, you can't use In C++, you can use
functions in structure. functions in structure.
Examination Paper Analysis

6 C does not support C++ supports


reference variables. reference variables.
7 In C, scanf() and C++ mainly uses stream
printf() are mainly cin and cout to perform
used for input/output. input and output
operations.
8 Operator overloading is Operator overloading is
not possible in C. possible in C++.
9 C programs are divided C++ programs are
into procedures and divided into
modules. functions and
classes.
10 C++ programs are C++ supports the
divided into functions feature of namespace.
and classes.
11 C does not support the C++ supports
inheritance. inheritance.
12
1.(c) Explain any four applications of OOP.
Solution -Applications of object-oriented programming are:
• Real time systems
• simulation and modeling
• Object-oriented databases
• Hypertext, hypermedia and expertext
• AI and expert systems
• Neural networks and parallel programming
• Decision support and office automation systems
• CIM/CAM/CAD systems
1.(f) Explain the input operator in C++.
The input operator, commonly known as the extraction operator and denoted by
Examination Paper Analysis

>>, is a powerful tool in C++ used for reading data from input streams. It works 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.

Example:
int number;
cin >> number;

2.(a) Explain the rules for naming variables in C++. (Any four points)
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 ( _ ).
• 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.
Examination Paper Analysis

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.
2.(d) Write a C++ program to find out whether the given number is even or odd (taking input from
keyboard).
(Note: Any relevant logic should be considered)
#include<iostream.h> #include<conio.h>
void main()
{
int num; clrscr();
cout<<"\nEnter a Number "; cin>>num;
if(num%2==0)
{
cout<<"\nEntered number is even";
}
else
{
cout<<"\nEntered number is odd";
}
getch();
}

3.(c) Write a C++ program to define structure student having data members name, roll no., age. Accept
and display data for one student.
#include<iostream>
#include<conio.h>
using namespace std;
struct student
{
char name[50];
int rollno;
Examination Paper Analysis

int age;
};
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();
}
Summer Unit:1 Q.1 (a) State any two features of object-oriented programming. 2
2024 Principal of • Emphasis in data rather than procedures.
object • Programs are divided into objects.
oriented • Data structures are design such that they characterize object.
programmi • Functions that operate on the data are tied together in the data structure called object.
ng • Data is hidden and cannot be accessed by external functions.
• Object may communicate with each other through functions.
• New data and functions can be easily added whenever necessary.
• It follows Bottom-Up approach.

Q.1 (b) Define class and object.


2
Class-
• The class is a way of binding data and functions associated with data together.
• The entire set of data and the functions can be made user defined data type with help of class. Thus
Class is a user defined data type.
• Class act as a template, which is used to create objects. The class contains data and member functions
that operate on the data.
• Once the class has been is defined, we can create any number of objects belonging to that class. Thus
object is nothing but an instance (variable) of class. In other words class is a collection of similar
objects.

Object
Examination Paper Analysis

• Objects are basic run time entities of an Object Oriented system. “


• It is nothing but an instance of class.
• An Object is encapsulation of data and code that operate on that data.
• The Object may represent a person, a place , a bank or any item that the program has to handle.
Q.1 (d) State the use of Memory Management Operator and explain it with example.
Memory Management operators are new and delete. Uses of Memory

management operators:

• Dynamic memory allocation in C++ refers to performing memory allocation manually by a


programmer.
• Use of dynamically allocated memory is to allocate memory of variable size, which is not
possible with compiler allocated memory except for variable-length arrays.
• The most important use is the flexibility provided to programmers.
• We are free to allocate and deallocate memory whenever we need it and whenever we don’t need
it anymore.

Explanation of new and delete:


new operator: This operator is used to allocate memory dynamically at runtime. It takes the data type
of the object you want to create as an argument and returns a pointer to the newly allocated memory
block.

delete operator: This operator is used to deallocate memory that was previously allocated using new
operator. It takes a pointer to the memory block you want to free as an argument.

Example:

int* numbers = new int[10]; for (int i = 0; i < 10;


i++)
{
numbers[i] = i * i;

}
Examination Paper Analysis

delete [ ] numbers;

Q.4 (e) With suitable example describe structure of C++ program.

General C++ program has following structure:

fig.: Structure of a C++ program


Description:
1. Include files
In this section a programmer includes 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 Definitions
This section allows programmer to design member functions of a class. The programmer can have inside
declaration of a function or outside declaration of a function.
4. Main Function Program
In this section programmer creates objects and calls various functions writer within various class.

Example:
Examination Paper Analysis

#include<iostream>
using namespace std;
class MyClass // The class
{
public: // Access specifier
int myNum; // Attribute (int variable) string myString; //
Attribute (string variable)
};
int main()
{
MyClass myObj; // Create an object of MyClass

// Access attributes and set values myObj.myNum = 15;


myObj.myString = "Some text";

// Print attribute values


cout << myObj.myNum << "\n"; cout <<
myObj.myString;
return 0;
}
Q.2 (a) Develop a program to find factorial of a given number using for loop.
#include<iostream> using namespace std;
int main()
{
int no, fact=1, i;

cout<<"Enter number:"; cin>>no;


for(i=1;i<=no;i++)
{
fact=fact*i;
}
cout<<"Factorial ="<<fact;
}
Examination Paper Analysis

Q.5 (c) Develop a C++ program to add two 3x3 matrices and display addition.
#include<iostream>
using namespace std;
int main()
int mat1[3][3], mat2[3][3], i, j, mat3[3][3];
cout<<"Enter Elements of First Matrix: ";
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
cin>>mat1[i][j];
}
cout<<"Enter Elements of Second Matrix: ";
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
cin>>mat2[i][j];
}
cout<<"\nAdding the Two Given Matrix...\n";
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
mat3[i][j] = mat1[i][j]+mat2[i][j];
}
cout<<"Addition Result of Two Given Matrix is:\n";
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
cout<<mat3[i][j]<<" ";
cout<<endl;
Examination Paper Analysis

}
return 0;
}
Summer Unit 2 Q1 d) Define constructor. List types of constructor . 2
2022 Classes and Solution: (Definition 1
objects Definition: A constructor in C++ is a special 'MEMBER FUNCTION' having the same name as that of its mark and
class which is used to initialize some valid values to the data members of an object. It is executed types 1
automatically whenever an object of a class is created. mark)
Types:
• Default Constructor
• Parameterized Constructor
• Copy Constructor
• Constructor with default value
Q 1 f) Write any two characteristics of friend function. 2
Solution: (Any 2
• A friend function is not in the scope of the class, in which it has been declared as friend. characterstics
• It cannot be called using the object of that class. )
• It can be invoked like a normal function without any object.
• Unlike member functions, it cannot use the member names directly.
• It can be declared in public or private part without affecting its meaning.
• Usually, it has objects as arguments.
Q2 b) Write a C++ program to declare a class college with name and code. Derive a new class as student 4(correct
with members as name. Accept and display details of one student along with college data. logic 4
Solution: marks)
class college
{ char name[20];
int code;
public: void getdata()
{ cout<<”Enter the college name and code”;
cin>> name>>code;
}
void putdata()
{ cout<<”College Name=”<<name;
Examination Paper Analysis

cout<<”College Code=”<<code;
}
};
class student: public college
{ char name[20];
public:void getdata()
{ college::getdata();
cout<<”Enter student name ” ;
cin>>name;
}
void putdata()
{ college::putdata();
cout<<”Student Name=”<<name;
}
};
void main()
{ student s;
s.getdata();
s.putdata();
getch();
}
Q2 c) Write a C++ program to declare a class student with data members as roll no and name. Declare a 4(correct
constructor to initialize data members of class. Display the data. logic 4
Solution: marks)
#include<iostream.h>
#include<conio.h>
class student
{
int rollno;
char name[20];
public: student(int r,char n[])
{ rollno =r;
strcpy (name,n);
}
Examination Paper Analysis

void putdata()
{ cout<<”RollNo=”<<rollno;
cout<<”Name=”<<name;
}
};
void main()
{
student s(12,”Sangeeta”);
s.putdata();
getch();
}
Q3 a) Write a C++ program to declare a class mobile having data members as price and model number. 4
Accept and display the data for Ten objects. (correct logic
Solution: 4M)
class mobile
{
float price:
int model_number;
public:
void getdata()
{ cout<< “Enter Mobile price and model number”;
cin>>price>>model_number;
}
void putdata()
{ cout<<”The price of mobile is :”<< price;
cout<<”Model Number of Mobile is :”<< model_number;
}
};
void main()
{ mobile m[10];
int i;
for(i=0;i<10;i++)
{ m[i].getdata();
}
Examination Paper Analysis

for(i=0;i<10;i++)
{ m[i].putdata();
}
getch();
}
Q3 c) Describe visibility modes and their effects used in inheritance. 4
Solution: (Explanation
1. Public mode: 4M)
In the public mode of inheritance, when a child class is derived from the base or parent class, then the
public member of the base class or parent class will become public in the child class also, in the same way,
the protected member of the base class becomes protected in the child class, and private members of the
base class are not accessible in the derived class.
2. Protected mode:
In protected mode, when a child class is derived from a base class or parent class, then both public and
protected members of the base class will become protected in the derived class, and private members of
the base class are again not accessible in the derived class. In contrast, protected members can be easily
accessed in the derived class.
3. Private mode:
In private mode, when a child class is derived from a base class, then both public and protected members
of the base class will become private in the derived class, and private members of the base class are again
not accessible in the derived class.

❖ Member function of a class can access all type of data ( private , protected , public).
❖ Private members are only accessed by member function of a class.
Examination Paper Analysis

❖ Object of a class can access only public member of a class. They cannot access private or protected
members of a class.
❖ Protected members are accessed only by member function of a class and member function of
immediately derived class.
Q4 e) Describe constructor with default arguments with an example. 4
Solution:A constructor with default arguments means passing default values to arguments. You can call (correct logic
a constructor without passing all its parameter but you have to specify default values when the 4M)
constructor is declared.
Example:
class student
{
int roll_no;
float std_marks;
public:
student(int a=10,float b=50)
{
roll_no=a;
std_marks=b;
}
void display()
{
cout<<"Roll no: "<<roll_no<<endl;
cout<<"Marks: "<<std_marks<<endl;
}
};
void main()
{
student s1;
student s2(1,60);
clrscr();
Examination Paper Analysis

cout<<"\n Output using default constructor arguments:\n";


s1.display();
cout<<"\n Output without using default constructor arguments:\n";
s2.display();
getch(); }

Q5 c) (Hint : class 1 contains m1 and class 2 contains m2) Write a C++ program to declare two classes 6
with data members as m1 and m2 respectively. Use friend function to calculate average of two (m1, m2) (correct logic
marks and display it. 6M)
Solution:

# include <iostream.h>
class test2; // prior declaration of class2
class test1
{
private: float m1;
public: void setdata (float a )
{
m1=a ;
}
friend void average ( test1 ,test2); // function average is a friend of test1
};
class test2
{
private: float m2;
public: void setdata (float a)
{
m2=a ;
}
friend void average ( test1 ,test2); // function average is a friend of test1
};
void average ( test1 t1 , test2 t2)
{
Examination Paper Analysis

float avg;
avg =( t1.m1 + t2.m2 )/2;
cout << avg;
}
void main ()
{
test t1;
test t2;
t1.setdata (34);
t2.setdata (40);
average ( t1 ,t2 ); // friend fun. is invoked like a normal c++ function without any object
}

Q6 a) Write any two characteristics of static data member. Write C++ program to count number of objects 6
created with the help of static data member. (Explanation
Solution: 2M and
Static data members are class members that are declared using static keywords. A static member has certain Program 4M)
special characteristics which are as follows:
• Only one copy of that member is created for the entire class and is shared by all the objects of that
class, no matter how many objects are created.
• It is initialized before any object of this class is created, even before the main starts.
• It is visible only within the class, but its lifetime is the entire program.
Syntax:
static data_type data_member_name;
#include<iostream>

using namespace std;

class counter

{ private: static int count;

public: counter()
Examination Paper Analysis

{ count++;

void display()
{ cout<<"The number of objects created are: "<<count;
}
};
int counter::count=0;
int main()
{ counter c1;
counter c2;
counter c3;
c2.display();
}
Winter Unit 2 Q 1 d) Define constructors and it's type. 2
2022 Classes and Constructors are special functions in C++ that are used to initialize objects. They are called automatically (1M
objects when an object is created, and they can be used to set the initial values of the object's data members. definition
Types of constructors are : and Types
• Default Constructor. 1M
• Parameterized Constructor.
• Copy Constructor.
• Overloaded constructor
• Constructors with default argument
Q 3 b) Explain with suitable example Friend Function. 4
• Private members of a class cannot be accessed from outside the class. However there could be a (Explanation
situation where more than one classes want to share a particular function. 2M and
• For example consider two classes ’manager’ and ‘scientist’. We can use a function incom_tax ( ) that Example
will operate on the objects of both these classes and calculate the total income tax. Here the function 2M)
income_tax () need to access private members of both the classes.
• This can be done with the help of friend function.
• “Friend function is a normal c++ function that can access private members of the class to whom it is
declared as friend.”
Examination Paper Analysis

• To make a normal c++ function “friendly” to the class , precede the declaration of the function with
keyword ‘friend’ inside the class.
Example
Class fun
{
private:
int a, b;
public:
friend void xyz ( fun s); // function xyz is a friend of class fun
};
• The function is defined somewhere in the program (outside the class) like a normal c++ function.
• The function can be declared as to any number of classes.
• The friend function is not a member of a class, but it has full access rights to the private members of
the class.
/*Program to find the mean value of a given number using friend function.
#include<iostream.h> #include<conio.h>
class sample
{
int val1,val2;
public:
void get()
{
cout<<"Enter two values:";
cin>>val1>>val2;
}
friend float mean(Sample ob);
};
float mean(sample ob)
{
return float(ob.val1+ob.val2)/2;
}
void main()
{
clrscr();
Examination Paper Analysis

sample obj;
obj.get();
cout<<"\n Mean value is : "<<mean(obj);
getch();
}
Output:
Enter two values: 10, 20
Mean Value is: 15
Q 6 a) Develop a c++ program for constructor with default argument and use of destructor.
# include <iostream.h> 6
class employee
{ float basic , TA, DA , HRA; (correct logic
public: employee ( float b , float T , float H =3500 , float D = 5000) 6M)
{ basic = b;
TA =T;
HRA = h;
DA = D;
}
void display_salary (void)
{ float GS;
GS = basic + TA + DA + HRA;
cout << GS << endl;
}
}
void main ()
{
employee e1 ( 8000,2000);
cout << “ Gross Salary of e1 = “ ;
e1.display_salary();
employee e2 ( 8000 , 2000 , 5000 , 10000);
cout << “ Gross Salary of e2 = “ ;
e2. Display_salary();
}
OUTPUT:
Examination Paper Analysis

cout << “ Gross Salary of e1 = 18500“ ;


cout << “ Gross Salary of e2 = 25000“ ;
Summer Unit 2 1.(b) Write the syntax for declaration of class. 2M
2023 Classes and Ans.
objects class class_name Correct
{ syntax 2M
private:
variable declarations;
function declarations;
public:
variable declarations;
function declarations;
};
1.(c) State the characteristics of static member function. 2M
Ans.
A static function can have access to only other static member declared in the class. Any two
A static member function can be called using the class name instead of objects as follows: characteristic
class_name::function_name. s 1M each
2.(b) Explain overloaded constructor with suitable example. 4
Ans. (Explanation
• Constructor overloading is a process of defining more than one constructor function in a class. 2M and
• A class can have more than one constructor function example 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.
Example:
class example
{
int a,b;
public:
example()
Examination Paper Analysis

{
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 executesand 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 createdwith two values, third constructor executes and assigns value 10 tovariable a and
value 20 to variable b.
3.(b) Compare static and non static data members. 4M Any four
Ans. correct points
1M each
Examination Paper Analysis

4.(b) List any four properties of constructor function. 4M Any four


Ans. properties
1M each
Examination Paper Analysis

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


#include <iostream> logic 3M
class Decimal Correct
{ syntax 3M
private:
int a;
public:
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();
Examination Paper Analysis

return 0;
}
6.(a) State the difference between constructor and destructor (any six points)
Ans. 6M
Any six
points 1M
each
Examination Paper Analysis
Examination Paper Analysis

Winter Unit 2 1.(e) Explain inline member function.


2024 Classes and An inline function in C++ is a function that the compiler attempts to replace with
objects its actual code whenever the function is called. This can potentially improve the performance of your
program by reducing the overhead associated with function calls.
Syntax:
inline <return type> function_name(parameter list)
{
// function body
}
Example:
inline int square(int x) {
return x * x;
}
int main() {
int y = square(5);
// ...
}
1.(b) Give the syntax of class.
Syntax:
class class_name
{
private:
variable declarations; function declarations;
public:
variable declarations; function declarations;
};
Examination Paper Analysis

2.(b) Explain the access specifiers in C++.


There are 3 types of access specifiers:
Public
Private
Protected
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.

3.(a) Write a C++ program to find the area of rectangle using class rectangle which has following details
i) Accept length and breadth from the user.
ii) Calculate the area
iii) Display the result.
#include<iostream>
#include<conio.h>
sing namespace std;
class rectangle
{
private:int length;
int breadth;
Examination Paper Analysis

public:
void accept()
{
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();
}
4.(c) Explain the characteristics of Friend function

CHARACTERISTICS OF FRIEND FUNCTION:


• It is not in the scope of the class to which it is declared as friend.
• Since it is not in the scope of the class it can not be called using an object of the class.
• It can be invoked like a normal c++ function without the help of any object.
• Unlike member function , it cannot access the data members directly and has to use an object
name and dot (.) operator.
• It can be declared either in the public or private sections of a class without affection its meaning.
• Usually it has object as argument.
Examination Paper Analysis

4.(d) Write a program to declare a class measure having data members add1, add2 and add3. Initialize
the data members using constructor and store their addition in third data member using function and
display the addition.
//Using Parameterized Constructor
#include<iostream>
#include<conio.h>
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;
}
Examination Paper Analysis

4.(e) Differentiate between constructor and destructor in C++. (Any four points)
Constructor Destructor
Constructor helps to initialize Whereas destructor is used to
the object of a class and allots destroy the instances.
the 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 A destructor is automatically
automatically invoked when invoked when the program
an object is created. ends or the object is no longer
in use.
Constructors can take Destructors do not take any
arguments. arguments.
A constructor enables an A destructor enables an object
object to initialize some of its to perform some actions or
values before it is used. execute some code at the time
of its destruction.
Constructors can be overloaded. Destructors cannot be
overloaded.
In the context of constructors, In the context of destructors, a
a class can have multiple class always has only one
constructors. destructor.
5.(c) Write a program to declare a class ‘employee’ containing data members ‘emp-id’ and ‘salary’.
Accept and display this data for 10 employees.
#include <iostream> using namespace std; class employee { private:
int emp_id; double salary;
public:
Examination Paper Analysis

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;
}
Summer Unit 2 2.(b) Explain friend function with suitable example.
2024 Classes and
objects The private members of a class cannot be accessed from outside the class but in some situations two
classes may need access of each other’s private data.
Examination Paper Analysis

The common function is made friendly with all those classes whose private data need to be shared in
that function. This common function is called as friend function.

Characteristics:

• Friend function is not in the scope of the class to which it has been declared as friend.
• It is called without any object of class like a normal function.
• It cannot access the member names directly and has to use an object name and dot membership
operator with each member name.
• It can be declared either in the public or the private part of a class without affecting its
meaning.
• It has the objects as arguments.

Example:
Program to interchange values of two integer numbers using friend function.
#include<iostream> using namespace std;
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: void accept()
Examination Paper Analysis

{
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;
}
int main()
{
A a;
B b; a.accept();
b.accept();
swap(a,b);
}
3.(a) Develop a program to declare a class student the data members are rollno, name and marks accept
and display data for one object of class student.
#include<iostream> using namespace std;
class student
{
int rollno;
char name[20]; float marks; public:
Examination Paper Analysis

void accept()
{
cout<<"\nEnter data of student:"; cout<<"\nRoll
number:";
cin>>rollno;
cout<<"\nName:";
cin>>name;
cout<<"\nMarks:";
cin>>marks;
}
void display()
{
cout<<"\nStudents data is:"; cout<<"\nRoll
number:"<<rollno; cout<<"\nName:"<<name;
cout<<"\nMarks:"<<marks;
}

};
int main()
{
student S; S.accept();
S.display();
}
4.(b) Differentiate between constructor and destructor (any 4 points)
Constructor Destructor

Constructor helps to initialize the destructor is used to destroy the


object of a class and allots the objects created by constructor.
memory to an objec
It is declared as className Whereas it is declared as
(arguments if any) ~ className(no
{Constructor’s Body }. arguments ){ }.
Examination Paper Analysis

Constructor can either accept While it can’t have any


arguments or not. arguments.
A constructor is called when an It is called while object of the
instance or object of a class is class is freed or deleted.
created.
Constructor is used to allocate While it is used to
the memory to an instance or deallocate the memory of
object. an object of a class.
Constructor can be overloaded. While it can’t be overloaded.
The constructor’s name is same Here, its name is also same as
as the class name. the class name preceded by
the tiled (~) operator.
In a class, there can be multiple While in a class, there is
constructors. always a single destructor.
Syntax: Syntax:
classname() destructor name is
{… preceded with tilde.
… ~classname()
} {….
….
}
There is a concept of copy While here, there is no
constructor which is used to copy destructor
initialize an object from another concept.
object.
6.(a) Develop a C++ program to create structure book with data members name, cost and author. Accept
and display data for 5 books using structure
#include<iostream> using namespace std;
struct book
{
Examination Paper Analysis

char name[30]; int cost;


char author[30];
}b[5];
int main()
{
int i;

for(i=0;i<5;i++)
{
cout<<"\n Enter the Book "<<i+1<<" Name:"; cin>>b[i].name;

cout<<"\n Enter the Book "<<i+1<<" Cost:"; cin>>b[i].cost;

cout<<"\n Enter the Author Name for Book "<<i+1<<":"; cin>>b[i].author;


}
cout<<"\n Book Information:"<<endl; for(i=0;i<5;i++)
{
cout<<"\n The Book "<<i+1<<" Name:"<<b[i].name; cout<<"\n The Book

"<<i+1<<" Cost:"<<b[i].cost;

cout<<"\n The Author Name for Book


"<<i+1<<":"<<b[i].author;

cout<<endl;
}
}
6.(b) Develop a C++ program using parameterized constructor
#include<iostream> using namespace std;
class number
{
int x;
public:
Examination Paper Analysis

number(int y)
{
x=y;
}
void display( )
{
cout<<"The sqaure of number is:"<<x*x;
}
};
int main( )
{
number n(50);
n.display( );
}
Summer Unit 3 Q4 a) Describe the concept of virtual base class with example. 4
2022 Extending (Explanation
classes Solution: 4M)
using
inheritance VIRTUAL BASE Class:
Examination Paper Analysis

The duplication of inherited members due to these multiple paths can be avoided by making the common
base class (ancestor class) as virtual base class while declaring the direct or intermediate base class.
Consider a situation where all three kinds of inheritance multiple, multilevel and hierarchical are involved
:
· Here in the above diagram the child has two direct base classes ‘ parent1’ and ‘parent2’ which themselves
have a common base class ‘grandparent’.
· The ‘child’ class inherits the traits of ‘grandparent’ via two separate paths. All the public and protected
members of ‘grandparent’ are inherited twice via ‘parent1’ and again via ‘parent2’.
· This means ‘child’ class would have duplicate sets of members inherited from ‘grandparent’ this
introduces ambiguity& should be avoided.
· To avoid duplication of inherited members due to the multiple paths we use virtual base classes.
Syntax:
class A { };
class B: virtual public A
{ // statement 1};
class C: public virtual A
{ // statement 2};
class D: public B,public C
{ };
Q5 b) Write a C++ program to implement following inheritance: Refer Fig. No. 1 6
(correct logic
6M)

Accept and display total of one object of result.


Solution:
class Science
Examination Paper Analysis

{ float Phy_marks, Chy_marks;


public: void getdata()
{ cout<<”Enter the marks of Physics and Chemistry”;
cin>> Phy_marks>>Chy_marks;
}
void putdata()
{ cout<<”Physics Marks”<< Phy_marks;
cout<<”Chemistry Marks”<< Chy_marks;
}
};
class Maths
{ float Algo_marks, Geo_marks;
public: void getdata()
{ cout<<”Enter the marks of Algo_marks, Geo_marks”;
cin>> Algo_marks>>Geo_marks;
}
void putdata()
{ cout<<”Marks in maths”<< Algo_marks<<Geo_marks;
}
};
class Result: public Science, public Maths
{ float Total;
void calculate()
{ Science::getdata();
Maths::getdata();
Total= Phy_marks+Chy_marks +Algo_marks+Geo_marks;
Science::putdata();
Maths::putdata();
}
};
void main()
{ Result r;
r.calculate();
getch();
Examination Paper Analysis

}
Q6 c) Write a program to implement inheritance as shown in figure No. 2. Assume suitable member 4
function
(correct logic
4M)

Accept and display data of one Teacher and one Officer.


Solution:
class Staff
{ int code;
public: void getdata()
{ cout<<”Enter Staff Code ”;
cin>>code;
}
void putdata()
{ cout << “Staff Code=”<<code;
}
};
class Teacher: public Staff
{ char subject[20];
public: void getdata()
{ Staff::getdata();
cout <<”Enter the Subject”<<endl;
cin >>subject;
}
void putdata()
{ Staff::putdata();
Examination Paper Analysis

cout<<”Subject=”<< subject;
}
};
class Officer: public Staff
{ char grade[20];
public: void getdata()
{ Staff::getdata();
cout <<”Enter the Grade”<<endl;
cin >>grade;
}
void putdata()
{ Staff::putdata();
cout<<”Grade=”<< grade;
}
};
void main()
{ Teacher t;
t.getdata();
t.putdata();
Officer o;
o.getdata();
o.putdata();
getch();
}
Examination Paper Analysis

Winter Unit 3 Q 2 b) Develop a c++ program for multilevel inheritance. 4


2022 Extending
classes (correct logic
using 4M)
inheritance

# include <iostream.h>
class student
{
private : int rollno;
char name[30];
public: void getdata(void)
{
cout <<”Enter rollno: “ << endl;
cin>>rollno;
cout << “Enter name : “<<endl
cin >> name;
}
void showdata(void)
{
cout <<”Rollno= “ <<rollno << endl;
cout << “Name = “<< name << endl;
}
};
class test : public student
{
protected :
Examination Paper Analysis

int marks1 , marks2; // merks1 and marks2 are protected members


public :
void getmarks ()
{
cout << “Enter marks of sub1 : “ ;
cin >> marks1;
cout << “Enter marks of sub2 : “ ;
cin >> marks2;
}
void showmarks (void)
{
cout << “Marks of sub1 = “ << marks1;
cout << “Marks of sub2 = “<< marks2;
}
};
class result : public test
{
private :
int total_marks;
public:
void display_result()
{
total_marks = marks1 + marks2;// marks1 and marks2 are accessible
// here b’cos they are declared as // protected in base class test
student :: showdata();
test :: showmarks () ;
cout << “Total Marks = “<< total_marks;
}
};
void main()
{
result r1;
r1.getdata(); // get rollno and name
r1.getmarks(); // get marks1 and marks2
Examination Paper Analysis

cout << “details of student: “ <<endl;


r1.display_result();
}
OUTPUT
Enter Rollno : 123
Enter Name : Deepali
Enter Marks of sub1: 45
Enter Marks of sub2 : 38
Details of student :
RollNo = 123
Name = Deepali
Marks of sub1 : 45
Marks of sub2: : 38
Total Marks : 83
Q 3 c) Describe all visibility modes and effects with example. 4
Access modifiers are also called as visibility mode: (Explanation
There are three types of visibility modes available in c++. 4M)
The members of the class can be,
1) private
2) protected
3) public
Private:
Private members can be accessed only by the member functions of a class. They are not accessible outside
the class.
Public:
Public members are accessible anywhere in the program.
Protected:
A member declared as ‘protected’ is accessed by member functions of its own class as well as member
functions of the immediately derived class.
class A
{
private : // Default visibility –mode
//
visible to member function of its own class
Examination Paper Analysis

protected :// visible to member functions of own class as well as member //functions of derived class
public : // visible to all functions in the program.
};
Visibility – Accessed from Accessed from Accessed from object of
mode member member fun of derived class
functions of own Derived class
class
Public YES YES YES
Protected YES YES NO
Private YES NO NO
NOTE :
• Member function of a class can access all type of data ( private , protected , public)
• Private members are only accessed by member function of a class.
• Object of a class can access only public member of a class. They cannot access private or protected
members of a class.
• Protected members are accessed only by member function of a class and member function of
immediately derived class.

4
(correct logic
4M)

# include <iostream.h>
Examination Paper Analysis

class employee // base class


{
private : int emp_id;

char emp_name [30];


long int mobile;
public: void getdata(void)
{ cout <<”Enter emp_id : “ <<endl;
cin >> emp_id;
cout <<”Enter emp_name : “<<endl;
cin >> emp_name ;
cout <<”Enter emp_mobile no : “<<endl;
cin >> mobile ;
}
void showdata(void)
{ cout <<”emp_id = “ <<emp_id <<endl;
cout <<”emp_name = “ << emp_name << endl;
cout <<”emp_mobile no = “ << mobile ;

}
};
class customer // base
class
{
private :
char name[40],add[100];
long int contact ;

public:
void getdata(void)
{
cout<<”\n Enter Customer Name”;
cin>>name;
cout<<”\n Enter Customer Address”;
Examination Paper Analysis

cin>>add;
cout<<”\n Enter Customer Mobile No”;
cin>>contact;
}
void showdata(void)
{
cout<<”\n Customer Name”<<name;
cout<<”\n Customer Address”<<add;
cout<<”\n Enter Customer Mobile No”<<contact;
}
};
class company : public employee , public customer // derived class
{
private :
char comp_name[40];
char location[50];
public: void getdata(void)
{ employee :: getdata();
customer :: getdata();
cout<<”\n Enter Company Name”;
cin>>comp_name;
cout<<”\n Enter Company Address”;
cin>>location;
}
void showdata(void)
{
employee :: showdata();
customer :: showdata();
cout<<”\n Company Name”<<comp_name;
cout<<”\n Company Address”<<location;
}

};
void main()
Examination Paper Analysis

{
company c1;
c1.getdata();
cout << “Details of Employee”<<endl;
c1.showdata();
}
Q 5 b) Develop a c++ program to implement virtual Base class.
Example: 6
#include <iostream> (correct logic
using namespace std; 6M)
class A {
public:
int a;
A(){
a = 10;
}

};
class B : public virtual A {
};
class C : public virtual A {
};
class D : public B, public C {
};
int main(){
//creating class D object
D object;
cout << "a = " << object.a << endl;
return 0;
}
Output
a=10
Q 6 c) Develop c++ program to implement inheritance shown in fig
6
Examination Paper Analysis

(correct logic
6M)

#include <iostream>
using namespace std;
class HOD
{
char name [30], dept [40];
public:
void getdata()
{
cout << "Enter name of HOD:";
cin >> name;
cout << "Enter department:";
cin>>dept;
}
void showdata()
{
cout << "Name:"<<name;
cout << "Department:"<< dept;
}
};
class faculty: public HOD
{
char name[30];
int salary;
public:
void showdata()
{
Examination Paper Analysis

HOD :: getdata();
cout << "Enter name: ";
cin>> name;
cout << " Enter Salary :";
cin>>salary;
}
void showdata()
{
HOD::showdata();
cout << " Name: "<< name << endl;
cout << "Salary:"<< salary <<endl;
}
};
class lab_incharge: public HOD
{
char name[30];
int no_of_labs;
public:
void getdata()
{
faculty::getdata();
cout<<"Enter name";
cin>>name;
cout<<"Enter the no of labs incharged:";
cin>> no_of_labs;
}
void showdata()
{
faculty :: showdata();
cout<<"Name:"<<name<<endl;
cout << "No of labs incharged:"<<no_of_labs<<endl;
}
};
class student:public HOD
Examination Paper Analysis

{
int roll_no;
char name[30];
public:
void getdata()
{
cout<<"Enter roll no:";
cin>> roll_no;
cout<<" Enter name:";
cin>>name;
}
void showdata()
{
cout<<"Enter Roll No.:"<< roll_no << endl;
cout<<" Name:"<< name<<endl;
}
};
class technical: public lab_incharge
{
int lab_id;
char name[30];
public:
void getdata()
{
lab_incharge::getdata();
cout<<"Enter lab_id:";
cin>> lab_id;
cout << " Enter name:";
cin>> name;
}
void showdata()
{
lab_incharge::showdata(); cout << "Lab id:"<<< lab_id; cout << " Name:"<< name;
}
Examination Paper Analysis

};
class non_technical:public lab_incharge
{
int lab_id;
char name[30];
public:
void getdata()
{
technical :: getdata();
cout << " Enter lab id:";
cin>>lab_id;
cout << " Enter Name:";
cin>>name;
}
void showdata()
{
technical::showdata();
cout<<" Lab id :"<<lab_id;
cout <<"Name:"<<name;
}
};
int main()
{
non_technical a1;
student s1;
s1.getdata();
s1.showdata();
a1.getdata();
a1.showdata();
return 0;
}
Summer Unit 3 1.(d) State different types of visibility mode in inheritance. 2M
2023 Ans.
Examination Paper Analysis

Extending 1. Private Any two


classes 2. Public types 1M
using 3. protected each
inheritance
1.(e) Give the syntax for constructor in derived classes. 2M Correct
Derived_constructor (arglist1, arglist2,…, arglistN,): syntax 2M
base1(arglist2), . . . ,baseN(arglistN)
{
Body of derived class constructor
}
2.(c) Write a program on single inheritance. 4M Correct
#include<iostream.h> logic 2M
#include<conio.h> Correct
class employee syntax 2M
{
protected:
int emp_id;
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()
Examination Paper Analysis

{
cout<<"\nEmp_id="<<emp_id;
cout<<"\nName="<<name;
cout<<"\nBasic Salary="<<basic_salary;
}
};
void main()
{
emp_info e;
clrscr();
e.getdata();
e.putdata();
getch();
}
5.(c) Write a program on hybrid inheritance. 6M Correct
#include <iostream> logic 3M
class A Correct
{ syntax 3M
public:
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
Examination Paper Analysis

{
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()
{
D obj1; //object of derived class D
obj1.sum();
return 0;
}
6.(b) Explain abstract class with suitable example 6M Correct
Ans explanation
By definition, a C++ abstract class must include at least one pure virtual function. 3M Correct
Alternatively, put a function without a definition. example 3M
Because the subclass would otherwise turn into an abstract class in and of itself, the abstract class's
descendants must specify the
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:
Examination Paper Analysis

#include<iostream>
class Figure
{
public:
virtual int Area() = 0;
void setBreadth(int br)
{
breadth = br;
}
void setHeight(int ht)
{
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()
Examination Paper Analysis

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

Winter Unit 3 2.(c) What is inheritance? Give different types of inheritance.


2024 Extending Inheritance:
classes
using The mechanism of deriving new class from an old/existing class is called inheritance.
inheritance
OR
Inheritance is the process by which objects of one class acquired the properties of objects of another
classes.
Syntax:
class derived-class-name: visibility-mode base-class-name
{

// members of derived class


//
};
The five types of inheritance in C++ are:

• Single inheritance: A derived class inherits properties from a single base class.
Examination Paper Analysis

• Multiple inheritance: A derived class inherits properties from multiple base classes.

• Multilevel inheritance: A derived class inherits properties from another derived class,
which in turn inherits properties from another derived class or the base class.
Examination Paper Analysis

• Hierarchical inheritance: Multiple derived classes inherit properties from a single base
class.

• Hybrid inheritance: A combination of multiple types of inheritance, such as mixing


multilevel inheritance with multiple inheritance.
Examination Paper Analysis

3.(b) Explain multilevel inheritance with an example


When a class is derived from another derived class then it is called as multilevel
inheritance.

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>
Examination Paper Analysis

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()
{
cout << "\nEnter value of z= ";
cin >> z;
}
void product()
{
cout << "\nProduct= " << x * y * z;
Examination Paper Analysis

}
};
int main()
{
derive2 a;
//object of derived class
a.getdata();
a.readdata();
a.indata();
a.product();
getch();
}
5.(a) Write a C++ program to implement multiple inheritance as shown in Figure No. 1. Accept and display
data of test marks and sport’s marks using object of class ‘result’

#include <iostream>
using namespace std;
// Base class for test marks class Test {
Examination Paper Analysis

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;
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:
Examination Paper Analysis

void TotalMarks() { total=Marks1+Marks2+sportMarks;


cout<<"Total:"<<total;
}
};
int main() {
Result r1;
r1.inputTestMarks();
r1.inputSportsMarks();
r1.displayTestMarks() ;
r1.displaySportMarks();
r1.TotalMarks();
return 0;
}

6.(b) Explain virtual base class with an example


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 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
Examination Paper Analysis
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
{
};

(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
{
Examination Paper Analysis

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;

}
};
class result: public test, public sports
{
int total;
public:
void display()
{
total=test1+test2+score;
putnumber();
putmarks();
putscore();
Examination Paper Analysis
cout<<"\n\t Total Score:"<<total;
}
};
int main()
{
result obj;
obj.getnumber();
obj.getmarks();
obj.getscore();
obj.display();
return 0;
}

Summer Unit 3
2024 Extending
classes
using
inheritanc

Q1.e) List different types of inheritance.


Types of inheritance:
1.Single inheritance
2. Multiple inheritance
3. Multilevel inheritance
4. Hierarchical inheritance
5. Hybrid inheritance

Q2.c) What is multilevel inheritance? Develop a C++ program for multilevel inheritance.
Multilevel Inheritance:
The inheritance in which a class can be derived from another derived class is known as Multilevel
Inheritance. Suppose there are three classes A, B, and C. A is the base class. B is the derived class of A.
and C is the class that is derived from class B
Examination Paper Analysis

Fig: Multilevel Inheritance


Example:
#include<iostream>
using namespace std;
class student
{
private : int rollno;
char name[30];
public: void getdata(void)
{
cout <<”Enter rollno: “ << endl;
cin>>rollno;
cout << “Enter name : “<<endl
cin >> name;
}
void showdata(void)
{
cout <<”Rollno= “ <<rollno << endl;
cout << “Name = “<< name << endl;
Examination Paper Analysis

}
};
class test : public student
{
protected :
int marks1 , marks2; // merks1 and marks2 are protected members
public :
void getmarks ()
{
cout << “Enter marks of sub1 : “ ;
cin >> marks1;
cout << “Enter marks of sub2 : “ ;
cin >> marks2;
}
void showmarks (void)
{
cout << “Marks of sub1 = “ << marks1;
cout << “Marks of sub2 = “<< marks2;
}
};
class result : public test
{
private : int total_marks;
public:void display_result()
{
total_marks = marks1 + marks2; // marks1 and marks2 are accessible
// here b’cos they are declared as
// protected in base class test
student :: showdata();
test :: showmarks () ;
cout << “Total Marks = “<< total_marks;
}
};
void main()
{
result r1;
r1.getdata(); // get rollno and name
Examination Paper Analysis

r1.getmarks(); // get marks1 and marks2


cout << “details of student: “ <<endl;
r1.display_result();
}
Q3.b) Describe the concept of virtual base class with suitable example.
Virtual Base Class:
An ancestor class is declared as virtual base class which is used to avoid duplication of inherited
members inside child class due to multiple path of inheritance.

fig.:Virtual Base Class (Multipath 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 inherits the
members of Grandparent via two separate paths. All the public and protected members of Grandparent
are inherited into Child twice, first via Parent1 and again via Parent2. This leads to duplicate sets of the
inherited members of Grandparent inside Child class. The duplication of inherited members can be
avoided by making the common base class as virtual base class while declaring the direct or intermediate
base classes as shown below.

class Grandparent
{
};
class Parent1: virtual public Grandparent
{
};
class Parent2: public virtual Grandparent
{
Examination Paper Analysis

};
class Child: public Parent1, public Parent2
{
};
Example:

#include<iostream> using namespace std;


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

{
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;
}
};
int main()
{
result obj;

obj.getnumber(); obj.getmarks();
obj.getscore(); obj.display();

}
Q3.c) Write a C++ program to declare a class college with name and code. Derive new class as student
with members as name. Accept and display details of one student along with college data.
Examination Paper Analysis

#include<iostream.h>
using namespace std;
class college
{
char name[10];
int code;
public:
void accept_college()
{
cout<<"Enter College Name:";
cin>>name;
cout<<"Enter Code:";
cin>>code;
}
void display_college()
{
cout<<endl<<"College Name:"<<name;
cout<<endl<<"College Code:"<<code;
}
};
class student:public college
{
char sname[10];
public:
void accept_student()
{
cout<<"Enter student Name:";
cin>>sname;
}
void display_student()
Examination Paper Analysis

{
cout<<endl<<"Student Name:"<<sname;
}
};
int main()
{
student s;
s.accept_college();
s.accept_student();
s.display_college();
s.display_student();
}
Q4.a) Describe all visibility modes and effects with example.

Fig.: Visibility of inherited members

Visibility modes, also known as access specifiers, are important concepts in object- oriented
programming language. They control how other parts of the code can access the properties (data
members) and functionalities (member functions) of a class. This is crucial for data hiding and creating
well-organized code.

Visibility modes and their effects:


Examination Paper Analysis

Public: When a base class is inherited with public visibility mode, the protected members of the base
class will be inherited as protected members of the derived class and the public members of the base
class will be inherited as public members of the derived class.

Private: When a base class is inherited with private visibility mode the public and protected
members of the base class become ‘Private’ members of the derived class.

Protected: When a base class is inherited with protected visibility mode the protected and public
members of the base class become ‘protected members’ of the derived class.
Example:
class A
{
public: int x; protected: int y; private:
int z;
};
class B: public A
{
// x is public
// y is protected
// z is not accessible from B
};
class C: protected A
{
// x is protected
// y is protected
// z is not accessible from C
};
class D : private A
{
// x is private
// y is private
// z is not accessible from D
};
Examination Paper Analysis

Note: Any relevant example should be considered

Q.5 a) Write a C++ program to implement inheritance as shown in fig:

#include <iostream> using namespace std;


// Base class Teacher class Teacher {
public:
string Name; int emp_id;

void setTeacherDetails()
{
cout<<"Enter Employee Name:"; cin>>Name;

cout<<"Enter Employee ID:"; cin>>emp_id;


}

void displayTeacherDetails()
{
cout << "Employee Name: " << Name << endl; cout << "Employee ID:
" << emp_id << endl;
}
};
// Base class Student class Student {
public:
string S_Name; int Roll_No;
Examination Paper Analysis

void setStudentDetails()
{
cout<<"Enter Student Name:"; cin>>S_Name;

cout<<"Enter Roll Number ID:"; cin>>Roll_No;


}
void displayStudentDetails() {
cout << "Student Name: " << S_Name << endl; cout << "Roll Number:
" << Roll_No << endl;
}
};

// Derived class Info inheriting from both Teacher and Student class Info : public
Teacher, public Student {
public:
void displayInfo() { displayTeacherDetails();
displayStudentDetails();
}
};

int main() { Info info;


info.setTeacherDetails();
info.setStudentDetails(); cout<<endl;
info.displayInfo();
}

Summer Unit 4 Q1 g) State the need of virtual function in C++. 2


2022 Pointers (Explanation
and • Virtual function is that which does not really exist but nevertheless appears real to some part of the 4M)
polymorphi program.
sm in c++ • We use virtual functions when we have number of objects of different classes and we want to put them
together on a list and perform a particular operation using a single function call.
Examination Paper Analysis

• When we use a pointer to base class to refer to the derived class object, always base class function is
invoked, even if the pointer contains the address of derived class object. This is because; compiler
ignores the contents of pointer and chooses the member function that matches the type of a pointer.
• Using virtual function mechanism we can force the compiler to look at the contents of the pointer rather
than the type of the pointer at the time invoking the function.
• When the same function exist in both base and derived class, the base class function is made virtual
using keyword virtual.
• When a function in a base class is made virtual, C++ determines which function to invoke at run time
based on the type of object pointed by the base class pointer rather than the type of the pointer.
• Thus by making the base class pointer to point to different objects, we can execute different versions
of the virtual functions.
• Runtime polymorphism is achieved only when a virtual function is accessed through a pointer to the
base class.
Q3 d) Differentiate between compile time and run time polymorphism. 4 (correct
Solution: points 4M)
Early Binding Late Binding
Linking of function call and the code to be Linking of function call and the code to be
executed in response to that call is done at executed in response to that call is done at Run
compile time time
Which function to be executed is decided by Which function is to be executed is decided by
matching the type and number of arguments looking at the contents of the base class pointer
passed to the function at run time.
Operator overloading and function overloading Virtual functions is the example of late binding
are the examples of early binding
It requires less execution time because process Late binding requires some overhead at run time
of binding is done at compile time i.e. prior to but provides increased power and flexibility
the execution of program.
Also known as compile time polymorphism or Also known as rum time polymorphism or
static binding Dynamic binding
This does not require use of pointers to objects This requires use of pointers to object
Function calls are faster Function calls execution are slower
Examination Paper Analysis

Q4 b) Write a C++ program to overload add function to add two integer numbers and two float numbers. 4(correct
Solution: logic 4
#include<iostream> marks)
int add(int,int);
double add(double, double);
int add(int a,int b)
{
return (a+b);
}
double add(double x, double y)
{
return (x+y);
}
int main()
{
std::cout<<"Addition of Two Integer numbers"<<add(5,8);
std::cout<<"Addition of Two double numbers"<<add(10.00,8.42);
return 0; }

Q5 a) i) Define pointer operator and address operator with example. 2 (Definition


Solution: 1M and
There are two special pointer operators, Example
1M)
1. & is called address of operator :
This returns the address of(its operand.) a variable. Where address is the internal computer’s memory
location of variable.
cout << &a ; //prints address of variable a

2) * operator which is called “ value at address “ operator.


The “*” is the indirection operator and it is the complement of &. It is the unary operator that returns
the value of the variable located at the address specified by its operand.
Q5 b) ii) Write a C++ program to declare a class train with members as train no and name. Accept and 4(correct
display data for one object of train. Use pointer to object to call functions of class. logic 4
Solution: marks)
Examination Paper Analysis

#include<iostream.h>
#include<conio.h>
class Train
{ int train_no;
char name[20];
void getdata()
{ cout<<”Enter train_no and train name”;
cin>>train_no>>name;
}
void putdata()
{cout<<”Train No=”<<train_no;
cout<<”Train Name=”<<name;
}
};
void main()
{ Train t ,*ptr;
ptr=&t;
ptr->getdata();
ptr->putdata();
getch();
}

Q 5 c) Explain rules of operator overloading and overload ‘+’ operator to concatenate two string. 6
#include<iostream> (correct logic
#include<conio.h> 6M)
using namespace std;
class String
{
char str[20]; //member variable for string input
public:
void input() //member function
{
cout<<"Enter your string: ";
cin.getline(str,20);
Examination Paper Analysis

}
void display() //member function for output
{
cout<<"String: "<<str;
}
String operator+(String s) //overloading
{
String obj;
strcat(str,s.str);
strcpy(obj.str,str);
return obj;
}
};
void main()
{
String str1,str2,str3; //creating three object
str1.input();
str2.input();
str3=str1+str2;
str3.display(); //displaying
getch();
}
Q 1 e) Explain concept of pointer with example. 2
A pointer is a variable that stores the address of another variable. Unlike other variables that hold values (Definition
of a certain type, pointer holds the address of a variable. For example, an integer variable holds (or you 1M and
can say stores) an integer value, however an integer pointer holds the address of a integer variable. Example
#include <stdio.h> 1M)
int main()
{
int num = 10;
printf("Value of variable num is: %d", num);
/* To print the address of a variable we use %p
* format specifier and ampersand (&) sign just
* before the variable name like &num.
Examination Paper Analysis

*/
printf("\nAddress of variable num is: %p", &num);
return 0;
}
Output:
Value of variable num is: 10
Address of variable num is: 0x7fff5694dc58
Q 4 c) Develop a c++ program to perform arithmetic operation using pointer. 4
POINTER ARITHMETIC: (correct
The following operation can be performed on a pointer. points 4M)
1) Addition of a number to a pointer.
int a =7;
int * p = & a;
p = p+2;
cout<<p;
2) Subtraction of a number from a pointer
int a =7;
int * p = & a;
p = p - 2;
cout<<p;
3) Subtraction of two pointers.
int a [4] = { 1,2,3,4};
int * p = & a [0]; // p points to the first element of array
int *q = & a [3]; // q points to the last element of array
cout << q –p;

Do not attempt the following operations on pointer.


1) Addition of two pointers.
2) Multiplying a pointer with a number.
3) Dividing a pointer by a number.
Q 5 c) Explain rules of operator overloading and overload ‘+’ operator to concatenate two string. 6
#include<iostream> (correct logic
#include<conio.h> 6M)
using namespace std;
Examination Paper Analysis

class String
{
char str[20]; //member variable for string input
public:
void input() //member function
{
cout<<"Enter your string: ";
cin.getline(str,20);
}
void display() //member function for output
{
cout<<"String: "<<str;
}
String operator+(String s) //overloading
{
String obj;
strcat(str,s.str);
strcpy(obj.str,str);
return obj;
}
};
void main()
{
String str1,str2,str3; //creating three object
str1.input();
str2.input();
str3=str1+str2;
str3.display(); //displaying
getch();
}
Q 5 a) Define class book with following Data member and member function for 10 book. 6(correct
Data Member Member Function logic 6M)
1. B - name → getdata ( )
2. B - author → put data ( )
Examination Paper Analysis

3. B - price
#include<iostream.h>
#include<conio.h>
class book
{
char name[40],author[30];
float p;
public:
void getdata()
{

cout<<”\n Enter Book Name”;


cin>>name;
cout<<”\n Enter Author of the book”;
cin>>author;
cout<<”\n Enter Book price”;
cin>>p;

}
void putdata()
{
cout<<”\n Book Name”<<name;
cout<<”\n Author of the book”<<author;
cout<<”\n Book price”<<p;
}
};
void main()
{

Book b[10],*b1;
B1=&b[0];
Int I;
for(i=0;i<10;i++)
{
Examination Paper Analysis

B1->getdata();
B1++;
}
for(i=0;i<10;i++)
{
B1->putdata();
B1++;
}
Q 6 b) Describe Function overloading with suitable program 6
Function overloading is a feature of c++ that allows a function to behave in more than one way (Explanation
depending upon the number and / or type of arguments passed to it. 3M and
There can be more than one function -definitions with the same name but with the different number of Example
arguments or different data types in a program. 3M)
The compiler determines which function code is be executed in response to the function call, just by
matching the number and /or type of arguments passed to it. Since the compiler knows this information
at compile time, it is called as compile time polymorphism or early binding.
Thus, more than one the functions with the same name but with the different no. of arguments or
different data types can exist in program.Thus, more than one the functions with the same name but
with the different no. of arguments or different data types can exist in program.
Example: consider the following function prototypes.
1) int ad (int ,int); // function prototype to add two integers
2) double add ( double ,double); // function prototype to add two doubles
3) int add (int ,int ,int); // function prototype to add three integers
Therefore when a function is called as add ( 10,20) ; the first definition will be called.
For function call add(12.3 ,56.8); the second definition will be called.
And for function call add ( 5,6,7); the third definition will be called.
NOTE : when functions are overloaded , compiler does not check for return data type. It only matches
the no. and the type of arguments passed to it.
Thus following function definitions will lead to an error.
int f (int );
void f (int );
Ex:
#include<iostream.h>
int volume(int);
Examination Paper Analysis

double volume(double, int);


int volume(int s)
{
return (s*s*s);
}
double volume(double r, int h)
{
return (3.14*r*r*h);
}
void main()
{
cout<<”volume of cube is”<<volume(5);
cout<<”volume of cylinder is”<<volume(10.5,8);
getch();
}

Q 3 d) Differentiate between compile time polymorphism and Runtime polymorphism 4


Early Binding Late Binding (Any 4
Linking of function call and the code Linking of function call and the code to be executed in points 4M)
to be executed in response to that response to that call is done at Run time
call is done at compile time
Which function to be executed is Which function is to be executed is decided by looking at
decided by matching the type and the contents of the base class pointer at run time.
number of arguments passed to the
function
Operator overloading and function Virtual functions is the example of late binding
overloading are the examples of
early binding
It requires less execution time Late binding requires some overhead at run time but
because process of binding is done provides increased power and flexibility
at compile time i.e. prior to the
execution of program.
Examination Paper Analysis

Also known as compile time Also known as rum time polymorphism or Dynamic
polymorphism or static binding binding
This does not require use of pointers This requires use of pointers to object
to objects
Function calls are faster Function calls execution are slower
Summer Unit 4 1.(f) Define polymorphism with its types. 2M
2023 Pointers Polymorphism is the ability to take more than one form. (Definition
and Types: 1M and
polymorphi 1. Compile time polymorphism Types 1M)
sm in c++ 2. Run time polymorphism

2.(d) Illustrate this pointer with example. 4M


Ans.
• C++ uses a unique keyword called ‘this’ to represent an object that invokes a member function. Explanation
• ‘this’ unique pointer is automatically passed to a member function when it is invoked. 2M Example
• ‘this’ is a pointer that always point to the object for which the member function is called. 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;
public:
void setdata(int x)
{
this ->a=x;
}
void putdata()
{
cout<<this ->a;
}
};
Examination Paper Analysis

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.

3.(c) State any four rules for virtual functions. 4M


Ans.
Any four
correct rules
1M each

4.(d) State the advantages of pointer. 4M


Examination Paper Analysis

Any four
correct
advantages
1M each

6.(c) Write C++ program to overload binary operator ‘+’ to concatenate two strings. 6M
#include<iostream> Correct logic
#include<string.h> 3M
class String Correct
{ syntax 3M
public:
char str[20];
public:
void accept_string()
{
Examination Paper Analysis

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

Winter Unit 4 1.(g) Explain reference and dereference operators w.r.t. pointer 2M
2024 Pointers Solution: Reference
and Following two operators are used while working with pointers: operator-1 M
polymorphi Dereference
1. Reference operator (&)
sm in c++ eoperator-
2. Dereference operator (*) 1M
Reference operator (&):
It is also known as the "address-of" operator. Used to access the memory address of a variable.
Examination Paper Analysis

Syntax: & variable name;


Dereference operator (*):
It is also known as the "indirection" operator. Use to retrieve the value stored at the memory address
pointed to by a pointer.
Syntax: datatype * pointer_variableName;
Example:
int x = 7;
int *ptr;
ptr=&x;
Any 4
correct
4.(b) Differentiate between compile time polymorphism and run time polymorphism. (Any four points)
pointes – 1M
each
Solution:

Compile time polymorphism Run time polymorphism.


1. In Compile time, Linking of function call to In Runtime, Linking of function call to
function definition happens at compile time function definition happens at run time

2. Complier knows which function to be executed Complier knows which function to be


for a respective function call before run time executed for a respective function call at run
time
3. It is also known as Static or early binding It is also known as Dynamic or Late binding

4. It is achieved through Function and operator It is achieved through virtual function


overloading
5. It takes less time for execution. It takes less time for execution.

Correct
5.(b) Describe ‘this’ pointer in C++ with an example. Explanation
– 4M
Examination Paper Analysis

Solution: Example-
2M

6.(c) Write a program to declare a class ‘item’ containing data members as ‘item_name’, ‘code’, ‘price’.
Accept and display the information for one object using pointer to that object.
Correct
Solution: Code- 6M

#include<iostream>

using namespace std;

class item

public:

char item_name[20];

int code,price;

public:

// accept function to get the information about the item

void accept()

cout<<"Enter the item name :"<<endl;

cin>>item_name;

cout<<"Enter the item code :"<<endl;


Examination Paper Analysis

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;

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
Examination Paper Analysis

i1->accept();

i1->display();

Summer Unit 4 1. (c) What are the rules for virtual function (write any two)
2024 Pointers 1. The virtual function must be a member of some class. The base class function is made virtual.
and 2. A virtual function must be defined in a base class, even though it may not be used.
polymorphi 3. The prototype of the base class version of a virtual function and all the derived class versions must be identical.
sm in c++ If two functions with same name but with different data types exist then C++. Considers it as overloaded
functions and the virtual function mechanism is ignored.
4. A base class pointer can point to any derived class object but the reverse is not true.
5. If a virtual function is defined in the base class it need not be necessarily redefined in the derived class. In such
cases the base class version is invoked.
6. They cannot be static members.
7. When a class containing a virtual function is inherited the derived class redefines the virtual function to fit its own needs.
8. Virtual function implement “one interface multiple methods”
9. It is capable of supporting run-time.
10. When a base pointer to derived object that contains a virtual function, c++ determines which version of that function to call
based upon the type of object pointed by the pointer and this is determined at run time.
11. Thus, when different objects are pointed to, different versions of functions are executed. Same effect applies to base class
reference.

2.(d) Explain virtual function with example. Give the rules for virtual functions.
• Virtual function is that which does not really exist but nevertheless appears real to some part of the program.
• We use virtual functions when we have number of objects of different classes and we want to put them together
on a list and perform a particular operation using a single function call.
• When we use a pointer to base class to refer to the derived class object, always base class function is invoked,
even if the pointer contains the address of derived class object. This is because; compiler ignores the contents
of pointer and chooses the member function that matches the type of a pointer.
• Using virtual function mechanism we can force the compiler to look at the contents of the pointer rather than
the type of the pointer at the time invoking the function.
Examination Paper Analysis

• When the same function exist in both base and derived class, the base class function is made virtual using
keyword virtual.
• When a function in a base class is made virtual, C++ determines which function to invoke at run time based on
the type of object pointed by the base class pointer rather than the type of the pointer.
• Thus by making the base class pointer to point to different objects, we can execute different versions of the
virtual functions.
• Runtime polymorphism is achieved only when a virtual function is accessed through a pointer to the base class.

Example:
#include<iostream>
using namespace std;
class base
{
public:
virtual void print()
{
cout << "print base class\n";
}
void show()
{
cout << "show base class\n";
}
};
class derived : public base
{
public:
void print()
{
cout << "print derived class\n";
Examination Paper Analysis

}
void show()
{
cout << "show derived class\n";
}
};
int main()
{
base* bptr;
derived d;
bptr = &d;
bptr->print();
bptr->show();

1. The virtual function must be a member of some class. The base class function is made virtual.
2. A virtual function must be defined in a base class, even though it may not be used.
3. The prototype of the base class version of a virtual function and all the derived class versions must be identical.
If two functions with same name but with different data types exist then C++. Considers it as overloaded
functions and the virtual function mechanism is ignored.
4. A base class pointer can point to any derived class object but the reverse is not true.
5. If a virtual function is defined in the base class it need not be necessarily redefined in the derived class. In such
cases the base class version is invoked.
6. They cannot be static members.
7. When a class containing a virtual function is inherited the derived class redefines the virtual function to fit its own needs.
8. Virtual function implement “one interface multiple methods”
9. It is capable of supporting run-time.
10. When a base pointer to derived object that contains a virtual function, c++ determines which version of that function to call
based upon the type of object pointed by the pointer and this is determined at run time.
Examination Paper Analysis
11. Thus, when different objects are pointed to, different versions of functions are executed. Same effect applies to base class
reference.

4. (c) Describe function overloading and function overriding with suitable example.

Function Overloading:

• Definition: Function overloading allows to define multiple functions with the same name within
the same scope but with different parameter lists. The compiler distinguishes between them based
on the number, type, or order of the parameters.
• It improves code readability and reusability.
• We can have functions with the same name that performs different operations based on
the data they receive.

Example:
#include <iostream> using namespace std;
// overloaded functions
void test(int);
void test(float);
void test(int, float);
int main()
{
int a = 5; float b = 5.5;

// Overloaded functions with different type and number of parameters test(a);


test(b);
test(a, b);

return 0;
}
Examination Paper Analysis

// Method 1 void test(int var)


{
cout << "Integer number: " << var << endl;
}

// Method 2
void test(float var)
{
cout << "Float number: "<< var << endl;
}

// Method 3
void test(int var1, float var2)
{
cout << "Integer number: " << var1; cout << " and float
number:" << var2;
}

Function Overriding:

• Definition: Function overriding occurs in inheritance. When a derived class inherits from a
base class and redefines a member function of the base class with the same name and same
parameter (return type and parameter list), it's called overriding.
• The derived class's function implementation overrides the base class's function for the object
of the derived class.

Example:
#include<iostream> using namespace std;
class BaseClass
Examination Paper Analysis

{
public:
virtual void Display()
{
cout << "\nThis is Display() method of BaseClass";
}
void Show()
{
cout << "\nThis is Show() method of BaseClass";
}
};
class DerivedClass : public BaseClass
{
public:
// Overriding method - new working of
// base class display method
void Display()
{
cout << "\nThis is Display() method of DerivedClass";
}
};
// Driver code
int main()
{
DerivedClass dr;
BaseClass &bs = dr;
bs.Display();
dr.Show();
}
Examination Paper Analysis

5.(b) Develop a program to declare class book containing data members as title
,authorname ,publication, price accept and display the information for one object using pointer to that
objects.
#include<iostream> using namespace std;
class book
{
char title[30];
char authorname[30]; char publication[30];
int price;

public:
void accept()
{
cout<<"\n Enter the title of Book:"; cin>>title;
cout<<"\n Enter the Book Author Name:"; cin>>authorname;
cout<<"\n Enter the Publication details:"; cin>>publication;
cout<<"\n Enter the Price of Book:"; cin>>price;
}
void display()
{
cout<<"\n Title of the Book:"<<title;
cout<<"\n The Name of Author is:"<<authorname; cout<<"\n The Publication
company is:"<<publication; cout<<"\n The Price of the Book is:"<<price;
}
};
int main()
Examination Paper Analysis

{
book b,*p; p=&b;

p->accept();
p->display(); return 0;
}

6.(c) Write a C++ program to overload “+” operator so that it will perform concatenation of two strings.
(Use class get data function to accept two strings.)

#include<iostream>
#include<string.h>

using namespace std;


class String
{
char str[20]; //member variable for string input
public:
void input( ) //member function
{
cout<<"Enter String: ";
cin>>str;
}
void display( ) //member function for output
{
cout<<"Concatenation of two Strings: "<<str;
}
Examination Paper Analysis

String operator+(String s) //operator overloading


{
String obj;
strcat(str,s.str);
strcpy(obj.str,str);
return obj;
}
};
int main( )
{
String str1,str2,str3; //creating three object
str1.input( );
str2.input( ); str3=str1+str2; str3.display( ); //displaying

OR

#include<iostream> #include<string.h> using namespace std;


class string_concat
{
char s1[30]; char s2[30];

public:
void getdata( )
{
cout<<"\n Enter the First String :"; cin>>s1;

cout<<"\n Enter the Second String :"; cin>>s2;


Examination Paper Analysis

}
void operator+( )
{
cout<<"\n The concatenation of two String
is:"<<strcat(s1,s2);
}
};
int main( )
{
string_concat s; s.getdata( );
+s;
}
Summer Unit 5 Q1 b) List c++ stream classes along with their function. (any two classes). 2
2022 File
operations Solution: ios class is topmost class in the stream classes hierarchy. It is the base class for istream, ostream, (Any 1 :one
and streambuf class. mark)

1. The ios class: The ios class is responsible for providing all input and output facilities to all other
stream classes.
2. The istream class: This class is responsible for handling input stream. It provides number of
function for handling chars, strings and objects such as get, getline, read etc.
3. The ostream class: This class is responsible for handling output stream. It provides number of
function for handling chars, strings and objects such as write, put etc..
4. The iostream: This class is responsible for handling both input and output stream as both istream
class and ostream class is inherited into it. It provides function of both istream class and ostream
class for handling chars, strings and objects such as get, getline, read, ignore, putback, put, write
etc..
5. istream_withassign class: This class is variant of istream that allows object assignment. The
predefined object cin is an object of this class and thus may be reassigned at run time to a different
istream object.
Examination Paper Analysis

6. ostream_withassign class: This class is variant of ostream that allows object assignment. The
predefined objects cout, cerr, clog are objects of this class and thus may be reassigned at run time
to a different ostream object.
Q1 e) Explain ios :: app and ios :: in flags. 2
• Solution: ios::app: This flag means that you want to open a file and append to it. (explanation
• ios::in: Include this flag if you want to read from a file. 2 marks)
Q3 b) Write a C++ program to copy the contents of a source file student 1.txt to a destination file student 4(correct
2.txt using file operations. logic 4
#include<iostream.h> marks)
#include<conio.h>
#include<fstream.h>
#include<stdlib.h>
void main()
{
clrscr();
ifstream fs;
ofstream ft;
char ch, fname1[20], fname2[20];
cout<<"Enter source file name with extension (student1.txt) : ";
gets(fname1);
fs.open(fname1);
if(!fs)
{
cout<<"Error in opening source file..!!";
getch();
exit(1);
}
cout<<"Enter target file name with extension (student2.txt) : ";
gets(fname2);
ft.open(fname2);
if(!ft)
{
cout<<"Error in opening target file..!!";
Examination Paper Analysis

fs.close();
getch();
exit(2);
}
while(fs.eof()==0)
{
fs>>ch;
ft<<ch;
}
cout<<"File copied successfully..!!";
fs.close();
ft.close();
getch();
}
Winter Unit 5 Q 4 e) Develop c++ program to check Detection of end of file.
2022 File 4(correct
operations logic 4
marks)

Q 1 f) Explain file modes used to perform file operations. 2


In C++, for every file operation, exists a specific file mode. (Any 2)
Examination Paper Analysis

These file modes allow us to create, read, write, append or modify a file.
The file modes are defined in the class ios.
A File stream act as an interface between the program and the files.

Q 1 g) List all stream classes used in stream operation. 2


(list any 4 )
Examination Paper Analysis

Class Functions
ios • Contains basic facilities that are used by all other input and output classes.
• Also contains a pointer to a buffer object.
• Declares constants and functions that are necessary for handling formatted
input and output functions.
istream • Inherits the properties of ios.
• Contains overloaded extraction operator >>
ostream • Inherits the properties of ios.
• Contains overloaded insertion operator <<
Q 4 e) Develop c++ program to check Detection of end of file. 4(correct
#include <iostream> logic 4
#include <fstream> marks)
using namespace std;

class student
{
int rollno;
char name[20];
char branch[3];
float marks;
char grade;

public:
void getdata()
{
cout << "Roll Number: ";
cin >> rollno;
cout << "Name: ";
cin >> name;
cout << "Course: ";
cin >> branch;
cout << "Mark: ";
cin >> marks;
Examination Paper Analysis

if (marks >= 75)


grade = 'A';
else if (marks >= 60)
grade = 'B';
else if (marks >= 50)
grade = 'C';
else if (marks >= 40)
grade = 'D';
else
grade = 'F';
}

void putdata()
{
cout << name << ", roll number " << rollno << " received " << marks << "% marks and ";
cout << grade << " grade." << "\n";
}

int getrno()
{
return rollno;
}
} stud1;

int main()
{
ofstream fout("marks.dat", ios::out);

char ans = 'y';


while (ans == 'y' || ans == 'Y')
{
stud1.getdata();
fout.write((char *)&stud1, sizeof(stud1));
Examination Paper Analysis

cout << "A new record has been added to the file.\n";
cout << "\nWant to enter more? (y/n): ";
cin >> ans;
}
fout.close();

int rno;
char found;
ifstream fin("marks.dat", ios::in);
if (!fin)
{
cout << "Error opening the file!\n";
return 0;
}

ans = 'y';
while (ans == 'y' || ans == 'Y')
{
found = 'n';
cout << "Enter the roll number to be searched for: ";
cin >> rno;

while (!fin.eof()) // end-of-file used here


{
fin.read((char *)&stud1, sizeof(stud1));
if (stud1.getrno() == rno)
{
stud1.putdata();
found = 'y';
break;
}
}
if (found == 'n')
{
Examination Paper Analysis

cout << "\nThe entered roll number does not exist in the file.\n";
return 0;
}
cout << "\nWant to search more? (y/n):";
cin >> ans;
}
fin.close();

return 0;
}

Summer Unit 5 1.(g) Define file with it’s operations.A file is a collection of related data stored in a particular area on the 2M
2023 File disk. Definition
operations File Operations: 1M Any two
• Open file operations
• Close file 1M
• Read file
• Write file
• Update file
4.(e) Write a program for closing a file.
#include <iostream> 4(correct
#include <fstream> logic 4
int main() marks)
{
fstreamFileName;
FileName.open("FileName.txt", ios::in);
if (!FileName)
{
cout<<"File doesn’t exist";
}
else
{
cout<<"File opened successfully";
Examination Paper Analysis

}
FileName.close();
return 0;
}
Winter Unit 5 1.(d) Give the syntax and use of fclose( ) function.
2024 File
operations Syntax:
int fclose(FILE* stream);

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.
3.(d) Write a C++ program for write into a file using file operations.
#include<iostream.h>
#include<fstream.h>
#include <conio.h>
int main( )
{
fstream newfile;
newfile.open(“Test.txt”,ios::out);

if(!newfile)
{
cout<<”\nFile cannot be created”;
}
Examination Paper Analysis

else
{
cout<<”\nFile created successfully”; newfile<<” Writing this to a file.”; newfile.close( );
}
getch( ); return 0;
}

4.(a) Write a C++ program to copy data from one file to another

#include <iostream> #include <fstream>


using namespace std; int main()
{
string line;
ifstream srcfile("source.txt"); ofstream
destifile("destination.txt");
if(srcfile && destifile)
{
while(getline(srcfile,line))
{
destifile <<line << "\n";
}
cout << "Copy Finished \n";
}
else
{
cout<<"Cannot read File";
}
srcfile.close(); destifile.close(); return 0;
}
Examination Paper Analysis

3.(d) Write a program to count number of lines in a file.


#include<iostream> #include<fstream>
using namespace std; int main()
{
int count = 0; string line;
ifstream file("abc.txt"); while (getline(file,
line))
count++;
cout << "Numbers of lines in the file : " << count << endl;
return 0;
}
4.(d) Develop a C++ program to read content of file abc.txt
#include <iostream> #include <fstream>
using namespace std; int main() {
ifstream inputFile("abc.txt");

// Check if the file was opened successfully if


(inputFile.is_open()) {
string line;

// Read the file line by line while (getline(inputFile,


line)) {
// Print each line to the console cout << line << endl;
}
inputFile.close();
} else {
//cerr << "Error: Could not open file 'abc.txt'." << endl; cout <<
"Error:'abc.txt' File not found." << endl;
}

return 0;
}
Examination Paper Analysis

1.(g) Give the syntax of fclose ( ) method.


Syntax:
int fclose(FILE* stream);
1.(f) List C++ stream classes along with their function (any two classes)
C++ stream classes:
istream class: This class is responsible for handling input stream. It provides number of functions for
handling chars, strings and objects such as get, getline, read etc.
ios class: The ios class is responsible for providing all input and output facilities to all other stream classes.
ostream class: This class is responsible for handling output stream. It provides number of functions for
handling chars, strings and objects such as write, put etc.
iostream class: This class is responsible for handling both input and output stream as both istream class
and ostream class is inherited into it. It provides function of both istream class and ostream class for
handling chars, strings and objects such as get, getline, read, put, write etc.
streambuf class: This class provide an interface to physical devices through buffer. It acts as a base for
filebuf class used for ios files.

You might also like