OOP EPA
OOP EPA
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
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
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
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
,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
• 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
{
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)
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
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
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
>>, 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.
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.
Object
Examination Paper Analysis
management operators:
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:
}
Examination Paper Analysis
delete [ ] numbers;
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
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
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>
class counter
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
{
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
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
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
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
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
for(i=0;i<5;i++)
{
cout<<"\n Enter the Book "<<i+1<<" Name:"; cin>>b[i].name;
"<<i+1<<" Cost:"<<b[i].cost;
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)
}
Q6 c) Write a program to implement inheritance as shown in figure No. 2. Assume suitable member 4
function
(correct logic
4M)
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
# 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
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 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
{
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;
}
• 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.
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
}
};
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
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
{
};
#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
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
}
};
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
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:
{
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.
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.
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
void setTeacherDetails()
{
cout<<"Enter Employee Name:"; cin>>Name;
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;
// Derived class Info inheriting from both Teacher and Student class Info : public
Teacher, public Student {
public:
void displayInfo() { displayTeacherDetails();
displayStudentDetails();
}
};
• 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; }
#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;
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()
{
}
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
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
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.
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
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
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>
class item
public:
char item_name[20];
int code,price;
public:
void accept()
cin>>item_name;
cin>>code;
cin>>price;
cout<<"-------------------------------------------"<<endl;
}
void display()
cout<<"ITEM NAME:"<<item_name<<endl;
cout<<"CODE:"<<code<<endl;
cout<<"PRICE:"<<price<<endl;
};
int main()
item i, *i1;
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;
return 0;
}
Examination Paper Analysis
// 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>
OR
public:
void getdata( )
{
cout<<"\n Enter the First String :"; cin>>s1;
}
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)
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.
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
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);
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;
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
return 0;
}
Examination Paper Analysis