0% found this document useful (0 votes)
49 views

Final Lab File

Uploaded by

monishrai1801
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)
49 views

Final Lab File

Uploaded by

monishrai1801
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/ 22

MONISH RAI, 23/CS/266, CSE-4, G-2

DELHI TECHNOLOGICAL UNIVERSITY


(Formerly Delhi College of Engineering)
Shahbad Daulatpur, Bawana Road, Delhi- 110042

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Object Oriented Programming Lab

Subject Code : CS-203

SUBMITTED TO: SUBMITTED BY:


Dr. Sanjay Kumar NAME : MONISH RAI
ROLLNO. : 23/CS/266
BATCH : CSE-4
SEMESTER: 3
MONISH RAI, 23/CS/266, CSE-4, G-2
MONISH RAI, 23/CS/266, CSE-4, G-2

INDEX

S No. Topic Date Sign

1 Employee Record Management System 13/8/2024

2 Write a basic C++ program of a class 14/8/2024

3 Write a program to illustrate different types of 21/8/2024


constructors

4 Write a C++ program to swap two number 28/8/2024

5 Write a C++ program to create a simple banking 4/9/2024


system

6 Write a program to demonstrate friend function. 5/9/2024

7 Write a program to perform string operations using 5/9/2024


operator overloading

8 Write a program to create a template. 14/9/2024

9 Write a program using multilevel inheritance. 14/9/2024

10 Write a C++ program to explain virtual function. 14/9/2024


MONISH RAI, 23/CS/266, CSE-4, G-2
MONISH RAI, 23/CS/266, CSE-4, G-2

Program 1 : Create a program that manages employee records using


structures in C.

Code:
#include <stdio.h>
#include <string.h>

#define MAX_EMPLOYEES 10
#define NAME_LENGTH 50
#define DESIGNATION_LENGTH 50
typedef struct {
int EmpNo;
char name[NAME_LENGTH];
float paygrade;
char Designation[DESIGNATION_LENGTH];
} Employee;
Employee employees[MAX_EMPLOYEES];
int employeeCount = 0;
void inputEmployeeDetails() {
if (employeeCount < MAX_EMPLOYEES) {
printf("Enter Employee Number: ");
scanf("%d", &employees[employeeCount].EmpNo);
printf("Enter Name: ");
scanf("%s", employees[employeeCount].name);
printf("Enter Paygrade: ");
scanf("%f", &employees[employeeCount].paygrade);
printf("Enter Designation: ");
scanf("%s", employees[employeeCount].Designation);
employeeCount++;
} else {
printf("Employee array is full.\n");
}
}
void displayEmployeeDetails() {
for (int i = 0; i < employeeCount; i++) {
printf("Employee Number: %d\n", employees[i].EmpNo);
printf("Name: %s\n", employees[i].name);
printf("Paygrade: %.2f\n", employees[i].paygrade);
printf("Designation: %s\n", employees[i].Designation);
printf("\n");
}
}
void calculateTax() {
for (int i = 0; i < employeeCount; i++) {
float tax;
if (employees[i].paygrade <= 25000) {
tax = 0;
} else if (employees[i].paygrade <= 50000) {
tax = 0.05 * employees[i].paygrade;
} else if (employees[i].paygrade <= 100000) {
tax = 0.10 * employees[i].paygrade;
} else if (employees[i].paygrade <= 150000) {
tax = 0.15 * employees[i].paygrade;
} else {
tax = 0.30 * employees[i].paygrade;
}
printf("Employee Number: %d, Tax: %.2f\n", employees[i].EmpNo, tax);
MONISH RAI, 23/CS/266, CSE-4, G-2

}
}
void searchEmployee() {
int empNo;
printf("Enter Employee Number to search: ");
scanf("%d", &empNo);
for (int i = 0; i < employeeCount; i++) {
if (employees[i].EmpNo == empNo) {
printf("Employee Number: %d\n", employees[i].EmpNo);
printf("Name: %s\n", employees[i].name);
printf("Paygrade: %.2f\n", employees[i].paygrade);
printf("Designation: %s\n", employees[i].Designation);
return;
}
}
printf("Employee with number %d not found.\n", empNo);
}
int main() {
int choice;
while (1) {
printf("Menu:\n");
printf("1. Input Employee Details\n");
printf("2. Display All Employee Details\n");
printf("3. Calculate Tax\n");
printf("4. Search Employee\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
inputEmployeeDetails();
break;
case 2:
displayEmployeeDetails();
break;
case 3:
calculateTax();
break;
case 4:
searchEmployee();
break;
case 5:
return 0;
default:
printf("Invalid choice. Please try again.\n");
}
}
return 0;
}
MONISH RAI, 23/CS/266, CSE-4, G-2

Output:
MONISH RAI, 23/CS/266, CSE-4, G-2

Program 2 : Write a C++ program of a class.

Code:
# include <iostream>
using namespace std;

class basic {
int val;
public:
void update_val(int data) {
val = data;
}
void get_val(){
cout<<val<<endl;
}
};
int main()
{
basic num;
num.update_val(2);
num.get_val();
return 0;
}

