RITVIKOOPS

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 26

Delhi Technological University

(Formerly Delhi College of Engineering)


Shahbad Daulatpur, Bawana Road, Delhi-110042

DEPARTMENT OF COMPUTER SCIENCE AND


ENGINEERING

OBJECT ORIENTED PROGRAMMING (OOPS)


Subject Code: CO203

Submitted To: Submitted By:

Prof. Minni Jain Ritvik Vashishtha


Department of Computer Science B.Tech (III rd Sem)
And Engineering 2K22/CO/373
INDEX
S. No. Experiment Date Signature

1 Write a C++ program to print your personal details name, surname


(single character), total marks, gender(M/F), result(P/F) by taking
input from the user.
2 Create a class called 'Employee' that has “Empnumber‟ and
“Empname‟ as data members and member functions getdata( ) to
input data display() to output data. Write a main function to create an
array of ‟Employee‟ objects. Accept and print and accept the details
of at least 6 employees.
3 Write a C++ program to swap two number by both call by value and
call by reference mechanism, using two functions swap_value() and
swap_reference respectively , by getting the choice from the user and
executing the user’s choice by switch-case.
4 Write a C++ program to create a simple banking system in which the
initial balance and the rate of interest are read from the keyboard and
these values are initialized using the constructor. The destructor
member function is defined in this program to destroy the class
object created using constructor member function. This program
consists of following member functions:
i. Constructor to initialize the balance and rate of interest
ii. Deposit - To make deposit
iii. Withdraw – To with draw an amount
iv. Compound – To find compound interest
v. getBalance – To know the balance amount
vi. Menu – To display menu options
vii. Destructor

5 Write a program to accept five different numbers by creating a class


called friendfunc1 and friendfunc2 taking 2 and 3 arguments
respectively and calculate the average of these numbers by passing
object of the class to friend function.
6 Write a program to accept the student detail such as name and 3
different marks by get_data() method and display the name and
average of marks using display() method. Define a friend class for
calculating the average of marks using the method mark_avg().
7 Write a C++ program to perform different arithmetic operation such
as addition, subtraction, division, modulus and multiplication using
inline function.
8 WAP to return absolute value of variable types integer and float
using function overloading .
9 WAP to perform string operations using operator overloading in C++
i. = String Copy
10 Consider a class network of figure given below. The class master
derives information from both account and admin classes which in
turn derived derive information from the class person. Define all the
four classes and write a program to create, update and display the
information contained in master objects. Also demonstrate the use
of different access specifiers by means of member variables and
member functions.
LAB EXPERIMENT: 1
Student Information and ResultsProgram
AIM: Write a C++ program to print your personal details name, surname(single
character), total marks, gender(M/F), result(P/F) by taking input from the user.

Theory:
The "Student Information and Results Program in C++" is a versatile tool for collecting
and presenting personal data. It facilitates the entry of a student's name, single-character
surname, total marks, gender, and result. Employing character arrays for names and
accommodating
single-character surnames, the program efficiently manages input and ensures accurate
display of this information. An educational resource, it serves as a practical
demonstration of C++ programming principles. It underscores the significance of
selecting appropriate data types and employing user-friendly input techniques, making it
a valuable reference for beginners learning C++ and highlighting best practices for data
handling and output.

Code:
#include <iostream>
using namespace std;
int main() {
char name[100];
char surname;
double totalMarks;
char gender;
char result;
cout << "Enter your name: "; cin >> name;
cout << "Enter your surname (single character): ";
cin >> surname;
cout << "Enter your total marks: "; cin >> totalMarks;
cout << "Enter your gender (M/F): "; cin >> gender;
cout << "Enter your result (P/F): "; cin >> result;
cout << "Student Information:" << endl;
cout << "Name: " << name << endl;
cout << "Surname: " << surname << endl;
cout << "Total Marks: " << totalMarks << endl;
cout << "Gender: " << gender << endl;
cout << "Result: " << result << endl;
return 0;
}

OUTPUT:
LAB EXPERIMENT: 2
Employee Information Management Program
AIM: Create a class called 'Employee' that has “Empnumber‟ and “Empname‟ as data
members and member functions getdata( ) to input data display() to output data. Write a
main function to create an array of ‟Employee‟ objects. Accept and print and accept the
details of at least 6 employees.

Theory:
The "Employee Information Management Program" in C++ defines an 'Employee' class
with data members for employee number and name. It allows users to input and display
details for at least six employees. This program demonstrates objectoriented
programming principles, ensuring data integrity and organized data management. It
offers a structured approach for handling employee information efficiently.

