0% found this document useful (0 votes)
35 views87 pages

Oops Concept in C++

The document contains the table of contents for experiments on control structures and looping structures in C++. It includes 8 experiments covering topics like simple if/else, nested if, switch case, while loop, do-while loop, for loop, goto statement, and one-dimensional arrays. Each experiment includes the aim, algorithm, program code, and sample output.

Uploaded by

bala sekaran
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views87 pages

Oops Concept in C++

The document contains the table of contents for experiments on control structures and looping structures in C++. It includes 8 experiments covering topics like simple if/else, nested if, switch case, while loop, do-while loop, for loop, goto statement, and one-dimensional arrays. Each experiment includes the aim, algorithm, program code, and sample output.

Uploaded by

bala sekaran
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 87

TABLE OF CONTENTS

Page
Exp.NO. Date Title of the Experiment Marks Signature
no.

1
EX.NO.: 1
CONTROL STRUCTURES AND LOOPING STRUCTURES
DATE:

1.1 SIMPLE IF:

AIM:
To write a c++ program to check a given number is ever using simple if statement.

ALGORITHM:
STEP 1: Start the program.
STEP 2: Read the value of a.
STEP 3: Check whether a is divisible by
2. STEP 4: If its divisible display ‘a’ is
even. STEP 5: Stop the program.

PROGRAM:
#include<iostream>
using namespace std;
int main()
{
int a;
cout<<"enter the value of a: ";
cin>>a;
if(a%2==0)
{
cout<<"The given number is even.";
}
return 0;
}

OUTPUT:

2
1.2. IF ELSE:

AIM:
To write a c++ program to check whether a number id even or odd using if else statements.

ALGORITHM:
STEP 1: Start the program.
STEP 2: Read the value of a.
STEP 3: Check whether a is divisible by 2.
STEP 4: If its divisible by 2
Display ‘a’ is even
STEP 5: Else
Display ‘a’ is odd
STEP 6: Stop the program.

PROGRAM:
#include<iostream>
using namespace std;
int main()
{
int a;
cout<<"Enter the value of a: ";
cin>>a;
if(a%2==0)
{
cout<<"The given number is even";
}
else
{
cout<<"The given number is odd";
}
return 0;
}

OUTPUT:

3
1.3. NESTED IF:

AIM:
To write a c++ program to find the largest of three numbers using nested if statements.

ALGORITHM:
STEP 1: Start the program
STEP 2: Read the value of a, b and c.
STEP 3: Check whether a is greater than b and c.
STEP 4: If its greater display a is the largest of three numbers.
STEP 5: Else check whether b is greater than c
STEP 6: If its greater display b is the largest of three numbers.
STEP 7: Else display c is the largest of three numbers.

PROGRAM:
#include<iostream>
using namespace std;
int main()
{
int a,b,c;
cout<<"Enter the value of x, y, z:
"; cin>>x>>y>>z;
if(x>y)
{
if(x>z)
{
cout<<"The greatest number is: "<<x;
}
else
{
cout<<"The greatest number is: "<<z;
}
}
else
{
if(y>z)
{
cout<<"The greatest number is: "<<y;
}
else
{
cout<<"The greatest number is: "<<z;
}

4
}
return 0;
}

OUTPUT:

1.4. SWITCH CASE:

AIM:
To write a c++ program to find the day using switch case statements.

ALGORITHM:
STEP 1: Start the program.
STEP 2: Read the value of days.
STEP 3: Using switch display the days by case 1,case 2, case 3 and so on upto case 4.
STEP 4: In default display “enter the correct number”.
STEP 5: Stop the program.

PROGRAM:
#include<iostream>
using namespace std;
int main()
{
int days;
cout<<"Enter the numbers between 1 to 7: ";
cin>>days;
switch(days)
{
case 1:
cout<<"naiyitru-kizhamai";
break;
case 2:
cout<<"thingaḷ-kizhamai";
break;
case 3:
cout<<"Sevvai-kizhamai";
break;
5
case 4:
cout<<"bhudhan-kizhamai";
break;
case 5:
cout<<"viyazha-kizhamai";
break;
case 6:
cout<<"velli-kizhama";
break;
case 7:
cout<<"sani-kizhama ";
break;
default:
cout<<"Enter the correct number!";
break;
}
return 0;
}

OUTPUT:

6
1.5. WHILE LOOP:

AIM:
To write a c++ program to find the sum of n positive numbers using while loop.

ALGORITHM:
STEP 1: Start the program.
STEP 2: Read the value of N and initialize the sum value to 0.
STEP 3: Open the while loop and check whether N is greater than
0. STEP 4: Now add the value of sum with the N value.
STEP 5: Display the sum.
STEP 6: Stop the program.

PROGRAM:
#include<iostream>
using namespace std;
int main()
{
int N,sum=0;
cout<<"Enter the N value: ";
cin>>N;
while(N>=0)
{
sum = sum + N;
cout<<"Enter the N value: ";
cin>>N;
}
cout<<"sum is: "<<sum;
return 0;
}

OUTPUT:

7
1.6. DO WHILE LOOP:

AIM:
To write a c++ program to check whether the given number is palindrome or not using do – while
loop.

ALGORITHM:
STEP 1: Start the program
STEP 2: Read the value of n and initialize the value of rev=0 and declare x and digit.
STEP 3: Store the value of n in x.
STEP 4: Open the do - while loop and do the following thing.
Divide the value of n by 10 and store its remainder in digit.
Multiply the rev value into 10 and digit value with it and store it in the rev
value.

Check whether n is greater than 0.


STEP 5: Check whether x is equal to the rev value.
STEP 6: If its equal,
Display the given value is palindrome.
STEP 7: Else,
Display the given value is not palindrome.
STEP 8: Stop the program.

PROGRAM:
#include<iostream>
using namespace std;
int main()
{
int n,rev=0,x,digit;
cout<<"Enter N value: ";
cin>>n;
x = n;
do
{
digit = n%10;
rev = (rev*10)+digit;
n = n/10;
8
}while(n>0);
cout<<"Reversed digits are: "<<rev<<endl;
if(x==rev)
{
cout<<"The given value is palindrome";
}
else
{
cout<<"The given value is not palindrome";
}
return 0;
}

OUTPUT:

9
1.7. FOR LOOP:

AIM:
To write a c++ program to find the sum of N numbers using for loop.

ALGORITHM:
STEP 1: Start the program
STEP 2: Read the value of N and initialize the sum to 0.
STEP 3: Open for loop and do the following things.
Declare and initialize the variable count to
1. Check count is greater than or equal to N.
Increment the count value.
STEP 4: Add the value of count with the value of sum.
STEP 5: Display the sum.
STEP 6: Stop the program.

PROGRAM:
#include<iostream>
using namespace std;
int main()
{
int N,sum=0;
cout<<"Enter N value: ";
cin>>N;
for(int count=1;count<=N;count++)
{
sum = sum + count;
}

cout<<"Sum is: "<<sum;


return 0;
}

