You are on page 1of 15

PRACTICAL - 3

EXPERIMENT NUMBER – 3.1


STUDENT’S NAME – Pragnya Errolla
STUDENT’S UID – 22BAI71029
CLASS AND GROUP – 22AML-109(B)
SEMESTER - 02

AIM OF THE EXPERIMENT –

Write a program to find the largest & smallest of three numbers. (Use inline function
MAX and MIN).

FLOWCHART/ ALGORITHM

Step 1: Start
Step 2: Declare inline functions Max() and Min() with three parameters of int type.
Step 3: In the main(), input three numbers.
Step 4: Call the Max() function and assign it to a variable ‘large’.
Step 5: Similarly, call the Min() function and assign it to a variable ‘small’.
Step 6: Now, define the function Max() and check for the greatest of three numbers.
Step 7: Similarly, define the Min() function and check for the smallest of three number
Step 8: Print the largest and smallest number.
Step 9: Stop.

SUBJECT NAME-Object Oriented Programming using C++ SUBJECT CODE-22CSH103


PROGRAM CODE

#include<iostream>
using namespace std;
inline int Max(int, int, int);
inline int Min(int, int, int);
int main()
{
int a, b, c, large, small;
cout<<”This program is written by Sudip”<<endl;
cout<<"Enter three numbers: ";
cin>>a>>b>>c;
large = Max(a,b,c);
small = Min(a,b,c);
cout<<large<<" is the largest."<<endl;
cout<<small<<" is the smallest."<<endl;
}

int Max(int x, int y, int z)


{
if(x>y&&x>z)
return x;
else
if(y>x&&y>z)
return y;
else
return z;
}

int Min(int x, int y, int z)