Code:
#include<iostream>
#include<string>
using namespace std;
class Employee {
private:
int empNumber;
string empName;
public:
void getData() {
cout << "Enter Employee Number: ";
cin >> empNumber;
cout << "Enter Employee Name: ";
cin.ignore();
getline(cin, empName);
}
void display() {
cout << "Employee Number: " << empNumber << endl;
cout << "Employee Name: " << empName << endl;
}
};
int main() {
Employee employees[6];
for (int i = 0; i < 6; i++) {
cout << "Enter details for Employee " << i + 1 << ":" << endl; employees[i].getData();
}
cout << "\nEmployee Information:\n";
for (int i = 0; i < 6; i++) {
cout << "Employee " << i + 1 << " Details:\n";
employees[i].display(); cout << endl;
}
return 0;
}

OUTPUT:
LAB EXPERIMENT: 3
Number Swapping Program with Call by Value , Call by Reference in
C++
AIM: Write a C++ program to swap two number by both call by value and call by
reference mechanism, using two functions swap_value() and swap_reference
respectively , by getting the choice from the user and executing the user’s choice by
switch-case.

Theory:
The "Number Swapping Program" in C++ provides two mechanisms for swapping two
numbers: call by value and call by reference. Users input two numbers and choose the
swapping method using a switch case. The 'swap_value()' function exchanges numbers
by copying their values, while 'swap_reference()' swaps them directly using references.
This program demonstrates the difference between call by value and call by
reference and illustrates the power of C++'s pass-by-reference feature in modifying
variables.

Code:
#include<iostream>
using namespace std;
void swap_value(int a, int b)
{ int temp = a;
a = b;
b = temp;
}
void swap_reference(int &a, int &b)
{ int temp = a;
a = b;
b = temp;
}
int main() {
int num1, num2, choice;
cout << "Enter two numbers: ";
cin >> num1 >> num2;
cout << "Choose swapping mechanism:\n";
cout << "1. Call by Value\n";
cout << "2. Call by Reference\n";
cin >> choice;
switch (choice) {
case 1: swap_value(num1, num2); break;
case 2: swap_reference(num1, num2); break;
default: cout << "Invalid choice!" << endl; return 1;
}
cout << "After swapping: " << num1 << " " << num2 << endl; return 0;
}

OUTPUT:
LAB EXPERIMENT: 4
Banking System
AIM: Write a C++ program to create a simple banking system in which the initial
balance and the rate of interest are read from the keyboard and these values are
initialized using the constructor. The destructor member function is defined in this
program to destroy the class object created using constructor member function. This
program consists of following member functions:
i. Constructor to initialize the balance and rate of interest
ii. Deposit - To make deposit
iii. Withdraw – To with draw an amount
iv. Compound – To find compound interest
v. getBalance – To know the balance amount
vi. Menu – To display menu options
vii. Destructor

Theory:

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

class BankingSystem {
private:
float balance;
float rate;

public:
BankingSystem(float initialBalance, float initialRate) {
balance = initialBalance;
rate = initialRate;
}

~BankingSystem() {
cout << "Destroying the object..." << endl;
}

void deposit(float amount) {


balance += amount;
cout << "Amount deposited successfully." << endl;
}
void withdraw(float amount) {
if (balance >= amount) {
balance -= amount;
cout << "Amount withdrawn successfully." << endl;
} else {
cout << "Insufficient balance." << endl;
}
}
void compound(int time) {
float compound_interest = balance * pow((1 + rate / 100), time);
float interest = compound_interest - balance;
balance = compound_interest;
cout << "Compound interest after " << time << " years: " << interest << endl;
}
float getBalance() {
return balance;
}
void menu() {
int choice, time;
float amount;
do {
cout << "\nBanking System Menu" << endl;
cout << "1. Deposit" << endl;
cout << "2. Withdraw" << endl;
cout << "3. Compound Interest" << endl;
cout << "4. Check Balance" << endl;
cout << "5. Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Enter amount to deposit: ";
cin >> amount;
deposit(amount);
break;
case 2:
cout << "Enter amount to withdraw: ";
cin >> amount;
withdraw(amount);
break;
case 3:
cout << "Enter the time period in years: ";
cin >> time;
compound(time);
break;
case 4:
cout << "Balance: " << getBalance() << endl;
break;
case 5:
cout << "Exiting..." << endl;
break;
default:
cout << "Invalid choice. Please try again." << endl;
}
} while (choice != 5);
}
};
int main() {
float initialBalance, initialRate;
cout << "Enter initial balance: ";
cin >> initialBalance;
cout << "Enter rate of interest: ";
cin >> initialRate;
BankingSystem account(initialBalance, initialRate);
account.menu();
return 0;
}