10
OUTPUT:

1.8. GOTO STATEMENT:

AIM:
To write a c++ program to check the given value from 1 to 100 natural numbers.

ALGORITHM:
STEP 1: Start the program.
STEP 2: Start the goto statement.
STEP 3: Read the value of n and m.
STEP 4: Check whether m is equal to
n.
STEP 5: If its equal display values are correct.
STEP 6: Else display values are not correct.
STEP 7: Stop the program.

PROGRAM:
#include<iostream>
using namespace std;
int main()
{
g:
int n,m;
cout<<"enter the natural numbers from 1 to 100: ";
cin>>n;
cout<<"enter the value to be check: ";
cin>>m;
if(m==n)

11
{
cout<<"values are correct"<<endl;
}
else
{
cout<<"try it again"<<endl;
goto g;
}
return 0;
}

OUTPUT:

RESULT:

12
EX.NO: 02
ARRAY USAGES
DATE:

2.1. ONE – DIMENSIONAL ARRAY:

AIM:
To write a c++ program to sort the given array using bubble sort.

ALGORITHM:
STEP 1: Start the program.
STEP 2: Read the value of array x and n and declare the variables i,j,temp and y;
STEP 3: Open for loop and check the condition,
Check the condition x[j] is greater than x[j+1],
Then swap the values,
temp=x[j]
x[j]=x[j+1]
x[j+1]=temp
STEP 4: Display the unsorted and sorted array.
STEP 5: Stop the program.

PROGRAM:
#include<iostream>
using namespace std;
int main()

{
int x[100],y[100],i,j,n,temp;
cout<<"Enter the value of n: ";
cin>>n;
cout<<"Enter data: ";
for(i=0;i<n;i++)
{

13
cin>>x[i];
y[i] = x[i];
}
for(i=0;i<n;i++)
{
for(j=0;j<(n-i);j++)
{
if(x[j]>x[j+1])
{
temp = x[j];
x[j] = x[j+1];
x[j+1] = temp;
}
}
}
cout<<"Unsorted array is: ";
for(i=0;i<n;i++)
{
cout<<y[i]<<" ";
}
cout<<endl;
cout<<"Sorted array is: ";
for(i=0;i<n;i++)
{
cout<<x[i]<<" ";
}
return 0;
}

OUTPUT:

14
2.2. TWO – DIMENSIONAL ARRAY:

AIM:
To write a c++ program to add the two matrices using two dimensional array

ALGORITHM:
STEP 1: Start the program.
STEP 2: Read the values of a and b.
STEP 3: Declare the variables i,j,rows and columns.
STEP 4: Add the two matrices and store it in the variable c.
STEP 5: Display the sum of the matrices.
STEP 6: Stop the program.

PROGRAM:
#include<iostream>
using namespace std;
int main()
{
int a[10][10],b[10][10],c[10][10];
int i,j,rows,columns;
cout<<"Enter rows,columns: ";
cin>>rows>>columns;
cout<<"Enter the first matrix: "<<endl;
for(i=0;i<rows;i++)
{
for(j=0;j<columns;j++)
{
cin>>a[i][j];
}
}
cout<<"Enter the second matrix: "<<endl;
for(i=0;i<rows;i++)
{
for(j=0;j<columns;j++)
{
cin>>b[i][j];
}
}

15
cout<<"Sum of two matrix is: ";
for(i=0;i<rows;i++)
{
for(j=0;j<columns;j++)
{
c[i][j] = a[i][j] + b[i][j];
}
}
cout<<"Output is: "<<"\n";
for(i=0;i<rows;i++)
{
for(j=0;j<columns;j++)
{
cout<<c[i][j]<<"\t";
}
cout<<"\n";
}
return 0;
}

OUTPUT:

16
2.3. MULTI – DIMENSIONAL ARRAY:

AIM:
To write a c++ program to print the multi dimensional array

ALGORITHM:
STEP 1: Start the program.
STEP 2: Declare the value of the variable test.
STEP 3: Decalre the variables I,j,k.
STEP 4: Open the for loop and display the variable test.
STEP 5: Stop the program.

PROGRAM:
#include<iostream>
using namespace std;
int main()
{
int test[2][3][2] =
{
{
{1,2},
{3,4},
{5,6}
},
{
{7,8},
{9,10},
{11,12}
}
};
for(int i=0;i<2;i++)
{
for(int j=0;j<3;j++)
{
for(int k=0;k<2;k++)
{
cout<<"test["<<i<<"]["<<j<<"]["<<k<<"]="<<test[i][j][k]<<endl;
}
}

17
}
return 0;
}

OUTPUT:

RESULT:

18
EX.NO: 03
CLASS DECLARATION, DEFINITION AND ACCESSING CLASS MEMBERS

DATE:

3.1. STUDENT MARKLIST:

AIM:
To write a c++ program to display the students mark list using class.

ALGORITHM:
STEP 1: Start the program.
STEP 2: Declare a class with the class name Student.
STEP 3: Declare the variables rollNo,m1,m2,m3,m4,m5,total,average,name,dept in the class.
STEP 4: Define the function getdata(),calculate(),display() inside the class
STEP 5: Get the values of rollNo,name,dept,m1,m2,m3,m4,m5 in the
getdata() STEP 6: Calculate the total and average in the calculate().
STEP 7: Display the rollNo,name,dept,total,average in the display().
STEP 8: Check the conditions for grade and display the grade
accordingly. STEP 9: Create an object for the class in the main function.
STEP 10: Call the functions using the object.
STEP 11: Stop the program.

PROGRAM:
#include <iostream>
using namespace
std; class Student
{
int rollNo,m1,m2,m3,m4,m5,total;
float average;
char name[50];
char dept[20];
public:
void getdata();

19
void calculate();
void display();
};
void Student::getdata()
{
cout<<"Enter your roll number: "<<endl;
cin>>rollNo;
cout<<"Enter your name: "<<endl;
cin>>name;
cout<<"Enter your department: "<<endl;
cin>>dept;
cout<<"Enter your marks: "<<endl;
cin>>m1>>m2>>m3>>m4>>m5;
}
void Student::calculate()
{
total = m1+m2+m3+m4+m5;
average = total/5;
}
void Student::display()
{
cout<<"your roll number is: "<<rollNo<<endl;
cout<<"your name is: "<<name<<endl;
cout<<"your department is: "<<dept<<endl;
cout<<"your marks are: "<<m1<<" "<<m2<<" "<<m3<<" "<<m4<<"
"<<m5<<endl; cout<<"your total is: "<<total<<endl;
cout<<"your average is: "<<average<<endl;
if(average>=80)
{
cout<<"first grade";
}
else if(average>=60)
{
cout<<"second grade";
}
else if(average>=50)
{
cout<<"third grade";
}
else
{
cout<<"you are not qualified";
}
}
int main()
{

20
Student s;
s.getdata();
s.calculate();
s.display();

return 0;
}

OUTPUT:

3.2. ELECTRICITY BILL:

