Harsh C++ File PDF
Harsh C++ File PDF
VISHWAVIDHYALAYA, INDORE
PRACTICAL-05
AIM:-
Write a program to define a class to represent a bank account,
including data members (Name of the depositor, Account number, Type of
account, Balance amount) and member functions to assign values, deposit
an amount, withdraw an amount, and display account details.
THEORY:-
To simulate a simple bank account system, we use a class in C++
containing relevant data members and member functions. The class
encapsulates the data and operations such as deposit, withdraw, and
display. This program helps understand basic concepts of object-oriented
programming like encapsulation, class, object, and member functions.
PROGRAM:-
#include <iostream>
#include <string>
using namespace std;
class BankAccount {
private:
string name;
string accountNumber;
public:
BankAccount() {
balance = 0.0;
}
balance += amount;
cout << "₹" << amount << " deposited successfully. New Balance: ₹" <<
balance << endl;
} else {
cout << "Deposit amount must be positive." << endl;
}
}
void withdraw(double amount) {
if (amount <= 0) {
cout << "Withdrawal amount must be positive." << endl;
} else if (amount > balance) {
cout << "Insufficient balance. Available Balance: ₹" << balance <<
endl;
} else {
balance -= amount;
cout << "₹" << amount << " withdrawn successfully. Remaining
Balance: ₹" << balance << endl;
}
}
void display() {
cout << "\n--- Account Details ---" << endl;
cout << "Name: " << name << endl;
int main() {
BankAccount acc;
acc.display();
acc.deposit(2500.00);
acc.withdraw(4000.00);
acc.display();
return 0;}
OUTPUT:-
PRACTICAL-06
AIM:- Write a program to create two classes DM and DB which store the
value of distances in meters/cen meters and feet/inches respec vely. Use a
friend func on to add objects of both classes and display the result.
PROGRAM:-
#include <iostream>
using namespace std;
class DB;
class DM {
private:
int meters;
int centimeters;
public:
DM(int m = 0, int cm = 0) {
meters = m;
centimeters = cm;
}
class DB {
private:
int feet;
int inches;
public:
DB(int ft = 0, int in = 0) {
feet = ft;
inches = in;
}
int main() {
DM metricDistance(3, 70);
DB imperialDistance(5, 8);
metricDistance.display();
imperialDistance.display();
return 0;
}
OUTPUT:-
PRACTICAL-07
AIM:- Write a program to create a class Student with data members:
name, roll number, marks of 3 subjects. Use a constructor to initialize the
values and display student details and total marks.
PROGRAM:-
#include <iostream>
#include <string>
using namespace std;
class Student {
private:
string name;
int rollNumber;
int marks[3];
public:
Student(string studentName, int roll, int mark1, int mark2, int mark3) {
name = studentName;
rollNumber = roll;
marks[0] = mark1;
marks[1] = mark2;
marks[2] = mark3
void display() {
int total = marks[0] + marks[1] + marks[2];
cout << "\n--- Student Details ---" << endl;
cout << "Name : " << name << endl;
cout << "Roll Number : " << rollNumber << endl;
cout << "Marks : " << marks[0] << ", " << marks[1] << ", " <<
marks[2] << endl;
cout << "Total Marks : " << total << endl;
}
};
int main() {
Student s1("Ravi Kumar", 101, 85, 90, 78);
s1.display();
return 0;
}
OUTPUT:-
PRACTICAL-08
AIM:- Write a program to create a class Complex to represent complex
numbers. Use constructor overloading to create multiple ways of initializing
complex numbers.
PROGRAM:-
#include <iostream>
using namespace std;
class Complex {
private:
float real;
float imag;
public:
Complex() {
real = 0;
imag = 0;
}
Complex(float r) {
real = r;
imag = 0;
}
Complex(float r, float i) {
real = r;
imag = i;
}
void display() const {
if (imag >= 0)
cout << real << " + " << imag << "i" << endl;
else
cout << real << " - " << -imag << "i" << endl;
}
};
int main() {
Complex c1;
Complex c2(5);
Complex c3(3.2, -4.5);
cout << "Complex number c1: ";
c1.display();
cout << "Complex number c2: ";
c2.display();
cout << "Complex number c3: ";
c3.display();
return 0;
OUTPUT:-
PRACTICAL-09
AIM:- Write a program to create a class StringJoin with a function that
joins two strings using operator overloading of the + operator.
PROGRAM:-
#include <iostream>
#include <string>
using namespace std;
class StringJoin {
private:
string str;
public:
StringJoin(string s = "") {
str = s;
}
return 0;
OUTPUT:-
PRACTICAL-10
AIM:- Write a program to create a class Bank that handles deposit and
withdrawal of money with balance checking.
THEORY:- This program shows how to use classes to perform deposit and
withdrawal operations. It includes balance checking during withdrawal.
PROGRAM:-
#include <iostream>
#include <string>
using namespace std;
class Bank {
private:
string accountHolder;
int accountNumber;
double balance;
public:
Bank(string name, int accNo, double initialBalance) {
accountHolder = name;
accountNumber = accNo;
if (initialBalance >= 0)
balance = initialBalance;
else {
cout << "Initial balance cannot be negative. Setting to 0.\n";
balance = 0;
}
}
void deposit(double amount) {
if (amount > 0) {
balance += amount;
cout << "₹" << amount << " deposited. New balance: ₹" << balance
<< endl;
} else {
cout << "Deposit amount must be positive.\n";
}
}
void withdraw(double amount) {
if (amount <= 0) {
cout << "Withdrawal amount must be positive.\n";
} else if (amount > balance) {
cout << "Insufficient balance! Withdrawal failed.\n";
} else {
balance -= amount;
cout << "₹" << amount << " withdrawn. Remaining balance: ₹" <<
balance << endl;
}
}
void display() const {
cout << "\n--- Account Details ---\n";
cout << "Account Holder : " << accountHolder << endl;
cout << "Account Number : " << accountNumber << endl;
cout << "Balance : ₹" << balance << endl;
}
};
int main() {
return 0;
OUTPUT:-
PRACTICAL-11
AIM:- Write a program to create a base class Employee and a derived class
Manager. Display the details of both using single inheritance.
THEORY:- Write a program to create a base class Employee and a derived class
Manager. Display the details of both using single inheritance.
PROGRAM:-
#include <iostream>
#include <string>
using namespace std;
class Employee {
protected:
string name;
int empID;
double salary;
public:
void getDetails(string n, int id, double sal) {
name = n;
empID = id;
salary = sal;
}
void displayDetails() const {
cout << "Employee Name : " << name << endl;
cout << "Employee ID : " << empID << endl;
cout << "Salary : ₹" << salary << endl;
}
};
class Manager : public Employee {
private:
string department;
public:
void setManagerDetails(string dept) {
department = dept;
}
}
};
int main() {
Manager mgr;
return 0;
}
OUTPUT:-