OUTPUT:
LAB EXPERIMENT: 5
Average Calculation Program Using Friend Functions
AIM: Write a program to accept five different numbers by creating a class called
friendfunc1 and friendfunc2 taking 2 and 3 arguments respectively and calculate the
average of these numbers by passing object of the class to friend function.

Theory:
The "Average Calculation Program Using Friend Functions" in C++ demonstrates the
use of friend functions to calculate the averages of a specified set of numbers. The class
FriendFunc encapsulates five numbers, and two friend functions, friendFunc1 and
friendFunc2, are used to calculate averages using either two or three of these numbers.
This program showcases the concept of friend functions, which can access private
members of a class without being a member of that class. It's a practical example of
C++'s object-oriented capabilities and encapsulation principles.

Code:
#include<iostream>
using namespace std;
class friendfunc2;
class friendfunc1{
public:
int a,b,c;
friendfunc1(int a1,int b1,int c1){
a=a1;
b=b1; c=c1;
}
friend void average(friendfunc1 f1,friendfunc2 f2);
};
class friendfunc2{
public:
int d,e;
friendfunc2(int d1,int e1){
d=d1;
e=e1;
}
friend void average(friendfunc1 f1,friendfunc2 f2);
};
void average(friendfunc1 f1,friendfunc2 f2){
cout<<(f1.a+f1.b+f1.c+f2.d+f2.e)/5<<endl;
}
int main(){
friendfunc1 a1(1,3,60);
friendfunc2 a2(7,13);
average(a1,a2);
return 0;
}
OUTPUT:
LAB EXPERIMENT: 6
Average Marks Using Friend Function
AIM: Write a program to accept the student detail such as name and 3 different marks
by get_data() method and display the name and average of marks using display()
method. Define a friend class for calculating the average of marks using the method
mark_avg().

Theory:
n C++, a friend class is a class that is granted access to the private and protected
members of another class. This is useful when you want a certain class to have access to
the private or protected members of another class for specific operations.
In the given program, we have two classes, Student and MarksAverage. The
MarksAverage class is declared as a friend of the Student class. This allows the
MarksAverage class to access the private members of the Student class, specifically the
marks array, to calculate the average of the marks.

Code:
#include <iostream>
using namespace std;
class MarksAverage; // Forward declaration of the friend class
class Student {
private:
string name;
int marks[3];
public:
void get_data() {
cout << "Enter student name: ";
cin >> name;
for (int i = 0; i < 3; i++) {
cout << "Enter mark " << i + 1 << ": ";
cin >> marks[i];
}
}
void display() {
cout << "Student Name: " << name << endl;
}
friend class MarksAverage;
};
class MarksAverage {
public:
static void mark_avg(Student &s) {
int sum = 0;
for (int i = 0; i < 3; i++) {
sum += s.marks[i];
}
float average = sum / 3.0;
cout << "Average marks: " << average << endl;
}
};
int main() {
Student student;
student.get_data();
student.display();
MarksAverage::mark_avg(student);
return 0;
}
OUTPUT:
LAB EXPERIMENT: 7
Program for Arithmetic Operation with Inline Functions.
AIM: Write a C++ program to perform different arithmetic operation such as addition,
subtraction, division, modulus and multiplication using inline function.

Theory:
Inline functions are a feature in C++ that instructs the compiler to insert the complete
body of the function wherever that function is used in the code.
Advantages of Inline Functions:

1) Speed of Execution: The execution speed of a program increases as it does not require
function calling overhead.

2) Efficient Code Generation: It can generate efficient code.


3) Readability: The readability of the program increases.
4) Overhead Savings: It saves the overhead of variables push/pop on the stack, while
function calling. It also saves the overhead of a return call from a function.
Disadvantages of Inline Functions:

1) Increased Size: The size of the executable file increases and more memory is required
as the body of inline function is inserted in the place of function call. It may increase
function size so that it may not fit on the cache, causing lots of cache misses.

2) Overhead on Register Variable Resource Utilization: After in-lining function if


