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

UI23CS10_OOT(LAB3)

Download as rtf, pdf, or txt
Download as rtf, pdf, or txt
Download as rtf, pdf, or txt
You are on page 1/ 26

ASSAIGNMENT 3

NAME :HARSHA VARDHAN


ROLL NO:UI23CS10

Question1 : Create a BankAccount class with attributes accountNumber, balance,


and customer name

a. Use a static member to keep track of the total number of accounts

created.

b. Implement constructors, deposit, and withdrawal functions.

CODE :

#include <iostream>

#include <string>

using namespace std;

class BankAccount {

private:

string accountNumber;
double balance;

string customerName;

public:

static int totalAccounts;

BankAccount(string accNum, string name, double initialBalance = 0.0) {

accountNumber = accNum;

customerName = name;

balance = initialBalance;

totalAccounts++;

void deposit(double amount) {

if (amount > 0) {

balance += amount;

cout << "Deposited " << amount << ". New balance: " << balance <<
endl;

} else {

cout << "Deposit amount must be positive." << endl;

}
}

void withdrawal(double amount) {

if (amount > 0) {

if (amount <= balance) {

balance -= amount;

cout << "Withdrew " << amount << ". New balance: " << balance
<< endl;

} else {

cout << "Insufficient funds." << endl;

} else {

cout << "Withdrawal amount must be positive." << endl;

static int getTotalAccounts() {

return totalAccounts;

};
int BankAccount::totalAccounts = 0;

int main() {

BankAccount account1("12345", "Alice", 1000.0);

BankAccount account2("67890", "Bob", 1500.0);

account1.deposit(500.0);

account1.withdrawal(200.0);

account2.deposit(1000.0);

account2.withdrawal(500.0);

cout << "Total accounts created: " << BankAccount::getTotalAccounts() <<


endl;

return 0;

}
OUTPUT :

Question 2 :

Create a Student class with name, rollNumber, and marks attributes.

a. Implement constructor, destructor, and static member functions to

count students and display the highest mark.

b. Create objects and display the details of students.

CODE:

#include <iostream>

#include <string>

using namespace std;

class Student {

private:

string name;

int rollNumber;
double marks;

public:

static int totalStudents;

static double highestMarks;

Student(string studentName, int studentRollNumber, double studentMarks) {

name = studentName;

rollNumber = studentRollNumber;

marks = studentMarks;

totalStudents++;

if (marks > highestMarks) {

highestMarks = marks;

~Student() {

totalStudents--;

static int getTotalStudents() {

return totalStudents;
}

static double getHighestMarks() {

return highestMarks;

void displayDetails() const {

cout << "Name: " << name << endl;

cout << "Roll Number: " << rollNumber << endl;

cout << "Marks: " << marks << endl;

};

int Student::totalStudents = 0;

double Student::highestMarks = 0.0;

int main() {

Student student1("Alice", 101, 85.5);

Student student2("Bob", 102, 92.0);

Student student3("Charlie", 103, 78.0);

cout << "Student 1 Details:" << endl;


student1.displayDetails();

cout << endl;

cout << "Student 2 Details:" << endl;

student2.displayDetails();

cout << endl;

cout << "Student 3 Details:" << endl;

student3.displayDetails();

cout << endl;

cout << "Total Students: " << Student::getTotalStudents() << endl;

cout << "Highest Marks: " << Student::getHighestMarks() << endl;

return 0;

OUTPUT :
QUESTION 3:Define a class Car with attributes like brand, model, and price.
Implement a

constructor to initialize the object and a destructor to display a message

when an object is destroyed.

CODE:

#include <iostream>

#include <string>
using namespace std;

class Car {

private:

string brand;

string model;

double price;

public:

Car(string carBrand, string carModel, double carPrice) {

brand = carBrand;

model = carModel;

price = carPrice;

~Car() {

cout << "Car object with brand " << brand << " and model " << model << "
is destroyed." << endl;

void displayDetails() const {

cout << "Brand: " << brand << endl;


cout << "Model: " << model << endl;

cout << "Price: $" << price << endl;

};

int main() {

Car car1("Toyota", "Corolla", 20000);

car1.displayDetails();

Car car2("Tesla", "Model S", 80000);

car2.displayDetails();

return 0;

OUTPUT:
Question4 :

Create a class Rectangle with attributes length and breadth. Use a

parameterized constructor to initialize the values and calculate the area.

CODE :

#include <iostream>

using namespace std;

class Rectangle {

private:

double length;

double breadth;

public:
Rectangle(double len, double br) {

length = len;

breadth = br;

double calculateArea() const {

return length * breadth;

void displayDetails() const {

cout << "Length: " << length << endl;

cout << "Breadth: " << breadth << endl;

cout << "Area: " << calculateArea() << endl;

};

int main() {

Rectangle rect1(10.5, 5.0);


rect1.displayDetails();

return 0;

OUTPUT :

Question 5 :

Define a class Book with attributes title and price. Implement a copy

constructor to create a duplicate object and display its details.

CODE :

#include <iostream>

#include <string>

using namespace std;

class Book {
private:

string title;

double price;

public:

Book(string bookTitle, double bookPrice) {

title = bookTitle;

price = bookPrice;

Book(const Book &other) {

title = other.title;

price = other.price;

void displayDetails() const {

cout << "Title: " << title << endl;

cout << "Price: " << price << endl;

};

int main() {
Book book1("C++ Programming", 29.99);

cout << "Original Book Details:" << endl;

book1.displayDetails();

cout << endl;

Book book2 = book1;

cout << "Copied Book Details:" << endl;

book2.displayDetails();

return 0;

OUTPUT:

Question 6:

Write a program using class to process Shopping List for a departmental


store. The list includes details such as the Code no and price of each item and

perform the operations like adding, deleting items to the list and printing the

total value of an order.

CODE :

#include <iostream>

#include <vector>

#include <algorithm>

using namespace std;

class Item {

private:

int code;

double price;

public:

Item(int itemCode, double itemPrice) {

code = itemCode;

price = itemPrice;

int getCode() const {


return code;

double getPrice() const {

return price;

void displayItem() const {

cout << "Item Code: " << code << ", Price: " << price << endl;

};

class ShoppingList {

private:

vector<Item> items;

public:

void addItem(int code, double price) {

Item newItem(code, price);

items.push_back(newItem);

cout << "Item added successfully!" << endl;

}
void deleteItem(int code) {

auto it = find_if(items.begin(), items.end(), [code](const Item& item) {

return item.getCode() == code;

});

if (it != items.end()) {

items.erase(it);

cout << "Item deleted successfully!" << endl;

} else {

cout << "Item with code " << code << " not found." << endl;

void printTotalValue() {

double total = 0.0;

for (const Item& item : items) {

total += item.getPrice();

cout << "Total value of the order: $" << total << endl;

}
void printShoppingList() {

if (items.empty()) {

cout << "Shopping list is empty." << endl;

} else {

cout << "Shopping List: " << endl;

for (const Item& item : items) {

item.displayItem();

};

int main() {

ShoppingList list;

int choice, code;

double price;

do {

cout << "\nDepartmental Store - Shopping List" << endl;

cout << "1. Add Item" << endl;

cout << "2. Delete Item" << endl;

cout << "3. Print Shopping List" << endl;


cout << "4. Print Total Value" << endl;

cout << "5. Exit" << endl;

cout << "Enter your choice: ";

cin >> choice;

switch (choice) {

case 1:

cout << "Enter Item Code: ";

cin >> code;

cout << "Enter Item Price: ";

cin >> price;

list.addItem(code, price);

break;

case 2:

cout << "Enter Item Code to delete: ";

cin >> code;

list.deleteItem(code);

break;

case 3:

list.printShoppingList();
break;

case 4:

list.printTotalValue();

break;

case 5:

cout << "Exiting program." << endl;

break;

default:

cout << "Invalid choice! Please try again." << endl;

} while (choice != 5);

return 0;

OUTPUT:
Question 7:

Create a class Employee containing employee id, name, Basic, DA, TA, net

salary member. Write function to read data, print data, and compute net

salary of each employee (DA=52%, of basic, TA=30% of gross salary) of each


employee.

CODE :

#include <iostream>

#include <string>

using namespace std;

class Employee {

private:

int id;

string name;

double basic, DA, TA, netSalary;

public:

void readData() {

cout << "Enter Employee ID: ";

cin >> id;

cout << "Enter Employee Name: ";

cin.ignore();

getline(cin, name);

cout << "Enter Basic Salary: ";

cin >> basic;


}

void computeSalary() {

DA = 0.52 * basic;

TA = 0.30 * (basic + DA);

netSalary = basic + DA + TA;

void printData() {

cout << "Employee ID: " << id << endl;

cout << "Employee Name: " << name << endl;

cout << "Basic Salary: " << basic << endl;

cout << "DA: " << DA << endl;

cout << "TA: " << TA << endl;

cout << "Net Salary: " << netSalary << endl;

};

int main() {

Employee emp;

emp.readData();

emp.computeSalary();
emp.printData();

return 0;

OUTPUT:

You might also like