Cpp October
Cpp October
ans- i) Class
Ans- i) ofstream
i) True, True iii) False, True ii) True, False iv) False, False
ans- An abstract class is a class that cannot create objects and is used as a base class to provide
common structure for other classes.
It can have abstract methods (without body) that must be defined in child classes.
// body of class
};
"r" Open for reading only. Starts at the beginning of the file.
"w" Open for writing only. Erases the file or creates a new one.
"w+" Open for reading and writing. Erases the file or creates new.
"a" Open for writing only. Writes at the end of the file.
"x" Create and open for writing only. Fails if the file exists.
"x+" Create and open for reading and writing. Fails if the file exists.
5) Define Encapsulation.
Ans- Encapsulation is the process of hiding the internal details of a class and protecting data
by bundling it with methods that control access. Bind data and function into single unit
Syntax:
inline return_type function_name(parameters) {
// Function body
}
Example:
#include<iostream>
using namespace std;
b) Write any 3 differences between OOP (Object Oriented Programming) and POP
(Procedure Oriented Programming)
class Rectangle {
public:
Rectangle(int l, int w) {
length = l;
width = w;
int area() {
};
int main() {
return 0;
Example:
class Test {
};
e) What is Exception? Which Keywords are used? Write its general syntax.
General Syntax:
cpp
CopyEdit
try {
catch (ExceptionType e) {
// Code to handle exception
Simple Example:
#include <iostream>
int main() {
try {
int num = 0;
if (num == 0)
return 0;
int main() {
int num;
cout << "Factors of " << num << " are: ";
Ans- Constructor:
A constructor is a special function in a class that is automatically called when an object of the
class is created. It is used to initialize the data members of the class.
Constructor Overloading:
Constructor overloading means having multiple constructors in the same class with the same
name but different parameters. This allows objects to be initialized in different ways.
#include <iostream>
class Rectangle {
public:
// Default constructor
Rectangle() {
length = 0;
width = 0;
// Parameterized constructor
Rectangle(int l, int w) {
length = l;
width = w;
void display() {
cout << "Length: " << length << ", Width: " << width << endl;
}
};
int main() {
rect1.display();
rect2.display();
return 0;
b) What is Inheritance? What ambiguity can arise in Multiple Inheritance? How it is solved?
Ans- Inheritance:
Inheritance is an OOP (Object-Oriented Programming) concept where one class (child/derived
class) can acquire properties and methods of another class (parent/base class). It helps in code
reuse and creating relationships between classes.
Example of Ambiguity:
#include <iostream>
class A {
public:
void display() {
};
class B {
public:
void display() {
};
int main() {
C obj;
return 0;
Simple Example:
cpp
CopyEdit
#include <iostream>
class Box {
private:
int length;
public:
};
void display(Box b) {
cout << "Length is: " << b.length << endl; // Accessing private data
int main() {
Box obj;
display(obj);
return 0;
1⃣ Only existing operators can be overloaded – You cannot create new operators.
2⃣ Syntax cannot be changed – The way the operator works (like number of operands) stays the
same.
• sizeof
• typeid
Summary:
Operator overloading allows you to redefine the behavior of operators for user-defined types
while following strict syntax and usage rules.
e) Write a program to create a class student having roll no, name and percentage. Write a
member function to accept and display details of students (use Array of objects)
class Student {
int roll_no;
char name[50];
float percentage;
public:
void accept() {
cin.getline(name, 50);
}
// Function to display student details
void display() {
cout << "Percentage: " << percentage << "%" << endl;
};
int main() {
int n;
cin >> n;
s[i].accept();
s[i].display();
return 0;
Syntax:
cpp
CopyEdit
endl Moves to a new line (like \n). cout << "Hello" << endl;
setw() Sets the width of the next output field. cout << setw(10) << "Hello";
Sets the number of digits after the cout << setprecision(3) <<
setprecision()
decimal. 3.14159;
g) What are the various file operations performed write a program to read the contents from
the text file
#include <fstream>
int main() {
string line;
ifstream file("example.txt");
if (file.is_open()) {
file.close();
} else {
return 0;
Virtual base class ensures only one copy of the common base class is inherited, avoiding
duplication.
/\
B C
\/
Here, both B and C inherit from A, and D inherits from both B and C. Without virtual, D would
have two copies of A.
Syntax:
class A {
public:
int x;
};
};
};
};
Complete Example:
#include <iostream>
class A {
public:
int x;
void getX() {
cin >> x;
};
};
public:
void display() {
};
int main() {
D obj;
obj.display();
return 0;
Ans- Function overloading means having multiple functions with the same name but different
types or numbers of parameters.
#include <iostream>
}
int main() {
cout << "Volume of Cylinder = " << volume(radius, height) << endl;
return 0;
Syntax:
cpp
CopyEdit
class ClassName {
int data;
public:
ClassName(int value) { // Constructor with basic type parameter
data = value;
}
void display() {
cout << "Data = " << data << endl;
}
};
Example:
cpp
CopyEdit
#include <iostream>
using namespace std;
class Number {
int n;
public:
// Constructor for basic to class type conversion
Number(int x) {
n = x;
}
void display() {
cout << "Number = " << n << endl;
}
};
int main() {
int value = 50;
Number obj = value; // Basic to class type conversion
obj.display();
return 0;
}
e) Write the rules for virtual function.
Ans- 1⃣ Declared with virtual keyword
➤ You must use the keyword virtual before the function in the base class.
2⃣ Must be a member of a class
➤ Virtual functions are only used inside classes.
3⃣ Overridden in derived classes
➤ The derived class can provide its own version of the virtual function.
4⃣ Works through pointers or references
➤ Virtual functions are usually called using base class pointers or references pointing to
derived class objects.
5⃣ Cannot be static
➤ Virtual functions cannot be declared as static.
6️⃣ Constructors cannot be virtual
➤ But destructors can and should be virtual when using inheritance.
7️⃣ Prototype must be same
➤ The function signature (name, return type, and parameters) should be the same in the
base and derived class.
i) New operator ii) Delete operator iii)Destructor iv) Friend function v) Insertion and extraction
operator
• The new operator is used to dynamically allocate memory for an object or variable during
runtime.
Example:
• The delete operator is used to free the memory that was allocated using the new operator.
Example:
delete ptr;
iii) Destructor
• A destructor is a special member function that is automatically called when an object is
destroyed.
• Syntax: ~ClassName() { }
• A friend function can access the private and protected members of a class.
• It is not a member of the class but is given special permission using the friend keyword.
Example:
• Insertion (<<) operator: Used with cout to output data to the console.
• Extraction (>>) operator: Used with cin to input data from the user.
Example:
ans- Output:
x = 40
2) #include <iostream>
using namespace std;
class Base1 {
public:
Base1() {
cout << "Base1 constructor is called" << endl;
}
};
class Base2 {
public:
Base2() {
cout << "Base2 constructor is called" << endl;
}
};
int main() {
Derived d;
return 0;
}