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.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
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
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education