Shafaq Assingment of C++
Shafaq Assingment of C++
Date: 20-11-2024
Assignment 1
Q1.
int main() { int num1, num2, num3; cout << "Enter three numbers: "; cin >> num1 >> num2
>> num3;
cout << "The largest of the three numbers is: " << max << endl; return 0;
Output:
Q2.
#include <iostream>
Std::string depositorName;
Int accountNumber;
Std::string accountType;
Double balance;
Public:
// Constructor to assign initial values
BankAccount :
int num,
double initialBalance) :
depositorName(name),
accountNumber(num),
accountType(type),
balance(initialBalance) {}
If (amount > 0) {
Balance += amount;
Balance -= amount;
Std::cout << “Name: “ << depositorName << “\nAccount Number: “ << accountNumber <<
“\nAccount Type: “ << accountType << “\nBalance: “ << balance << std::endl;
};
Int main() {
Account.display();
Account.deposit(800);
Account.withdraw(400);
Account.display();
Return 0;
Output:
Name: shafaq
Balance: 1400
Name: Tom
Balance: 1500
Name: shafaq
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:
TimeAdder(int h1, int m1, int h2, int m2) : hour1(h1), minute1(m1), hour2(h2), minute2(m2) {}
Void input() {
Std::cout << “Enter the second time (hour and minute): “; Std::cin >> hour2;
Hour1 = h1;
Minute1 = m1;
Hour2 = h2;
Minute2 = m2;
else {
newMinute = totalMinutes;
} else {
newHour = totalHours;
Std::cout << “The result is: “ << newHour << “ hours and “ << newMinute << “ minutes” <<
std::endl;
};
Int main() {
return 0;
Output:
Q4:
#include <iostream>
Void getdata() {
Void show() {
Cout << real << “ + “ << img << “I” << endl;
};
Int main() {
C1.getdata();
C2.getdata();
C3.sum(c1, c2);
C3.show();
Return 0;
Output:
4 + 8i
Assignment 2
Q1
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 3: "; cin >> mark3;
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;
}
};
Student students[n];
cout << "\nEnter details for student " << i + 1 << ":\n"; students[i].getdata();
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
};
Complex result;
cout << "Complex Number: " << c.real << " + " << c.imag << "i" << endl;
int main() {
cout << "Enter real and imaginary part of first complex number: ";
cout << "Enter real and imaginary part of second complex number: ";
return 0;
Output:
Q3
int privateData;
public:
int publicData;
void displayData() {
cout << "Private Data: " << privateData << endl; cout << "Public Data: " << publicData <<
endl;
};
int main() {
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 private member 'privateData' via a public member function: 60 Private Data: 200
Public Data: 50
Q4
cout << "Count of objects created: " << count << endl;
};
int Counter::count = 0;
int main() {
Counter::displayCount();
Counter::displayCount();
return 0;
Output:
Assignment 3
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 main() {
int number;
cout << "Enter a number to check if it's prime: "; cin >> number; Number obj(number);
operator
if (--obj) {
} else {
cout << number << " is not a prime number!" << endl;
return 0;
Output:
Q2
public:
// Friend function to overload the + operator friend Complex operator+(const Complex& c1,
const Complex& c2);
void display() {
cout << real << " + " << imag << "i" << endl;
}
};
int main() {
// 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
Q3
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;
};
public:
void input_marks() {
cout << "Enter marks for subject 1: "; cin >> mark1;
cout << "Enter marks for subject 2: "; cin >> mark2;
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() {
Exam students[numStudents];
cout << "Enter details for " << numStudents << " students:" << 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:
...
Results of students:
Student 1:
Roll Number: 34
Name: shafaq
Marks in Subject 1: 80
Marks in Subject 2: 90
Marks in Subject 3: 75
Q4
};
public:
void calculate_area() override { double area = length * width; cout << "Rectangle Area: "
<< area << endl;
};
void calculate_area() override { double area = 0.5 * base * height; cout << "Triangle
Area: " << area << endl;
};
int main() {
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:
Triangle Area: 6
Assignment 4
File handling
Q1
#include <iostream>
int main() {
cerr << "Error: Unable to open file for writing." << endl; return 1;
cout << "Enter a string to write to the file: "; getline(cin, inputString);
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:
Q2
#include <iostream>
int main() {
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) {
<< endl;
return 1;
cerr << "Error: Unable to open destination file for writing." << endl; inFile.close();
return 1;
inFile.close(); outFile.close();
cout << "Content copied from " << sourceFile << " to " << destinationFile << " successfully." <<
endl;
return 0;
Output:
Q3
#include <iostream>
int roll;
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);
cout << "Roll Number: " << roll << endl; cout << "Name: " << name << endl; cout <<
"Course Name: " << courseName << endl;
};
int main() {
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();
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:
Student 1:
Roll Number: 09
Roll Number: 10
Name: Hamdan
Q4
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
int main() {
}}
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.