Guru Gobind Singh Indraprastha University: Institute of Innovation in Technology & Management
Guru Gobind Singh Indraprastha University: Institute of Innovation in Technology & Management
Guru Gobind Singh Indraprastha University: Institute of Innovation in Technology & Management
class D {
int money = 20;
friend void C(A, B);
};
int main() {
A obj1;
B obj2;
C(obj1, obj2);
return 0;
}
Output:
Q2. WAP to implement ‘Function Overloading’?
Code:
#include <iostream>
using namespace std;
int main() {
int a, b, c, d;
float x, y, z;
a = 10;
b = 20;
x = 5.5;
y = 6.7;
c = sum(a, b);
d = sum(a, b, x);
z = sum(x, y);
return 0;
}
int main() {
int result = add(3, 4);
std::cout << "Result: " << result << std::endl;
return 0;
}
class MyClass {
public:
MyClass(int x) {
data = x;
std::cout << "Parameterized constructor called\n";
}
~MyClass() {
std::cout << "Destructor called\n";
}
private:
int data;
};
int main() {
MyClass obj1(10);
MyClass obj2(obj1); // Copy constructor is called
return 0;
}
Q5. WAP to implement call by reference and return by
reference using class. [Hint. Assume necessary
functions]
Code:
#include <iostream>
// Call by reference
void increment(int& num) {
num++;
}
// Return by reference
int& getValue() {
return value;
}
};
int main() {
MyClass obj;
obj.value = 10;
int x = 5;
cout << "Before call by reference: " << x << endl;
obj.increment(x);
cout << "After call by reference: " << x << endl;
return 0;
}
class Base1 {
public:
Base1(int x) {
cout << "Base1 constructor called with x = " << x
<< endl;
}
};
class Base2 {
public:
Base2(int y) {
cout << "Base2 constructor called with y = " << y
<< endl;
}
};
int main() {
Derived obj(10, 20, 30);
return 0;
}
OUTPUT:
class Book {
public:
string title;
string author;
void display() {
cout << "Title: " << title << endl;
cout << "Author: " << author << endl;
}
};
class Library {
public:
vector<Book> books;
void displayBooks() {
for (Book book : books) {
book.display();
}
}
};
int main() {
Library library;
library.displayBooks();
return 0;
}
Q8 WAP to show swapping using template
function (Generic)
CODE:
#include <iostream>
using namespace std;
int main() {
int x = 5, y = 10;
float f1 = 1.5, f2 = 2.5;
char c1 = 'A', c2 = 'B';
// Swapping integers
swapValues(x, y);
// Swapping floats
swapValues(f1, f2);
// Swapping characters
swapValues(c1, c2);
return 0;
}
OUTPUT
Q9 WAP to implement ‘Exception Handling’
CODE:
#include <iostream>
int main() {
int x, y;
try {
divide(x, y);
} catch (const char* msg) {
cerr << "Error: " << msg << endl;
}
return 0;
}
OUTPUT:
Q10 WAP to read and write values through object using
file handling
CODE:
#include <iostream>
#include <fstream>
using namespace std;
class Student {
public:
int roll_no;
string name;
float marks;
void readData() {
cout << "Enter Roll No: ";
cin >> roll_no;
cout << "Enter Name: ";
cin.ignore(); // Ignore the newline character left by
previous input
getline(cin, name);
cout << "Enter Marks: ";
cin >> marks;
}
void displayData() {
cout << "Roll No: " << roll_no << endl;
cout << "Name: " << name << endl;
cout << "Marks: " << marks << endl;
}
};
int main() {
Student s;
return 0;
}
OUTPUT:
Q11Create a class employee which have name, age
and address of employee, include functions getdata()
and showdata(), getdata() takes the input from the
user, showdata() display the data in following format:
Name: Age: Address:
CODE:
#include <iostream>
#include <string>
class Employee {
public:
string name;
int age;
string address;
void getdata() {
cout << "Enter Name: ";
getline(cin, name);
cout << "Enter Age: ";
cin >> age;
cin.ignore(); // Ignore the newline character left by
previous input
cout << "Enter Address: ";
getline(cin, address);
}
void showdata() {
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
cout << "Address: " << address << endl;
}
};
int main() {
Employee emp;
emp.getdata();
emp.showdata();
return 0;
}
Q12 Write a class called CAccount which contains two
private data elements, an integer accountNumber and
a floating point accountBalance, and three member
functions: • A constructor that allows the user to set
initial values for accountNumber and accountBalance
and a default constructor that prompts for the input of
the values for the above data numbers. • A function
called inputTransaction, which reads a character value
for transactionType(‘D’ for deposit and ‘W’ for
withdrawal), and a floating point value for
transactionAmount, which updates accountBalance. A
function called printBalance, which prints on the screen
the accountNumber and accountBalance.
CODE:
#include <iostream>
class CAccount {
public:
CAccount(int accountNumber = 0, float
accountBalance = 0.0) {
if (accountNumber == 0 && accountBalance ==
0.0) {
cout << "Enter account number: ";
cin >> accountNumber;
cout << "Enter initial balance: ";
cin >> accountBalance;
}
this->accountNumber = accountNumber;
this->accountBalance = accountBalance;
}
void inputTransaction() {
char transactionType;
float transactionAmount;
if (transactionType == 'D') {
accountBalance += transactionAmount;
} else if (transactionType == 'W') {
if (transactionAmount > accountBalance) {
cout << "Insufficient balance!\n";
} else {
accountBalance -= transactionAmount;
}
} else {
cout << "Invalid transaction type!\n";
}
}
void printBalance() {
cout << "Account Number: " << accountNumber
<< endl;
cout << "Account Balance: " << accountBalance
<< endl;
}
private:
int accountNumber;
float accountBalance;
};
int main() {
CAccount account;
account.inputTransaction();
account.printBalance();
return 0;
}
OUTPUT:
Q13 Define a class Counter which contains an int
variable count defined as static and a static function
Display () to display the value of count. Whenever an
object of this class is created count is incremented by
1. Use this class in main to create multiple objects of
this class and display value of count each time
CODE:
#include <iostream>
class Counter {
public:
static int count;
Counter() {
count++;
}
int Counter::count = 0;
int main() {
Counter obj1, obj2, obj3;
Counter::Display();
Counter::Display();
Counter::Display();
return 0;
}
OUTPUT:
Q14 WAP to add and subtract two complex numbers
using classes
CODE:
#include <iostream>
class Complex {
public:
int real, imag;
Complex(int r = 0, int i = 0) {
real = r;
imag = i;
}
void print() {
cout << real << " + i" << imag << endl;
}
};
int main() {
Complex c1(10, 5), c2(2, 4);
Complex c3 = c1 + c2;
Complex c4 = c1 - c2;
class Complex {
public:
int real, imag;
Complex(int r = 0, int i = 0) {
real = r;
imag = i;
}
// Overloading + operator
Complex operator+(Complex const &obj) {
Complex res;
res.real = real + obj.real;
res.imag = imag + obj.imag;
return res;
}
void print() {
cout << real << " + i" << imag << endl;
}
};
int main() {
Complex c1(10, 5), c2(2, 4);
Complex c3 = c1 + c2; // Operator overloading is
used here
c3.print();
}
OUTPUT:
Q16 WAP to implement += and = operator
CODE:
#include <iostream>
class Complex {
public:
int real, imag;
Complex(int r = 0, int i = 0) {
real = r;
imag = i;
}
// Overloading += operator
Complex& operator+=(const Complex& obj) {
real += obj.real;
imag += obj.imag;
return *this;
}
// Overloading = operator
Complex& operator=(const Complex& obj) {
real = obj.real;
imag = obj.imag;
return *this;
}
void print() {
cout << real << " + i" << imag << endl;
}
};
int main() {
Complex c1(10, 5), c2(2, 4);
// Using += operator
c1 += c2;
c1.print();
// Using = operator
c1 = c2;
c1.print();
return 0;
}
OUTPUT:
Q17 Implement the following class hierarchy
considering appropriate data members and member
functions
CODE:
#include <iostream>
#include <string>
using namespace std;
public:
Student(string n, int id) : name(n), student_id(id) {}
void displayInfo() {
cout << "Student Name: " << name << endl;
cout << "Student ID: " << student_id << endl;
}
};
// Derived class: Test (inherits from Student)
class Test : virtual public Student {
protected:
int test_score;
public:
Test(string n, int id, int score) : Student(n, id),
test_score(score) {}
void displayTestScore() {
cout << "Test Score: " << test_score << endl;
}
};
public:
Sports(string n, int id, int score) : Student(n, id),
sports_score(score) {}
void displaySportsScore() {
cout << "Sports Score: " << sports_score <<
endl;
}
};
void displayPerformance() {
displayInfo();
displayTestScore();
displaySportsScore();
int total_score = test_score + sports_score;
cout << "Total Performance Score: " <<
total_score << endl;
}
};
int main() {
Performance student1("Alice", 101, 85, 90);
student1.displayPerformance();
return 0;
}
OUTPUT:
Q18 Implement the following hierarchy considering
appropriate data members and member functions(USE
VIRTUAL FUNCTION)
CODE :
#include <iostream>
#include <cmath>
using namespace std;
// Base class
class Shape {
public:
// Virtual function for calculating area
virtual double area() const = 0; // Pure virtual
function
};
int main() {
// Create objects of each shape
Shape* shapes[3];
shapes[0] = new Triangle(5.0, 10.0);
shapes[1] = new Rectangle(4.0, 6.0);
shapes[2] = new Circle(3.0);
return 0;
}
OUTPUT
Q19 WAP to convert meter to centimeter and vice
versa, using data conversions and operator overloading
CODE:
#include <iostream>
using namespace std;
class Meter {
private:
double meters;
public:
// Constructor to initialize meters
Meter(double m = 0) : meters(m) {}
class Centimeter {
private:
double centimeters;
public:
// Constructor to initialize centimeters
Centimeter(double cm = 0) : centimeters(cm) {}
int main() {
Meter m1(5); // 5 meters
Centimeter cm1;
return 0;
}
OUTPUT
Q20 WAP to count digits, alphabets and spaces, stored
in a text file using streams
CODE:
#include <iostream>
#include <fstream>
using namespace std;
int main() {
// Open the file for reading
ifstream file("input.txt");
return 0;
}
OUTPUT
File content
output