You are on page 1of 14

DATE: 1:3:24 Assignment 3

FUNCTION OVERLOADING &


OPERATOR OVERLOADING

1.Write a C++ program to overload a function called orderFoodOnline() in three possible


ways to take up orders
from three different food delivery services.
1. Domino’s offers a flat discount of 20% on all food
orders of above ₹500.
2. Zomato offers discount only on selected food items.
3. Swiggy offers discounts on selected item from selected
restaurants and hotels.
Aim:
To write a C++ Program to execute Function overloading concept in a Food Ordering
Program.
Program

#include<iostream>
using namespace std;
int orderFoodOnline(int cost)
{
if(cost>500)
{
cost-=cost*0.02;
return cost;
}
else
return cost;
}
int orderFoodOnline(string item,int cost)
{
if(item=="ABC"||item=="XYZ"||item=="PQR")
{
cost-=cost*0.02;
return cost;
}
else
return cost;
}
int orderFoodOnline(string item,string hotel,int cost)
{
if((item=="ABC"&&hotel=="h1")||(item=="XYZ"&&hotel=="h2"))
{
cost-=cost*0.02;
return cost;
}
else
return cost;
}
int main()
{
int cost;
string item,hotel;
cout<<"Enter the cost of the product:";
cin>>cost;
cout<<"The cost after discount:"<< orderFoodOnline(cost)<<endl;;
cout<<"Enter the cost of the product:";
cin>>cost;
cout<<"Enter the item:";
cin>>item;
cout<<"The cost after discount:"<< orderFoodOnline(item,cost)<<endl;
cout<<"Enter the cost of the product:";
cin>>cost;
cout<<"Enter the item:";
cin>>item;
cout<<"Enter the hotel name:";
cin>>hotel;
cout<<"The cost after discount:"<< orderFoodOnline(item,hotel,cost)<<endl;
return 0;
}

OUTPUT:

Enter the cost of the product:550


The cost after discount:539
Enter the cost of the product:543
Enter the item:ABC
The cost after discount:532
Enter the cost of the product:320
Enter the item:ABC
Enter the hotel name:h1
The cost after discount:313
2. Write a C++ program to overload + and * to perform addition and multiplication
respectively on two matrices.
Aim
To write a C++ program to overload + and * to perform addition and multiplication
respectively on two matrices.
Program
#include<iostream>
using namespace std;
class matrix
{
private:
int order;
int a[10][10];
public:
matrix()
{
order=0;
for(int i=0; i < 10; ++i)
{
for(int j=0; j < 10; ++j)
{
a[i][j] = 0;
}
}

}
matrix(int o,int m[][10])
{
int i=0,j=0;
order=o;
for(i=0;i<o;i++)
{
for(j=0;j<o;j++)
{
a[i][j]= m[i][j];
}
}
}
matrix operator +(matrix &m1)
{
matrix temp;
temp.order=order;
int i=0,j=0;
for(i=0;i<order;i++)
{
for(j=0;j<order;j++)
{
temp.a[i][j]=a[i][j]+m1.a[i][j];
}
}
return temp;
}
matrix operator *(matrix &m1)
{
matrix tem;
tem.order=order;
int i=0,j=0,k=0;
for(i=0;i<order;i++)
{
for(j=0;j<order;j++)
{
for(k=0;k<order;k++)
{
tem.a[i][j]+=a[i][k]*m1.a[k][j];
}
}
}
return tem;
}
void display()
{
int i,j;
for(i=0;i<order;i++)
{
for(j=0;j<order;j++)
{
cout<<a[i][j]<<" ";
}
cout<<"\n";
}
}
};
int main()
{
int order,a[10][10],i=0,j=0;
cout<<"Enter the order of the matrix:"<<endl;
cin>>order;
cout<<"Enter the elements of 1st matrix:"<<endl;
for(i=0;i<order;i++)
{
for(j=0;j<order;j++)
{
cin>>a[i][j];
}
}
matrix a1(order,a);
cout<<"Enter the order of the matrix:"<<endl;
cin>>order;
cout<<"Enter the elements of 2nd matrix:";
for(i=0;i<order;i++)
{
for(j=0;j<order;j++)
{
cin>>a[i][j];
}
}
matrix a2(order,a);
matrix a3=a1+a2;
cout<<"ADDITION:"<<endl;
a3.display();
cout<<"MULTIPLICATION:";
matrix a4=a1*a2;
cout<<endl;
a4.display();
return 0;
}
OUTPUT:
Enter the order of the matrix:
3
Enter the elements of 1st matrix:
123
456
789
Enter the order of the matrix:
3
Enter the elements of 2nd matrix:
123
456
789
ADDITION:
246
8 10 12
14 16 18
MULTIPLICATION:
30 36 42
66 81 96
102 126 150
Assignment 3
Constructor overloading
1. Friend Class and Friend Function:
In geometry, a point is a position in space. We can define a point in 3d-space as the
set of coordinates x, y, and z. For example, Point { 2.0, 1.0, 0.0 } would be the point
at coordinate space x=2.0, y=1.0, and z=0.0. In physics, a vector is a quantity that has
a magnitude (length) and a direction (but no position). We can define a vector in 3d-
space as an x, y, and z value representing the direction of the vector along the x, y,
and z axis (the length can be derived from these). For example, Vector { 2.0, 0.0, 0.0
} would be a vector representing a direction along the positive x-axis (only), with
length 2.0.
A Vector can be applied to a Point to move the Point to a new position. This is done
by adding the vector’s direction to the point’s position to yield a new position. For
example, Point { 2.0, 1.0, 0.0 } + Vector { 2.0, 0.0, 0.0 } would yield Point { 4.0, 1.0,
0.0 }.
-Define Point3d and Vector3d classes with appropriate constructors.
-Make Vector3d, a friend class of Point3d and implement function moveByVector()
in Point3d.
-Instead of making Vector3d class a friend of class Point3d , make the member
function moveByVector a friend of class Point3d.Aim

