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++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter10: Classes And Data Abstraction
Section: Chapter Questions
Problem 28SA
icon
Related questions
Question
  1. Consider the following code segment for a class named   vehicle:

class vehicle {

  private:

    string type;

    string model;

    string color;

    int numWheels;

  public:

    …

};

  1. 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
  2. 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);
steps

Step by step

Solved in 3 steps

Blurred answer
Knowledge Booster
ADT and Class
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.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning
C++ for Engineers and Scientists
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr
Microsoft Visual C#
Microsoft Visual C#
Computer Science
ISBN:
9781337102100
Author:
Joyce, Farrell.
Publisher:
Cengage Learning,