AIM:
To write a c++ program to display the electricity bill using a class and objects

ALGORITHM:
STEP 1: Start a program.
STEP 2: Define the class with the class name Eb.
STEP 3: Declare the variables unit,cu,pu,amt in the class.
STEP 4: Define the function get(), cal(), display().
STEP 6: Get the values of cu and pu in the get().
STEP 7: Calculate the unit in the cal().

21
STEP 8: Display the values of cu,pu,unit in the display().
STEP 9: Check the conditions and display the amount accordingly.
STEP 10: Create an object e for the class Eb in the main function.
STEP 11: Call the function using the objects.
STEP 12: Stop the program.

PROGRAM:
#include<iostream>
using namespace std;
class Eb
{
private:

int unit,cu,pu,amt;
public:
void get();
void cal();
void
display();
};
void Eb::get()
{
cout<<"Enter current unit: ";
cin>>cu;
cout<<"Enter previous unit: ";
cin>>pu;
}
void Eb::cal()
{
unit = cu - pu;
}
void Eb::display()
{
cout<<"The current unit is: "<<cu<<endl;
cout<<"The previous unit is: "<<pu<<endl;
cout<<"The unit is: "<<unit<<endl;
if(unit>=100 && unit<=200)
{
amt = unit*1;
cout<<"Amount is: "<<amt;
}
else if(unit>=200 && unit<=300)
{
amt = unit*2;
22
cout<<"Amount is: "<<amt;
}
else if(unit>=300 && unit<=400)
{
amt = unit*3;
cout<<"Amount is: "<<amt;
}
else if(unit>=400 && unit<=500)
{
amt = unit*4;
cout<<"Amount is: "<<amt;
}
else
{
amt = 0;
cout<<"Amount is: "<<amt;
}
}
int main()
{
Eb e;
e.get();
e.cal();
e.display();
return 0;
}

OUTPUT:

RESULT:

23
EX.NO: 04
CONSTRUCTOR
DATE:

4.1. PARAMETERIZED CONSTRUCTOR:

AIM:
To write a c++ program to display the student details using a parameterized constructor.

ALGORIHTM:
STEP 1: Start the program
STEP 2: Define the class with the class name student
STEP 3: Declare the variables rno, m1,m2 inside the class.
STEP 4: Access the class with the arguments a, b and c.
STEP 5: Get the values of the variables rno,m1,m2 in the getdata() function.
STEP 6: Display the values of the variables rno, m1, m2 in the putdata() function.
STEP 7: Create an object s for the student.
STEP 8: Create an object b for the constructor.
STEP 9: Call the function and the constructor using the objects.
STEP 10: Stop the program.

PROGRAM:
#include<iostream>
using namespace std;
class student
{
private:
int rno,m1,m2;
public:
student()
{
rno=m1=m2=0;
}
student(int a, int b, int c)
{

23
rno=a
;
m1=b
;
m2=c;
}
void getdata()
{
cout<<"Enter register number: ";
cin>>rno;
cout<<"Enter mark 1";
cin>>m1;
cout<<"Enter mark 2";
cin>>m2;
}
void putdata()
{
cout<<"The details:"<<endl;
cout<<"Reg number: "<<rno<<endl;
cout<<"mark 1: "<<m1<<endl;
cout<<"mark 2: "<<m2<<endl;
}
};
int main()
{
student s;
student b(12,99,90);
s.putdata();
b.putdata();
s.getdata();
s.putdata();
}

OUTPUT:

24
25
4.2. DEFAULT CONSTRUCTOR:

AIM:
To write a c++ program to display the student details using default constructor.

ALGORITHM:
STEP 1: Start the program.
STEP 2: Define the class with the class name exam.
STEP 3: Declare the variables rno, m1, m2, m3, total, average, name.
STEP 4: Define the constructor without the arguments.
STEP 5: Read the values of rno, name, m1, m2, m3 in the getdata().
STEP 6: Calculate the total and average in the cal().
STEP 7: Display the rno, name, total and average in the display().
STEP 8: Create an object e for the exam.
STEP 9: Call the function using the objects.
STEP 10: Stop the program.

PROGRAM:
#include<iostream>
using namespace std;
class exam
{
private:
int rno,m1,m2,m3,total,average;
char name[20];
public:
exam()
{
rno=m1=m2=m3=total=average=name==0;
}
void getdata()
{
cout<<"Enter roll number: "<<endl;
cin>>rno;
cout<<"Enter name: "<<endl;
cin>>name;
cout<<"Enter m1,m2,m3: "<<endl;

26
cin>>m1>>m2>>m3;
}
void cal()
{
total = m1 + m2 + m3;
average = total/3;
}
void display()
{
cout<<"roll number: "<<rno<<endl;
cout<<"name: "<<name<<endl;
cout<<"total: "<<total<<endl;
cout<<"average: "<<average<<endl;
}
};
int main()
{
exam e;
e.getdata();
e.cal();
e.display();
}

OUTPUT:

27
4.3. COPY CONSTRUCTOR:

AIM:
To write a c++ program to display the student details using copy constructor.

ALGORITHM:
STEP 1: Start the program.
STEP 2: Define a class with the class name exam.
STEP 3: Declare the variables rno, m1, m2, name in the class.
STEP 4: Copy the name using the strcpy method.
STEP 5: Read the values of the variables rno, name, m1, m2 in the showdata().
STEP 6: Create two objects s1 and s2 for the exam.
STEP 7: Call the function using the object and copy the values using the second object.
STEP 8: Stop the program.

PROGRAM:
#include<iostream>
#include<string.h>
using namespace std;
class exam
{
private:
int rno,m1,m2;
char
name[20];
public:
exam(int a, int b, int c, char * d)
{
rno =
a; m1 =
b; m2
= c;
strcpy(name,d);
}
exam(exam &ptr)
{
rno=ptr.rno
;
m1=ptr.m1;
28
m2=ptr.m2;

29
strcpy(name, ptr.name);
}
void showdata()
{
cout<<"register number: "<<rno<<endl;
cout<<"Name: "<<name<<endl;
cout<<"Mark 1: "<<m1<<endl;
cout<<"Mark 2: "<<m2<<endl;
}
};
int main()
{
exam s1(101,90,99,"aruna");
exam s2(s1);
cout<<"student 1: "<<endl;
s1.showdata();
cout<<"Copy of student 1: "<<endl;
s2.showdata();
}

OUTPUT:

RESULT:

30
EX.NO: 05
FRIEND FUNCTION
DATE:

AIM:
To write a c++ program to display the student details using the friend function.

ALGORITHM:
STEP 1: Start the program.
STEP 2: Define the class with the class name student.
STEP 3: Define the variables rno, name.
STEP 4: Define the functions getdata(), and putdata() with the friend keyword.
STEP 5: Get the values of the variables rno and name in the getdata()
STEP 6: Display the values of the variables rno and name in the putdata()
STEP 7: Create an object s for the student.
STEP 8: Call the function using the objects.
STEP 9: Stop the program.