a). AIM:
To write a C++ Program to manipulate a point and change its magnitude using vector by
using a friend function from a class.

Program
#include<iostream>
using namespace std;
class Vector;
class Point
{
private:
int x;
int y;
int z;
public:
Point(int x1,int y1,int z1)
{
x=x1;
y=y1;
z=z1;
}

int Displacement(Vector &v);


void display()
{
cout<<x<<","<<y<<","<<z;
}
};
class Vector
{
private:
int vx;
int vy;
int vz;
friend int Point::Displacement(Vector &v);
public:
Vector(int x1,int y1,int z1)
{
vx=x1;
vy=y1;
vz=z1;
}

};

int Point :: Displacement(Vector &v)


{
x+=v.vx;
y+=v.vy;
z+=v.vz;
return 0;
}
int main()
{
int x1,y1,z1;
cout<<"Enter X,Y,Z coordinates:";
cin>>x1>>y1>>z1;
Point p1(x1,y1,z1);
cout<<"Enter X,Y,Z of vector:";
cin>>x1>>y1>>z1;
Vector v1(x1,y1,z1);
p1.Displacement(v1);

p1.display();
return 0;
}

OUTPUT:

Enter X,Y,Z coordinates:2 4 5


Enter X,Y,Z of vector:2 3 4
4,7,9
b). AIM:
To write a C++ Program to manipulate a point and change its magnitude using vector by
using a friend class.

PROGRAM:
#include<iostream>
using namespace std;
class Vector;
class Point
{
private:
int x;
int y;
int z;
public:
Point(int x1,int y1,int z1)
{
x=x1;
y=y1;
z=z1;
}

int Displacement(Vector &v);


void display()
{
cout<<x<<","<<y<<","<<z;
}
};
class Vector
{
private:
int vx;
int vy;
int vz;
friend class Point;
public:
Vector(int x1,int y1,int z1)
{
vx=x1;
vy=y1;
vz=z1;
}

};
int Point :: Displacement(Vector &v)
{
x+=v.vx;
y+=v.vy;
z+=v.vz;
return 0;
}
int main()
{
int x1,y1,z1;
cout<<"Enter X,Y,Z coordinates:";
cin>>x1>>y1>>z1;
Point p1(x1,y1,z1);
cout<<"Enter X,Y,Z of vector:";
cin>>x1>>y1>>z1;
Vector v1(x1,y1,z1);
p1.Displacement(v1);

p1.display();
return 0;
}

OUTPUT:

Enter X,Y,Z coordinates:1 2 3


Enter X,Y,Z of vector:1 1 1
2,3,4
STATIC VARIABLES AND MEMBER FUNCTIONS

2. Static members and Member Functions


Define a class called IDGenerator which generates unique ID for candidates
registering for two different courses offered by an Academic Institution. ( Example :
Course 1 IDs: C1001,C1002….
Course 2 IDs:C2001,C2002….
Include a static member function that displays the total number of students registered
in each course separately. 2. Static members and Member Functions
Define a class called IDGenerator which generates unique ID for candidates
registering for two different courses offered by an Academic Institution. ( Example :
Course 1 IDs: C1001,C1002….
Course 2 IDs:C2001,C2002….
Include a static member function that displays the total number of students registered
in each course separately.

AIM:
To write a C++ code that generates unique id’s for students registering in different
courses.

PROGRAM:
#include<iostream>
#define TRUE 1
using namespace std;
class IDGenerator
{
private:
static int course1ID;
static int course2ID;
static int count1;
static int count2;
public:

IDGenerator()
{
course1ID=1000;
course2ID=2000;
}
int IDset(int x)
{
if(x==1)
{
course1ID++;
disp1();
count1++;
}
else
{
course2ID++;
disp2();
count2++;
}
return 0;
}
static void disp1()
{
cout<<"C1"<<course1ID<<endl;
}
static void disp2()
{
cout<<"C2"<<course2ID<<endl;
}
static void display()
{
cout<<"Course 1:"<<count1<<endl;
cout<<"Course 2:"<<count2<<endl;
}
};

int IDGenerator::count1=0;
int IDGenerator::count2=0;
int IDGenerator::course1ID=1000;
int IDGenerator::course2ID=2000;

int main()
{
IDGenerator ID[10];
int x,i;
while(TRUE)
{
cout<<"Enter the course you want:1/2<0 to end>";
cin>>x;
if(x==0)
break;
ID[i].IDset(x);
}
ID[i].display();

return 0;
}
OUTPUT:
Enter the course you want:1/2<0 to end>1
C11001
Enter the course you want:1/2<0 to end>2
C22001
Enter the course you want:1/2<0 to end>2
C22002
Enter the course you want:1/2<0 to end>1
C11002
Enter the course you want:1/2<0 to end>1
C11003
Enter the course you want:1/2<0 to end>0
Course 1:3
Course 2:2

You might also like