SlideShare a Scribd company logo
1 of 3
Download to read offline
Operator overloading; unary and binary math operators. Design a class called Feetinches. Class
is designed to hold distances or measurements expressed in feet and inches. It consists of eight
member functions: A constructor that allows the feet and inches members to be set. The default
values for these members is zero. A setFeet function for storing a value in the feet member. A
setinches function for storing a value in the inches member. A getFeet function for returning the
value in the feet member. A getinches function for returning the value in the inches member. A
simplify function for normalizing the values held in feet and inches. This function adjusts any set
of values where the inches member is greater than 12 or less than 0. An operator + function that
overloads the standard + math operator. An operator - function that overloads the standard -
math operator. ++ Prefix and postfix increment operators. These operators should increment the
number of inches stored in the object. When incremented, the number of feet should be
automatically simplified. -- Prefix and postfix decrement operators. These operators should
decrement the number of inches stored in the object. When decremented, the number of feet
should be automatically simplified.
Solution
Here is the code for you:
class FeetInches
{
int feet;
int inches;
public:
FeetInches()
{
feet = inches = 0;
}
FeetInches(int f, int i)
{
feet = f;
inches = i;
}
void setFeet(int f)
{
feet = f;
}
void setInches(int i)
{
inches = i;
}
int getFeet()
{
return feet;
}
int getInches()
{
return inches;
}
void simplify()
{
feet += inches / 12;
inches %= 12;
}
FeetInches operator+(FeetInches &other)
{
FeetInches temp;
temp.feet = feet + other.feet;
temp.inches = inches + other.inches;
return temp;
}
FeetInches operator-(FeetInches &other)
{
FeetInches temp;
temp.feet = feet - other.feet;
temp.inches = inches - other.inches;
return temp;
}
FeetInches& operator++()
{
feet++;
inches++;
return *this;
}
FeetInches operator++(int)
{
FeetInches temp(*this);
operator++();
return temp;
}
FeetInches& operator--()
{
feet--;
inches--;
return *this;
}
FeetInches operator--(int)
{
FeetInches temp(*this);
operator--();
return temp;
}
};

More Related Content

Similar to Operator overloading; unary and binary math operators. Design a clas.pdf

03 Variables - Chang.pptx
03 Variables - Chang.pptx03 Variables - Chang.pptx
03 Variables - Chang.pptx
Dileep804402
 
08 c-operator-overloadingppt2563
08 c-operator-overloadingppt256308 c-operator-overloadingppt2563
08 c-operator-overloadingppt2563
Youth For Peace
 
08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt
Tareq Hasan
 
Functional programming basics
Functional programming basicsFunctional programming basics
Functional programming basics
openbala
 
C++ Linux Need help with lab assignment- Instructions and code is post.docx
C++ Linux Need help with lab assignment- Instructions and code is post.docxC++ Linux Need help with lab assignment- Instructions and code is post.docx
C++ Linux Need help with lab assignment- Instructions and code is post.docx
RichardjOZTerryp
 
Programming techniques
Programming techniquesProgramming techniques
Programming techniques
Prabhjit Singh
 
The Final Programming Project
The Final Programming ProjectThe Final Programming Project
The Final Programming Project
Sage Jacobs
 
C programming session 02
C programming session 02C programming session 02
C programming session 02
Dushmanta Nath
 

Similar to Operator overloading; unary and binary math operators. Design a clas.pdf (20)

Code optimization
Code optimization Code optimization
Code optimization
 
Code optimization
Code optimization Code optimization
Code optimization
 
Function in C++, Methods in C++ coding programming
Function in C++, Methods in C++ coding programmingFunction in C++, Methods in C++ coding programming
Function in C++, Methods in C++ coding programming
 
Functional Programming in Swift
Functional Programming in SwiftFunctional Programming in Swift
Functional Programming in Swift
 
Elegant Solutions for Everyday Python Problems Pycon 2018 - Nina Zakharenko
Elegant Solutions for Everyday Python Problems Pycon 2018 - Nina ZakharenkoElegant Solutions for Everyday Python Problems Pycon 2018 - Nina Zakharenko
Elegant Solutions for Everyday Python Problems Pycon 2018 - Nina Zakharenko
 
