Professional Documents
Culture Documents
The class has the following member functions: a. void setradius(float) to set the radius of
data member. b. void setheight(float) to set the height of data member. c. float volume( ) to
calculate and return the volume of the cylinder. Write a C++ program to create cylinder
object and display its volume.
class Cylinder {
private:
float radius;
float height;
public:
// Constructor
Cylinder() {
radius = 0.0f;
height = 0.0f;
void setRadius(float r) {
radius = r;
void setHeight(float h) {
height = h;
}
// Member function to calculate volume
float volume() {
};
int main() {
Cylinder myCylinder;
myCylinder.setRadius(5.0f);
myCylinder.setHeight(10.0f);
std::cout << "Volume of the cylinder: " << myCylinder.volume() << std::endl;
return 0;
S1].Write a C++ program to create a class Array that contains one float array as member.
Overload the Unary ++ and -- operators to increase or decrease the value of each element
of an array. Use friend function for operator function.
class Array {
private:
public:
// Constructor
Array(float values[]) {
arr[i] = values[i];
++obj.arr[i];
return obj;
--obj.arr[i];
return obj;
}
// Display function to show array elements
void display() {
};
int main() {
Array myArray(values);
myArray.display();
++myArray;
myArray.display();
--myArray;
myArray.display();
return 0;
}
S2] Write a C++ program to create two classes Rectangle1 and Rectangle2. Compare area
of both the rectangles using friend function.
class Rectangle1 {
private:
float length;
float width;
public:
// Constructor
// Getter functions
return length;
return width;
}
};
class Rectangle2 {
private:
float length;
float width;
public:
// Constructor
// Getter functions
return length;
};
} else {
std::cout << "Areas of Rectangle1 and Rectangle2 are equal." << std::endl;
int main() {
// Comparing areas
compareAreas(rect1, rect2);
return 0;
S2] Write a C++ program to copy the contents of one file to another
#include <fstream>
int main() {
if (!inputFile.is_open()) {
return 1;
if (!outputFile.is_open()) {
return 1;
}
// Copy contents from input to output
char ch;
while (inputFile.get(ch)) {
outputFile.put(ch);
// Close files
inputFile.close();
outputFile.close();
std::cout << "Contents copied successfully from input.txt to output.txt." << std::endl;
return 0;
S3] [uncompiled]Write a C++ program to overload function Volume and find Volume of
Cube, Cylinder and Sphere.
#include <cmath>
}
// Function to calculate volume of a cylinder
int main() {
// Display volumes
S3] Write a C++ program with Student as abstract class and create derive classes
Engineering, Medicine and Science having data member rollno and name from base class
Student. Create objects of the derived classes and access them using array of pointer of
base class Student
#include <string>
class Student {
protected:
int rollno;
std::string name;
public:
};
public:
// Constructor
rollno = r;
name = n;
}
std::cout << "Engineering Student - Roll No: " << rollno << ", Name: " << name <<
std::endl;
};
public:
// Constructor
rollno = r;
name = n;
std::cout << "Medicine Student - Roll No: " << rollno << ", Name: " << name <<
std::endl;
};
public:
// Constructor
rollno = r;
name = n;
std::cout << "Science Student - Roll No: " << rollno << ", Name: " << name << std::endl;
};
int main() {
Student* students[3];
students[0] = &engStudent;
students[1] = &medStudent;
students[2] = &sciStudent;
students[i]->display();
return 0;
S4].Write a C++ program to print area of circle, square and rectangle using inline function.
}
int main() {
std::cout << "Area of Circle with radius " << radius << " is: " << areaCircle(radius) <<
std::endl;
std::cout << "Area of Square with side " << side << " is: " << areaSquare(side) << std::endl;
std::cout << "Area of Rectangle with length " << length << " and width " << width << " is: "
<< areaRectangle(length, width) << std::endl;
return 0;
S4] Write a C++ program to create a class which contains two dimensional integer array of
size m*n Write a member function to display transpose of entered matrix.(Use Dynamic
Constructor for allocating memory and Destructor to free memory of an object).
class Matrix {
private:
int **matrix;
int m, n;
public:
~Matrix() {
delete[] matrix[i];
delete[] matrix;
void inputMatrix() {
void displayTranspose() {
};
int main() {
std::cout << "Enter the number of rows and columns for the matrix: ";
myMatrix.inputMatrix();
myMatrix.displayTranspose();
return 0;
S5] Write a C++ program to create a class Mobile which contains data members as
Mobile_Id, Mobile_Name, Mobile_Price. Create and Initialize all values of Mobile object by
using parameterized constructor. Display the values of Mobile object.
#include <string>
class Mobile {
private:
int Mobile_Id;
std::string Mobile_Name;
double Mobile_Price;
public:
void display() {
};
int main() {
mobile1.display();
return 0;
S5] Create a base class Student (Roll_No, Name) which derives two classes Theory and
Practical. Theory class contains marks of five Subjects and Practical class contains marks
of two practical subjects. Class Result (Total_Marks, Class) inherits both Theory and
Practical classes. (Use concept of Virtual Base Class) Write a menu driven program to
perform the following functions: a. Build a master table. b. Display master table.
#include <string>
#include <iomanip>
protected:
int Roll_No;
std::string Name;
public:
// Parameterized constructor
: Roll_No(rollNo), Name(name) {}
void display() {
std::cout << "Roll No: " << Roll_No << ", Name: " << Name << std::endl;
};
protected:
int theoryMarks[5];
public:
// Parameterized constructor
: Student(rollNo, name) {
};
protected:
int practicalMarks[2];
public:
// Parameterized constructor
: Student(rollNo, name) {
practicalMarks[i] = marks[i];
};
private:
int Total_Marks;
std::string Class;
public:
// Parameterized constructor
Result(int rollNo, const std::string& name, int theoryMarks[], int practicalMarks[], int
totalMarks, const std::string& studentClass)
void display() {
Student::display();
std::cout << "Total Marks: " << Total_Marks << ", Class: " << Class << std::endl;
};
int main() {
Result* masterTable[MAX_STUDENTS];
int choice = 0;
int numStudents = 0;
while (true) {
switch (choice) {
case 1: {
std::cin.ignore();
std::getline(std::cin, name);
break;
case 2: {
std::cout << std::left << std::setw(15) << "Roll No" << std::setw(20) << "Name" <<
std::setw(15) << "Total Marks" << std::setw(10) << "Class" << std::endl;
masterTable[i]->display();
break;
case 3: {
// Exit program
return 0;
default:
}
return 0;
S6] Write a C++ program to implement a class printdata to overload print function as
follows: void print(int) - outputs value followed by the value of the integer. Eg. print(10)
outputs - value 10 void print(char *) – outputs value followed by the string in double quotes.
Eg. print(“hi”) outputs value “hi
#include <cstring>
class printdata {
public:
std::cout << "value \"" << value << "\"" << std::endl;
};
int main() {
printdata obj;
obj.print(10);
obj.print("hi");
return 0;
S6] Write a C++ program to design a class complex to represent complex number. The
complex class uses an external function (as a friend function) to add two complex number.
The function should return an object of type complex representing the sum of two complex
Numbers.
class Complex {
private:
double real;
double imaginary;
public:
// Getter functions
return real;
return imaginary;
}
};
int main() {
std::cout << "Sum: " << sum.getReal() << " + " << sum.getImaginary() << "i" << std::endl;
return 0;
}
S7] Write a C++ program using class which contains two data members as type integer.
Create and initialize the objects using default constructor, parameterized constructor with
default value. Write a member function to display maximum from given two numbers for all
objects. [10]
class Numbers {
private:
int num1;
int num2;
public:
// Default constructor
void displayMax() {
std::cout << "Maximum of " << num1 << " and " << num2 << " is: " << (num1 > num2 ?
num1 : num2) << std::endl;
};
int main() {
obj1.displayMax();
Numbers obj2(5);
obj2.displayMax();
obj3.displayMax();
return 0;
#include <string>
class College {
private:
int College_Id;
std::string College_Name;
int Establishment_year;
std::string University_Name;
public:
// Default constructor
College() {}
// Parameterized constructor
College(int id, const std::string& name, int year, const std::string& university)
void acceptDetails() {
std::cin.ignore();
std::getline(std::cin, College_Name);
std::cin.ignore();
std::getline(std::cin, University_Name);
void displayDetails() {
if (University_Name == university) {
displayDetails();
if (Establishment_year == year) {
displayDetails();
};
int main() {
College colleges[MAX_COLLEGES];
int n;
colleges[i].acceptDetails();
std::string searchUniversity;
std::cin.ignore();
std::getline(std::cin, searchUniversity);
colleges[i].displayDetails(searchUniversity);
int searchYear;
colleges[i].displayDetails(searchYear);
}
return 0;
S8] Write a C++ program to subtract two integer numbers of two different classes using
friend function
class B;
// Class A
class A {
private:
int numA;
public:
// Constructor
};
// Class B
class B {
private:
int numB;
public:
// Constructor
};
int main() {
A objA(20);
B objB(10);
S8] Write a C++ program to create a class Date which contains three data members as
dd,mm,yyyy. Create and initialize the object by using parameterized constructor and
display date in dd-monthyyyy format. (Input: 19-12-2014 Output: 19-Dec-2014) Perform
validation for month.
#include <string>
class Date {
private:
int dd;
int mm;
int yyyy;
public:
// Parameterized constructor
void displayDate() {
std::cout << dd << "-" << monthNames[mm - 1] << "-" << yyyy << std::endl;
} else {
};
int main() {
date.displayDate();
return 0;
S9] Write a C++ program to create a class Item with data members Item_code, Item_name,
Item_Price. Write member functions to accept and display item information and also
display number of objects created for a class. (Use Static data member and Static member
function)
#include <string>
class Item {
private:
int Item_code;
std::string Item_name;
float Item_Price;
static int count; // Static data member to count number of objects created
public:
void acceptItem() {
std::cin.ignore();
std::getline(std::cin, Item_name);
}
// Function to display item information
void displayItem() {
std::cout << "Number of objects created: " << count << std::endl;
};
int Item::count = 0;
int main() {
item1.displayItem();
Item::displayCount();
return 0;
S9] Create a class Time which contains data members as: Hours, Minutes and Seconds.
Write a C++ program to perform following necessary member functions: a. To read time b.
To display time in format like: hh:mm:ss c. To add two different times (Use Objects as
argument)
class Time {
private:
int Hours;
int Minutes;
int Seconds;
public:
void readTime() {
void displayTime() {
Time result;
result.Seconds %= 60;
result.Minutes %= 60;
return result;
}
};
int main() {
time1.readTime();
time2.readTime();
time1.displayTime();
time2.displayTime();
sum.displayTime();
return 0;
}
S10] 1.Write a C++ program to create a class employee containing salary as a data
member. Write necessary member functions to overload the operator unary pre and post
decrement "--" for decrementing salary.
class Employee {
private:
double salary;
public:
// Constructor
Employee(double s) : salary(s) {}
Employee& operator--() {
--salary;
return *this;
Employee operator--(int) {
Employee temp(*this);
--salary;
return temp;
}
// Function to display salary
};
int main() {
Employee emp(5000);
emp.displaySalary();
// Pre-decrement salary
--emp;
emp.displaySalary();
// Post-decrement salary
emp--;
emp.displaySalary();
return 0;
}
S10] Design a base class Product(Product _Id, Product _Name, Price). Derive a class
Discount (Discount_In_Percentage) from Product. A customer buys n Products. Calculate
total price, total discount and display bill using appropriate manipulators.
#include <vector>
#include <iomanip>
class Product {
protected:
int Product_Id;
std::string Product_Name;
double Price;
public:
// Parameterized constructor
return Price;
std::cout << std::setw(10) << Product_Id << std::setw(20) << Product_Name <<
std::setw(10) << Price;
}
};
private:
double Discount_In_Percentage;
public:
// Parameterized constructor
std::cout << std::setw(10) << Product_Id << std::setw(20) << Product_Name <<
std::setw(10) << Price << std::setw(10) << Discount_In_Percentage << "%";
};
int main() {
int n;
std::cin >> n;
std::vector<Product*> products;
std::cout << "Enter product details for " << n << " products:\n";
int id;
std::string name;
double price;
double discount;
std::cin.ignore();
std::getline(std::cin, name);
if (discount > 0) {
} else {
totalPrice += products.back()->calculateTotalPrice();
// Display bill
std::cout << std::setw(10) << "ID" << std::setw(20) << "Name" << std::setw(10) << "Price"
<< std::setw(10) << "Discount" << std::endl;
products[i]->display();
// Cleanup
delete products[i];
}
return 0;
S11] Write a C++ program to read two float numbers. Perform arithmetic binary operations
+,-,*,/ on these numbers using inline function. Display the resultant value.
return a + b;
return a - b;
return a * b;
if (b != 0) {
return a / b;
} else {
return 0;
int main() {
return 0;
S11] Write a C++ program to create a class Person that contains data members as
Person_Name, City, Mob_No. Write a C++ program to perform following functions: a. To
accept and display Person information b. To search the Person details of a given mobile
number c. To search the Person details of a given city. (Use Function Overloading)
#include <string>
class Person {
private:
std::string Person_Name;
std::string City;
std::string Mob_No;
public:
void acceptPersonInfo() {
if (Mob_No == mobile) {
displayPersonInfo();
} else {
std::cout << "Person not found with the given mobile number.\n";
if (City == cityName) {
displayPersonInfo();
} else {
};
int main() {
p1.acceptPersonInfo();
std::cout << "\nEnter information for Person 2:\n";
p2.acceptPersonInfo();
p3.acceptPersonInfo();
p1.displayPersonInfo();
p2.displayPersonInfo();
p3.displayPersonInfo();
std::string mobile;
p1.searchPerson(mobile);
p2.searchPerson(mobile);
p3.searchPerson(mobile);
std::string city;
p2.searchPerson(city, 0);
p3.searchPerson(city, 0);
return 0;
S12] Write a C++ program to accept length and width of a rectangle. Calculate and display
perimeter as well as area of a rectangle by using inline function.
int main() {
std::cout << "Perimeter of rectangle: " << calculatePerimeter(length, width) << std::endl;
std::cout << "Area of rectangle: " << calculateArea(length, width) << std::endl;
return 0;
S12] Write a C++ program to create a class which contains single dimensional integer array
of given size. Define member function to display median of a given array. (Use Dynamic
Constructor to allocate and Destructor to free memory of an object)
#include <algorithm>
class ArrayWithMedian {
private:
int *arr;
int size;
public:
~ArrayWithMedian() {
delete[] arr;
void displayMedian() {
double median;
if (size % 2 == 0) {
} else {
std::cout << "Median of the array: " << median << std::endl;
};
int main() {
int size;
std::cout << "Enter the size of the array: ";
ArrayWithMedian arrObj(size);
arrObj.displayMedian();
return 0;
S13] 1.Write a C++ program to implement a class ‘student’ to overload following functions
as follows: a. int maximum(int, int) – returns the maximum score of two students b. int
maximum(int *a, int arraylength) – returns the maximum score from an array ‘a’ [
class Student {
public:
max = a[i];
return max;
};
int main() {
Student studentObj;
std::cout << "Maximum score from the array: " << studentObj.maximum(scoresArray,
arrayLength) << std::endl;
return 0;
S13] Write a C++ program to read the contents of a text file. Count and display number of
characters, words and lines from a file. Find the number of occurrences of a given word
present in a file.
ANS- #include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <algorithm>
std::string line;
std::istringstream iss(line);
std::string word;
int count = 0;
std::string word;
while (file >> word) {
if (word == targetWord) {
++count;
return count;
int main() {
std::string filename;
std::ifstream file(filename);
if (!file.is_open()) {
return 1;
file.clear();
file.seekg(0, std::ios::beg);
std::string targetWord;
std::cout << "Number of occurrences of \"" << targetWord << "\": " << wordOccurrences
<< std::endl;
file.close();
return 0;
S14] 1. Write a C++ program to interchange values of two integer numbers (use call by
reference
int temp = a;
a = b;
b = temp;
int main() {
interchange(num1, num2);
return 0;
}
S14] Create a class Fraction that contains two data members as numerator and
denominator. Write a C++ program to overload following operators a. ++ Unary (pre and
post both) b. << and >> Overload as friend functions
class Fraction {
private:
int numerator;
int denominator;
public:
// Constructor
Fraction& operator++() {
numerator += denominator;
return *this;
Fraction operator++(int) {
Fraction temp(*this);
numerator += denominator;
return temp;
}
return out;
in >> frac.numerator;
in >> frac.denominator;
return in;
};
int main() {
++frac1;
std::cout << "Fraction 1 (after pre-increment): " << frac1 << std::endl;
// Testing post-increment operator
std::cout << "Fraction 2 (after post-increment): " << frac2 << std::endl;
std::cout << "Fraction 3 (post-increment result): " << frac3 << std::endl;
Fraction frac4;
std::cout << "Fraction 4 entered by user: " << frac4 << std::endl;
return 0;
S15] Write a C++ program to check minimum and maximum of two integer number (use
inline function and conditional operator)
return (a < b) ? a : b;
return (a > b) ? a : b;
}
int main() {
std::cout << "Minimum of " << num1 << " and " << num2 << " is: " << findMin(num1, num2)
<< std::endl;
std::cout << "Maximum of " << num1 << " and " << num2 << " is: " << findMax(num1,
num2) << std::endl;
return 0;
S15] Create a base class Conversion. Derive three different classes Weight (Gram,
Kilogram), Volume (Milliliter, Liter), Currency (Rupees, Paise) from Conversion class. Write
a program to perform read, convert and display operations. (Use Pure virtual function)
#include <string>
class Conversion {
public:
};
private:
double value;
std::string unit;
public:
// Constructor
if (unit == "Gram") {
std::cout << "Converted value: " << value / 1000 << " Kilograms" << std::endl;
std::cout << "Converted value: " << value * 1000 << " Grams" << std::endl;
};
private:
double value;
std::string unit;
public:
// Constructor
if (unit == "Milliliter") {
std::cout << "Converted value: " << value / 1000 << " Liters" << std::endl;
std::cout << "Converted value: " << value * 1000 << " Milliliters" << std::endl;
};
private:
double value;
std::string unit;
public:
// Constructor
std::cout << "Converted value: " << value * 100 << " Paise" << std::endl;
std::cout << "Converted value: " << value / 100 << " Rupees" << std::endl;
};
int main() {
weightObj.convert();
volumeObj.convert();
currencyObj.convert();
return 0;
}
S16] 1. Write a C++ program to create a class Number which contains two integer data
members. Create and initialize the object by using default constructor, parameterized
constructor. Write a member function to display maximum from given two numbers for all
objects.
class Number {
private:
int num1;
int num2;
public:
// Default constructor
Number() {
num1 = 0;
num2 = 0;
// Parameterized constructor
num1 = n1;
num2 = n2;
};
int main() {
Number numObj1;
numObj1.displayMaximum();
numObj2.displayMaximum();
return 0;
S16] Create a class Time containing members as: - hours - minutes - seconds Write a C++
program for overloading operators >> and << to accept and display a Time also write a
member function to display time in total seconds.
class Time {
private:
int hours;
int minutes;
int seconds;
public:
// Default constructor
// Parameterized constructor
in >> time.hours;
in >> time.minutes;
in >> time.seconds;
return in;
out << "Time: " << time.hours << " hours, " << time.minutes << " minutes, " <<
time.seconds << " seconds";
return out;
}
// Function to display time in total seconds
};
int main() {
Time t;
std::cin >> t;
// Display time
return 0;
if (num % i == 0) {
return false; // If num is divisible by any number other than 1 and itself, it's not prime
return true; // If num is not divisible by any number other than 1 and itself, it's prime
int main() {
int num;
if (isPrime(num)) {
} else {
std::cout << num << " is not a prime number." << std::endl;
return 0;
}
S17] Create a class Fraction containing data members as Numerator and Denominator.
Write a program to overload operators ++ , -- and * to increment, decrement a Fraction and
multiply two Fraction respectively. (Use constructor to initialize values of an object)
class Fraction {
private:
int numerator;
int denominator;
public:
Fraction& operator++() {
numerator += denominator;
return *this;
Fraction operator++(int) {
Fraction temp(*this);
numerator += denominator;
return temp;
}
// Overloading -- operator for prefix decrement
Fraction& operator--() {
numerator -= denominator;
return *this;
Fraction operator--(int) {
Fraction temp(*this);
numerator -= denominator;
return temp;
};
int main() {
Fraction frac1(3, 4);
frac1.display();
frac2.display();
(frac1++).display();
(--frac2).display();
(frac1 * frac2).display();
S18] Write a C++ program to calculate following series: (1) + (1+2) + (1+2+3) + (1+2+3+4)
+ ... +(1+2+3+4+...+n)
int calculateSeriesSum(int n) {
int sum = 0;
int term = 0;
term += i;
sum += term;
return sum;
int main() {
int n;
std::cout << "Sum of the series is: " << seriesSum << std::endl;
return 0;
S18] Write a C++ program to read student information such as rollno, name and
percentage of n students. Write the student information using file handling.
#include <iostream>
#include <fstream>
struct Student {
int rollNo;
std::string name;
float percentage;
};
int main() {
int n;
std::cin >> n;
// Create an array of Student structures to store student information
Student students[n];
std::cout << "\nEnter details for student " << i + 1 << ":\n";
std::getline(std::cin, students[i].name);
std::ofstream outFile("student_info.txt");
if (!outFile) {
return 1;
outFile.close();
std::cout << "\nStudent information has been written to the file 'student_info.txt'." <<
std::endl;
return 0;
if (num % i == 0) {
}
int main() {
int number;
displayFactors(number);
return 0;
S19] Design a two base classes Employee (Name, Designation) and Project(Project_Id,
title). Derive a class Emp_Proj(Duration) from Employee and Project. Write a menu driven
program to a. Build a master table. b. Display a master table c. Display Project details in
the ascending order of duration.
#include <vector>
#include <algorithm>
class Employee {
protected:
std::string name;
std::string designation;
public:
// Default constructor
Employee() {}
// Parameterized constructor
std::cout << "Name: " << name << ", Designation: " << designation;
};
class Project {
protected:
int projectID;
std::string title;
public:
// Default constructor
Project() {}
// Parameterized constructor
std::cout << "Project ID: " << projectID << ", Title: " << title;
};
private:
int duration;
public:
// Default constructor
Emp_Proj() {}
// Parameterized constructor
Emp_Proj(const std::string& n, const std::string& d, int id, const std::string& t, int dur)
Employee::display();
Project::display();
std::cout << ", Duration: " << duration << " months";
}
// Getter function for duration
return duration;
};
int main() {
std::vector<Emp_Proj> masterTable;
int choice;
do {
switch (choice) {
case 1: {
int n;
std::cin >> n;
for (int i = 0; i < n; ++i) {
std::cout << "\nEnter details for Employee " << i + 1 << ":\n";
std::cout << "Enter details for Project " << i + 1 << ":\n";
break;
case 2: {
emp_proj.display();
break;
case 3: {
});
emp_proj.display();
break;
case 4:
break;
default:
return 0;
}
S20] Write a C++ program to sort integer and float array elements in ascending order by
using function overloading.
arr[j + 1] = temp;
template<typename T>
int main() {
displayArray(intArr, intSize);
sortArray(intArr, intSize);
displayArray(floatArr, floatSize);
sortArray(floatArr, floatSize);
displayArray(floatArr, floatSize);
return 0;
S20] Write a C++ program to create a class Department which contains data members as
Dept_Id, Dept_Name, H.O.D., Number_Of_staff. Write necessary member functions to a.
Accept details from user for ‘n’ departments and write it in a file “Dept.txt”. b. Display
details of department from a file.
#include <fstream>
#include <vector>
class Department {
private:
std::string deptId;
std::string deptName;
std::string hod;
int numberOfStaff;
public:
std::getline(std::cin, deptName);
std::getline(std::cin, hod);
outFile << "Number of Staff: " << numberOfStaff << std::endl << std::endl;
}
};
int main() {
int n;
std::vector<Department> departments;
std::cin >> n;
Department dept;
std::cout << "\nEnter details for Department " << i + 1 << ":\n";
dept.acceptDetails();
departments.push_back(dept);
std::ofstream outFile("Dept.txt");
if (!outFile) {
return 1;
outFile.close();
std::ifstream inFile("Dept.txt");
if (!inFile) {
return 1;
std::string line;
inFile.close();
return 0;