variables number which are going to use register increases then they may create
overhead on registervariable resource utilization.

Code:
#include <iostream>
using namespace std;
inline int add(int a, int b)
{return a + b;
}
inline int subtract(int a, int b)
{return a - b;
}
inline int multiply(int a, int b)
{return a * b;
}
inline double divide(int a, int b)
{if (b != 0) return (double)a / b;
else
return 0;
}
inline int modulus1(int a, int b)
{if (b != 0) return a % b;
else return 0;
}
int main()
{int x, y;
cin >> x >> y;
cout << "Addition: " << add(x, y) << endl;
cout << "Subtraction: " << subtract(x, y) << endl;
cout << "Multiplication: " << multiply(x, y) << endl;
cout << "Division: " << divide(x, y) << endl;
cout << "Modulus: " << modulus1(x, y) << endl; return 0;

OUTPUT:
LAB EXPERIMENT: 8
Function Overloading
AIM: WAP to return absolute value of variable types integer and float using function
overloading .

Theory: In this program, two functions named absolute are defined. One function is
overloaded to handle integers, and the other function is overloaded to handle floating-point
numbers. The function absolute for integers returns the absolute value of the given integer,
while the function absolute for floats returns the absolute value of the given float.

Code:
#include <iostream>
using namespace std;

// Function to return the absolute value of an integer


int absolute(int num) {
if (num < 0) {
return -num;
} else {
return num;
}
}

// Function to return the absolute value of a float


float absolute(float num) {
if (num < 0.0) {
return -num;
} else {
return num;
}
}
int main() {
int intNum;
float floatNum;

// Input an integer and a float


cout << "Enter an integer: ";
cin >> intNum;
cout << "Absolute value of the integer: " << absolute(intNum) << endl;

cout << "Enter a float: ";


cin >> floatNum;
cout << "Absolute value of the float: " << absolute(floatNum) << endl;

return 0;
}
OUTPUT:
LAB EXPERIMENT: 9
Operator Overloading for String Operations in C++
AIM: WAP to perform string operations using operator overloading in C++
i. = String Copy
ii. ==,<,> Equality
iii. + Concatenation

Theory: Operator overloading in C++ allows custom definitions for operators like '+', '==',
'>', and '<'. In the provided code, the MyString class overloads these operators to perform
string concatenation and comparison. This enables intuitive and expressive string operations
by providing a natural syntax. The operators are implemented using member functions
within the class, making it possible to use them like built-in operators, enhancing code
readability and reusability.

Code:
#include <iostream>
using namespace std;
class mystring
{
public:
string s1;
mystring(string s1)
{
this->s1 = s1;
}
mystring operator+(mystring s2)
{
return s1 + s2.s1;
}void operator=(mystring s2)
{
s1 = s2.s1;
}
bool operator==(mystring s2)
{
return s1 == s2.s1;
}
void display()
{
cout << s1 << endl;
}
};
int main()
{
mystring s3("RO");
s3.display();
mystring s4("HAN");
s4.display();
mystring s5 = s3;
s5.display();
bool res = s5 == s3;
cout << res << endl;
mystring s6 = s5 + s4;
s6.display();
return 0;
}

OUTPUT:
LAB EXPERIMENT: 10
Inheritence
AIM: Consider a class network of figure given below. The class master derives information
from both account and admin classes which in turn derived derive information from the class
person. Define all the four classes and write a program to create, update and display the
information contained in master objects. Also demonstrate the use of different access
specifiers by means of member variables and member functions.

Theory: Inheritance is a fundamental concept in object-oriented programming (OOP) that


allows a class (derived or child class) to inherit properties and behavior from another class
(base or parent class). In the given code, the Account and Admin classes derive from the
Person class, and the Master class derives from both the Account and Admin classes.

Code:
#include <iostream>
using namespace std;

// Function to return the absolute value of an integer


int absolute(int num) {
if (num < 0) {
return -num;
} else {
return num;
}
}

// Function to return the absolute value of a float


float absolute(float num) {
if (num < 0.0) {
return -num;
} else {
return num;
}
}

int main() {
int intNum;
float floatNum;

// Input an integer and a float


cout << "Enter an integer: ";
cin >> intNum;
cout << "Absolute value of the integer: " << absolute(intNum) << endl;

cout << "Enter a float: ";


cin >> floatNum;
cout << "Absolute value of the float: " << absolute(floatNum) << endl;

return 0;
}
OUTPUT:

You might also like