PROGRAM:
#include<iostream>
using namespace std;
class student
{
private:
int rno;
char name[20];
public:
void getdata();
friend void putdata(student);
};
void student::getdata()
{
cout<<"Enter student roll number: "<<endl;
cin>>rno;
cout<<"Enter student name: "<<endl;
cin>>name;

31
}
void putdata(student f)
{
cout<<"student details:"<<endl;
cout<<”------------------------“<<endl;
cout<<"Student number: "<<f.rno<<endl;
cout<<"Student name: "<<f.name<<endl;
}
int main()
{
student s;
s.getdata();
putdata(s);
}

OUTPUT:

RESULT:

32
EX.NO: 06
FUNCTION OVERLAODING AND CONSTRUCTOR
OVERLOADING
DATE:

6.1. FUNCTION OVERLOADING:

AIM:
To write a c++ program to find sum of numbers using function overloading

ALGORITHM:
STEP 1: Start the program
STEP 2: Create a function using the arguments.
STEP 3: Define a variables fun1 and fun2.
STEP 4: Assign a function of sum() to the variables fun1 and fun2 by passing the values as
an arguments.
STEP 5: Display the functions.
STEP 6: Call the functions and return the resultant values.
STEP 7: Stop the program.

PROGRAM:
#include<iostream>
using namespace std;
int sum(int, int);
int sum(int, int, int);
int main()
{
int fun1, fun2;
fun1 = sum(15,25);
fun2 = sum(15,30,40);
cout<<"Sum(10,15)= "<<fun1<<endl;
cout<<"Sum(10,20,30)= "<<fun2<<endl;
}
int sum(int a, int b)
{
return(a+b);

31
}
int sum(int a, int b, int c)
{
return(a+b+c);
}

OUTPUT:

6.2. CONSTRUCTOR OVERLOADING:

AIM:
To write a c++ program to display the marks of the students using constructor overloading.

ALGORITHM:
STEP 1: Start the program.
STEP 2: Define a class with the class name exam.
STEP 3: Declare the variables rno, m1, m2 in the class.
STEP 4: Call the constructor with the arguments.
STEP 5: Display the values of the variables in the display().
STEP 6: Create an object for the exam.
STEP 7: Call the function using the object.
STEP 8: Stop the program.

PROGRAM:
#include<iostream>
using namespace std;
class exam
{

32
private:
int rno,m1,m2;
public:
exam()
{
rno=m1=m2= 0;
}
exam(int a, int b, int c)
{
rno=a
;
m1=b
;
m2=c;
}
void display()
{
cout<<"Register number is: "<<rno<<endl;
cout<<"Mark 1 is: "<<m1<<endl;
cout<<"Mark 2 is: "<<m2<<endl;
}
};
int main()
{
exam s1;
exam s2(99521025,45,56);
s1.display();
s2.display();
}

OUTPUT:

RESULT:

33
EX.NO: 07
OPERATOR OVERLOADING
DATE:

7.1. BINARY OPERATOR:

AIM:
To write a c++ program to find the volume of a box using binary operator.

ALGORITHM:
STEP 1: Start the program.
STEP 2: Define a class with a class name Box.
STEP 3: Declarer a variables length, breadth and height in the class.
STEP 4: Calculate the volume of a box in a getVolume function.
STEP 5: Assign a value to the variables length, breadth and height with the values len, bre,
hei.

STEP 6: Create an object box for the Box and call it.
STEP 7: Set the values for the variable using the objects.
STEP 8: Get the volumes of the box1 and box2 using the getVolume functions.
STEP 9: Calculate box3 by adding the volume of box1 and box2.
STEP 10: Display the value of box3.
STEP 11: Stop the program.

PROGRAM:
#include<iostream>
using namespace std;
class Box
{
double length;
double breadth;
double height;
public:
double getVolume(void)
{
return length * breadth * height;

34
}
void setLength(double len)
{
length = len;
}
void setBreadth(double bre)
{
breadth = bre;
}
void setHeight(double hei)
{
height = hei;
}
Box operator+(const Box& b)
{
Box box;
box.length = this->length + b.length;
box.breadth = this -> breadth + b.breadth;
box.height = this->height + b.height;
return box;
}
};
int main()
{
Box
Box1;
Box
Box2;
Box
Box3;
double volume = 0.0;
Box1.setLength(10.0);
Box1.setBreadth(8.0);
Box1.setHeight(9.0);
Box2.setLength(15.0);
Box2.setBreadth(20.0);
Box2.setHeight(10.0);
volume =
Box1.getVolume();
cout<<"\n\tVolume of Box1: "<<volume<<endl;
volume = Box2.getVolume();
cout<<"\tVolume of Box2: "<<volume<<endl;
Box3 = Box1+Box2;
volume = Box3.getVolume(); cout<<"\
tVolume of Box3: "<<volume<<endl; return
0;
}
35
OUTPUT:

7.2. UNARY OPERATOR:

AIM:
To write a c++ program to display the feet and inches using unary operator.

ALGORITHM:
STEP 1: Start the program.
STEP 2: Define the class with the class name Distance.
STEP 3: Declare the variables feet and inches in the class.
STEP 4: Assign a variable f and I to the feet and inches respectively.
STEP 5: Display the feet and inches in the display function.
STEP 6: Using a operator (-) assign –feet and –inches to the variables feet and inches.
STEP 7: Create an object d1 and d2 with the values assigned as an arguments.
STEP 8: Call the function display using the objects.
STEP 9: Stop the program.

PROGRAM:
#include<iostream>
using namespace std;
class Distance
{
private:
int feet,inches;
public:

36
Distance()
{
feet = inches = 0;
}
Distance(int f, int i)
{
feet = f;
inches = i;
}
void display()
{
cout<<"\n\tF: "<<feet<<"\tI: "<<inches<<endl;
}
Distance operator-()
{
feet = -feet;
inches = -inches;
return Distance(feet, inches);
}
};
int main()
{
Distance d1(28,20), d2(-8,20);
-d1;
d1.display();
-d2;
d2.display();
return 0;
}

OUTPUT:

RESULT:

37
EX.NO: 08

ACCESS MEMBERS OF A CLASS USING POINTER TO


OBJECT MEMBERS
DATE:

AIM:

To write a c++ program to display the number from the keyboard using a pointer.

ALGORITHM:

STEP 1: Start the program.

STEP 2: Define a class with the class name Number.

STEP 3: Declare a variable num in the class.

STEP 4: Get the value of the variable num in the function inputNumber.

STEP 5: Display the value of the variable num in the function displayNumber.

STEP 6: Create an object N to the class Number.

STEP 7: Call the functions inputNumber and displayNumber using the object N.

STEP 8: Using pointer point the class Number.

STEP 9: Using pointer call the functions displayNumber and inputNumber.

STEP 10: Stop the program.