Output:
MONISH RAI, 23/CS/266, CSE-4, G-2

Program 3 : Write a program to illustrate different types of constructors

Code:
#include <iostream>
using namespace std;

class Number
{
int a;
public:
// Default Constructor
Number()
{
this->a = 0;
}
// Parameterized Constructor
Number(int a)
{
this->a = a;
}
// Copy Constructor
Number(Number &obj)
{
cout << "Copy constructor is called" << endl;
this->a = obj.a;
}
void display()
{
cout << "Number is --> " << a << endl;
}
};
int main()
{
Number x, y, z(45);
x.display();
y.display();
z.display();
Number z1(z);
z1.display();

return 0;
}

Output:
MONISH RAI, 23/CS/266, CSE-4, G-2

Program 4 : Write a C++ program to swap two number

Code:
#include <iostream>
using namespace std;

void swap_value(int obj1, int obj2)


{
int temp = obj1;
obj1 = obj2;
obj2 = temp;

cout << "After swapping :-" << endl;


cout << "a = " << obj1 << endl;
cout << "b = " << obj2 << endl;
}

void swap_reference(int &obj1, int &obj2)


{
int temp = obj1;
obj1 = obj2;
obj2 = temp;

cout << "After swapping :-" << endl;


cout << "a = " << obj1 << endl;
cout << "b = " << obj2 << endl;
}

int main()
{
int a, b;
a = 3;
b = 5;

int choice;
cout << "Enter 1 for Swap by Value or 2 for swap by reference" << endl;
cin >> choice;
cout << "Before swapping :-" << endl;
cout << "a = " << a << endl;
cout << "b = " << b << endl;

switch (choice)
{
case 1:
swap_value(a, b);
break;

case 2:
swap_reference(a, b);
break;
}
return 0;
}
MONISH RAI, 23/CS/266, CSE-4, G-2

Output:
MONISH RAI, 23/CS/266, CSE-4, G-2

Program 5 : Write a C++ program to create a simple banking system.

Code:
#include <iostream>
#include <cmath>

using namespace std;

class BankAccount {
private:
double balance;
double interestRate;

public:
BankAccount(double initialBalance, double rate) : balance(initialBalance),
interestRate(rate) {
cout << "Account created with a balance of $" << balance << " and an interest rate of "
<< interestRate << "%\n";
}

~BankAccount() {
cout << "Account closed. Final balance: $" << balance << "\n";
}

void deposit(double amount) {


if (amount > 0) {
balance += amount;
cout << "Successfully deposited $" << amount << ". New balance: $" << balance <<
"\n";
} else {
cout << "Deposit amount must be positive!\n";
}
}

void withdraw(double amount) {


if (amount > 0 && amount <= balance) {
balance -= amount;
cout << "Successfully withdrew $" << amount << ". New balance: $" << balance <<
"\n";
} else if (amount > balance) {
cout << "Insufficient funds! Withdrawal failed.\n";
} else {
cout << "Withdrawal amount must be positive!\n";
}
}

void applyCompoundInterest(int years) {


double futureBalance = balance * pow((1 + interestRate / 100), years);
cout << "After " << years << " years, your balance with compound interest will be: $" <<
futureBalance << "\n";
balance = futureBalance;
}

double getCurrentBalance() const {


return balance;
}

void showMenu() const {


cout << "\n--- Bank Account Menu ---\n";
cout << "1. Deposit Funds\n";
MONISH RAI, 23/CS/266, CSE-4, G-2

cout << "2. Withdraw Funds\n";


cout << "3. Calculate Compound Interest\n";
cout << "4. View Current Balance\n";
cout << "5. Exit\n";
}
};

int main() {
double initialBalance, interestRate;
int userChoice;
double amount;
int years;

cout << "Welcome to the Simple Banking System!\n";


cout << "Please enter your initial balance: $";
cin >> initialBalance;
cout << "Please enter the annual interest rate (in %): ";
cin >> interestRate;

BankAccount myAccount(initialBalance, interestRate);

do {
myAccount.showMenu();
cout << "Please select an option (1-5): ";
cin >> userChoice;

switch (userChoice) {
case 1:
cout << "Enter the amount to deposit: $";
cin >> amount;
myAccount.deposit(amount);
break;
case 2:
cout << "Enter the amount to withdraw: $";
cin >> amount;
myAccount.withdraw(amount);
break;
case 3:
cout << "Enter the number of years to calculate compound interest: ";
cin >> years;
myAccount.applyCompoundInterest(years);
break;
case 4:
cout << "Your current balance is: $" << myAccount.getCurrentBalance() << "\n";
break;
case 5:
cout << "Thank you for using the Simple Banking System. Goodbye!\n";
break;
default:
cout << "Invalid option! Please choose a number between 1 and 5.\n";
}
} while (userChoice != 5);

return 0;
}
MONISH RAI, 23/CS/266, CSE-4, G-2

Output:
MONISH RAI, 23/CS/266, CSE-4, G-2

Program 6 : Write a C++ program to demonstrate friend function.

Code:
#include <iostream>
using namespace std;