{
if(x<y&&x<z)
return x;
if(y<x&&y<z)
return y;
else
return z;

SUBJECT NAME-Object Oriented Programming using C++ SUBJECT CODE-22CSH103


ERRORS ENCOUNTERED DURING PROGRAM’S EXECUTION
(Kindly jot down the compile time errors encountered)

No errors encountered

PROGRAMS’ EXPLANATION (in brief)


In this program, we declare and define two inline functions Max() and Min(). Then, we
input three numbers and check for the largest and smallest number by calling these
functions in the main() function.

OUTPUT
This program is written by Sudip.
Enter three Number: 7 2 3
7 is the Largest.
2 is the Smallest

SUBJECT NAME-Object Oriented Programming using C++ SUBJECT CODE-22CSH103


EXPERIMENT NUMBER – 3.2

AIM OF THE EXPERIMENT –

A dining hall can accommodate only 50 guests. Create a class to store seat number
(Generated Automatically) and name of the guests who are seated on first come first
seated basis. Define functions to display name of all guests along with seat number.
Write a program to show the working of this class using the concept of static data
member and static function

FLOWCHART/ ALGORITHM

Step 1: Start
Step 2: Define a class Guest and declare variables as private. Declare member
functions for inputting and displaying. Define the static member function
within the class.
Step 3: Input the maximum seating capacity, name.
Step 4: Increment the static variable and assign it to another variable.
Step 5: Inside the main function, create an array of objects and input the names of the
upto the maximum seating capacity that is allowed.
Step 6: Display the name along with the seat number.
Step 7: Display that the seats are full when the maximum seating capacity is fulfilled.
Step 8: Stop.

SUBJECT NAME-Object Oriented Programming using C++ SUBJECT CODE-22CSH103


PROGRAM CODE

#include<iostream>
using namespace std;
class Guest
{
int seatno;
char name[100];
static int count, m;
public:
static void seat()
{
if(count >= m)
cout<<"\nSeat capacity full!\n";
}
void input();
int maxcapacity();
void display();
};

int Guest::maxcapacity()
{
cout<<"Enter the maximum seating capacity: ";
cin>>m;
return m;
}

void Guest::input()
{
Cout<<
cout<<"Enter the name: ";
cin>>name;
count++;
seatno = count;
}

void Guest::display()
{
cout<<"\nName: "<<name<<endl;
cout<<"Seat no. "<<seatno<<endl;
SUBJECT NAME-Object Oriented Programming using C++ SUBJECT CODE-22CSH103
}

int Guest::count=0; int


Guest::m=0;

int main()
{
int i, max, c;
Guest g[50], m;
max = m.maxcapacity();
for(i=1; i<=max; i++)
{
g[i].input();
}
Guest::seat();
for(i=1; i<=max; i++)
{
g[i].display();
}
}

SUBJECT NAME-Object Oriented Programming using C++ SUBJECT CODE-22CSH103


ERRORS ENCOUNTERED DURING PROGRAM’S EXECUTION
(Kindly jot down the compile time errors encountered)

At first, I tried defining the static member function, seat(), outside the class. But it was
showing some errors. So, I defined the function inside the class. Then I got the correct
output without any errors.

PROGRAMS’ EXPLANATION (in brief)


In this program, we declare and define a Class Guest. Inside the class we declare
variables name, seatno, static variables count and m as private. We declare functions
input(), display(), maxcapacity(). We define a static function seat() within the class.
Then, we input the maximum seating capacity in the maxcapacity() function. We,
input the names of the guests in the input() function. Also we increment the static
variable count once and assign it to a variable seatno. Within, the main() function, we
create an array of object for the class Guest. Now, with the help of loop, we input the
names of the guests and display the names along with their seat number. After
maximum seating capacity is reached, we call the static function and print that the
seats are full.

SUBJECT NAME-Object Oriented Programming using C++ SUBJECT CODE-22CSH103


OUTPUT

Enter the maximum seating capacity: 5


Enter the name: Sudip
Enter the name: Mohit
Enter the name: Pranay
Enter the name: Ayushi
Enter the name: Lucky

Seat capacity full:

Name: Sudip
Seat No.: 01
Name: Mohit
Seat No.: 02
Name: Pranay
Seat No.: 03
Name: Ayushi
Seat No.: 04
Name: Lucky
Seat No.: 05

SUBJECT NAME-Object Oriented Programming using C++ SUBJECT CODE-22CSH103


EXPERIMENT NUMBER – 3.3

AIM OF THE EXPERIMENT –

WAP to swap private data members of classes named as class_1, class_2 using friend
function.

FLOWCHART/ ALGORITHM

Step 1: Start
Step 2: Forward declare the class i.e., class_2.
Step 3: Define another class i.e., class_1. Declare a variable. Declare a default
constructor and initialize the variable. Define another member function,
show() to display the value of the initialized variable and later to display the
swapped value.
Step 4: Declare a friend function swap() with parameters as
(class_1 num1,class_2 num2)
Step 5: Follow similar procedures while defining class_2 as stated in steps 3 and 4.
Step 6: Now, define the function swap() outside the class and swap the values by
using a third variable.
Step 7: Inside the main() function, display the values before and after swapping by
calling the function swap().
Step 8: Stop.

SUBJECT NAME-Object Oriented Programming using C++ SUBJECT CODE-22CSH103


PROGRAM CODE

#include<iostream>
using namespace std;
class class_2;
class class_1
{
private:
int num1;
public:
class_1()
{
num1 = 10;
}
void show()
{
cout<<"\n Value of Class 1: "<<num1;
}
friend void swap(class_1 num1, class_2 num2);
};
class class_2
{
private:
int num2;
public:
class_2()
{
num2 = 20;
}
void show()
{
cout<<"\n Value of Class 2: "<<num2;
}
friend void swap(class_1 num1, class_2 num2);
};
void swap(class_1 n1, class_2 n2)
{
int n3;
n3 = n1.num1;
n1.num1 = n2.num2;
n2.num2 = n3;
SUBJECT NAME-Object Oriented Programming using C++ SUBJECT CODE-22CSH103
}

int main()
{
class_1 a;
class_2 b;
cout<<”This program is created by Nishant Raj”;
cout<< "Values before Swapping: ";
a.show();
b.show();
swap(a,b);
cout<< "\n\nValues after Swapping: ";
a.show();
b.show();
}

ERRORS ENCOUNTERED DURING PROGRAM’S EXECUTION


(Kindly jot down the compile time errors encountered)

No error

PROGRAMS’ EXPLANATION (in brief)


In this program, we swap the values of the class using the concept of friend function
and constructors. First, we initialize a variable of class_1 using default constructor.
Next, we declare a friend function swap() in class_1. Similarly, we initialize a variable
of class_2. Again, we declare a friend function swap() in class_2. Then, we swap the
values of both the classes in the swap() function using a third variable. Then, we
print the values of the classes before and after swapping

OUTPUT

This program is created By Nishant Raj.


Value before swapping:
Value of class 1: 23
Value of class 2: 41
Value after Swapping:
Value of class 1: 41
Value of class 2: 23
SUBJECT NAME-Object Oriented Programming using C++ SUBJECT CODE-22CSH103
EXPERIMENT NUMBER – 3.4

AIM OF THE EXPERIMENT –

WAP to create a class complex to represent complex numbers. The complex class
should use a function to add two complex numberswhich are passed as arguments.
The function should return an object of type complex representing the sum of two
complex numbers.

FLOWCHART/ ALGORITHM

Step 1: Start.
Step 2: Declare all the necessary variables.
Step 3: Create a class complex to store real and imaginary part of complex number.
Step 4: Pass 2 complex numbers using two different objects to function set ().
Step 5: Create object C3 to store the sum which is obtained from function sum().
Step 6: Print the two complex numbers using function disp().
Step 7: Print the sum using store in C3 using function disp().
Step 8: Stop.

SUBJECT NAME-Object Oriented Programming using C++ SUBJECT CODE-22CSH103


PROGRAM CODE

#include<iostream>
using namespace std;
class complex
{
private:
float r;
float i;
public:
void set(float real, float img)
{
r = real;
i = img;
}
complex sum(complex c)
{
complex t;
t.r = r + c.r;
t.i = i + c.i;
return t;
}
void disp()
{
if (i == -1)
cout<< r << " + -i" <<endl;
else
if (i == 1)
cout<< r << " + i" <<endl;
else
if (i == 0)
cout<< r <<endl;
else
cout<< r << " + " <<i<< "i" <<endl;
}
};

int main()
{
complex c1, c2, c3;
c1.set(2.5, 3.5);
SUBJECT NAME-Object Oriented Programming using C++ SUBJECT CODE-22CSH103
c2.set(1.5, 5.5);
c3 = c1.sum(c2);
cout<<”This Program is created by Sudip”;
cout<<"Complex Number 1 = ";
c1.disp();
cout<<"Complex Number 2 = ";
c2.disp();
cout<<"Complex Number 3 = ";
c3.disp();
}

ERRORS ENCOUNTERED DURING PROGRAM’S EXECUTION


(Kindly jot down the compile time errors encountered)

No error

PROGRAMS’ EXPLANATION (in brief)


In this program, we have created a class named complex and after that we have taken
two float type variables i and r and then in the main function we have taken some
values and displayed the result.

OUTPUT

This program is created By Sudip.


Complex Number 1: 2.5+3.5i
Complex Number 2: 1.5+5.5i
Complex Number 3: 4+9i

SUBJECT NAME-Object Oriented Programming using C++ SUBJECT CODE-22CSH103


LEARNING OUTCOMES

• Understand the concepts of object-oriented programming including


programming process and compilation process.

• Apply different techniques to decompose a problem and programmed a solution


with its sub modules.

• Analyze and explain the behavior of simple programs involving the programming
addressed in the course.

• Implement and evaluate the programs using the syntax and semantics of object-
oriented programming.

• Design the solution of real-world problems in order to determine that the


program performs as expected.

EVALUATION COLUMN (To be filled by concerned faculty only)

Sr. No. Parameters Maximum Marks


Marks Obtained
1. Worksheet Completion including writing 12
learning objective/ Outcome
2. Post-Lab Quiz Result 8

3. Student engagement in Simulation/ 10


Performance/ Pre-Lab Questions
4. Total Marks 30

SUBJECT NAME-Object Oriented Programming using C++ SUBJECT CODE-22CSH103

You might also like