0% found this document useful (0 votes)
36 views50 pages

V2V - OOP - Super - 25 - Solutions - by - Akshay - Sir - (24-25)

The document provides a comprehensive overview of Object-Oriented Programming (OOP) concepts, including definitions, benefits, and applications. It covers key concepts such as classes, objects, inheritance, polymorphism, and exception handling, along with examples and C++ programming structures. Additionally, it lists keywords in C++ and includes a sample program demonstrating class functionality.

Uploaded by

tanayhingane03
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views50 pages

V2V - OOP - Super - 25 - Solutions - by - Akshay - Sir - (24-25)

The document provides a comprehensive overview of Object-Oriented Programming (OOP) concepts, including definitions, benefits, and applications. It covers key concepts such as classes, objects, inheritance, polymorphism, and exception handling, along with examples and C++ programming structures. Additionally, it lists keywords in C++ and includes a sample program demonstrating class functionality.

Uploaded by

tanayhingane03
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 50

V2V EdTech LLP | OSY (313304) | Super 25 Question Set

OOP – Super 25 – VIMP Question Set by Akshay Sir

Fy-Diploma (URJA) [LIVE] (Sem 2) only at 4999/- BUY NOW


Sy-Diploma (UMANG ) [LIVE] (Sem 3 + sem 4) : only at 4999/- BUY NOW
Ty-Diploma (YUKTI ) [LIVE] (Sem 3 + sem 4) : only at 4999/- BUY NOW
All Courses : CHECK NOW YOUTUBE : SUBSCRIBE NOW INSTA : FOLLOW NOW
Download V2V APP on Playstore for more FREE STUDY MATERIAL
Contact No : 9326050669 / 9326881428
V2V EdTech LLP | OSY (313304) | Super 25 Question Set

1. POP vs OOP
Ans.

Sr. OBJECT ORIENTED PROCEDURE ORIENTED


PROGRAMMING (OOP) PROGRAMMING (POP)

1 Focus is on data rather than Focus is on doing things (procedure)


procedure.

2 Programs are divided into multiple Large programs are divided into multiple
objects functions

3 Data is hidden and cannot be accessed Data move openly around the system from
by external functions. function to function

4 Objects communicate with each other Functions transform data from one form to
through function another by calling each other.

5 Employs bottom-up approach in Employs top-down approach in program


program design design.

6 Object oriented approach is used in Procedure oriented approach is used in C


C++ language language.

2. Write any four benefits / basic concepts of OOP.


Ans. Benefits of OOP: -
1. We can eliminate redundant code and extend the use of existing classes.
2. We can build programs from the standard working modules that communicate with one
another, rather than having to start writing the code from scratch. This leads to saving of
development time and higher productivity.
3. The principle of data hiding helps the programmer to build secure programs that cannot be
invaded by code in other parts of the program.
4. It is possible to have multiple instances of an object to co-exist without any interference.
5. It is possible to map objects in the problem domain to those in the program.
6. It is easy to partition the work in a project based on objects.
7. The data-centered design approach enables us to capture more details of a model in
implementable form.
8. Object-oriented systems can be easily upgraded from small to large systems.
9. Message passing techniques for communication between objects makes the interface
descriptions with external systems much simpler.
10. Software complexity can be easily managed.

Fy-Diploma (URJA) [LIVE] (Sem 2) only at 4999/- BUY NOW


Sy-Diploma (UMANG ) [LIVE] (Sem 3 + sem 4) : only at 4999/- BUY NOW
Ty-Diploma (YUKTI ) [LIVE] (Sem 3 + sem 4) : only at 4999/- BUY NOW
All Courses : CHECK NOW YOUTUBE : SUBSCRIBE NOW INSTA : FOLLOW NOW
Download V2V APP on Playstore for more FREE STUDY MATERIAL
Contact No : 9326050669 / 9326881428
V2V EdTech LLP | OSY (313304) | Super 25 Question Set

Basic Concepts of OOP: -


Objects
Objects are the basic unit of OOP. They are instances of class, which have data members
and use various member functions to perform tasks. You can imagine it to be similar to
variable of class like we have variable of structure.
Class
It is similar to structures in C language. Class can also be defined as user defined data type but it
also contains functions in it. So, class is basically a blueprint for object. It declares & defines
what data variables the object will have and what operations can be performed on the class's
object. For examples, Mango, Apple and orange members of class fruit.
Data Abstraction
Data Abstraction refers to showing only the essential features of the application and hiding the
details. In C++, classes provide methods to the outside world to access & use the data variables,
but the variables are hidden from direct access.
For example: if there is a class for circle we need to just define the radius of the object of this
class. We need not bother about anything else of that object.
Data Encapsulation
The data of an object is hidden from other objects. This is called encapsulation.
It can also be said data binding. Encapsulation is all about binding the data variables and
functions together in class. Encapsulation is achieved by putting data and functions associated
with it into a class. The advantage of data encapsulation is that the data is hidden from other
functions and hence they cannot access it or change it directly. Thus unexpected changes in the
data is not possible.
Inheritance
Inheritance is a way to reuse once written code again and again. The class which is inherited is
called base calls & the class which inherits is called derived class. So when, a derived class
inherits a base class, the derived class can use all the functions which are defined in base class,
hence making code reusable.
Polymorphism
Polymorphism makes the code more readable. It is a features, which lets is create functions
with same name but different arguments, which will perform differently. That is function with
same name, functioning in different
Dynamic Binding
As you know, polymorphism is a technique of calling functions when needed b the objects of
that class. This decision to call a function for that specific class is done t runtime. This is known
as ‘Dynamic Binding’ or ‘Late Binding’
Overloading
Overloading is a part of polymorphism. Where a function or operator is made & defined many
times, to perform different functions they are said to be overloaded.

Fy-Diploma (URJA) [LIVE] (Sem 2) only at 4999/- BUY NOW


Sy-Diploma (UMANG ) [LIVE] (Sem 3 + sem 4) : only at 4999/- BUY NOW
Ty-Diploma (YUKTI ) [LIVE] (Sem 3 + sem 4) : only at 4999/- BUY NOW
All Courses : CHECK NOW YOUTUBE : SUBSCRIBE NOW INSTA : FOLLOW NOW
Download V2V APP on Playstore for more FREE STUDY MATERIAL
Contact No : 9326050669 / 9326881428
V2V EdTech LLP | OSY (313304) | Super 25 Question Set

Exception Handling
Exception handling is a feature of OOP, to handle unresolved exceptions or errors produced at
runtime.

3. Write the applications / features of object oriented programming.


