Name : shafaq
Class : Bsc General
Sub: ASssignment of c++
Roll no. 23bsc034
Date: 20-11-2024
Assignment 1
Unit 1 Introduction to OOPS
Q1.
#include <iostream> using namespace std;
inline int largest(int a, int b, int c) {
return (a > b) ? ((a > c) ? a : c) : ((b > c) ? b : c);
int main() { int num1, num2, num3; cout << "Enter three numbers: "; cin >> num1 >> num2
>> num3;
int max = largest(num1, num2, num3);
cout << "The largest of the three numbers is: " << max << endl; return 0;
Output:
Enter three numbers: 24 67 88
The largest of the three numbers is: 88
Q2.
#include <iostream>
#include <string> Class BankAccount { Private:
Std::string depositorName;
Int accountNumber;
Std::string accountType;
Double balance;
Public:
// Constructor to assign initial values
BankAccount :
(const std::string& name,
int num,
const std::string& type,
double initialBalance) :
depositorName(name),
accountNumber(num),
accountType(type),
balance(initialBalance) {}
// Member function to deposit an amount
Void deposit(double amount) {
If (amount > 0) {
Balance += amount;
// Member function to withdraw an amount if available
Void withdraw(double amount) {
If (amount > 0 && balance >= amount) {
Balance -= amount;
// Member function to display the name and balance
Void display() const {
Std::cout << “Name: “ << depositorName << “\nAccount Number: “ << accountNumber <<
“\nAccount Type: “ << accountType << “\nBalance: “ << balance << std::endl;
};
Int main() {
BankAccount account(“Tom”, 12355, “Savings”, 2000);
Account.display();
Account.deposit(800);
Account.withdraw(400);
Account.display();
Return 0;
Output:
Name: shafaq
Account Number: 123459
Account Type: Savings
Balance: 1400
Name: Tom
Account Number: 12345059
Account Type: Savings
Balance: 1500
Name: shafaq
Account Number: 1234055
Account Type: Savings
Balance: 600
Q3:
#include <iostream>
#include <stdexcept>
Class TimeAdder { Private:
Int hour1;
Int minute1;
Int hour2;
Int minute2;
Int totalMinutes;
Int totalHours;
Int newHour;
Int newMinute;
Public:
// Constructor to initialize the time values
TimeAdder(int h1, int m1, int h2, int m2) : hour1(h1), minute1(m1), hour2(h2), minute2(m2) {}
Void input() {
Std::cout << “Enter the first time (hour and minute): “;
Std::cin >> hour1;
Std::cin >> minute1;
Std::cout << “Enter the second time (hour and minute): “; Std::cin >> hour2;
Std::cin >> minute2;
Void gettime(int h1, int m1, int h2, int m2) {
Hour1 = h1;
Minute1 = m1;
Hour2 = h2;
Minute2 = m2;
// Member function to add the minutes and hours Void sum() {
totalMinutes = minute1 + minute2; totalHours = hour1 + hour2;
if (totalMinutes >= 60)
{ totalHours += totalMinutes / 60;
newMinute = totalMinutes % 60;
else {
newMinute = totalMinutes;
If (totalHours >= 24) { newHour = totalHours / 24; newHour %= 24;
} else {
newHour = totalHours;
Void display() const {
Std::cout << “The result is: “ << newHour << “ hours and “ << newMinute << “ minutes” <<
std::endl;
};
Int main() {
TimeAdder timeAdder(12, 30, 20, 50); timeAdder.input();
// Get time values from the user
timeAdder.gettime(timeAdder.hour1, timeAdder.minute1, timeAdder.hour2,
timeAdder.minute2); timeAdder.sum(); timeAdder.display();
return 0;
Output:
Enter the first time (hour and minute): 2 30
Enter the second time (hour and minute): 4 50
The result is: 6 hours and 80 minutes
Q4:
#include <iostream>
#Using namespace std;
Class Complex { Public:
Double real, img;
Void getdata() {
Cout << “Enter real part: “;
Cin >> real;
Cout << “Enter imaginary part: “;
Cin >> img;
Void show() {
Cout << real << “ + “ << img << “I” << endl;
Void sum(Complex c1, Complex c2) {
Real = c1.real + c2.real;
Img = c1.img + c2.img;
};
Int main() {
Complex c1, c2, c3;
Cout << “Enter the first complex number:\n”;
C1.getdata();
Cout << “Enter the second complex number:\n”;
C2.getdata();
C3.sum(c1, c2);
Cout << “\nSum of the two complex numbers:\n”;
C3.show();
Return 0;
Output:
Enter the first complex number:
Enter real part: 1
Enter imaginary part: 6
Enter the second complex number:
Enter real part: 3
Enter imaginary part: 2
Sum of the two complex numbers:
4 + 8i
Assignment 2
Class, object, Function
Q1
#include <iostream> #include <string> using namespace std;
class Student { private:
int rollno; string name;
float mark1, mark2, mark3, total, average;
public:
void getdata() {
cout << "Enter Roll No: "; cin >> rollno; cin.ignore(); cout << "Enter Name: ";
getline(cin, name); // To allow spaces in the name cout << "Enter Marks for Subject 1: ";
cin >> mark1;
cout << "Enter Marks for Subject 2: ";
cin >> mark2;
cout << "Enter Marks for Subject 3: "; cin >> mark3;
total = mark1 + mark2 + mark3; average = total / 3;
void displaydata() {
cout << "\nStudent Details:" << endl; cout << "Roll No: " << rollno << endl; cout <<
"Name: " << name << endl; cout << "Marks in Subject 1: " << mark1 << endl; cout <<
"Marks in Subject 2: " << mark2 << endl; cout << "Marks in Subject 3: " << mark3 << endl;
cout << "Total Marks: " << total << endl; cout << "Average Marks: " << average << endl;
}
};
int main() { int n;
cout << "Enter number of students: "; cin >> n;
Student students[n];
for (int i = 0; i < n; i++) {
cout << "\nEnter details for student " << i + 1 << ":\n"; students[i].getdata();
cout << "\nDisplaying student details:\n"; for (int i = 0; i < n; i++) {
students[i].displaydata();
return 0;
Output:
Student Details:
Roll No: 10
Name: shafaq
Marks in Subject 1: 20
Marks in Subject 2: 10
Marks in Subject 3: 30
Total Marks: 300
Average Marks: 20.00 %
Q2 #include <iostream> using namespace std;
class Complex { private:
float real, imag;
public: Complex() { real = 0; imag = 0;
}
Complex(float value) { real = value; imag = value;
Complex(float r, float i) { real = r; imag = i;
friend Complex addComplex(const Complex &c1, const Complex &c2);
friend void displayComplex(const Complex &c);
};
Complex addComplex(const Complex &c1, const Complex &c2) {
Complex result;
result.real = c1.real + c2.real; result.imag = c1.imag + c2.imag; return result;
void displayComplex(const Complex &c) {
cout << "Complex Number: " << c.real << " + " << c.imag << "i" << endl;
int main() {
float real1, imag1, real2, imag2;
cout << "Enter real and imaginary part of first complex number: ";
cin >> real1 >> imag1;
cout << "Enter real and imaginary part of second complex number: ";
cin >> real2 >> imag2;
Complex c1(real1, imag1);
Complex c2(real2, imag2);
Complex result = addComplex(c1, c2); cout << "\nResult of addition: ";
displayComplex(result);
return 0;
Output:
Enter real and imaginary part of first complex number: 5 4
Enter real and imaginary part of second complex number: 6 2
Result of addition: Complex Number: 11 + 6i
Q3
#include <iostream> using namespace std;
class Example { private:
int privateData;
public:
int publicData;
Example(int pData, int priData) { publicData = pData; privateData = priData;
void displayData() {
cout << "Private Data: " << privateData << endl; cout << "Public Data: " << publicData <<
endl;
void setPrivateData(int priData) { privateData = priData;
int getPrivateData() { return privateData;
};
int main() {
Example obj(50, 60);
cout << "Accessing public member 'publicData' from outside the class: " << obj.publicData << endl;
cout << "Accessing private member 'privateData' via a public member function: " <<
obj.getPrivateData() << endl;
obj.setPrivateData(200); obj.displayData();
return 0;
Output:
Accessing public member 'publicData' from outside the class: 50
Accessing private member 'privateData' via a public member function: 60 Private Data: 200
Public Data: 50
Q4
#include <iostream> using namespace std;
class Counter { private:
static int count;
public: Counter() { count++;
static void displayCount() {
cout << "Count of objects created: " << count << endl;
static void resetCount() { count = 0;
};
int Counter::count = 0;
int main() {
Counter::displayCount();
Counter obj1, obj2, obj3;
Counter::displayCount(); Counter::resetCount(); cout << "After resetting count:\n";
Counter::displayCount();
return 0;
Output:
Count of objects created: 0
Count of objects created: 3 After resetting count:
Count of objects created: 0
Assignment 3
Operator Overloading, Inheritance, Virtual Function Q1:
#include <iostream> using namespace std;
class Number { private: int num; public:
Number(int n) : num(n) {} bool operator--() { if (num <= 1) return false; for (int i = 2; i
<= num / 2; ++i) { if (num % i == 0) { return false;
return true;
int getNumber() const { return num;
};
int main() {
int number;
cout << "Enter a number to check if it's prime: "; cin >> number; Number obj(number);
operator
if (--obj) {
cout << number << " is a prime number!" << endl;
} else {
cout << number << " is not a prime number!" << endl;
return 0;
Output:
Enter a number to check if it's prime: 17 17 is a prime number!
Enter a number to check if it's prime: 8 8 is not a prime number!
Q2
#include <iostream> using namespace std; class Complex { private:
float real, imag;
public:
Complex(float r = 0, float i = 0) : real(r), imag(i) {}
// Member function to overload the + operator
Complex operator+(const Complex& other) {
return Complex(real + other.real, imag + other.imag);
// Friend function to overload the + operator friend Complex operator+(const Complex& c1,
const Complex& c2);
void display() {
cout << real << " + " << imag << "i" << endl;
}
};
// Friend function definition
Complex operator+(const Complex& c1, const Complex& c2)
return Complex(c1.real + c2.real, c1.imag + c2.imag);
int main() {
Complex c1(3, 5), c2(3, 8);
// Adding two complex numbers using the member function
Complex result1 = c1 + c2;
cout << "Result of addition using member function: "; result1.display();
// Adding two complex numbers using the friend function Complex result2 = operator+(c1, c2);
cout << "Result of addition using friend function: "; result2.display(); return 0;
Output
Result of addition using member function: 6 + 13i
Result of addition using friend function: 6 + 13i
Q3
#include <iostream> #include <string> using namespace std;
class Student { protected: int rollNumber; string name; string course;
public:
void input_student() { cout << "Enter Roll Number: "; cin >> rollNumber;
cin.ignore(); cout << "Enter Name: "; getline(cin, name); cout << "Enter Course: ";
getline(cin, course);
}
void display_student() const {
cout << "Roll Number: " << rollNumber << endl; cout << "Name: " << name << endl;
cout << "Course: " << course << endl;
};
// Derived class: Exam class Exam : public Student { private:
float mark1, mark2, mark3;
public:
void input_marks() {
cout << "Enter marks for subject 1: "; cin >> mark1;
cout << "Enter marks for subject 2: "; cin >> mark2;
cout << "Enter marks for subject 3: ";
cin >> mark3;
// Function to display results void display_result() const { display_student();
cout << "Marks in Subject 1: " << mark1 << endl; cout << "Marks in Subject 2: " << mark2
<< endl; cout << "Marks in Subject 3: " << mark3 << endl; cout << "Total Marks: " <<
(mark1 + mark2 + mark3) << endl;
cout << "Average Marks: " << (mark1 + mark2 + mark3) / 3.0 << endl;
};
int main() {
const int numStudents = 5;
Exam students[numStudents];
cout << "Enter details for " << numStudents << " students:" << endl;
for (int i = 0; i < numStudents; ++i) {
cout << "Student " << i + 1 << ":" << endl;
students[i].input_student(); students[i].input_marks();
cout << "\nResults of students:" << endl; for (int i = 0; i < numStudents; ++i) { cout <<
"Student " << i + 1 << ":" << endl; students[i].display_result();
return 0;
Output:
Student 1:
Enter Roll Number: 34
Enter Name: shafaq
Enter Course: Computer Science
Enter marks for subject 1: 80
Enter marks for subject 2: 90 Enter marks for subject 3: 75
...
Results of students:
Student 1:
Roll Number: 34
Name: shafaq
Course: Computer Science
Marks in Subject 1: 80
Marks in Subject 2: 90
Marks in Subject 3: 75
Total Marks: 245
Average Marks: 81.666%
Q4
#include <iostream> using namespace std;
class Polygon { public:
virtual void calculate_area() = 0; virtual ~Polygon() {}
};
class Rectangle : public Polygon { private:
double length, width;
public:
Rectangle(double l, double w) : length(l), width(w) {}
void calculate_area() override { double area = length * width; cout << "Rectangle Area: "
<< area << endl;
};
class Triangle : public Polygon { private:
double base, height; public:
Triangle(double b, double h) : base(b), height(h) {}
void calculate_area() override { double area = 0.5 * base * height; cout << "Triangle
Area: " << area << endl;
};
int main() {
Rectangle rect(20.0, 5.0);
Triangle tri(4.0, 3.0);
Polygon* polyPtr = nullptr;
polyPtr = ▭
cout << "Using base class pointer for Rectangle:" << endl; polyPtr->calculate_area();
polyPtr = &tri;
cout << "Using base class pointer for Triangle:" << endl; polyPtr->calculate_area();
return 0;
Output:
Using base class pointer for Rectangle:
Rectangle Area: 100
Using base class pointer for Triangle:
Triangle Area: 6
Assignment 4
File handling
Q1
#include <iostream>
#include <fstream> #include <string> using namespace std;
int main() {
string filename = "solution.txt"; string inputString; char ch; int charCount = 0;
ofstream outFile(filename); if (!outFile) {
cerr << "Error: Unable to open file for writing." << endl; return 1;
cout << "Enter a string to write to the file: "; getline(cin, inputString);
outFile << inputString; outFile.close();
cout << "String written to the file successfully." << endl;
ifstream inFile(filename); if (!inFile) {
cerr << "Error: Unable to open file for reading." << endl; return 1;
}
while (inFile.get(ch)) { charCount++;
inFile.close();
cout << "The number of characters in the file is: " << charCount << endl; return 0;
Output:
Enter a string to write to the file: Hello!
String written to the file successfully.
The number of characters in the file is: 12
Q2
#include <iostream>
#include <fstream> #include <string> using namespace std;
int main() {
string sourceFile, destinationFile; char ch;
cout << "Enter the name of the source file: "; cin >> sourceFile;
cout << "Enter the name of the destination file: "; cin >> destinationFile; ifstream
inFile(sourceFile, ios::in); if (!inFile) {
cerr << "Error: Unable to open source file for reading."
<< endl;
return 1;
ofstream outFile(destinationFile, ios::out); if (!outFile) {
cerr << "Error: Unable to open destination file for writing." << endl; inFile.close();
return 1;
while (inFile.get(ch)) { outFile.put(ch);
inFile.close(); outFile.close();
cout << "Content copied from " << sourceFile << " to " << destinationFile << " successfully." <<
endl;
return 0;
Output:
Enter the name of the source file: source.txt
Enter the name of the destination file: destination.txt
Content copied from source.txt to destination.txt successfully.
Q3
#include <iostream>
#include <fstream> #include <string> using namespace std;
class Student { private:
int roll;
string name; string courseName;
public:
// Function to input student details void getdata() { cout << "Enter Roll Number: "; cin
>> roll; cin.ignore(); cout << "Enter Name: "; getline(cin, name); cout << "Enter
Course Name: "; getline(cin, courseName);
// Function to display student details void putdata() const {
cout << "Roll Number: " << roll << endl; cout << "Name: " << name << endl; cout <<
"Course Name: " << courseName << endl;
// Function to write student details to file void writeToFile(ofstream& outFile) const {
outFile << roll << endl; outFile << name << endl; outFile << courseName << endl;
// Function to read student details from file void readFromFile(ifstream& inFile) {
inFile >> roll;
inFile.ignore(); // Clear input buffer getline(inFile, name); getline(inFile, courseName);
};
int main() {
const int numStudents = 2; Student students[numStudents]; string filename =
"students.txt";
// Write records to file ofstream outFile(filename); if (!outFile) {
cerr << "Error: Could not open file for writing." << endl; return 1;
cout << "Enter details for 5 students:" << endl; for (int i = 0; i < numStudents; ++i) { cout <<
"Student " << i + 1 << ":" << endl; students[i].getdata(); students[i].writeToFile(outFile);
outFile.close();
cout << "Records written to file successfully." << endl;
// Read records from file ifstream inFile(filename); if (!inFile) {
cerr << "Error: Could not open file for reading." << endl; return 1;
cout << "\nReading records from file:" << endl; for (int i = 0; i < numStudents; ++i) { cout
<< "Student " << i + 1 << ":" << endl; students[i].readFromFile(inFile);
students[i].putdata();
inFile.close();
return 0;
Intput
Enter details for 2 students:
Student 1:
Enter Roll Number: 34
Enter Name: shafaq
Enter Course Name: Computer Science Student 2:
Enter Roll Number: 10
Enter Name: Hamdan Enter Course Name: Mathematics Output:
Records written to file successfully.
Reading records from file:
Student 1:
Roll Number: 09
Name: Hunain Imran Course Name: Computer Science Student 2:
Roll Number: 10
Name: Hamdan
Course Name: Mathematics
Q4
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
using namespace std;
int main() {
string filename = "example.txt"; string wordToSearch = "Intelligent"; string currentWord;
int count = 0; ifstream inFile(filename); if (!inFile) {
cerr << "Error: Unable to open file." << endl; return 1;
while (inFile >> currentWord) {
currentWord.erase(remove_if(currentWord.begin(), currentWord.end(), ::ispunct),
currentWord.end()); if (currentWord == wordToSearch) count++;
}}
inFile.close(); if (count > 0) { cout << "The word \"" << wordToSearch << "\" was found "
<< count << " times in the file." << endl;
} else {
cout << "The word \"" << wordToSearch << "\" was not found in the file." << endl;
return 0;
Output:
The Intelligent agent acts intelligently. An intelligent system exhibits intelligent behavior.
The word "Intelligent" was found 4 times in the file.