0% found this document useful (0 votes)
2 views24 pages

Harsh C++ File PDF

Uploaded by

info.sharma051
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views24 pages

Harsh C++ File PDF

Uploaded by

info.sharma051
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

SHRI VAISHNAV VIDHYAPEETH

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;

SECTION-M HARSH 24100BTAIMLM17289


SHRI VAISHNAV VIDHYAPEETH
VISHWAVIDHYALAYA, INDORE
string accountType;
double balance;

public:
BankAccount() {
balance = 0.0;
}

void assignValues(string accName, string accNumber, string accType,


double initialBalance) {
name = accName;
accountNumber = accNumber;
accountType = accType;
if (initialBalance >= 0) {
balance = initialBalance;
} else {
cout << "Initial balance cannot be negative." << endl;
balance = 0.0;
}
}

void deposit(double amount) {


if (amount > 0) {

SECTION-M HARSH 24100BTAIMLM17289


SHRI VAISHNAV VIDHYAPEETH
VISHWAVIDHYALAYA, INDORE

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;

SECTION-M HARSH 24100BTAIMLM17289


SHRI VAISHNAV VIDHYAPEETH
VISHWAVIDHYALAYA, INDORE

cout << "Account Number: " << accountNumber << endl;

cout << "Account Type: " << accountType << endl;


cout << "Balance: ₹" << balance << endl;
}
};

int main() {
BankAccount acc;

acc.assignValues("Alice Sharma", "AC12345678", "Savings", 10000.00);

acc.display();

acc.deposit(2500.00);

acc.withdraw(4000.00);

acc.display();

return 0;}

SECTION-M HARSH 24100BTAIMLM17289


SHRI VAISHNAV VIDHYAPEETH
VISHWAVIDHYALAYA, INDORE

OUTPUT:-

SECTION-M HARSH 24100BTAIMLM17289


SHRI VAISHNAV VIDHYAPEETH
VISHWAVIDHYALAYA, INDORE

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.

THEORY:- In this program, we deal with distance conversion and


addition using classes and friend functions.Class DM stores distances in
meters and centimeters. Class DB stores distances in feet and inches. To
add values of both classes, We convert all distances to a common unit (e.g.,
meters). Then we use a friend function that can access private members of
both classes to perform the addition. The result is then displayed in meters
and centimeters or any desired unit.

PROGRAM:-
#include <iostream>
using namespace std;

class DB;

class DM {
private:
int meters;
int centimeters;

public:
DM(int m = 0, int cm = 0) {

SECTION-M HARSH 24100BTAIMLM17289


SHRI VAISHNAV VIDHYAPEETH
VISHWAVIDHYALAYA, INDORE

meters = m;
centimeters = cm;
}

void display() const {


cout << "Distance in Metric: " << meters << " meters and " <<
centimeters << " centimeters." << endl;
}

friend DM addDistances(const DM&, const DB&);


};

class DB {
private:
int feet;
int inches;

public:
DB(int ft = 0, int in = 0) {
feet = ft;
inches = in;
}

SECTION-M HARSH 24100BTAIMLM17289


SHRI VAISHNAV VIDHYAPEETH
VISHWAVIDHYALAYA, INDORE

void display() const {


cout << "Distance in Imperial: " << feet << " feet and " << inches << "
inches." << endl;
}

friend DM addDistances(const DM&, const DB&);


};

DM addDistances(const DM& d1, const DB& d2) {


const float meterPerInch = 0.0254;
const float meterPerFoot = 0.3048;

float totalMetersFromDB = d2.feet * meterPerFoot + d2.inches *


meterPerInch;

float totalMetersDM = d1.meters + d1.centimeters / 100.0;


float sumMeters = totalMetersDM + totalMetersFromDB;
int metersResult = (int)sumMeters;
int centimetersResult = (int)((sumMeters - metersResult) * 100);

return DM(metersResult, centimetersResult);

SECTION-M HARSH 24100BTAIMLM17289


SHRI VAISHNAV VIDHYAPEETH
VISHWAVIDHYALAYA, INDORE

int main() {
DM metricDistance(3, 70);
DB imperialDistance(5, 8);
metricDistance.display();
imperialDistance.display();

DM result = addDistances(metricDistance, imperialDistance);


cout << "\nAfter adding both distances:" << endl;
result.display();

return 0;
}

OUTPUT:-

SECTION-M HARSH 24100BTAIMLM17289


SHRI VAISHNAV VIDHYAPEETH
VISHWAVIDHYALAYA, INDORE

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.

THEORY:- In this program, we learn how to use constructors in C++ for


automatic initialization of object data. The Student class includes: Private
data members: name, roll number, marks of 3 subjects. A parameterized
constructor to ini alize these values at object creation. A member function
to display student details and compute the total marks. This helps
understand the concept of encapsulation, constructors, and class objects.

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) {

SECTION-M HARSH 24100BTAIMLM17289


SHRI VAISHNAV VIDHYAPEETH
VISHWAVIDHYALAYA, INDORE

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;
}

