Consider the following code segment for a class named vehicle: class vehicle { private: string type; string model; string color; int numWheels; public: … }; Write the PROTOTYPE for a function that overloads the + (plus) operator to add two vehicle objects. This prototype would add the numWheels of the objects. Write as a member Write the PROTOTYPE for a function that overloads the << (insertion) operator to print a vehicle object.
Consider the following code segment for a class named vehicle: class vehicle { private: string type; string model; string color; int numWheels; public: … }; Write the PROTOTYPE for a function that overloads the + (plus) operator to add two vehicle objects. This prototype would add the numWheels of the objects. Write as a member Write the PROTOTYPE for a function that overloads the << (insertion) operator to print a vehicle object.
C++ for Engineers and Scientists
4th Edition
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Bronson, Gary J.
Chapter12: Adding Functionality To Your Classes
Section12.2: Providing Class Conversion Capabilities
Problem 6E
Related questions
Question
- Consider the following code segment for a class named vehicle:
class vehicle {
private:
string type;
string model;
string color;
int numWheels;
public:
…
};
- Write the PROTOTYPE for a function that overloads the + (plus) operator to add two vehicle objects. This prototype would add the numWheels of the objects. Write as a member
- Write the PROTOTYPE for a function that overloads the << (insertion) operator to print a vehicle object.
Expert Solution
Step 1: Definitions
Operator overloading:
Operator overloading is giving new meaning or new definition to the existing operator but we cannot change the operator priority.
- It is a compile time polymorphism in which operator is overloaded.
- It simply means to do more than one task at a time on any operator.
- Operators that cannot be overloaded are,
- Scope resolution operator(::)
- sizeof()
- Ternary operator(?:)
Function prototype:·
- The function prototype is used to declare a function.
- It does not include the body of the function and informs the compiler about the function's name and return type.
- The prototype specifies the list of arguments passed, if any, along with their data types.
- The compiler makes use of prototype information to ensure the presence of corresponding function definition and correctness of argument data types.
- In a function prototype, the name of the arguments is optional.
- It is always terminated with a semicolon, unlike function definition which does not have a semicolon at the end.
Step 2: Answer for 1.
The prototype for a function that overloads the “+” operator is as follows,
vehicle operator + (vehicle const &v);
Step by step
Solved in 3 steps
Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Recommended textbooks for you
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning
Microsoft Visual C#
Computer Science
ISBN:
9781337102100
Author:
Joyce, Farrell.
Publisher:
Cengage Learning,
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning
Microsoft Visual C#
Computer Science
ISBN:
9781337102100
Author:
Joyce, Farrell.
Publisher:
Cengage Learning,