PROGRAM:
#include<iostream>
using namespace std;
class Number
{
private:
int num;
public:
Number()

38
{
num = 0;
};
void inputNumber(void)
{
cout<<"\n\tEnter an integer number: ";
cin>>num;
}
void displayNumber()
{
cout<<"\tNumber is: "<<num<<endl;
}
};
int main()
{
Number N;
N.inputNumber();
N.displayNumber();
Number *ptrN;
ptrN = new Number; cout<<"\
tDefault value... "<<endl; ptrN-
>displayNumber();
ptrN->inputNumber();
ptrN-
>displayNumber();
return 0;
}

OUTPUT:

RESULT:

39
EX.NO: 09
SINGLE INHERITENCE AND MULTILPLE INHERITENCE
DATE:

9.1. SINGLE INHERITENCE:

AIM:
To write a c++ program to display the fees details for technical courses and courses using single
inheritance.

ALGORITHM:

STEP 1: Start the program.

STEP 2: Define the first class with the class name tcourse.

STEP 3: Define the functions get_fees and list_fees in the class.

STEP 4: Define the second class with the class name course and inherit the first class tcourse.

STEP 5: Declare the variables BSC and BCA in the class course.

STEP 6: Define the fucntions course_get_fees and course_list_fees in the class course.

STEP 7: Get the values of ME and BE in the function get_fees.

STEP 8: Display the values of ME and BE in the function list_fees.

STEP 9: Get the values of BSC and BCA in the function course_get_fees.

STEP 10: Display the values of ME and BE in the function course_list_fees.

STEP 11: Create two objects m and n for the two classes tcourse and course respectively.

STEP 12: Call the functions using the respective objects.

STEP 13: Stop the program.

40
PROGRAM:

#include<iostream>
using namespace std;
class tcourse
{
private:
float ME,BE;
public:
void get_fees();
void list_fees();
};
class course:public tcourse
{
private:
float BSC,BCA;
public:
void course_get_fees();
void course_list_fees();
};
void tcourse::get_fees()
{
cout<<"Enter the fees amount for M.E: ";
cin>>ME;
cout<<"Enter the fees amount for B.E: ";
cin>>BE;
}
void tcourse::list_fees()
{
cout<<"M.E: "<<"Rs. "<<ME<<endl;
cout<<"B.E: "<<"Rs. "<<BE<<endl;
}
void course::course_get_fees()
{
get_fees();
cout<<"Enter the fees amount for BSC: ";
cin>>BSC;
cout<<"Enter the fees amount for BCA: ";
cin>>BCA;
}
void course::course_list_fees()
{
list_fees();
cout<<"BSC: "<<"Rs. "<<BSC<<endl;
cout<<"BCA: "<<"Rs. "<<BCA<<endl;
}

41
int main()
{
tcourse m;
course n;
cout<<"Fees details for technical course: "<<endl;
m.get_fees();
cout<<"Fees details for course: "<<endl;
n.course_get_fees();
cout<<"Fees list for technical course: "<<endl;
m.list_fees();
cout<<"Fees list for course: "<<endl;
n.course_list_fees();
return 0;
}

OUTPUT:

9.2. MULTIPLE INHERITENCE:

AIM:
To write a c++ program to display the values of m and n using multiple inheritance.

ALGORITHM:
STEP 1: Start the program.
STEP 2: Define a class with the class m.
STEP 3: Declare a variable m in the class m.

42
STEP 4: Define a function get_m in the class.
STEP 5: Define the second class with the class name n.
STEP 6: Declare a variable n in the class n.
STEP 7: Define a function get_n in the class n.
STEP 8: Assign a variable x to the variable m in the function
get_m. STEP 9: Assign a variable y to the variable m in the
function get_n. STEP 10: Display the values of m and n in the
function display.
STEP 11: Create an object p and q for the class m and n.
STEP 12: Call the functions using the objects.
STEP 13: Stop the program.

PROGRAM:
#include<iostream>
using namespace std;
class m
{
protected:
int m;
public:
void get_m(int);
};
class n
{
protected:
int n;
public:
void get_n(int);
};
class p:public m, public n
{
public:
void display();
};
void m::get_m(int x)
{
m = x;
}
void n::get_n(int y)
{
n = y;

43
}

44
void p::display()
{
cout<<"M: "<<m<<endl;
cout<<"N: "<<n<<endl;
}
int main()
{
p q;
q.get_m(30);
q.get_n(20);
q.display();
return 0;
}

OUTPUT:

RESULT:

45
EX.NO: 10
MULTILEVEL INHERITENCE, HIERARCHICAL INHERITENCE,
HYBRID INHERITENCE
DATE:

10.1. MULTILEVEL INHERITENCE:

AIM:

To write a c++ program to display the total of the student using multilevel inheritance.

ALGORITHM:

STEP 1: Start the program.

STEP 2: Define a class with the class name student.

STEP 3: Declare a variable no in the class student.

STEP 4: Define the function getno and putno in the class student.

STEP 5: Assign a variable x to the variable no in the getno function.

STEP 6: Display the value of the variable in the putno function.

STEP 7: Define the second class with the class name test and inherit the class student.

STEP 8: Declare the variables s1 and s2 in the class test.

STEP 9: Define the function getmarks and putmarks in the class test.

STEP 10: Assign a variable a and b to the variable s1 and s2 respectively in the getmarks
function.

STEP 11: Display the values of the variables s1 and s2 in the putmarks function.

STEP 12: Create a third class with the class name result and inherit the class

test. STEP 13: Declare a variable total in the class result.

STEP 14: Define a function display in the class result.

STEP 15: Calculate the total by adding the values of s1 and s2 and display the total in the
display function.

45
STEP 16: Create an object s for the third class result.

STEP 17: Call the function using the object.

STEP 18: Stop the program.

PROGRAM:
#include<iostream>
using namespace std;
class student
{
protected:
int no;
public:
void getno(int x);
void putno();
};
void student::getno(int x)
{
no = x;
}
void student::putno()
{
cout<<"\tRoll no.: "<<no<<endl;
}
class test:public student
{
protected:
float
s1,s2;
public:
void getmarks(float a, float b);
void putmarks();
};
void test::getmarks(float a, float b)
{
s1 = a;
s2 =
b;
}
void test::putmarks()
{
cout<<"\tMark in subject1: "<<s1<<endl;
cout<<"\tMark in subject2: "<<s2<<endl;
}

46
class result: public test

47
{
protected:
float
total;
public:
void display();
};
void result::display()
{
total = s1 + s2;
putno();
putmarks();
cout<<"\tTotal: "<<total<<endl;
}
int main()
{
result s;
s.getno(99521025);
s.getmarks(66.0,78.0);
s.display();
return 0;
}

OUTPUT:

10.2. HIERARCHICAL INHERITENCE:

AIM:

To write a c++ program to calculate the sum and product of the two values using hierarchical
inheritance.

ALGORITHM:

STEP 1: Start the program.

STEP 2: Define the class with the class name A.

STEP 3: Declare the variable x and y in the class A.

48
STEP 4: Define the function getdata and get the values of x and y in the get function.

