0% found this document useful (0 votes)
50 views

Function Overloading: Using A Single Function Name To Handle Different Numbers

The document discusses function overloading in C++ by defining multiple input functions for a rectangle class that take different numbers and types of arguments. It also covers operator overloading by defining a + operator for the rectangle class that adds the lengths and widths of two rectangles. Finally, it provides tasks to design a Job class with overloaded extraction, insertion, +, and - operators to work with job times and rates.

Uploaded by

arifa
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)
50 views

Function Overloading: Using A Single Function Name To Handle Different Numbers

The document discusses function overloading in C++ by defining multiple input functions for a rectangle class that take different numbers and types of arguments. It also covers operator overloading by defining a + operator for the rectangle class that adds the lengths and widths of two rectangles. Finally, it provides tasks to design a Job class with overloaded extraction, insertion, +, and - operators to work with job times and rates.

Uploaded by

arifa
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/ 3

Lab 04 : Understanding friends and overloading in C++

Function Overloading: Using a single function name to handle different numbers


and different types of arguments to perform different types of tasks is known as
function overloading.
class rectangle
{
private:
int length;
int width;
int area;
public:
input();
input(int,int);
void output();
};
void rectangle::input()
{
length=5;
width=10;
}
void rectangle::input(int l, int w)
{
length=l;
width=w;
}
void rectangle::output(void)
{
area=length*width;
cout<<area:
}
void main()
{ rectangle r1, r2;
r1.input();
r2.input(10,20);
r1.output();
r2.output();
}
Lab 04 : Understanding friends and overloading in C++

Operator Overloading:
class rectangle
{
private:
int length;
int width;
public:
void input();
rectangle operator+(rectangle);
void display();
};
void rectangle::input()
{
cout<<"enter length:";
cin>>length;
cout<<"enter width:";
cin>>width;
}
rectangle rectangle::operator+(rectangle r4)
{
rectangle r5;
r5.length=length+r4.length;
r5.width=width+r4.width;
return(r5);
}
void rectangle::display()
{
cout<<"length:"<<length;
cout<<"\nwidth:"<<width;
}
void main()
{
clrscr();
rectangle r1,r2,r3;
r1.input();
r2.input();
r3=r1+r2; // r3=r1.operator+(r2);
Lab 04 : Understanding friends and overloading in C++

r3.display();
getch();
}

Complete the following tasks:


a. Design a Job class with three data fields—Job number, time in hours to
complete the Job, and per-hour rate charged for the Job.
b. Include overloaded extraction and insertion operators that get and display
a Job’s values.
c. Include overloaded + and – operators that return integers that indicate the
total time for two Jobs, and indicate the difference in time between two Jobs,
respectively.
d. Write a main()function demonstrating that all the functions work
correctly. Save the file as Jobs.cpp.

You might also like