From object oriented to functional domain modeling
From object oriented to functional domain modelingFrom object oriented to functional domain modeling
From object oriented to functional domain modeling
 
From object oriented to functional domain modeling
From object oriented to functional domain modelingFrom object oriented to functional domain modeling
From object oriented to functional domain modeling
 
Python Lecture 4
Python Lecture 4Python Lecture 4
Python Lecture 4
 
03 Variables - Chang.pptx
03 Variables - Chang.pptx03 Variables - Chang.pptx
03 Variables - Chang.pptx
 
08 c-operator-overloadingppt2563
08 c-operator-overloadingppt256308 c-operator-overloadingppt2563
08 c-operator-overloadingppt2563
 
08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt
 
Functional programming basics
Functional programming basicsFunctional programming basics
Functional programming basics
 
Lecture 8.pdf
Lecture 8.pdfLecture 8.pdf
Lecture 8.pdf
 
C++ Linux Need help with lab assignment- Instructions and code is post.docx
C++ Linux Need help with lab assignment- Instructions and code is post.docxC++ Linux Need help with lab assignment- Instructions and code is post.docx
C++ Linux Need help with lab assignment- Instructions and code is post.docx
 
Programming techniques
Programming techniquesProgramming techniques
Programming techniques
 
Programing techniques
Programing techniquesPrograming techniques
Programing techniques
 
Operators
OperatorsOperators
Operators
 
The Final Programming Project
The Final Programming ProjectThe Final Programming Project
The Final Programming Project
 
Functions
FunctionsFunctions
Functions
 
C programming session 02
C programming session 02C programming session 02
C programming session 02
 

More from arenamobiles123

Describe how to use sanction such that a process can send a payload w.pdf
Describe how to use sanction such that a process can send a payload w.pdfDescribe how to use sanction such that a process can send a payload w.pdf
Describe how to use sanction such that a process can send a payload w.pdf
arenamobiles123
 
based on this evidence Syconoid sponges were derived from leuconoid.pdf
based on this evidence  Syconoid sponges were derived from leuconoid.pdfbased on this evidence  Syconoid sponges were derived from leuconoid.pdf
based on this evidence Syconoid sponges were derived from leuconoid.pdf
arenamobiles123
 
A) If a DNA double helix that is 100 base pairs in length has 32 a.pdf
A) If a DNA double helix that is 100 base pairs in length has 32 a.pdfA) If a DNA double helix that is 100 base pairs in length has 32 a.pdf
A) If a DNA double helix that is 100 base pairs in length has 32 a.pdf
arenamobiles123
 
Chapter2...22.            (Problem 3) Which of the following are.pdf
Chapter2...22.            (Problem 3) Which of the following are.pdfChapter2...22.            (Problem 3) Which of the following are.pdf
Chapter2...22.            (Problem 3) Which of the following are.pdf
arenamobiles123
 
Write a program that takes any input text and produces both a frequen.pdf
Write a program that takes any input text and produces both a frequen.pdfWrite a program that takes any input text and produces both a frequen.pdf
Write a program that takes any input text and produces both a frequen.pdf
arenamobiles123
 
why is reversibility an important characteristic of childrens .pdf
why is reversibility an important characteristic of childrens .pdfwhy is reversibility an important characteristic of childrens .pdf
why is reversibility an important characteristic of childrens .pdf
arenamobiles123
 
What are 2 ways that Alexander Hamilton’s ideas influenced the Unite.pdf
What are 2 ways that Alexander Hamilton’s ideas influenced the Unite.pdfWhat are 2 ways that Alexander Hamilton’s ideas influenced the Unite.pdf
What are 2 ways that Alexander Hamilton’s ideas influenced the Unite.pdf
arenamobiles123
 
Which key numbers help you asses your performance in an organizat.pdf
Which key numbers help you asses your performance in an organizat.pdfWhich key numbers help you asses your performance in an organizat.pdf
Which key numbers help you asses your performance in an organizat.pdf
arenamobiles123
 
Social behavior in termites does not include the reproductive isolat.pdf
Social behavior in termites does not include the reproductive isolat.pdfSocial behavior in termites does not include the reproductive isolat.pdf
Social behavior in termites does not include the reproductive isolat.pdf
arenamobiles123
 