STEP 5: Define the second class with the class name B and inherit the class A.

STEP 6: Define the function product and calculate the products of x and y in the product
function.

STEP 7: Define the third class with the class name C and inherit the class A.

STEP 8: Define the function sum and the calculate the sum of x and y in the sum function.

STEP 9: Create two objects obj1 and obj2 for the classes B and C respectively.

STEP 10: Call the functions using the objects.

STEP 11: Stop the program.

PROGRAM:
#include<iostream>
using namespace std;
class A
{
public:
int x,y;
void getdata()
{
cout<<"\n\tEnter value of X: ";
cin>>x;
cout<<"\n\tEnter value of Y: ";
cin>>y;
}
};
class B: public A
{
public:
void product()
{
cout<<"\n\tProduct = "<<x * y<<endl;
}
};
class C:public A
{
public:
void sum()
{
cout<<"\n\tSum = "<<x + y<<endl;

49
}
};
int main()
{
B
obj1;
C
obj2;
obj1.getdata();
obj1.product();
obj2.getdata();
obj2.sum();
return 0;
}

OUTPUT:

10.3. HYBRID INHERITENCE:

AIM:

To write a c++ program to calculate and display the sports weight and total score of a student using
hybrid inheritance.

ALGORITHM:

STEP 1: Start the program.

STEP 2: Define a class with the class name student.

STEP 3: Declare a variable roll_no in the class student.

STEP 4: Define a function get_no and assign a value a to the variable roll_no.

STEP 5: Display the value of the roll_no in the put_no function.

50
STEP 6: Define the second class with the class name test and inherit the class student.

STEP 7: Declare the variables part1, part2 in the class test.

STEP 8: Assign the variables x and y to the variables part1 and part2 in the get_marks
function.

STEP 9: Display the values of part1 and part2 in the put_marks function.

STEP 10: Define the third class with the class name sports.

STEP 11: Declare a variable score in the class sports.

STEP 12: Assign the variable s to the variable score in the get_score function.

STEP 13: Display the value of the score in the put_score function.

STEP 14: Define the last class with the class name result and inherit the classes test and
sports.

STEP 15: Declare the variable total in the class result.

STEP 16: Define the function display and calculate the total by adding part1, part2 and score.

STEP 17: Create an object s for the last class result.

STEP 18: Call the functions using the object s with the values as an arguments.

STEP 19: Stop the program.

PROGRAM:
#include<iostream>
using namespace std;
class student
{
protected:
int roll_no;
public:
void get_no(int a)
{
roll_no = a;
}
void put_no()
{
cout<<"Roll No.: "<<roll_no<<endl;
}

51
};
class test:public student
{
protected:
float part1,part2;
public:
void get_marks(float x, float y)
{
part1 = x;
part2 = y;
}
void put_marks()
{
cout<<"Marks obtained: "<<endl;
cout<<"part1 = "<<part1<<"\npart2 = "<<part2<<endl;
}
};
class sports
{
protected:
float
score;
public:
void get_score(float s)
{
score = s;
}
void put_score()
{
cout<<"Sports weight: "<<score<<endl;
}
};
class result:public test, public sports
{
float total;
public:
void display();
};
void result::display()
{
total = part1 + part2 + score;
put_no();
put_marks();
put_score();
cout<<"Total score: "<<total<<endl;
}
int main()
52
{
result s;
s.get_no(99521025);
s.get_marks(95,89);
s.get_score(9.7);
s.display();
return 0;
}

OUTPUT:

RESULT:

53
EX.NO: 11

VIRTUAL CLASS AND ABSTRACT CLAS

DATE:

11.1. VIRTUAL CLASS:

AIM:
To write a c++ program to display the values of A,B,C,D using virtual class.

ALGORITHM:
STEP 1: Start the program.
STEP 2: Define a class with the class name A.
STEP 3: Declare a variable a in the class A.
STEP 4: Define a second class with the class name B and virtually inherit the class A.
STEP 5: Declare a variable b in the class B.
STEP 6: Define a third class with the class name C and virtually inherit the class A.
STEP 7: Declare a variable c in the class C.
STEP 8: Define a fourth class with the class name D and inherit the properties of class A andC.
STEP 9: Declare a variable d in the class D.
STEP 10: Create an object obj for the fourth class D.
STEP 11: Assign values to the variable a, b, c, d using object obj.
STEP 12: Display the values of a, b, c, d.
STEP 13: Stop the program.

PROGRAM:
#include<iostream>
using namespace std;
class A
{
public:
int a;

54
};
class B: virtual public A
{
public:
int b;
};
class C: virtual public A
{
public:
int c;
};
class D: public B, public C
{
public:
int d;
};
int main()
{
D obj;
obj.a = 50;
obj.b = 60;
obj.c = 70;
obj.d = 80; cout<<"\n\
tA: "<<obj.a; cout<<"\
n\tB: "<<obj.b;
cout<<"\n\tC: "<<obj.c;
cout<<"\n\tD: "<<obj.d;
return 0;
}

OUTPUT:

55
11.2. ABSTRACT CLASS:

AIM:
To write a c++ program to display the new derived value from the old derived value using abstract
clss.

ALGORITHM:
STEP 1: Start the program.
STEP 2: Define a class with the class name Base.
STEP 3: Declare a variable x in the class Base.
STEP 4: Define a function fun() virtually and initialize It to 0.
STEP 5: Assign a variable i to the variable x.
STEP 6: Define a second class with the class name Derived which inherits the properities of
class base.
STEP 7: Declare a variable y in the class Derived.
STEP 8: Assign a variable j to the variable y and display the value of x in the function fun().
STEP 9: Create an object d for the class Derived with the values as an arguments.
STEP 10: Call the function fun() using the object d.
STEP 11: Using pointer *ptr assign the Derived class value to the Base class.
STEP 12: Call the function using the pointer.
STEP 13: Stop the program.

PROGRAM:
#include<iostream>
using namespace std;
class Base
{
protected:
int x;
public:
virtual void fun() = 0;
Base(int i)
{
x = i;
cout<<"Constructor of base called: \n";
}
};

56
class Derived: public Base
{
int y;
public:
Derived(int i, int j):Base(i)
{
y = j;
}
void fun()
{
cout<<"X = "<<x<<"\nY = "<<y<<endl;
}
};
int main()
{
Derived d(4,8);
d.fun();
Base *ptr = new
Derived(8,9); ptr->fun();
return 0;
}

OUTPUT:

RESULT:

57
EX.NO: 12
EXCEPTIONAL HANDLING
DATE:

AIM:
To write a c++ program to check zero division.

ALGORITHM:
STEP 1: Start a program.
STEP 2: Initialize a function zeroDivision and declare a variables x and y as an arguments.
STEP 3: Check whether y is equal to zero
Throw – “Division by zero”
STEP 4: Return x is divided by y.
STEP 5: Declare and initialize the variables a, b and c.
STEP 6: Follow the try statement and opening the loop and perform the following things.
C = zeroDivision(a,b)
Display the value of
c.
STEP 7: Follow the catch statement and opening the loop and perform the following things.
Cerr<<message<<endl;
STEP 8: Stop the program.