Ans. Applications of OOP: -
1. Software Development: OOP is extensively used in software development to model real-
world entities, structure code, and facilitate code reuse. Object-oriented languages like C++,
Java, and Python are used to build a wide range of software applications, including desktop
applications, web applications, mobile apps, and more. OOP helps in organizing code into
classes and objects, making it more modular and maintainable.
2. Game Development: Game development often involves complex systems with multiple
interacting elements. OOP is well-suited for modeling game objects, characters, behaviors, and
interactions. Game engines like Unity (C#), Unreal Engine (C++), and Godot (GDScript) use
OOP principles to create games with rich and dynamic environments.
3. Simulation and Modeling: OOP is used in simulations and modeling to represent and
simulate real-world processes, systems, and phenomena. Whether it's simulating traffic flow,
financial markets, ecological systems, or physical simulations in scientific research, OOP
allows developers to create modular and easily extendable models.
4. Graphical User Interface (GUI) Development: Creating user interfaces for software
applications is a common use case for OOP. OOP allows developers to design and manage
GUI components as objects, making it easier to create interactive and user-friendly interfaces.
Frameworks like Qt (C++), JavaFX (Java), and Tkinter (Python) use OOP concepts for GUI
development.

Features of OOP: -
1. Emphasis is on data rather than procedure.
2. Programs are divided into what are known as objects.
3. Data structures are designed such that they characterize the objects.
4. Functions that operate on the data of an object are ties together in the data structure.
5. Data is hidden and cannot be accessed by external function.
6. Objects may communicate with each other through function.
7. New data and functions can be easily added whenever necessary.
8. Follows bottom up approach in program design. Object-oriented programming is the
most recent concept among programming paradigms and still means different things to
different people.

4. What are keywords? List any 5 Keywords used in C++?


Ans. Keywords are reserved words in a programming language that have predefined meanings and
cannot be used as identifiers. They are part of the language's syntax and are used to perform specific
tasks or define structures in the code.

1. int – Used to declare integer-type variables.

Fy-Diploma (URJA) [LIVE] (Sem 2) only at 4999/- BUY NOW


Sy-Diploma (UMANG ) [LIVE] (Sem 3 + sem 4) : only at 4999/- BUY NOW
Ty-Diploma (YUKTI ) [LIVE] (Sem 3 + sem 4) : only at 4999/- BUY NOW
All Courses : CHECK NOW YOUTUBE : SUBSCRIBE NOW INSTA : FOLLOW NOW
Download V2V APP on Playstore for more FREE STUDY MATERIAL
Contact No : 9326050669 / 9326881428
V2V EdTech LLP | OSY (313304) | Super 25 Question Set

2. float – Used to declare floating-point variables.


3. return – Specifies the return value of a function.
4. class – Used to define a class in C++.
5. if – Used to create conditional statements.

5. Structure of C++ Program?


Ans.
INCLUDE HEADER FILES

CLASS DECLARATION

MEMBER FUNCTIONS DEFINITIONS

MAIN FUNCTION PROGRAM


Description:-
1. Include header files
In this section a programmer include all header files which are require to execute given
program. The most important file is iostream.h header file. This file defines most of the
C++statements like cout and cin. Without this file one cannot load C++ program.
2. Class Declaration
In this section a programmer declares all classes which are necessary for given program. The
programmer uses general syntax of creating class.
3. Member Functions Definition
This section allows programmer to design member functions of a 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.

6. Type Casting with Suitable program example?


Ans. Type casting, also known as type conversion, is a fundamental concept in programming
that allows you to convert a value from one data type to another. This is essential when you
need to work with different data types in your code.
There are two types of type casting in C++:

1. Implicit Type Casting (Automatic Type Conversion):


- Implicit type casting occurs automatically by the compiler when you assign a value of one
data type to a variable of another data type.
- This type of casting is safe when there is no loss of data.
- For example, assigning an `int` to a `float` is an implicit type casting. The `int` is
automatically converted to a `float` without any data loss.

2. Explicit Type Casting (Manual Type Conversion):

Fy-Diploma (URJA) [LIVE] (Sem 2) only at 4999/- BUY NOW


Sy-Diploma (UMANG ) [LIVE] (Sem 3 + sem 4) : only at 4999/- BUY NOW
Ty-Diploma (YUKTI ) [LIVE] (Sem 3 + sem 4) : only at 4999/- BUY NOW
All Courses : CHECK NOW YOUTUBE : SUBSCRIBE NOW INSTA : FOLLOW NOW
Download V2V APP on Playstore for more FREE STUDY MATERIAL
Contact No : 9326050669 / 9326881428
V2V EdTech LLP | OSY (313304) | Super 25 Question Set

- Explicit type casting is performed manually by the programmer using casting operators.
- It is used when there may be a risk of data loss or when you want to enforce a specific type
conversion.
- C++ provides casting operators like `static_cast`, `dynamic_cast`, `const_cast`, and
`reinterpret_cast`.
- For example, using `static_cast` allows you to explicitly convert a `float` to an `int`, but
you must be aware that it may truncate the fractional part of the number.

Example:-
#include <iostream>
using namespace std;

int main() {
// Implicit type casting (int to float)
int num = 10;
float floatValue = num; // Implicitly converts int to float

cout << "Implicit type casting (int to float): " << floatValue << endl;

// Explicit type casting (float to int)


float anotherFloatValue = 15.75;
int intNum = static_cast<int>(anotherFloatValue); // Explicitly converts float to int

cout << "Explicit type casting (float to int): " << intNum << endl;

return 0;
}

Implicit type casting (int to float): 10


Explicit type casting (float to int): 15

Type casting is a way to ensure that data of one data type can be used in operations or
assignments involving another data type. Implicit type casting happens automatically when it's
safe, while explicit type casting is done manually when you need control or when data loss
may occur. It's important to use type casting carefully to avoid unexpected results and data loss
in your programs.

7. Define class and Object


Ans. Class: Class is a user defined data type that combines data and functions together. It is a
collection of objects of similar type.

Fy-Diploma (URJA) [LIVE] (Sem 2) only at 4999/- BUY NOW


Sy-Diploma (UMANG ) [LIVE] (Sem 3 + sem 4) : only at 4999/- BUY NOW
Ty-Diploma (YUKTI ) [LIVE] (Sem 3 + sem 4) : only at 4999/- BUY NOW
All Courses : CHECK NOW YOUTUBE : SUBSCRIBE NOW INSTA : FOLLOW NOW
Download V2V APP on Playstore for more FREE STUDY MATERIAL
Contact No : 9326050669 / 9326881428
V2V EdTech LLP | OSY (313304) | Super 25 Question Set

Object: It is a basic run time entity that represents a person, place or any item that the program
has to handle.

8. Simple class program like below given example:


(i) Write a C++ program to declare a class COLLEGE with members as college code. Derive
a new class as STUDENT with members as studid. Accept and display details of student along
with college for one object of student.
Ans. #include
#include
class COLLEGE
{
protected:
int collegecode;
};

class STUDENT:public COLLEGE


{
int studid;
public:
void accept()
{
cout<<"Enter college code:";
cin>>collegecode;
cout<<"Enter student id";
cin>>studid;
}
void display()
{
cout<<"College code:"<<collegecode<<endl;
cout<<"Student id:"<<studid;
}
};
void main()
{
STUDENT s;
clrscr();
s.accept();
s.display();
getch();
}

Fy-Diploma (URJA) [LIVE] (Sem 2) only at 4999/- BUY NOW


Sy-Diploma (UMANG ) [LIVE] (Sem 3 + sem 4) : only at 4999/- BUY NOW
Ty-Diploma (YUKTI ) [LIVE] (Sem 3 + sem 4) : only at 4999/- BUY NOW
All Courses : CHECK NOW YOUTUBE : SUBSCRIBE NOW INSTA : FOLLOW NOW
Download V2V APP on Playstore for more FREE STUDY MATERIAL
Contact No : 9326050669 / 9326881428
V2V EdTech LLP | OSY (313304) | Super 25 Question Set

Enter college code: 101


Enter student id: 12345
College code: 101
Student id: 12345

9. Member Function with Program Example (Internal, External, Inline & Friend Function)
Ans. Static Member Function in C++
Static Member Function in a class is the function that is declared as static because of which
function attains certain properties as defined below:
A static member function is independent of any object of the class.
A static member function can be called even if no objects of the class exist.
A static member function can also be accessed using the class name through the scope
resolution operator.
A static member function can access static data members and static member functions inside
or outside of the class.
// C++ Program to show the working of
// static member functions
#include <iostream>
using namespace std;

class Box
{
private:
static int length;
static int breadth;
static int height;

public:

static void print()


{
cout << "The value of the length is: " << length << endl;
cout << "The value of the breadth is: " << breadth << endl;
cout << "The value of the height is: " << height << endl;
}
};

// initialize the static data members

int Box :: length = 10;

Fy-Diploma (URJA) [LIVE] (Sem 2) only at 4999/- BUY NOW


Sy-Diploma (UMANG ) [LIVE] (Sem 3 + sem 4) : only at 4999/- BUY NOW
Ty-Diploma (YUKTI ) [LIVE] (Sem 3 + sem 4) : only at 4999/- BUY NOW
All Courses : CHECK NOW YOUTUBE : SUBSCRIBE NOW INSTA : FOLLOW NOW
Download V2V APP on Playstore for more FREE STUDY MATERIAL
Contact No : 9326050669 / 9326881428
V2V EdTech LLP | OSY (313304) | Super 25 Question Set

int Box :: breadth = 20;


int Box :: height = 30;

// Driver Code

int main()
{

Box b;

cout << "Static member function is called through Object name: \n" << endl;
b.print();

cout << "\nStatic member function is called through Class name: \n" << endl;
Box::print();

return 0;
}

Friend Function Programs:


a. Explain the friend function with proper example.
Ans. Friend function:
The private members of a class cannot be accessed from outside the class but in some situations
two classes may need access of each other‟s private data. So a common function can be
declared which can be made friend of more than one class to access the private data of more
than one class. 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.
Friend function is not in the scope of the class in which it is declared. It is called without any
object. The class members are accessed with the object name and dot membership operator
inside the friend function. It accepts objects as arguments.

Example:
Program to interchange values of two integer numbers using friend function.

#include <iostream.h>
#include <conio.h>
class B;
class A
{
int x;
public:
void accept()

Fy-Diploma (URJA) [LIVE] (Sem 2) only at 4999/- BUY NOW


Sy-Diploma (UMANG ) [LIVE] (Sem 3 + sem 4) : only at 4999/- BUY NOW
Ty-Diploma (YUKTI ) [LIVE] (Sem 3 + sem 4) : only at 4999/- BUY NOW
All Courses : CHECK NOW YOUTUBE : SUBSCRIBE NOW INSTA : FOLLOW NOW
Download V2V APP on Playstore for more FREE STUDY MATERIAL
Contact No : 9326050669 / 9326881428
V2V EdTech LLP | OSY (313304) | Super 25 Question Set

{
cout<<"\n Enter the value for x:";
cin>>x;
}
friend void swap(A,B);
};
class B
{
int y;
public:
void accept()
{
cout<<"\n Enter the value for y:";
cin>>y;
}
friend void swap(A,B);
};
void swap(A a,B b)
{
cout<<"\n Before swapping:";
cout<<"\n Value for x="<<a.x;
cout<<"\n Value for y="<<b.y;
int temp;
temp=a.x;
a.x=b.y;
b.y=temp;
cout<<"\n After swapping:";
cout<<"\n Value for x="<<a.x;
cout<<"\n Value for y="<<b.y;
}
void main()
{
A a;
B b;
clrscr();
a.accept();
b.accept();
swap(a,b);
getch();
}

Enter the value for x: 5


Enter the value for y: 10

Fy-Diploma (URJA) [LIVE] (Sem 2) only at 4999/- BUY NOW


Sy-Diploma (UMANG ) [LIVE] (Sem 3 + sem 4) : only at 4999/- BUY NOW
Ty-Diploma (YUKTI ) [LIVE] (Sem 3 + sem 4) : only at 4999/- BUY NOW
All Courses : CHECK NOW YOUTUBE : SUBSCRIBE NOW INSTA : FOLLOW NOW
Download V2V APP on Playstore for more FREE STUDY MATERIAL
Contact No : 9326050669 / 9326881428
V2V EdTech LLP | OSY (313304) | Super 25 Question Set

Before swapping:
Value for x=5
Value for y=10
After swapping:
Value for x=10
Value for y=5

b. Write a C++ program to find smallest number from two numbers using friend function.
(Hint: use two classes).
(Note: Any other correct logic shall be considered)
Ans. #include
#include
class class2;
class class1
{
int no1;
public:
void get1()
{
cout<<"Enter number 1:";
cin>>no1;
}
friend void smallest(class1 no1,class2 no2);
};
class class2
{
int no2;
public:
void get2()
{
cout<<"Enter number 2:";
cin>>no2;
}
friend void smallest(class1 no1,class2 no2);
};
void smallest(class1 c1,class2 c2)
{
if(c1.no1<c2.no2)
cout<<"no1 is smallest";
else
cout<<"no2 is smallest";
}

Fy-Diploma (URJA) [LIVE] (Sem 2) only at 4999/- BUY NOW


Sy-Diploma (UMANG ) [LIVE] (Sem 3 + sem 4) : only at 4999/- BUY NOW
Ty-Diploma (YUKTI ) [LIVE] (Sem 3 + sem 4) : only at 4999/- BUY NOW
All Courses : CHECK NOW YOUTUBE : SUBSCRIBE NOW INSTA : FOLLOW NOW
Download V2V APP on Playstore for more FREE STUDY MATERIAL
Contact No : 9326050669 / 9326881428
V2V EdTech LLP | OSY (313304) | Super 25 Question Set

void main()
{
class1 c1;
class2 c2;
clrscr();
c1.get1();
c2.get2();
smallest(c1,c2);
getch();
}

Enter number 1: 5
Enter number 2: 3
no2 is smallest

c. Write a C++ program to find greatest number among two numbers from two different classes
using friend function.
Ans. #include
#include
class second;
class first
{
int x;
public:
void getx()
{
cout<<"\nEnter the value of x:";
cin>>x;
}
friend void max(first,second);
};
class second
{
int y;
public:
void gety()
{
cout<<"\nEnter the value of y:";
cin>>y;
}
friend void max(first,second);

Fy-Diploma (URJA) [LIVE] (Sem 2) only at 4999/- BUY NOW


Sy-Diploma (UMANG ) [LIVE] (Sem 3 + sem 4) : only at 4999/- BUY NOW
Ty-Diploma (YUKTI ) [LIVE] (Sem 3 + sem 4) : only at 4999/- BUY NOW
All Courses : CHECK NOW YOUTUBE : SUBSCRIBE NOW INSTA : FOLLOW NOW
Download V2V APP on Playstore for more FREE STUDY MATERIAL
Contact No : 9326050669 / 9326881428
V2V EdTech LLP | OSY (313304) | Super 25 Question Set

};
void max(first a,second b)
{
if(a.x>b.y)
{
cout<<"\Greater value is:"<<a.x;
}
else
{
cout<<"\nGreater value is:"<<b.y;
}
}
void main()
{
first a;
second b;
clrscr();
a.getx();
b.gety();
max(a,b);
getch();
}

Enter the value of x: 10


Enter the value of y: 5
Greater value is: 10

d. Write a C++ program to declare two classes with data members as ml and m2 respectively.
Use friend function to calculate average of two (m1, m2) marks and display it.
(Hint: class 1 contains ml and class 2 contains m2)
Ans. #include <iostream>
using namespace std;

// Forward declaration of Class2 to make it a friend of Class1


class Class2;

// Class1 declaration
class Class1 {
private:
int m1;

public:

Fy-Diploma (URJA) [LIVE] (Sem 2) only at 4999/- BUY NOW


Sy-Diploma (UMANG ) [LIVE] (Sem 3 + sem 4) : only at 4999/- BUY NOW
Ty-Diploma (YUKTI ) [LIVE] (Sem 3 + sem 4) : only at 4999/- BUY NOW
All Courses : CHECK NOW YOUTUBE : SUBSCRIBE NOW INSTA : FOLLOW NOW
Download V2V APP on Playstore for more FREE STUDY MATERIAL
Contact No : 9326050669 / 9326881428
V2V EdTech LLP | OSY (313304) | Super 25 Question Set

Class1(int m) : m1(m) {}

// Declare Class2 as a friend class to access its data members


friend double calculateAverage(const Class1&, const Class2&);
};

// Class2 declaration
class Class2 {
private:
int m2;

public:
Class2(int m) : m2(m) {}

// Declare Class1 as a friend class to access its data members


friend double calculateAverage(const Class1&, const Class2&);
};

// Friend function to calculate the average of m1 and m2


double calculateAverage(const Class1& c1, const Class2& c2) {
return (static_cast<double>(c1.m1) + c2.m2) / 2.0;
}

int main() {
Class1 obj1(80); // Create an object of Class1 with m1 = 80
Class2 obj2(90); // Create an object of Class2 with m2 = 90

// Calculate and display the average using the friend function


double average = calculateAverage(obj1, obj2);
cout << "Average of m1 and m2: " << average << endl;

return 0;
}

Average of m1 and m2: 85

This program demonstrates the use of friend functions to access and calculate values involving
data members of two different classes.

e. Characteristics of Friend Function.

Fy-Diploma (URJA) [LIVE] (Sem 2) only at 4999/- BUY NOW


Sy-Diploma (UMANG ) [LIVE] (Sem 3 + sem 4) : only at 4999/- BUY NOW
Ty-Diploma (YUKTI ) [LIVE] (Sem 3 + sem 4) : only at 4999/- BUY NOW
All Courses : CHECK NOW YOUTUBE : SUBSCRIBE NOW INSTA : FOLLOW NOW
Download V2V APP on Playstore for more FREE STUDY MATERIAL
Contact No : 9326050669 / 9326881428
V2V EdTech LLP | OSY (313304) | Super 25 Question Set

Ans. 1. Access to Private Members: A friend function can access and modify the private and
protected members of the class it is a friend of. This provides a way to break the encapsulation
of a class temporarily, allowing external functions to work with the class's private data.

2. Declared Outside the Class: Friend functions are declared outside the class in which they
are defined as friends. They are not members of the class, but they are associated with it. This
means that they can be used to provide external functions that have privileged access to the
class without being part of it.

These characteristics make friend functions useful for scenarios where you need external
functions to interact with a class's private members while still maintaining data integrity and
access control. However, it's important to use friend functions judiciously, as they can
potentially violate the encapsulation principle if overused.

10. Question on Constructor and Destructor


a. Define Constructor and its Types ?
Ans. Concepts of Constructors
Constructor is a special function used to initialize class data members or
we can say constructor is used to initialize the object of class.

Types of Constructor
• Default Constructor
• Parameterize Constructor
• Copy Constructor

Default Constructor:
Construct without parameter is called default constructor.

Parameterize Constructor:
Constructor with parameter is called parameterize constructor. In parameterize constructor, we
have to pass values to the constructor through object.

Copy Constructor:
Initialization of an object through another object is called copy constructor. In other words,
copying the values of one object into another object is called copy constructor.

b. State any four characteristics of Constructor.


Ans. Characteristics of a constructor.
• Constructor name class name must be same.
• Constructor doesn't return value.
• Constructor is invoked automatically, when the object of class is Created.

c. Constructor vs Destructor.

Fy-Diploma (URJA) [LIVE] (Sem 2) only at 4999/- BUY NOW


Sy-Diploma (UMANG ) [LIVE] (Sem 3 + sem 4) : only at 4999/- BUY NOW
Ty-Diploma (YUKTI ) [LIVE] (Sem 3 + sem 4) : only at 4999/- BUY NOW
All Courses : CHECK NOW YOUTUBE : SUBSCRIBE NOW INSTA : FOLLOW NOW
Download V2V APP on Playstore for more FREE STUDY MATERIAL
Contact No : 9326050669 / 9326881428
V2V EdTech LLP | OSY (313304) | Super 25 Question Set

Ans.
Constructor Destructor

A constructor is a special member function A destructor is a special member function


whose task is to initialize the objects of its whose task is to destroy the objects that have
class. been created by constructor.

It constructs the values of data members of It does not construct the values for the data
the class. members of the class

It is invoked automatically when the objects It is invoked implicitly by the compiler upon
are created exit of a program/block/function.

Constructors are classified in various types Destructors are not classified in any types.
such as : Default constructor Parameterized
constructor Copy constructor Overloaded
constructor

A class can have more than one constructor. A class can have at most one constructor.

Constructor accepts parameter. Also it can Destructor never accepts any parameter.
have default value for its parameter

Syntax: Syntax:
classname() destructor name is preceded with tilde.
{ ~classname()
… {
… ….
} ….
}

Example: ABC() { … } Example: ~ABC() { … .}

d. Describe constructor with default arguments with an example.


Ans. #include <iostream>

class Rectangle {
private:
double length;
double width;

public:
// Constructor with default arguments

Fy-Diploma (URJA) [LIVE] (Sem 2) only at 4999/- BUY NOW


Sy-Diploma (UMANG ) [LIVE] (Sem 3 + sem 4) : only at 4999/- BUY NOW
Ty-Diploma (YUKTI ) [LIVE] (Sem 3 + sem 4) : only at 4999/- BUY NOW
All Courses : CHECK NOW YOUTUBE : SUBSCRIBE NOW INSTA : FOLLOW NOW
Download V2V APP on Playstore for more FREE STUDY MATERIAL
Contact No : 9326050669 / 9326881428
V2V EdTech LLP | OSY (313304) | Super 25 Question Set

Rectangle(double len = 1.0, double wid = 1.0) {


length = len;
width = wid;
}

double calculateArea() {
return length * width;
}
};

int main() {
// Creating objects of the Rectangle class with default arguments
Rectangle rect1; // Uses default values (length = 1.0, width = 1.0)
Rectangle rect2(5.0); // Sets length to 5.0, width remains default (1.0)
Rectangle rect3(3.0, 4.0); // Sets both length and width

std::cout << "Area of rect1: " << rect1.calculateArea() << std::endl;


std::cout << "Area of rect2: " << rect2.calculateArea() << std::endl;
std::cout << "Area of rect3: " << rect3.calculateArea() << std::endl;

return 0;
}

Area of rect1: 1
Area of rect2: 5
Area of rect3: 12

e. Write a C++ program to declare a class student with members as roll no, name and
department. Declare a parameterized constructor with default value for department as ‘CO’ to
initialize members of object. Initialize and display data for two students. (Note: Any other
relevant logic should be considered).
Ans. #include<iostream.h>
#include<conio.h>
#include<string.h>
class student
{
int roll_no;
char name[20],department[40];
public:

Fy-Diploma (URJA) [LIVE] (Sem 2) only at 4999/- BUY NOW


Sy-Diploma (UMANG ) [LIVE] (Sem 3 + sem 4) : only at 4999/- BUY NOW
Ty-Diploma (YUKTI ) [LIVE] (Sem 3 + sem 4) : only at 4999/- BUY NOW
All Courses : CHECK NOW YOUTUBE : SUBSCRIBE NOW INSTA : FOLLOW NOW
Download V2V APP on Playstore for more FREE STUDY MATERIAL
Contact No : 9326050669 / 9326881428
V2V EdTech LLP | OSY (313304) | Super 25 Question Set

student(int rno,char *n,char *d="CO")


{
roll_no=rno;
strcpy(name,n);
strcpy(department,d);
}
void display()
{
cout<<"\n Roll No:"<<roll_no;
cout<<"\n Name:"<<name;
cout<<"\n Department:"<<department;
}
};
void main()
{
student s1(112," Chitrakshi"),s2(114,"Anjali");
clrscr();
s1.display();
s2.display();
getch();
}

Roll No:112
Name: Chitrakshi
Department:CO

Roll No:114
Name:Anjali
Department:CO

f. Develop a c++ program for constructor with default argument and use of destructor.
Ans. #include <iostream>
using namespace std;

class MyClass
{
private:
int value;

public:
// Constructor with default argument
MyClass(int x = 0) {
value = x;

Fy-Diploma (URJA) [LIVE] (Sem 2) only at 4999/- BUY NOW


Sy-Diploma (UMANG ) [LIVE] (Sem 3 + sem 4) : only at 4999/- BUY NOW
Ty-Diploma (YUKTI ) [LIVE] (Sem 3 + sem 4) : only at 4999/- BUY NOW
All Courses : CHECK NOW YOUTUBE : SUBSCRIBE NOW INSTA : FOLLOW NOW
Download V2V APP on Playstore for more FREE STUDY MATERIAL
Contact No : 9326050669 / 9326881428
V2V EdTech LLP | OSY (313304) | Super 25 Question Set

cout << "Constructor called with value: " << value << endl;
}

// Destructor
~MyClass() {
cout << "Destructor called for value: " << value << endl;
}

void display() {
cout << "Value: " << value << endl;
}
};

int main() {
// Create objects with and without arguments
MyClass obj1; // Default constructor (value = 0)
MyClass obj2(42); // Constructor with argument (value = 42)

// Display values
obj1.display();
obj2.display();

// Destructor will be called automatically when objects go out of scope


return 0;
}

Constructor called with value: 0


Constructor called with value: 42
Value: 0
Value: 42
Destructor called for value: 0
Destructor called for value: 42

g. Write any two characteristics of destructor.


Ans. Characteristics:
1. It is used to destroy objects created by a constructor.
2. Name of destructor and name of the class is same.
3. Its name is preceded with tilde (~) symbol.
4. It never takes any argument.
5. It does not return any value.

Fy-Diploma (URJA) [LIVE] (Sem 2) only at 4999/- BUY NOW


Sy-Diploma (UMANG ) [LIVE] (Sem 3 + sem 4) : only at 4999/- BUY NOW
Ty-Diploma (YUKTI ) [LIVE] (Sem 3 + sem 4) : only at 4999/- BUY NOW
All Courses : CHECK NOW YOUTUBE : SUBSCRIBE NOW INSTA : FOLLOW NOW
Download V2V APP on Playstore for more FREE STUDY MATERIAL
Contact No : 9326050669 / 9326881428
V2V EdTech LLP | OSY (313304) | Super 25 Question Set

6. It is invoked implicitly by the compiler upon exit from the program (or block or function)
i.e when scope of object is over.

11. Array Programs:


Write a program to sort an 1-d array in ascending order.
Ans. #include<iostream.h>
#include<conio.h>
void main()
{
int arr[20];
int i, j, temp,n;
clrscr();
cout<<"\n Enter the array size:";
cin>>n;
cout<<"\n Enter array elements:";
for(i=0;i<n;i++)
{
cin>>arr[i];
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(arr[i]>arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
cout<<”Sorted Array:”;
for(i=0;i<n;i++)
{
cout<<”\n”<<arr[i];
}
getch();
}

Enter the array size: [You will enter the size of the array]
Enter array elements: [You will enter the elements of the array]
Sorted Array:
[The sorted array elements will be displayed here]

Fy-Diploma (URJA) [LIVE] (Sem 2) only at 4999/- BUY NOW


Sy-Diploma (UMANG ) [LIVE] (Sem 3 + sem 4) : only at 4999/- BUY NOW
Ty-Diploma (YUKTI ) [LIVE] (Sem 3 + sem 4) : only at 4999/- BUY NOW
All Courses : CHECK NOW YOUTUBE : SUBSCRIBE NOW INSTA : FOLLOW NOW
Download V2V APP on Playstore for more FREE STUDY MATERIAL
Contact No : 9326050669 / 9326881428
V2V EdTech LLP | OSY (313304) | Super 25 Question Set

(Note: Any other correct logic shall be considered)


Write a C++ program to display a sum of array elements of array size n.
Ans. #include
#include
void main()
{
int arr[20],i,n,sum=0;
clrscr();
cout<<"\nEnter size of an array:";
cin>>n;
cout<<"\nEnter the elements of an array:";
for(i=0;i<n;i++)
{
cin>>arr[i];
}
for(i=0;i<n;i++)
{
sum=sum+arr[i];
}
cout<<"\nArray elements are:";
for(i=0;i<n;i++)
{
cout<<arr[i]<<" ";
}
cout<<"\nSum of array elements is:"<<sum;
getch();
}

Enter size of an array: 5


Enter the elements of an array: 10 20 30 40 50
Array elements are: 10 20 30 40 50
Sum of array elements is: 150

(Note: Any other correct logic shall be considered)


Write a C++ program to accept array of five elements, find and display smallest number from
an array.
Ans. #include<iostream.h>
#include<conio.h>
void main()
{

Fy-Diploma (URJA) [LIVE] (Sem 2) only at 4999/- BUY NOW


Sy-Diploma (UMANG ) [LIVE] (Sem 3 + sem 4) : only at 4999/- BUY NOW
Ty-Diploma (YUKTI ) [LIVE] (Sem 3 + sem 4) : only at 4999/- BUY NOW
All Courses : CHECK NOW YOUTUBE : SUBSCRIBE NOW INSTA : FOLLOW NOW
Download V2V APP on Playstore for more FREE STUDY MATERIAL
Contact No : 9326050669 / 9326881428
V2V EdTech LLP | OSY (313304) | Super 25 Question Set

int a[5],smallest,i;
clrscr();
cout<<" Enter array elements:";
for(i=0;i<5;i++)
cin>>a[i];
smallest=a[0];
for(i=1;i<5;i++)
{
if(a[i]<smallest)
{
smallest=a[i];
}
}
cout<<endl<<"Smallest number="<<smallest;
getch();
}

Enter array elements: 15 8 24 3 12


Smallest number=3

Develop a c++ program to print sum of 10 no. using array.


Ans. #include <iostream>
using namespace std;

int main()
{
const int numCount = 10; // Number of elements in the array
int numbers[numCount]; // Declare an array to store the numbers
int sum = 0; // Initialize a variable to store the sum

// Input: Ask the user to enter 10 numbers


cout << "Enter 10 numbers, one at a time:" << endl;
for (int i = 0; i < numCount; ++i)
{
cout << "Enter number " << (i + 1) << ": ";
cin >> numbers[i];
}

// Calculate the sum of the numbers


for (int i = 0; i < numCount; ++i)
{

Fy-Diploma (URJA) [LIVE] (Sem 2) only at 4999/- BUY NOW


Sy-Diploma (UMANG ) [LIVE] (Sem 3 + sem 4) : only at 4999/- BUY NOW
Ty-Diploma (YUKTI ) [LIVE] (Sem 3 + sem 4) : only at 4999/- BUY NOW
All Courses : CHECK NOW YOUTUBE : SUBSCRIBE NOW INSTA : FOLLOW NOW
Download V2V APP on Playstore for more FREE STUDY MATERIAL
Contact No : 9326050669 / 9326881428
V2V EdTech LLP | OSY (313304) | Super 25 Question Set

sum += numbers[i];
}

// Output: Print the sum


cout << "Sum of the 10 numbers is: " << sum << endl;

Return
0;
}

Enter 10 numbers, one at a time:


Enter number 1: 5
Enter number 2: 12
Enter number 3: 7
Enter number 4: 23
Enter number 5: 1
Enter number 6: 9
Enter number 7: 8
Enter number 8: 15
Enter number 9: 3
Enter number 10: 10
Sum of the 10 numbers is: 93

12. Define Inheritance ? Explain its Types?


Ans. Inheritance:
The mechanism of deriving a new class from an old/existing class is called inheritance. OR
Inheritance is the process by which objects of one class acquired the properties of objects of
another class.
Syntax:
class derived_class_name : visibility_mode base_class_name
{
//
-----// members of derived class
//
};

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

Fy-Diploma (URJA) [LIVE] (Sem 2) only at 4999/- BUY NOW


Sy-Diploma (UMANG ) [LIVE] (Sem 3 + sem 4) : only at 4999/- BUY NOW
Ty-Diploma (YUKTI ) [LIVE] (Sem 3 + sem 4) : only at 4999/- BUY NOW
All Courses : CHECK NOW YOUTUBE : SUBSCRIBE NOW INSTA : FOLLOW NOW
Download V2V APP on Playstore for more FREE STUDY MATERIAL
Contact No : 9326050669 / 9326881428
V2V EdTech LLP | OSY (313304) | Super 25 Question Set

2) Multiple inheritance: In multiple inheritance, derived class is derived from more than
one base classes.
Diagram:

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


derived from a single class.
Diagram:

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


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

Fy-Diploma (URJA) [LIVE] (Sem 2) only at 4999/- BUY NOW


Sy-Diploma (UMANG ) [LIVE] (Sem 3 + sem 4) : only at 4999/- BUY NOW
Ty-Diploma (YUKTI ) [LIVE] (Sem 3 + sem 4) : only at 4999/- BUY NOW
All Courses : CHECK NOW YOUTUBE : SUBSCRIBE NOW INSTA : FOLLOW NOW
Download V2V APP on Playstore for more FREE STUDY MATERIAL
Contact No : 9326050669 / 9326881428
V2V EdTech LLP | OSY (313304) | Super 25 Question Set

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


and hierarchical inheritance.
Diagram:

13. Explain different visibility modes in Inheritance?


Ans. Different visibility modes are:
1. Private
2. Protected
3. Public

Effects of visibility modes in inheritance:

Fy-Diploma (URJA) [LIVE] (Sem 2) only at 4999/- BUY NOW


Sy-Diploma (UMANG ) [LIVE] (Sem 3 + sem 4) : only at 4999/- BUY NOW
Ty-Diploma (YUKTI ) [LIVE] (Sem 3 + sem 4) : only at 4999/- BUY NOW
All Courses : CHECK NOW YOUTUBE : SUBSCRIBE NOW INSTA : FOLLOW NOW
Download V2V APP on Playstore for more FREE STUDY MATERIAL
Contact No : 9326050669 / 9326881428
V2V EdTech LLP | OSY (313304) | Super 25 Question Set

Private members of base class are not inherited directly in any visibility mode.
1. Private visibility mode In this mode,
protected and public members of base class become private members of derived class.
2. Protected visibility mode In this mode,
protected and public members of base class become protected members of derived class.
3. Public visibility mode In this mode,
protected members of base class become protected members of derived class and public
members of base class become public members of derived class

14. Inheritance Program:


a. Write a program to implement single inheritance from the following Refer
Figure No.1

(Note: Any other correct logic shall be considered)


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

class Employee {
protected:
int emp_id;
string name;

Fy-Diploma (URJA) [LIVE] (Sem 2) only at 4999/- BUY NOW


Sy-Diploma (UMANG ) [LIVE] (Sem 3 + sem 4) : only at 4999/- BUY NOW
Ty-Diploma (YUKTI ) [LIVE] (Sem 3 + sem 4) : only at 4999/- BUY NOW
All Courses : CHECK NOW YOUTUBE : SUBSCRIBE NOW INSTA : FOLLOW NOW
Download V2V APP on Playstore for more FREE STUDY MATERIAL
Contact No : 9326050669 / 9326881428
V2V EdTech LLP | OSY (313304) | Super 25 Question Set

public:
void getEmployeeDetails() {
cout << "Enter Employee ID: ";
cin >> emp_id;
cin.ignore();
cout << "Enter Employee Name: ";
getline(cin, name);
}

void displayEmployeeDetails() const {


cout << "Employee ID: " << emp_id << endl;
cout << "Employee Name: " << name << endl;
}
};

class EmpInfo : public Employee {


private:
float basic_salary;

public:
void getBasicSalary() {
cout << "Enter Basic Salary: ";
cin >> basic_salary;
}

void displayEmpInfo() const {


displayEmployeeDetails();
cout << "Basic Salary: " << basic_salary << endl;
}
};

int main() {
EmpInfo emp;
emp.getEmployeeDetails();
emp.getBasicSalary();
cout << "\nEmployee Information:\n";
emp.displayEmpInfo();
return 0;
}

Fy-Diploma (URJA) [LIVE] (Sem 2) only at 4999/- BUY NOW


Sy-Diploma (UMANG ) [LIVE] (Sem 3 + sem 4) : only at 4999/- BUY NOW
Ty-Diploma (YUKTI ) [LIVE] (Sem 3 + sem 4) : only at 4999/- BUY NOW
All Courses : CHECK NOW YOUTUBE : SUBSCRIBE NOW INSTA : FOLLOW NOW
Download V2V APP on Playstore for more FREE STUDY MATERIAL
Contact No : 9326050669 / 9326881428
V2V EdTech LLP | OSY (313304) | Super 25 Question Set

Enter the first number: 15


Enter the second number: 7
Sum: 15 + 7 = 22
Difference: 15 - 7 = 8
Product: 15 * 7 = 105
Quotient: 15 / 7 = 2.14286

b. Write a C++ program for following multilevel inheritance

Accept and display data for one car with all details.
Ans. #include <iostream>
#include <string>
using namespace std;

class Carmanufacturer {
protected:
string name;

public:
void setManufacturerName(const string& manufacturerName) {
name = manufacturerName;
}
void displayManufacturer() {
cout << "Manufacturer Name: " << name << endl;
}
};

class Carmodel : public Carmanufacturer {


protected:

Fy-Diploma (URJA) [LIVE] (Sem 2) only at 4999/- BUY NOW


Sy-Diploma (UMANG ) [LIVE] (Sem 3 + sem 4) : only at 4999/- BUY NOW
Ty-Diploma (YUKTI ) [LIVE] (Sem 3 + sem 4) : only at 4999/- BUY NOW
All Courses : CHECK NOW YOUTUBE : SUBSCRIBE NOW INSTA : FOLLOW NOW
Download V2V APP on Playstore for more FREE STUDY MATERIAL
Contact No : 9326050669 / 9326881428
V2V EdTech LLP | OSY (313304) | Super 25 Question Set

string modelName;
int modelNumber;

public:
void setModelDetails(const string& name, int number) {
modelName = name;
modelNumber = number;
}
void displayModel() {
displayManufacturer();
cout << "Model Name: " << modelName << endl;
cout << "Model Number: " << modelNumber << endl;
}
};

class Car : public Carmodel {


private:
int carNumber;
string color;

public:
void setCarDetails(int number, const string& carColor) {
carNumber = number;
color = carColor;
}
void displayCar() {
displayModel();
cout << "Car Number: " << carNumber << endl;
cout << "Car Color: " << color << endl;
}
};

int main() {
Car car;
car.setManufacturerName("Toyota");
car.setModelDetails("Corolla", 2023);
car.setCarDetails(1001, "Red");
car.displayCar();
return 0;
}

Fy-Diploma (URJA) [LIVE] (Sem 2) only at 4999/- BUY NOW


Sy-Diploma (UMANG ) [LIVE] (Sem 3 + sem 4) : only at 4999/- BUY NOW
Ty-Diploma (YUKTI ) [LIVE] (Sem 3 + sem 4) : only at 4999/- BUY NOW
All Courses : CHECK NOW YOUTUBE : SUBSCRIBE NOW INSTA : FOLLOW NOW
Download V2V APP on Playstore for more FREE STUDY MATERIAL
Contact No : 9326050669 / 9326881428
V2V EdTech LLP | OSY (313304) | Super 25 Question Set

Manufacturer Name: Toyota


Model Name: Corolla
Model Number: 2023
Car Number: 1001
Car Color: Red

c. Write a program to implement multiple inheritance as shown in following Figure :

Accept and display data for one object of class result.


Ans. #include <iostream>
using namespace std;

class Subject1 {
protected:
int m1;

public:
void setMarks1(int marks) {
m1 = marks;
}
int getMarks1() {
return m1;
}
};

class Subject2 {
protected:
int m2;

public:
void setMarks2(int marks) {
m2 = marks;

Fy-Diploma (URJA) [LIVE] (Sem 2) only at 4999/- BUY NOW


Sy-Diploma (UMANG ) [LIVE] (Sem 3 + sem 4) : only at 4999/- BUY NOW
Ty-Diploma (YUKTI ) [LIVE] (Sem 3 + sem 4) : only at 4999/- BUY NOW
All Courses : CHECK NOW YOUTUBE : SUBSCRIBE NOW INSTA : FOLLOW NOW
Download V2V APP on Playstore for more FREE STUDY MATERIAL
Contact No : 9326050669 / 9326881428
V2V EdTech LLP | OSY (313304) | Super 25 Question Set

}
int getMarks2() {
return m2;
}
};

class Result : public Subject1, public Subject2 {


private:
int total;

public:
void calculateTotal() {
total = m1 + m2;
}
void displayResult() {
cout << "Marks in Subject 1: " << m1 << endl;
cout << "Marks in Subject 2: " << m2 << endl;
cout << "Total Marks: " << total << endl;
}
};

int main() {
Result studentResult;
studentResult.setMarks1(85);
studentResult.setMarks2(90);
studentResult.calculateTotal();
studentResult.displayResult();
return 0;
}

Marks in Subject 1: 85
Marks in Subject 2: 90
Total Marks: 175

Fy-Diploma (URJA) [LIVE] (Sem 2) only at 4999/- BUY NOW


Sy-Diploma (UMANG ) [LIVE] (Sem 3 + sem 4) : only at 4999/- BUY NOW
Ty-Diploma (YUKTI ) [LIVE] (Sem 3 + sem 4) : only at 4999/- BUY NOW
All Courses : CHECK NOW YOUTUBE : SUBSCRIBE NOW INSTA : FOLLOW NOW
Download V2V APP on Playstore for more FREE STUDY MATERIAL
Contact No : 9326050669 / 9326881428
V2V EdTech LLP | OSY (313304) | Super 25 Question Set

d. Develop c++ program to implement inheritance shown in fig

Ans. #include <iostream>


#include <string>
using namespace std;

class HOD {
protected:
string name;

public:
void setHODName(const string& hodName) {
name = hodName;
}
void displayHOD() {
cout << "HOD Name: " << name << endl;
}
};

class Faculty : public HOD {


protected:
string facultyName;

public:
void setFacultyName(const string& name) {

Fy-Diploma (URJA) [LIVE] (Sem 2) only at 4999/- BUY NOW


Sy-Diploma (UMANG ) [LIVE] (Sem 3 + sem 4) : only at 4999/- BUY NOW
Ty-Diploma (YUKTI ) [LIVE] (Sem 3 + sem 4) : only at 4999/- BUY NOW
All Courses : CHECK NOW YOUTUBE : SUBSCRIBE NOW INSTA : FOLLOW NOW
Download V2V APP on Playstore for more FREE STUDY MATERIAL
Contact No : 9326050669 / 9326881428
V2V EdTech LLP | OSY (313304) | Super 25 Question Set

facultyName = name;
}
void displayFaculty() {
displayHOD();
cout << "Faculty Name: " << facultyName << endl;
}
};

class Student : public HOD {


protected:
string studentName;

public:
void setStudentName(const string& name) {
studentName = name;
}
void displayStudent() {
displayHOD();
cout << "Student Name: " << studentName << endl;
}
};

class LabIncharge : public HOD {


protected:
string labName;

public:
void setLabName(const string& name) {
labName = name;
}
void displayLabIncharge() {
displayHOD();
cout << "Lab Name: " << labName << endl;
}
};

class Technical : public LabIncharge {


private:
string technicalSpecialization;

Fy-Diploma (URJA) [LIVE] (Sem 2) only at 4999/- BUY NOW


Sy-Diploma (UMANG ) [LIVE] (Sem 3 + sem 4) : only at 4999/- BUY NOW
Ty-Diploma (YUKTI ) [LIVE] (Sem 3 + sem 4) : only at 4999/- BUY NOW
All Courses : CHECK NOW YOUTUBE : SUBSCRIBE NOW INSTA : FOLLOW NOW
Download V2V APP on Playstore for more FREE STUDY MATERIAL
Contact No : 9326050669 / 9326881428
V2V EdTech LLP | OSY (313304) | Super 25 Question Set

public:
void setTechnicalDetails(const string& specialization) {
technicalSpecialization = specialization;
}
void displayTechnical() {
displayLabIncharge();
cout << "Technical Specialization: " << technicalSpecialization << endl;
}
};

class NonTechnical : public LabIncharge {


private:
string nonTechnicalWork;

public:
void setNonTechnicalDetails(const string& work) {
nonTechnicalWork = work;
}
void displayNonTechnical() {
displayLabIncharge();
cout << "Non-Technical Work: " << nonTechnicalWork << endl;
}
};

int main() {
Technical tech;
tech.setHODName("Dr. Sharma");
tech.setLabName("Physics Lab");
tech.setTechnicalDetails("Hardware Maintenance");
tech.displayTechnical();

cout << endl;

NonTechnical nonTech;
nonTech.setHODName("Dr. Sharma");
nonTech.setLabName("Chemistry Lab");
nonTech.setNonTechnicalDetails("Inventory Management");
nonTech.displayNonTechnical();

return 0;

Fy-Diploma (URJA) [LIVE] (Sem 2) only at 4999/- BUY NOW


Sy-Diploma (UMANG ) [LIVE] (Sem 3 + sem 4) : only at 4999/- BUY NOW
Ty-Diploma (YUKTI ) [LIVE] (Sem 3 + sem 4) : only at 4999/- BUY NOW
All Courses : CHECK NOW YOUTUBE : SUBSCRIBE NOW INSTA : FOLLOW NOW
Download V2V APP on Playstore for more FREE STUDY MATERIAL
Contact No : 9326050669 / 9326881428
V2V EdTech LLP | OSY (313304) | Super 25 Question Set

}
HOD Name: Dr. Sharma
Lab Name: Physics Lab
Technical Specialization: Hardware Maintenance

HOD Name: Dr. Sharma


Lab Name: Chemistry Lab
Non-Technical Work: Inventory Management

15. Pointer Programs :


a. Develop a c++ program to perform arithmetic operation using Pointer.
Ans. #include <iostream>
using namespace std;

int main() {
int num1, num2;
int *ptr1, *ptr2;

// Input: Ask the user to enter two numbers


cout << "Enter the first number: ";
cin >> num1;
cout << "Enter the second number: ";
cin >> num2;

// Assign the addresses of the variables to pointers


ptr1 = &num1;
ptr2 = &num2;

// Perform arithmetic operations using pointers


int sum = *ptr1 + *ptr2;
int difference = *ptr1 - *ptr2;
int product = *ptr1 * *ptr2;
float quotient = static_cast<float>(*ptr1) / *ptr2; // Ensure floating-point division

// Output: Display the results


cout << "Sum: " << *ptr1 << " + " << *ptr2 << " = " << sum << endl;
cout << "Difference: " << *ptr1 << " - " << *ptr2 << " = " << difference << endl;
cout << "Product: " << *ptr1 << " * " << *ptr2 << " = " << product << endl;
cout << "Quotient: " << *ptr1 << " / " << *ptr2 << " = " << quotient << endl;

return 0;
}

Fy-Diploma (URJA) [LIVE] (Sem 2) only at 4999/- BUY NOW


Sy-Diploma (UMANG ) [LIVE] (Sem 3 + sem 4) : only at 4999/- BUY NOW
Ty-Diploma (YUKTI ) [LIVE] (Sem 3 + sem 4) : only at 4999/- BUY NOW
All Courses : CHECK NOW YOUTUBE : SUBSCRIBE NOW INSTA : FOLLOW NOW
Download V2V APP on Playstore for more FREE STUDY MATERIAL
Contact No : 9326050669 / 9326881428
V2V EdTech LLP | OSY (313304) | Super 25 Question Set

Enter the first number: 15


Enter the second number: 7
Sum: 15 + 7 = 22
Difference: 15 - 7 = 8
Product: 15 * 7 = 105
Quotient: 15 / 7 = 2.14286

b. Write a program to declare a class ‘book’ containing data members as ‘title’, ‘author-name’,
‘publication’, ‘price’. Accept and display the information for one object using pointer to that
object. (Note: Any other correct logic shall be considered)
Ans. #include<iostream.h>
#include<conio.h>
class book
{
char author_name[20];
char title[20];
char publication[20];
float price; public:
void Accept();
void Display();
};
void book::Accept()
{
cout<<"\n Enter book‟s title, author_name, publication and price \n:";
cin>> title >>author_name>> publication >> price;
}
void student::Display()
{
cout<<title <<”\t”<<author_name<<”\t”<<publication <<”\t”<<
price<<”\n”<<;
}
void main()
{
book b, *p;
clrscr();
p=&b;
p->Accept();
cout<<”title \t author_name \t publication \t price\n”;
p-> Display();
getch();
}

Fy-Diploma (URJA) [LIVE] (Sem 2) only at 4999/- BUY NOW


Sy-Diploma (UMANG ) [LIVE] (Sem 3 + sem 4) : only at 4999/- BUY NOW
Ty-Diploma (YUKTI ) [LIVE] (Sem 3 + sem 4) : only at 4999/- BUY NOW
All Courses : CHECK NOW YOUTUBE : SUBSCRIBE NOW INSTA : FOLLOW NOW
Download V2V APP on Playstore for more FREE STUDY MATERIAL
Contact No : 9326050669 / 9326881428
V2V EdTech LLP | OSY (313304) | Super 25 Question Set

Enter book's title, author name, publication, and price: The Catcher in the Rye J.D. Salinger
Little, Brown and Company 12.99
title author_name publication price
The Catcher in the Rye J.D. Salinger Little, Brown and Company 12.99

c. Define pointer operator and address operator with example.


Ans. In C++, the pointer operator and address operator are essential for working with
pointers.

1. Pointer Operator (`*`):


- The `*` operator is used to declare a pointer and to access the value stored at the address
pointed to by the pointer.
- It is also used in pointer arithmetic when you want to increment or decrement the pointer
to access elements in an array or to move to the next memory location.

int main() {
int num = 42;
int* ptr = &num; // Declare a pointer and assign the address of 'num'

// Access the value using the pointer operator


cout << "Value of num: " << *ptr << endl; // Output: Value of num: 42

return 0;
}

2. Address Operator (`&`):


- The `&` operator is used to get the memory address (or pointer) of a variable.
- It allows you to obtain the address of a variable, which you can then store in a pointer or
use for other purposes.

int main() {
int num = 42;

// Use the address operator to get the memory address of 'num'


int* ptr = &num;

// Display the memory address of 'num'


cout << "Memory address of num: " << ptr << endl;

return 0;

Fy-Diploma (URJA) [LIVE] (Sem 2) only at 4999/- BUY NOW


Sy-Diploma (UMANG ) [LIVE] (Sem 3 + sem 4) : only at 4999/- BUY NOW
Ty-Diploma (YUKTI ) [LIVE] (Sem 3 + sem 4) : only at 4999/- BUY NOW
All Courses : CHECK NOW YOUTUBE : SUBSCRIBE NOW INSTA : FOLLOW NOW
Download V2V APP on Playstore for more FREE STUDY MATERIAL
Contact No : 9326050669 / 9326881428
V2V EdTech LLP | OSY (313304) | Super 25 Question Set

Output:
Memory address of num:
0x7ffe65db36b8 (The address may vary)

`&` operator is used to obtain the memory address of the `num` variable, and that address is
then stored in the `ptr` pointer.

The pointer operator (`*`) is used for dereferencing pointers and accessing the value stored at
a specific memory address. The address operator (`&`) is used to get the memory address of
a variable. These operators are fundamental when working with pointers in C++.

d. Write a C++ program to declare a class train with members as train no and name. Accept
and display data for one object of train. Use pointer to object to call functions of class.
Ans. #include <iostream>
using namespace std;

class Train
{
private:
int trainNo;
string trainName;

public:
// Member function to accept data
void acceptData() {
cout << "Enter Train Number: ";
cin >> trainNo;
cin.ignore(); // Ignore any previous newline character
cout << "Enter Train Name: ";
getline(cin, trainName);
}

// Member function to display data


void displayData() {
cout << "Train Number: " << trainNo << endl;
cout << "Train Name: " << trainName << endl;
}
};

int main() {

Fy-Diploma (URJA) [LIVE] (Sem 2) only at 4999/- BUY NOW


Sy-Diploma (UMANG ) [LIVE] (Sem 3 + sem 4) : only at 4999/- BUY NOW
Ty-Diploma (YUKTI ) [LIVE] (Sem 3 + sem 4) : only at 4999/- BUY NOW
All Courses : CHECK NOW YOUTUBE : SUBSCRIBE NOW INSTA : FOLLOW NOW
Download V2V APP on Playstore for more FREE STUDY MATERIAL
Contact No : 9326050669 / 9326881428
V2V EdTech LLP | OSY (313304) | Super 25 Question Set

Train* ptrTrain = new Train(); // Create a pointer to a Train object

// Call member functions using the pointer


ptrTrain->acceptData();
ptrTrain->displayData();

// Deallocate memory for the object


delete ptrTrain;
return 0;
}

Enter Train Number: 123


Enter Train Name: Sample Train
Train Number: 123
Train Name: Sample Train

This program demonstrates how to use a pointer to an object of a class to call its member
functions and access its data members.

16. Describe ‘this’ pointer with an example.


Ans. C++ uses a unique keyword called „this‟ to represent an object that invokes a member
function. This unique pointer is automatically passed to a member function when it is
invoked. „this‟ is a pointer that always point to the object for which the member function
was called.
For example, the function call A.max ( ) will set the pointer „this‟ to the address of the object
A. Then suppose we call B.max ( ), the pointer „this‟ will store address of object B.
Example:
#include <iostream>
using namespace std;

class sample {
int a;
public:
void setdata(int x) {
this->a = x;
}
void putdata() {
cout << a;
}
};

Fy-Diploma (URJA) [LIVE] (Sem 2) only at 4999/- BUY NOW


Sy-Diploma (UMANG ) [LIVE] (Sem 3 + sem 4) : only at 4999/- BUY NOW
Ty-Diploma (YUKTI ) [LIVE] (Sem 3 + sem 4) : only at 4999/- BUY NOW
All Courses : CHECK NOW YOUTUBE : SUBSCRIBE NOW INSTA : FOLLOW NOW
Download V2V APP on Playstore for more FREE STUDY MATERIAL
Contact No : 9326050669 / 9326881428
V2V EdTech LLP | OSY (313304) | Super 25 Question Set

int main() {
sample s;
s.setdata(100);
s.putdata();
return 0;
}

100

In the above example, this pointer is used to represent object s when setdata ( ) and putdata (
) functions are called.’

17 . Overloading
(i) Write a C++ program to overload "+" or ‘-‘ operator so that it will perform concatenation
of two strings. (Use class get data function to accept two strings)
Ans.
(ii)Describe Function overloading with suitable program.
Ans. #include <iostream>
using namespace std;

// Function to add two integers


int add(int a, int b) {
return a + b;
}

// Function to add two double numbers


double add(double a, double b) {
return a + b;
}

// Function to concatenate two strings


string add(const string& str1, const string& str2) {
return str1 + str2;
}

int main() {
int result1 = add(5, 10);
double result2 = add(3.5, 2.5);
string result3 = add("Hello, ", "world!");

cout << "Result 1: " << result1 << endl; // Output: Result 1: 15
cout << "Result 2: " << result2 << endl; // Output: Result 2: 6

Fy-Diploma (URJA) [LIVE] (Sem 2) only at 4999/- BUY NOW


Sy-Diploma (UMANG ) [LIVE] (Sem 3 + sem 4) : only at 4999/- BUY NOW
Ty-Diploma (YUKTI ) [LIVE] (Sem 3 + sem 4) : only at 4999/- BUY NOW
All Courses : CHECK NOW YOUTUBE : SUBSCRIBE NOW INSTA : FOLLOW NOW
Download V2V APP on Playstore for more FREE STUDY MATERIAL
Contact No : 9326050669 / 9326881428
V2V EdTech LLP | OSY (313304) | Super 25 Question Set

cout << "Result 3: " << result3 << endl; // Output: Result 3: Hello, world!

return 0;
}

Result 1: 15
Result 2: 6
Result 3: Hello, world!

18. Differentiate between run time and compile time polymorphism


Ans.
Aspect Compile time polymorphism Run time polymorphisim

Also Known As Static or Early Binding Dynamic or Late Binding


Polymorphism Polymorphism

Resolution of Function Resolved by the Compiler Not Resolved by the Compiler


Calls

Flexibility Less Flexible More Flexible

Implementation Function Overloading, Operator Virtual Functions, Function


Mechanisms Overloading Overriding

Application Examples Method Overloading Method Overriding

Execution Speed Faster Execution (Compile Slower Execution (Run Time)


Time)

Use Cases Less Preferred for Complex Suitable for Handling


Problems Complex Problems

Knowledge of Methods Known During Compilation Revealed During Runtime


and Details

19. What are the rules for virtual function?


Ans. 1. The virtual functions must be members of some class.
2. They cannot be static members.
3. They are accessed by using object pointers.
4. A virtual function can be a friend of another class.

Fy-Diploma (URJA) [LIVE] (Sem 2) only at 4999/- BUY NOW


Sy-Diploma (UMANG ) [LIVE] (Sem 3 + sem 4) : only at 4999/- BUY NOW
Ty-Diploma (YUKTI ) [LIVE] (Sem 3 + sem 4) : only at 4999/- BUY NOW
All Courses : CHECK NOW YOUTUBE : SUBSCRIBE NOW INSTA : FOLLOW NOW
Download V2V APP on Playstore for more FREE STUDY MATERIAL
Contact No : 9326050669 / 9326881428
V2V EdTech LLP | OSY (313304) | Super 25 Question Set

5. A virtual function in a base class must be defined, even though it may not be used.
6. The prototypes of the base class version of a virtual function and all the derived class
versions must be identical.
7. We cannot have virtual constructors, but we can have virtual destructors.
8. While a base pointer can point to any type of the derived object, the reverse is not true.
9. When a base pointer points to a derived class, incrementing ordecrementing it will not
make it to point to the next object of the derived class.
10. If a virtual function is defined in the base class, it need not be necessarily redefined in the
derived class

20. Describe the concept of virtual base class with suitable example.
Ans. 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.

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:virtual public Grandparent
{
};

Fy-Diploma (URJA) [LIVE] (Sem 2) only at 4999/- BUY NOW


Sy-Diploma (UMANG ) [LIVE] (Sem 3 + sem 4) : only at 4999/- BUY NOW
Ty-Diploma (YUKTI ) [LIVE] (Sem 3 + sem 4) : only at 4999/- BUY NOW
All Courses : CHECK NOW YOUTUBE : SUBSCRIBE NOW INSTA : FOLLOW NOW
Download V2V APP on Playstore for more FREE STUDY MATERIAL
Contact No : 9326050669 / 9326881428
V2V EdTech LLP | OSY (313304) | Super 25 Question Set

class Child: public Parent1,public Parent2


{
};

Example:-

#include<iostream.h>
#include<conio.h>
class student
{
int rno;

public:
void getnumber()
{
cout<<"Enter Roll No:";
cin>>rno;
}
void putnumber()
{
cout<<"\n\n\t Roll No:"<<rno<<endl;
}
};

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

Fy-Diploma (URJA) [LIVE] (Sem 2) only at 4999/- BUY NOW


Sy-Diploma (UMANG ) [LIVE] (Sem 3 + sem 4) : only at 4999/- BUY NOW
Ty-Diploma (YUKTI ) [LIVE] (Sem 3 + sem 4) : only at 4999/- BUY NOW
All Courses : CHECK NOW YOUTUBE : SUBSCRIBE NOW INSTA : FOLLOW NOW
Download V2V APP on Playstore for more FREE STUDY MATERIAL
Contact No : 9326050669 / 9326881428
V2V EdTech LLP | OSY (313304) | Super 25 Question Set

class sports: public virtual student


{
public:

int score;
void getscore()
{
cout<<"Enter Sports Score:";
cin>>score;
}
void putscore()
{
cout<<"\n\t Sports Score is:"<<score;
}
};

class result: public test, public sports


{
int total;
public:
void display()
{
total=part1+part2+score;
putnumber();
putmarks();
putscore();
cout<<"\n\t Total Score:"<<total;
}
};

void main()
{
result obj;
clrscr();
obj.getnumber();
obj.getmarks();
obj.getscore();
obj.display();
getch();
}

Output:-

Fy-Diploma (URJA) [LIVE] (Sem 2) only at 4999/- BUY NOW


Sy-Diploma (UMANG ) [LIVE] (Sem 3 + sem 4) : only at 4999/- BUY NOW
Ty-Diploma (YUKTI ) [LIVE] (Sem 3 + sem 4) : only at 4999/- BUY NOW
All Courses : CHECK NOW YOUTUBE : SUBSCRIBE NOW INSTA : FOLLOW NOW
Download V2V APP on Playstore for more FREE STUDY MATERIAL
Contact No : 9326050669 / 9326881428
V2V EdTech LLP | OSY (313304) | Super 25 Question Set

Enter Roll No:30


Enter Marks
Part1:45
Part2:48
Enter Sports Score:40

Roll No:30
Marks Obtained

Part1:45
Part2:48
Sports Score is:40
Total Score:133

Note: Program/diagram with syntax shall be considered as an example

21. Write a C++ program to implement following in heritance. Refer Figure No.2:

Accept and display data for one object of class result (Hint: use virtual base class).
Ans. #include <iostream>
using namespace std;

class CollegeStudent {
protected:
int student_id;
string college_code;

public:

Fy-Diploma (URJA) [LIVE] (Sem 2) only at 4999/- BUY NOW


Sy-Diploma (UMANG ) [LIVE] (Sem 3 + sem 4) : only at 4999/- BUY NOW
Ty-Diploma (YUKTI ) [LIVE] (Sem 3 + sem 4) : only at 4999/- BUY NOW
All Courses : CHECK NOW YOUTUBE : SUBSCRIBE NOW INSTA : FOLLOW NOW
Download V2V APP on Playstore for more FREE STUDY MATERIAL
Contact No : 9326050669 / 9326881428
V2V EdTech LLP | OSY (313304) | Super 25 Question Set

void setCollegeStudentDetails(int id, string code) {


student_id = id;
college_code = code;
}

void displayCollegeStudentDetails() {
cout << "Student ID: " << student_id << endl;
cout << "College Code: " << college_code << endl;
}
};

class Test : virtual public CollegeStudent {


protected:
float percentage;

public:
void setTestDetails(float perc) {
percentage = perc;
}

void displayTestDetails() {
cout << "Percentage: " << percentage << "%" << endl;
}
};

class Sports : virtual public CollegeStudent {


protected:
char grade;

public:
void setSportsDetails(char grd) {
grade = grd;
}

void displaySportsDetails() {
cout << "Sports Grade: " << grade << endl;
}
};

class Result : public Test, public Sports {


public:
void displayResult() {
displayCollegeStudentDetails();

Fy-Diploma (URJA) [LIVE] (Sem 2) only at 4999/- BUY NOW


Sy-Diploma (UMANG ) [LIVE] (Sem 3 + sem 4) : only at 4999/- BUY NOW
Ty-Diploma (YUKTI ) [LIVE] (Sem 3 + sem 4) : only at 4999/- BUY NOW
All Courses : CHECK NOW YOUTUBE : SUBSCRIBE NOW INSTA : FOLLOW NOW
Download V2V APP on Playstore for more FREE STUDY MATERIAL
Contact No : 9326050669 / 9326881428
V2V EdTech LLP | OSY (313304) | Super 25 Question Set

displayTestDetails();
displaySportsDetails();
}
};

int main() {
Result r;
r.setCollegeStudentDetails(101, "CC123");
r.setTestDetails(85.6);
r.setSportsDetails('A');
r.displayResult();
return 0;
}

Student ID: 101


College Code: CC123
Percentage: 85.6%
Sports Grade: A

22. Question on Files


(i) Write a program to count the number of lines in file.
Ans. #include <iostream.h>
#include <fstream.h>
#include <conio.h>

void main() {
ifstream file;
char ch;
int n = 0;
clrscr();
file.open("abc.txt");

while (file) {
file.get(ch);
if (ch == '\n') {
n++;
}
}

cout << "\nNumber of lines in the file are: " << n;


file.close();
getch();
}

Fy-Diploma (URJA) [LIVE] (Sem 2) only at 4999/- BUY NOW


Sy-Diploma (UMANG ) [LIVE] (Sem 3 + sem 4) : only at 4999/- BUY NOW
Ty-Diploma (YUKTI ) [LIVE] (Sem 3 + sem 4) : only at 4999/- BUY NOW
All Courses : CHECK NOW YOUTUBE : SUBSCRIBE NOW INSTA : FOLLOW NOW
Download V2V APP on Playstore for more FREE STUDY MATERIAL
Contact No : 9326050669 / 9326881428
V2V EdTech LLP | OSY (313304) | Super 25 Question Set

Number of lines in the file are: X

(ii) Write a C++ program to count number of spaces in text file.


Ans. #include <iostream.h>
#include <fstream.h>
#include <conio.h>
void main()
{
ifstream file;
int s=0;
char ch;
clrscr();
file.open("abc.txt");
while(file)
{
file.get(ch);
if(ch==' ')
{
s++;
}
}
cout<<"\nNumber of spaces in text file are:"<<s;
getch();
}

Number of spaces in text file are: X

(iii) Write a C++ program to append data from abc.txt to xyz.txt file.
Ans. Assuming input file as abc.txt with contents "World" and output file
named as xyz.txt with contents "Hello" have been already created.

#include <iostream.h>
#include<fstream.h>
int main()
{
fstream f;
ifstream fin;
fin.open("abc.txt",ios::in);
ofstream fout;
fout.open("xyz.txt", ios::app);
if (!fin)

Fy-Diploma (URJA) [LIVE] (Sem 2) only at 4999/- BUY NOW


Sy-Diploma (UMANG ) [LIVE] (Sem 3 + sem 4) : only at 4999/- BUY NOW
Ty-Diploma (YUKTI ) [LIVE] (Sem 3 + sem 4) : only at 4999/- BUY NOW
All Courses : CHECK NOW YOUTUBE : SUBSCRIBE NOW INSTA : FOLLOW NOW
Download V2V APP on Playstore for more FREE STUDY MATERIAL
Contact No : 9326050669 / 9326881428
V2V EdTech LLP | OSY (313304) | Super 25 Question Set

{
cout<< "file not found";
}
else
{
fout<<fin.rdbuf();
}
char ch;
f.seekg(0);
while (f)
{
f.get(ch);
cout<< ch;
}
f.close();
return 0;
}

Output:
Hello World

(iv) Write a C++ program to copy the contents of a source file student 1.txt to a destination file
student 2.txt using file operations.
Ans. #include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main() {
// Open the source file for reading
ifstream sourceFile("student1.txt");
if (!sourceFile) {
cerr << "Error: Unable to open source file." << endl;
return 1;
}

// Open the destination file for writing


ofstream destinationFile("student2.txt");
if (!destinationFile) {
cerr << "Error: Unable to open destination file." << endl;
sourceFile.close(); // Close the source file
return 1;

Fy-Diploma (URJA) [LIVE] (Sem 2) only at 4999/- BUY NOW


Sy-Diploma (UMANG ) [LIVE] (Sem 3 + sem 4) : only at 4999/- BUY NOW
Ty-Diploma (YUKTI ) [LIVE] (Sem 3 + sem 4) : only at 4999/- BUY NOW
All Courses : CHECK NOW YOUTUBE : SUBSCRIBE NOW INSTA : FOLLOW NOW
Download V2V APP on Playstore for more FREE STUDY MATERIAL
Contact No : 9326050669 / 9326881428
V2V EdTech LLP | OSY (313304) | Super 25 Question Set

// Copy the contents from the source file to the destination file
string line;
while (getline(sourceFile, line)) {
destinationFile << line << endl;
}

// Close both files


sourceFile.close();
destinationFile.close();

cout << "Contents copied from student1.txt to student2.txt." << endl;

return 0;
}

Contents copied from student1.txt to student2.txt.

Fy-Diploma (URJA) [LIVE] (Sem 2) only at 4999/- BUY NOW


Sy-Diploma (UMANG ) [LIVE] (Sem 3 + sem 4) : only at 4999/- BUY NOW
Ty-Diploma (YUKTI ) [LIVE] (Sem 3 + sem 4) : only at 4999/- BUY NOW
All Courses : CHECK NOW YOUTUBE : SUBSCRIBE NOW INSTA : FOLLOW NOW
Download V2V APP on Playstore for more FREE STUDY MATERIAL
Contact No : 9326050669 / 9326881428

You might also like