Sam, a 27-year-old African-American male, was admitted to the hospit.pdf
Sam, a 27-year-old African-American male, was admitted to the hospit.pdfSam, a 27-year-old African-American male, was admitted to the hospit.pdf
Sam, a 27-year-old African-American male, was admitted to the hospit.pdf
arenamobiles123
 

More from arenamobiles123 (20)

Consider the following experiment. There are 5 members of a team Jo.pdf
Consider the following experiment. There are 5 members of a team Jo.pdfConsider the following experiment. There are 5 members of a team Jo.pdf
Consider the following experiment. There are 5 members of a team Jo.pdf
 
A prolific couple had ten children.Through hard work and diligence a.pdf
A prolific couple had ten children.Through hard work and diligence a.pdfA prolific couple had ten children.Through hard work and diligence a.pdf
A prolific couple had ten children.Through hard work and diligence a.pdf
 
Describe how to use sanction such that a process can send a payload w.pdf
Describe how to use sanction such that a process can send a payload w.pdfDescribe how to use sanction such that a process can send a payload w.pdf
Describe how to use sanction such that a process can send a payload w.pdf
 
based on this evidence Syconoid sponges were derived from leuconoid.pdf
based on this evidence  Syconoid sponges were derived from leuconoid.pdfbased on this evidence  Syconoid sponges were derived from leuconoid.pdf
based on this evidence Syconoid sponges were derived from leuconoid.pdf
 
At noon Joyce drove to the lake at 30 miles per hour, but she made t.pdf
At noon Joyce drove to the lake at 30 miles per hour, but she made t.pdfAt noon Joyce drove to the lake at 30 miles per hour, but she made t.pdf
At noon Joyce drove to the lake at 30 miles per hour, but she made t.pdf
 
A) If a DNA double helix that is 100 base pairs in length has 32 a.pdf
A) If a DNA double helix that is 100 base pairs in length has 32 a.pdfA) If a DNA double helix that is 100 base pairs in length has 32 a.pdf
A) If a DNA double helix that is 100 base pairs in length has 32 a.pdf
 
Chapter2...22.            (Problem 3) Which of the following are.pdf
Chapter2...22.            (Problem 3) Which of the following are.pdfChapter2...22.            (Problem 3) Which of the following are.pdf
Chapter2...22.            (Problem 3) Which of the following are.pdf
 
Write a program that takes any input text and produces both a frequen.pdf
Write a program that takes any input text and produces both a frequen.pdfWrite a program that takes any input text and produces both a frequen.pdf
Write a program that takes any input text and produces both a frequen.pdf
 
why is reversibility an important characteristic of childrens .pdf
why is reversibility an important characteristic of childrens .pdfwhy is reversibility an important characteristic of childrens .pdf
why is reversibility an important characteristic of childrens .pdf
 
Which of the following tools is used to generate the profiles and se.pdf
Which of the following tools is used to generate the profiles and se.pdfWhich of the following tools is used to generate the profiles and se.pdf
Which of the following tools is used to generate the profiles and se.pdf
 
What are 2 ways that Alexander Hamilton’s ideas influenced the Unite.pdf
What are 2 ways that Alexander Hamilton’s ideas influenced the Unite.pdfWhat are 2 ways that Alexander Hamilton’s ideas influenced the Unite.pdf
What are 2 ways that Alexander Hamilton’s ideas influenced the Unite.pdf
 
Which of the following is a non-respiratory function of the lunga.pdf
Which of the following is a non-respiratory function of the lunga.pdfWhich of the following is a non-respiratory function of the lunga.pdf
Which of the following is a non-respiratory function of the lunga.pdf
 
Which key numbers help you asses your performance in an organizat.pdf
Which key numbers help you asses your performance in an organizat.pdfWhich key numbers help you asses your performance in an organizat.pdf
Which key numbers help you asses your performance in an organizat.pdf
 
what two things can system elements do with energy in the system.pdf
what two things can system elements do with energy in the system.pdfwhat two things can system elements do with energy in the system.pdf
what two things can system elements do with energy in the system.pdf
 
What is an Intervening Variable, Casual Prior Variable, and Partial .pdf
What is an Intervening Variable, Casual Prior Variable, and Partial .pdfWhat is an Intervening Variable, Casual Prior Variable, and Partial .pdf
What is an Intervening Variable, Casual Prior Variable, and Partial .pdf
 