PROGRAM:
#include<iostream>
using namespace
std;
double zeroDivision(int x, int y)
{
if(y==0)
{
throw "\n\tDivision by Zero!";
}
return(x/y);
}
int main()
{
int a = 20;
int b = 0;
57
double c =0;
try
{
c = zeroDivision(a,b);
cout<<c<<endl;
}
catch (const char*message)
{
cerr<<message<<endl;
}
return 0;
}

OUTPUT:

RESULT:

58
EX.NO: 13

IOSTREAM, ISTREAM, OSTREAM CLASSES AND THEIR USAGES


DATE:

13.1. IOSTREAM:
13.1.1. IOSTREAM – CERR:

AIM:
To write a c++ program for the sample of iostream – cerr.

ALGORITHM:
STEP 1: Start the program.
STEP 2: Declare a file filename[] with the value “data.txt”.
STEP 3: Using ifstream infile check the variable filaName.
STEP 4: Check if(infile)
Display infile.rdbuf()
STEP 5: Else
Display error while opening the file.
STEP 6: Stop the program.

PROGRAM:
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
char fileName[] = "data.txt";
ifstream infile(fileName);
if(infile)
cout<<infile.rdbuf();
else
cerr<<"\n\tError while opening the file"<<fileName<<endl;
return 0;
}

OUTPUT:

59
13.1.2. IOSTREAM – WCOUT:

AIM:
To write a c++ program for the sample of iostream – wcout

ALGORITHM:
STEP 1: Start the program.
STEP 2: Read the value of a string line .
STEP 3: Display the string line using wcout.write()
STEP 4: Stop the program.

PROGRAM:
#include<iostream>
using namespace std;
int main()
{
wchar_t str[] = L"\n\tsmvec www.smvec.ac.in";
wcout.write(str,10);
wcout<<endl;
return 0;
}

OUTPUT:

60
13.2. ISTREAM:

AIM:
To write a c++ program display the file.

ALGORITHM:
STEP 1: Start a program.
STEP 2: Declare a standard file buffer variable flush.
STEP 3: Check if(fb.open(“lee.txt”,std::ios::in)
STEP 4: Using istream create is with parameter
of(&fb). STEP 5: Check while (is), then print the char
(is.get()). STEP 6: Close the file.
STEP 7: Display the output.
STEP 8: Stop the program.

PROGRAM:
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
std::filebuf fb;
if(fb.open("lee.txt",std::ios::in))
{
std::istream is(&fb);
while(is)
std::cout<<char(is.get());
fb.close();
}
return 0;
}
OUTPUT:

61
13.3. OSTREAM:

AIM:
To write a c++ program using ostream.

ALGORITHM:
STEP 1: Start the program.
STEP 2: Declare ostream outfile(“aruna.txt”).
STEP 3: Check the loop condition,
For(n=0,n<100;n++)
STEP 4: Using outfile display n with flush.
STEP 5: Close the outfile.
STEP 6: Stop the program.

