Object-Oriented Programming LAB. 5 asst. Lec. :Sura Nasser LAB 5: In this lab we will focus on operator overloading in Classes. Example 1: write C+ program to create class called distance, that contains data members of feet and inches. The class have two constructors, one to set values, and another to assign values, function to enter values from user, function to display values, and function to find the summation of distances. #include using namespace std; class Distance //English Distance class private: int feet; float inches; public: Distance() : feet(0), inches(0.0) {} /constructor (two args) Distance(int ft, float in) : feet(ft), inches(in) void getdist() //get length from user cout << "nEnter feet: "; cin >> feet; cout << "Enter inches: "; cin >> inches; void showdist() cout << feet << "."« inches << "<= 12.0) //if total exceeds 12.0, i- 12.0; ft; //increase feet by 1 return Distance(f.i); //initialized to sum Object-Oriented Programming LAB. 5 asst. Lec. Sura Nasser

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question
I need the answer as soon as possible
Object-Oriented Programming LAB. 5
asst. Lec. :Sura Nasser
LAB 5:
In this lab we will focus on operator overloading in Classes.
Example 1: write C++ program to create class called distance, that
contains data members of feet and inches . The class have two constructors,
one to set values, and another to assign values , function to enter values
from user, function to display values, and function to find the summation
of distances.
#include <iostream>
using namespace std;
class Distance //English Distance class
private:
int feet;
float inches;
public:
Distance() : feet(0), inches(0.0)
{ } //constructor (two args)
Distance(int ft, float in) : feet(ft), inches(in)
{}
void getdist() //get length from user
cout <<"nEnter feet: ";
cin >> feet;
cout << "Enter inches: ";
cin >> inches;
void showdistO
cout << feet << "'-" << inches << *""<<endl;
Distance operator + (Distance d2) //return sum
int f = feet + d2.feet; //add the feet
float i = inches + d2.inches; /ladd the inches
if(i >= 12.0) //if total exceeds 12.0,
{
i- 12.0;
ft: //increase feet by 1
return Distance(f,i); //initialized to sum
2
Object-Oriented Programming LAB. 5
asst. Lec. :Sura Nasser
int main()
Distance distl, dist3, dist4; /define distances
dist1.getdist(); /lget distl from user
Distance dist2(11, 6.25); //define, initialize dist2
dist3 = dist1 + dist2; //single *+' operator
dist4 = distl + dist2 + dist3; //multiple +' operators
I/display all lengths
cout << "distl = ":
dist1.showdist():
cout << "dist2 = ":
dist2.showdist(O;
cout << "dist3 = ":
dist3.showdist();
cout << "dist4 = ";
dist4.showdist();
return 0;
Exercise:
1- Modify example I to decrement measures by one.
Transcribed Image Text:Object-Oriented Programming LAB. 5 asst. Lec. :Sura Nasser LAB 5: In this lab we will focus on operator overloading in Classes. Example 1: write C++ program to create class called distance, that contains data members of feet and inches . The class have two constructors, one to set values, and another to assign values , function to enter values from user, function to display values, and function to find the summation of distances. #include <iostream> using namespace std; class Distance //English Distance class private: int feet; float inches; public: Distance() : feet(0), inches(0.0) { } //constructor (two args) Distance(int ft, float in) : feet(ft), inches(in) {} void getdist() //get length from user cout <<"nEnter feet: "; cin >> feet; cout << "Enter inches: "; cin >> inches; void showdistO cout << feet << "'-" << inches << *""<<endl; Distance operator + (Distance d2) //return sum int f = feet + d2.feet; //add the feet float i = inches + d2.inches; /ladd the inches if(i >= 12.0) //if total exceeds 12.0, { i- 12.0; ft: //increase feet by 1 return Distance(f,i); //initialized to sum 2 Object-Oriented Programming LAB. 5 asst. Lec. :Sura Nasser int main() Distance distl, dist3, dist4; /define distances dist1.getdist(); /lget distl from user Distance dist2(11, 6.25); //define, initialize dist2 dist3 = dist1 + dist2; //single *+' operator dist4 = distl + dist2 + dist3; //multiple +' operators I/display all lengths cout << "distl = ": dist1.showdist(): cout << "dist2 = ": dist2.showdist(O; cout << "dist3 = ": dist3.showdist(); cout << "dist4 = "; dist4.showdist(); return 0; Exercise: 1- Modify example I to decrement measures by one.
Expert Solution
steps

Step by step

Solved in 4 steps with 2 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY