Practical - 3 Experiment Number - 3.1: SUBJECT NAME-Object Oriented Programming Using C++ Subject Code-22Csh103
Practical - 3 Experiment Number - 3.1: SUBJECT NAME-Object Oriented Programming Using C++ Subject Code-22Csh103
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.
#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;
}
No errors encountered
OUTPUT
This program is written by Sudip.
Enter three Number: 7 2 3
7 is the Largest.
2 is the Smallest
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.
#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 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();
}
}
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.
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
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.
#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();
}
No error
OUTPUT
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.
#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();
}
No error
OUTPUT
• 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.