SECTION-M HARSH 24100BTAIMLM17289


SHRI VAISHNAV VIDHYAPEETH
VISHWAVIDHYALAYA, INDORE

OUTPUT:-

SECTION-M HARSH 24100BTAIMLM17289


SHRI VAISHNAV VIDHYAPEETH
VISHWAVIDHYALAYA, INDORE

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.

THEORY:- In this program, we demonstrate the concept of constructor


overloading in C++. Constructor overloading means creating multiple
constructors in the same class with different parameter lists, allowing
different ways to initialize an object. We define a class Complex having two
data members: real and imaginary parts. Three constructors: Default
constructor (0 + 0i), One-parameter constructor (real only), Two-
parameter constructor (real and imaginary).A display function to show
complex numbers in a + bi format. This helps understand the concept of
polymorphism through constructor overloading.

PROGRAM:-
#include <iostream>
using namespace std;

class Complex {
private:
float real;
float imag;

public:
Complex() {

SECTION-M HARSH 24100BTAIMLM17289


SHRI VAISHNAV VIDHYAPEETH
VISHWAVIDHYALAYA, INDORE

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;

SECTION-M HARSH 24100BTAIMLM17289


SHRI VAISHNAV VIDHYAPEETH
VISHWAVIDHYALAYA, INDORE

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:-

SECTION-M HARSH 24100BTAIMLM17289


SHRI VAISHNAV VIDHYAPEETH
VISHWAVIDHYALAYA, INDORE

PRACTICAL-09
AIM:- Write a program to create a class StringJoin with a function that
joins two strings using operator overloading of the + operator.

THEORY:- In this program, we implement operator overloading in C++


to perform string concatenation using the + operator.C++ allows us to
overload built-in operators so that they can work with user-defined types
(classes). We define a class StringJoin that stores a string, and we overload
the + operator to join two StringJoin objects.

PROGRAM:-
#include <iostream>
#include <string>
using namespace std;

class StringJoin {
private:
string str;

public:
StringJoin(string s = "") {
str = s;
}

SECTION-M HARSH 24100BTAIMLM17289


SHRI VAISHNAV VIDHYAPEETH
VISHWAVIDHYALAYA, INDORE

StringJoin operator+(const StringJoin& other) {


return StringJoin(str + other.str);
}
void display() const {
cout << str << endl;
}
};
int main() {
StringJoin s1("Hello, ");
StringJoin s2("World!");
StringJoin result = s1 + s2;
cout << "Joined string: ";
result.display();

return 0;

OUTPUT:-

SECTION-M HARSH 24100BTAIMLM17289


SHRI VAISHNAV VIDHYAPEETH
VISHWAVIDHYALAYA, INDORE

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)

SECTION-M HARSH 24100BTAIMLM17289


SHRI VAISHNAV VIDHYAPEETH
VISHWAVIDHYALAYA, INDORE

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;

SECTION-M HARSH 24100BTAIMLM17289


SHRI VAISHNAV VIDHYAPEETH
VISHWAVIDHYALAYA, INDORE

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() {

Bank myAccount("Priya Sharma", 12345678, 5000.00);


myAccount.display();
myAccount.deposit(1500);
myAccount.withdraw(2000);
myAccount.withdraw(6000);
myAccount.display();

return 0;

SECTION-M HARSH 24100BTAIMLM17289


SHRI VAISHNAV VIDHYAPEETH
VISHWAVIDHYALAYA, INDORE

OUTPUT:-

SECTION-M HARSH 24100BTAIMLM17289


SHRI VAISHNAV VIDHYAPEETH
VISHWAVIDHYALAYA, INDORE

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;

SECTION-M HARSH 24100BTAIMLM17289


SHRI VAISHNAV VIDHYAPEETH
VISHWAVIDHYALAYA, INDORE

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;
}

void displayManager() const {


displayDetails(); // call base class function
cout << "Department : " << department << endl;

SECTION-M HARSH 24100BTAIMLM17289


SHRI VAISHNAV VIDHYAPEETH
VISHWAVIDHYALAYA, INDORE

}
};

int main() {
Manager mgr;

mgr.getDetails("Amit Verma", 1001, 75000.0); // Base class data


mgr.setManagerDetails("Finance"); // Derived class data

cout << "--- Manager Details ---" << endl;


mgr.displayManager();

return 0;
}

OUTPUT:-

SECTION-M HARSH 24100BTAIMLM17289

You might also like