class friendfunc2;

class friendfunc1 {
int num1, num2;
public:
friendfunc1(int a, int b) : num1(a), num2(b) {}
friend float calculateAverage(friendfunc1, friendfunc2);
};

class friendfunc2 {
int num3, num4, num5;
public:
friendfunc2(int c, int d, int e) : num3(c), num4(d), num5(e) {}
friend float calculateAverage(friendfunc1, friendfunc2);
};

float calculateAverage(friendfunc1 f1, friendfunc2 f2) {


int total = f1.num1 + f1.num2 + f2.num3 + f2.num4 + f2.num5;
return total / 5.0;
}

int main() {
int a, b, c, d, e;
cout << "Enter five numbers: ";
cin >> a >> b >> c >> d >> e;

friendfunc1 obj1(a, b);


friendfunc2 obj2(c, d, e);
cout << "Average: " << calculateAverage(obj1, obj2) << endl;
return 0;
}

Output:
MONISH RAI, 23/CS/266, CSE-4, G-2

Program 7 : Write a C++ program to perform string operations using operator


overloading

Code:
#include <iostream>
#include <cstring>
using namespace std;

class MyString
{
char *str;

public:
MyString(const char *s = "")
{
str = new char[strlen(s) + 1];
strcpy(str, s);
}

MyString &operator=(const MyString &s)


{
if (this != &s)
{
delete[] str;
str = new char[strlen(s.str) + 1];
strcpy(str, s.str);
}
return *this;
}

bool operator==(const MyString &s) const


{
return strcmp(str, s.str) == 0;
}

MyString operator+(const MyString &s) const


{
MyString temp;
delete[] temp.str;
temp.str = new char[strlen(str) + strlen(s.str) + 1];
strcpy(temp.str, str);
strcat(temp.str, s.str);
return temp;
}

void display() const


{
cout << str << endl;
}

~MyString()
{
delete[] str;
}
};

int main()
MONISH RAI, 23/CS/266, CSE-4, G-2

{
MyString s1("Hello"), s2(" World"), s3;

s3 = s1;
cout << "After Copy: ";
s3.display();

if (s1 == s3)
{
cout << "Strings are equal" << endl;
}
else
{
cout << "Strings are not equal" << endl;
}

MyString s4 = s1 + s2;
cout << "After Concatenation: ";
s4.display();

return 0;
}

Output:
MONISH RAI, 23/CS/266, CSE-4, G-2

Program 8 : Write a program to create a template.

Code:
#include <iostream>
using namespace std;

template <typename T>


class Pair
{
private:
T first, second;

public:
Pair(T a, T b)
{
first = a;
second = b;
}

T get_max()
{
return (first > second) ? first : second;
}
};

int main()
{
Pair<int> intPair(10, 20);
cout << "Max (int): " << intPair.get_max() << endl;

Pair<float> floatPair(15.5, 10.3);


cout << "Max (float): " << floatPair.get_max() << endl;

Pair<char> charPair('a', 'z');


cout << "Max (char): " << charPair.get_max() << endl;

return 0;
}

Output:
MONISH RAI, 23/CS/266, CSE-4, G-2

Program 9 : Write a program using multilevel inheritance.

Code:
#include <iostream>
using namespace std;

class A
{
protected:
int a1;

public:
void set_a(int a)
{
a1 = a;
}

void display_a()
{
cout << "Class A, a1: " << a1 << endl;
}
};

class B : public A
{
protected:
int b1;

public:
void set_b(int b)
{
b1 = b;
}

void display_b()
{
cout << "Class B, b1: " << b1 << endl;
}
};

class C : public B
{
private:
int c1;

public:
void set_c(int c)
{
c1 = c;
}

void display_c()
{
cout << "Class C, c1: " << c1 << endl;
}
};
MONISH RAI, 23/CS/266, CSE-4, G-2

int main()
{
C obj;
obj.set_a(10);
obj.set_b(20);
obj.set_c(30);

obj.display_a();
obj.display_b();
obj.display_c();

return 0;
}

Output:
MONISH RAI, 23/CS/266, CSE-4, G-2

Program 10 : Write a program to explain virtual function.

Code:
#include <iostream>
using namespace std;

class Polygon
{
public:
virtual double area() = 0;
virtual ~Polygon() {}
};

class Rectangle : public Polygon


{
private:
double length, breadth;

public:
Rectangle(double l, double b) : length(l), breadth(b) {}

double area() override


{
return length * breadth;
}
};

class Triangle : public Polygon


{
private:
double base, height;

public:
Triangle(double b, double h) : base(b), height(h) {}

double area() override


{
return 0.5 * base * height;
}
};

void displayArea(Polygon *obj)


{
cout << "Area: " << obj->area() << endl;
}

int main()
{
Polygon *poly1 = new Rectangle(5.0, 3.0);
Polygon *poly2 = new Triangle(4.0, 6.0);
displayArea(poly1);
displayArea(poly2);

delete poly1;
delete poly2;
return 0;
}
MONISH RAI, 23/CS/266, CSE-4, G-2

Output:

You might also like