include
include
class Student {
private:
int age;
float gpa;
public:
strcpy(name, n);
age = a;
gpa = g;
cout << "Constructor called for: " << name << endl;
~Student() {
cout << "Destructor called for: " << name << endl;
--count;
cout << "Name: " << name << ", Age: " << age << ", GPA: " << gpa << endl;
return count;
};
int Student::count = 0;
s.gpa = newGPA;
int main() {
updateGPA(*s1, 9.7);
s1->display();
// Display the count of students (static function)
delete s1;
cout << "Total students after deletion: " << Student::getCount() << endl;
return 0;
Output-
7) #include <iostream>
class Complex {
private:
double real;
double imag;
public:
// 1. Constructors
out << c.real << (c.imag >= 0 ? " + " : " - ") << abs(c.imag) << "i";
return out;
in >> c.real;
in >> c.imag;
return in;
};
int main() {
// Demonstrating addition
result = c1 + c2;
// Demonstrating subtraction
result = c1 - c2;
// Demonstrating multiplication
result = c1 * c2;
// Demonstrating division
result = c1 / c2;
return 0;
Output-
9) // Online C++ compiler to run C++ program online
#include <iostream>
class Base {
public:
void display() {
};
public:
void display() {
};
void print(double a) {
class Complex {
private:
public:
// Overload + operator
// Display function
cout << real << " + " << imag << "i" << endl;
};
int main() {
print(5);
print(3.14);
print("Hello");
Complex c3 = c1 + c2;
c3.display();
Base *basePtr;
Derived derivedObj;
basePtr = &derivedObj;
return 0;
}
10) #include <iostream>
#include <string>
#include <stdexcept>
public:
};
public:
};
public:
};
throw InvalidEmailException();
throw IncorrectPasswordException();
}
if (pin != correctPin) {
throw IncorrectPinException();
int main() {
try {
// Gmail login
gmailLogin(email, password);
int pin;
atmPinVerification(pin);
}
catch (const InvalidEmailException& e) {
catch (...) {
return 0;
}
11) #include <iostream>
class Stack {
private:
public:
Stack() : top(-1) {}
cout << "Stack Overflow! Cannot push " << element << endl;
return;
arr[++top] = element;
T pop() {
if (top < 0) {
}
return arr[top--];
T peek() {
if (top < 0) {
return arr[top];
bool isEmpty() {
void display() {
if (isEmpty()) {
return;
};
// Main function to demonstrate the stack for integers and characters
int main() {
// Stack of integers
Stack<int> intStack;
intStack.push(10);
intStack.push(20);
intStack.push(30);
intStack.display();
intStack.display();
// Stack of characters
Stack<char> charStack;
charStack.push('A');
charStack.push('B');
charStack.push('C');
charStack.display();
charStack.display();
return 0;
}
12) #include <iostream>
#include <map>
#include <string>
cout << "| " << entry.first << " | " << entry.second << " |\n";
int main() {
displayCurrencyChart(currencyMap);
string searchCountry;
cout << "\nEnter the name of the country to get its currency: ";
getline(cin, searchCountry);
auto it = currencyMap.find(searchCountry);
if (it != currencyMap.end()) {
cout << "Currency of " << searchCountry << " is: " << it->second << endl;
} else {
return 0;
}
13) ss#include <iostream>
#include <fstream>
#include <string>
class Student {
private:
string name;
int id;
public:
cout << "Name: " << name << ", ID: " << id << endl;
getline(in, name);
in >> id;
in.ignore();
};
int main() {
ofstream outFile("students.txt");
if (!outFile) {
return 1;
s1.writeToFile(outFile);
s2.writeToFile(outFile);
s3.writeToFile(outFile);
outFile.close();
ifstream inFile("students.txt");
if (!inFile) {
return 1;
Student students[3];
students[i].readFromFile(inFile);
inFile.close();
cout << "Student details from the file:" << endl;
student.display();
return 0;