PROGRAM:
#include<iostream>
#include<fstream>
int main()
{
std::ofstreamoutfile("lee.txt”);
for(int n=0;n<100;n++)
outfile<<n<<std::flush;
outfile.close();
return 0;
}

OUTPUT:
Before using Flush:

62
After using flush:

RESULT:

63
EX.NO: 14
FILE STREAM OPERATIONS
DATE:

AIM:
To write a c++ program for file stream operation.

ALGORITHM:
STEP 1: Start the program.
STEP 2: Declare a variable input.
STEP 3: Create an os object using
ofstream. STEP 4: Invoke os
open(“testout.txt”) STEP 5: Print the
writing to the text file.
STEP 6: Using the getline get the line for input with the required size.
STEP 7: Display input using os.
STEP 8: Close the os.
STEP 9: Declare is object using ifstream.
STEP 10: Invoke is open(“testout.txt”)
STEP 11: Check the reading from text
file. STEP 12: Check
while(getline(is.line)) STEP 13: Display
the line.
STEP 14: Close the is and display the output.
STEP 15: Stop the program.

PROGRAM:
#include<iostream>
#include<fstream>
using namespace
std; int main()
{
char input[75];
ofstream os;
os.open("testout.txt");
cout<<"Writing to a text file:\
n"<<endl; cout<<"Please Enter
company name: ";
64
cin.getline(input,100);
os<<input<<endl;
cout<<"Please Enter your website: ";
cin.getline(input,100);
os<<input<<endl;
os.close();
ifstream is;
string line;
is.open("testout.txt");
cout<<"\nReading from a text file: \n"<<endl;
while(getline(is,line))
{
cout<<line<<endl;
}
is.close();
return 0;
}

OUTPUT:

RESULT:

65
EX.NO: 15

TEMPLATE BASED PROGRAM TO SORT THE GIVEN LIST


OF ELEMENTS
DATE:

AIM:
To write a c++ program for sorting the elements using template.

ALGORITHM:
STEP 1: Start the program
STEP 2: Create a template <class T>
STEP 3: Declare InsertionSort with arguments of(T arr[], int n)
STEP 4: Declare a variable i, j and temp
STEP 5: Check the condition of loop for(i=1;i<n;i++)
STEP 6: If true go inside the loop and execute the statements.
STEP 7: In for loop assign temp+arr[i] and j=i-
1. STEP 8: Check the while (j>=0 &&
arr[i]>temp) STEP 9: In while block, assign
arr[j+1]=arr[j].
STEP 10: Assign arr[j+1] = temp.
STEP 11: If the loop condition is false, the loops will be terminated.
STEP 12: Create a template <class T>
STEP 13: Declare and define print array() method with the arguments (T arr[], int n).
STEP 14: Check the loop condition for(i=0;i<n;i++)
STEP 15: Print the arr[i].
STEP 16: Declare and assign the variable of array[]={6,9,5,3,9,10,11,35,1,33,3,5}.
STEP 17: Declare a variable n and assign n = size of(int array)/size of (int).
STEP 18: Display the integer array before sort by invoke the printarray() and InsertionSort()
using arguments (int array,n).
STEP 19: Print the integer array after sort.
STEP 20: Invoke the print array() method with arguments(int array,n)
STEP 21: Display the output.

66
STEP 22: Stop the program.

67
PROGRAM:
#include<iostream>
#include<vector>
using namespace std;
template <class T>
void InsertionSort(T arr[], int n)
{
int i,j;
T
temp;
for(int i=1;i<n;i++)
{
temp = arr[i];
j = i-1;
while(j>=0 && arr[j]>temp)
{
arr[j+1] = arr[j];
j = j-1;
}
arr[j+1] = temp;
}
}
template <class T>
void PrintArray(T arr[],int n)
{
for(int i=0;i<n;i++)
{
cout<<arr[i]<<' ';
}
cout<<endl;
}
int main()
{
int intArray[]={6,9,5,3,9,10,11,35,1,33,3,5};
int n = sizeof(intArray) / sizeof(int);
cout<<"\n Integer Array Before Sort: ";
PrintArray(intArray,n);
InsertionSort(intArray,n);
cout<<"\n Integer Array After Sort: ";
PrintArray(intArray,n);
cout<<"\n";
return 0;
}

68
OUTPUT:

RESULT:

69
EX.NO: 16
REAL WORLD EXAMPLE
DATE:

AIM:
To write a c++ program for the hotel management system.

ALGORITHM:
STEP 1: Start the program.
STEP 2: Create classes for Hotel data and User data.
STEP 3: Initialize variables that stores Hotel data and User data.
STEP 4: Create objects for Hotel and User classes that access the Hotel data and User data.
STEP 5: Initialize two vector array that holds the hotel data and user data.
STEP 6: Print the hotel data.
STEP 7: Sort Hotels by name.
STEP 8: Sort Hotels by highest rating.
STEP 9: Print Hotel data for Puducherry Location.
STEP 10: Sort Hotels by maximum number of rooms available.
STEP 11: Print user booking data.
STEP 12: Stop the program.

PROGRAM:
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

// Create class for hotel data.


class Hotel {
public:
string name;
int roomAvl;
string
70
location;

71
int rating;
int
pricePr;
};

// Create class for user data.


class User : public Hotel {
public:
string uname;
int uId;
int cost;
};

// Function to Sort Hotels by


// puducherry location
bool sortByBan(Hotel& A, Hotel& B)
{
return A.name > B.name;
}

// Function to sort hotels


// by rating.
bool sortByr(Hotel& A, Hotel& B)
{
return A.rating > B.rating;
}

// Function to sort hotels


// by rooms availability.
bool sortByRoomAvalable(Hotel& A,
Hotel& B)
{
return A.roomAvl < B.roomAvl;
}

// Print hotels data.


void PrintHotelData(vector<Hotel>
hotels)
{
cout << "PRINT HOTELS DATA:" << endl;
cout << "HotelName"
<< " "
<< "Room Avalable"
<< " "
<< "Location"
<< " "
<< "Rating"
72
<< " "

73
<< "PricePer Room:" << endl;

for (int i = 0; i < 3; i++) {


cout <<
hotels[i].name
<< " "
<< hotels[i].roomAvl
<< " "
<< hotels[i].location
<< " "
<< hotels[i].rating
<< " "
<< hotels[i].pricePr
<< endl;
}
cout << endl;
}

// Sort Hotels data by name.


void SortHotelByName(vector<Hotel> hotels)
{
cout << "SORT BY NAME:" << endl;

std::sort(hotels.begin(), hotels.end(),
sortByBan);

for (int i = 0; i < hotels.size(); i++) {


cout << hotels[i].name << " "
<< hotels[i].roomAvl << " "
<< hotels[i].location << " "
<< hotels[i].rating << " "
<< " " << hotels[i].pricePr
<< endl;
}
cout << endl;
}

// Sort Hotels by rating


void SortHotelByRating(vector<Hotel> hotels)
{
cout << "SORT BY A RATING:" << endl;

std::sort(hotels.begin(),
hotels.end(), sortByr);

for (int i = 0; i < hotels.size(); i++) {


cout << hotels[i].name << " "
74
<< hotels[i].roomAvl << " "
<< hotels[i].location << " "
<< hotels[i].rating << " "
<< " " << hotels[i].pricePr
<< endl;
}
cout <<
endl;
}

// Print Hotels for any city


Location. void
PrintHotelBycity(string s, vector<Hotel> hotels)

{
cout << "HOTELS FOR " << s
<< " LOCATION IS:"
<< endl;
for (int i = 0; i < hotels.size(); i++) {

if (hotels[i].location == s) {

cout << hotels[i].name << " "


<< hotels[i].roomAvl << " "
<< hotels[i].location << " "
<< hotels[i].rating << " "
<< " " << hotels[i].pricePr
<< endl;
}
}
cout << endl;
}

// Sort hotels by room Available.


void SortByRoomAvailable(vector<Hotel> hotels)
{
cout << "SORT BY ROOM AVAILABLE:" << endl;

std::sort(hotels.begin(), hotels.end(),
sortByRoomAvalable);

for (int i = hotels.size() - 1; i >= 0; i--) {

cout << hotels[i].name << " "


<< hotels[i].roomAvl << " "
<< hotels[i].location << " "
<< hotels[i].rating << " "
75
<< " " << hotels[i].pricePr

76
<< endl;
}
cout << endl;
}

// Print the user's data


void PrintUserData(string userName[],
int userId[],
int bookingCost[],
vector<Hotel>
hotels)
{

vector<User>
user;
User u;

// Access user data.


for (int i = 0; i < 3; i++) {
u.uname = userName[i];
u.uId = userId[i];
u.cost = bookingCost[i];
user.push_back(u);
}

// Print User data.


cout << "PRINT USER BOOKING DATA:"
<< endl;
cout << "UserName"
<< " "
<< "UserID"
<< " "
<< "HotelName"
<< " "
<< "BookingCost" << endl;

for (int i = 0; i < user.size(); i++) {


cout << user[i].uname
<< " "
<< user[i].uId
<< " "
<< hotels[i].name
<< " "
<< user[i].cost
<< endl;
}
}
77
// Functiont to solve
// Hotel Management problem
void HotelManagement(string userName[],
int userId[],
string hotelName[],
int bookingCost[],
int rooms[],
string locations[],
int ratings[],
int prices[])
{
// Initialize arrays that stores
// hotel data and user data
vector<Hotel> hotels;

// Create Objects for


// hotel and
user.
Hotel h;

// Initialise the data


for (int i = 0; i < 3; i++) {
h.name = hotelName[i];
h.roomAvl = rooms[i];
h.location = locations[i];
h.rating = ratings[i];
h.pricePr = prices[i];
hotels.push_back(h);
}
cout << endl;

// Call the various operations


PrintHotelData(hotels);
SortHotelByName(hotels);
SortHotelByRating(hotels);
PrintHotelBycity("puducherry
hotels);
SortByRoomAvailable(hotels);
PrintUserData(userName,
userId,
bookingCost,
hotels);
}

// Driver Code.
int main()
{
78
// Initialize variables to stores
// hotels data and user data.
string userName[] = { "U1", "U2", "U3" };
int userId[] = { 56,89,85};
string hotelName[] = { "H1", "H2", "H3" };
int bookingCost[] = { 2000, 4000, 5000 };
int rooms[] = { 4, 5, 6 };
string locations[] = { "puducherry","Nagaland","Manipur"};
int ratings[] = { 5, 5, 3 };
int prices[] = { 200, 400, 600 };

// Function to perform operations


HotelManagement(userName, userId,
hotelName, bookingCost,
rooms, locations,
ratings, prices);

return 0;
}

OUTPUT:

79
RESULT:

80

You might also like