The basic functional plan of the gonads includes all of the followin.pdf
The basic functional plan of the gonads includes all of the followin.pdfThe basic functional plan of the gonads includes all of the followin.pdf
The basic functional plan of the gonads includes all of the followin.pdf
 
The color of the small lymphocytes cytoplasm is a very () lilac a.pdf
The color of the small lymphocytes cytoplasm is a very () lilac a.pdfThe color of the small lymphocytes cytoplasm is a very () lilac a.pdf
The color of the small lymphocytes cytoplasm is a very () lilac a.pdf
 
Social behavior in termites does not include the reproductive isolat.pdf
Social behavior in termites does not include the reproductive isolat.pdfSocial behavior in termites does not include the reproductive isolat.pdf
Social behavior in termites does not include the reproductive isolat.pdf
 
Sam, a 27-year-old African-American male, was admitted to the hospit.pdf
Sam, a 27-year-old African-American male, was admitted to the hospit.pdfSam, a 27-year-old African-American male, was admitted to the hospit.pdf
Sam, a 27-year-old African-American male, was admitted to the hospit.pdf
 
question 4) Which of following elements are nonmetals Na O Ar S .pdf
question 4) Which of following elements are nonmetals Na O Ar S .pdfquestion 4) Which of following elements are nonmetals Na O Ar S .pdf
question 4) Which of following elements are nonmetals Na O Ar S .pdf
 

Recently uploaded

Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
EADTU
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
AnaAcapella
 
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lessonQUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
httgc7rh9c
 

Recently uploaded (20)

Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.ppt
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
Model Attribute _rec_name in the Odoo 17
Model Attribute _rec_name in the Odoo 17Model Attribute _rec_name in the Odoo 17
Model Attribute _rec_name in the Odoo 17
 
Tatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsTatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf arts
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lessonQUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Introduction to TechSoup’s Digital Marketing Services and Use Cases
Introduction to TechSoup’s Digital Marketing  Services and Use CasesIntroduction to TechSoup’s Digital Marketing  Services and Use Cases
Introduction to TechSoup’s Digital Marketing Services and Use Cases
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
Economic Importance Of Fungi In Food Additives
Economic Importance Of Fungi In Food AdditivesEconomic Importance Of Fungi In Food Additives
Economic Importance Of Fungi In Food Additives
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 

Operator overloading; unary and binary math operators. Design a clas.pdf

  • 1. Operator overloading; unary and binary math operators. Design a class called Feetinches. Class is designed to hold distances or measurements expressed in feet and inches. It consists of eight member functions: A constructor that allows the feet and inches members to be set. The default values for these members is zero. A setFeet function for storing a value in the feet member. A setinches function for storing a value in the inches member. A getFeet function for returning the value in the feet member. A getinches function for returning the value in the inches member. A simplify function for normalizing the values held in feet and inches. This function adjusts any set of values where the inches member is greater than 12 or less than 0. An operator + function that overloads the standard + math operator. An operator - function that overloads the standard - math operator. ++ Prefix and postfix increment operators. These operators should increment the number of inches stored in the object. When incremented, the number of feet should be automatically simplified. -- Prefix and postfix decrement operators. These operators should decrement the number of inches stored in the object. When decremented, the number of feet should be automatically simplified. Solution Here is the code for you: class FeetInches { int feet; int inches; public: FeetInches() { feet = inches = 0; } FeetInches(int f, int i) { feet = f; inches = i; } void setFeet(int f) { feet = f; }
  • 2. void setInches(int i) { inches = i; } int getFeet() { return feet; } int getInches() { return inches; } void simplify() { feet += inches / 12; inches %= 12; } FeetInches operator+(FeetInches &other) { FeetInches temp; temp.feet = feet + other.feet; temp.inches = inches + other.inches; return temp; } FeetInches operator-(FeetInches &other) { FeetInches temp; temp.feet = feet - other.feet; temp.inches = inches - other.inches; return temp; } FeetInches& operator++() { feet++; inches++; return *this;
  • 3. } FeetInches operator++(int) { FeetInches temp(*this); operator++(); return temp; } FeetInches& operator--() { feet--; inches--; return *this; } FeetInches operator--(int) { FeetInches temp(*this); operator--(); return temp; } };