Question

Overload a relational operator for the Job class: Assume that this operator is not overloaded for...

Overload a relational operator for the Job class:
Assume that this operator is not overloaded for the Salary class.

a) Write how the function will be declared in your code.
b) Write an external function definition (not inside the class).

solve it in C++10

to solve this you nedd question number 1.

Create a composition between the classes Job and Salary.

Class Salary

a. data member:
   1. money
b. constructors:
   1. default constructor
   2. user defined constructor with a parameter to set money
c. standard accessor and mutator functions

Class Job

a. data members:
   title (name of the job)
   salary (object of type Salary)

b. constructors:
   1. default constructor
   2. user defined constructor to set title and salary
   3. implement constructor delegation

c. standard accessor and mutator function for title
d. accessor function to read the money value of the internal Salary object
e. mutator function to update the money value of the internal Salary object
f. accessor function to return a copy of the internal Salary object
g. mutator function to replace the internal Salary object

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Output:

CODE:

 #include<iostream> using namespace std; //Salary class class Salary { //private members private: int money; public: //constructors Salary() { money=0; } Salary(int m) { money=m; } //accessor int getMoney() { return money; } //mutator void setMoney(int m) { money=m; } }; class Job { //private members private: string title; Salary salary; public: //constructors Job() { } Job(string t,Salary s) { title=t; salary=s; } Job(string t,int money) { Salary s(money); title=t; salary=s; } //accessors and mutators for title,money and salary string getTitle() { return title; } void setTitle(string t) { title=t; } int getMoney() { return salary.getMoney(); } void setMoney(int money) { salary.setMoney(money); } Salary getSalary() { return salary; } void setSalary(Salary s) { salary=s; } //operator < overloaded //returns true if this object's salary is less than Job j's salary bool operator<(Job &j) { return (salary.getMoney()<j.salary.getMoney()); } }; int main() { //create 2 objects and display their details Job j1("Engineer",100000); cout<<j1.getTitle()<<" "<<j1.getMoney()<<"\n"; Job j2("Scientist",11000); cout<<j2.getTitle()<<" "<<j2.getMoney()<<"\n"; //compare 2 objects, using < operator if(j2<j1) { cout<<j2.getTitle()<<" gets more salary than "<<j1.getTitle()<<endl; } else { cout<<j1.getTitle()<<" gets more salary than "<<j2.getTitle()<<endl; } return 0; }
Add a comment
Know the answer?
Add Answer to:
Overload a relational operator for the Job class: Assume that this operator is not overloaded for...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • overload the relational operator == for the class stacktype

    How do overload the relational operator == for the class stackType that returns true if two stacks of the same type are the same, false otherwise. Also write thedefintion of the function template to overload this operator.

  • Student.h Student.cpp Person.h Person.cpp In this assignment, you need to create a class to keep track...

    Student.h Student.cpp Person.h Person.cpp In this assignment, you need to create a class to keep track of books borrowed bya student. Each book has a name, Student (who borrowed it), and the date that was borrowed (see the .h file below). Use also need to use the Person class and the Student class included in the folder. Student class inherits from the Person class as was shown in the classroom Create a Date class (in the same project of the...

  • Design a class named Month. The class should have the following private members:

    Design a class named Month. The class should have the following private members:   • name - A string object that holds the name of a month, such as "January", "February", etc.   • monthNumber - An integer variable that holds the number of the month. For example, January would be 1, February would be 2, etc. Valid values for this variable are 1 through 12.  In addition, provide the following member functions:• A default constructor that sets monthNumber to 1 and name...

  • C++ This chapter uses the class rectangleType to illustrate how to overload the operators +, *,...

    C++ This chapter uses the class rectangleType to illustrate how to overload the operators +, *, ==, !=, >>, and <<. In this exercise, first redefine the class rectangleType by declaring the instance variables as protected and then overload additional operators as defined in parts 1 to 3. Overload the pre- and post-increment and decrement operators to increment and decrement, respectively, the length and width of a rectangle by one unit. (Note that after decrementing the length and width, they...

  • Question 3: Q3 (Polymorphism & virtual function) a) Consider the class, derived class, and the virtual functions as shown in Display for Q3. Create a derived class (of Sale) called 'Mailorder...

    Question 3: Q3 (Polymorphism & virtual function) a) Consider the class, derived class, and the virtual functions as shown in Display for Q3. Create a derived class (of Sale) called 'MailorderSale' having one private member variable called 'shipping charge'. It should have constructor function and virtual function 'bill' (this function adds shipping charge to the price). Define all these functions (no need of default constructors). b) In the main function, define one object of DiscountSale with (11,10) initial values and...

  • 5. A complex number consists of two components: the real component and the imaginary component. A...

    C++ //add as many comments as possible 5. A complex number consists of two components: the real component and the imaginary component. An example of a complex number is 2+3i, where 2 is the real component and 3 is the imaginary component of the data. Define a class MyComplexClass. It has two data values of float type: real and imaginary This class has the following member functions A default constructor that assigns 0.0 to both its real and imaginary data...

  • 1. Create an “include guard” (all lines of code required) for a file “plane.h” 2. When...

    1. Create an “include guard” (all lines of code required) for a file “plane.h” 2. When you look at a constructor, how can you determine if it is THE default constructor? 3. What can a “friend” function do that other non-member functions can not do? 4. class plane { int x;} ---------------- is x public or private? 5. What kind of a search first sorts the data into ascending or descending order, then splits the data in half, searches, splits,...

  • C++ Assignment: Create a class called FitnessMember that has only one variable, called name. The FitnessMember...

    C++ Assignment: Create a class called FitnessMember that has only one variable, called name. The FitnessMember class should have a constructor with no parameters, a constructor with a parameter, a mutator function and an accessor function. Also, include a function to display the name. Define a class called Athlete which is derived from FitnessMember. An Athlete record has the Athlete's name (defined in the FitnessMember class), ID number of type String and integer number of training days. Define a class...

  • 1a. Create a class named Inventory - Separate declaration from implementation (i.e. Header and CPP files)...

    1a. Create a class named Inventory - Separate declaration from implementation (i.e. Header and CPP files) 1b. Add the following private member variables - a int variable named itemNumber - an int variable named quantity - a double variable named cost - a double variable named totalCost. Total cost is defined as quantity X cost. - updateTotalCost() which takes no arguments and sets the values of the variable totalCost. 1c. Add the following public functions: - Accessor and Mutators for...

  • write a C++ program using classes, friend functions and overloaded operators. Computer class: Design a computer...

    write a C++ program using classes, friend functions and overloaded operators. Computer class: Design a computer class (Computer) that describes a computer object. A computer has the following properties: Amount of Ram in GB (examples: 8, 16), defaults to 8. Hard drive size in GB (examples: 500, 1000), defaults to 500. Speed in GHz (examples: 1.6, 2.4), defaults to 1.6. Type (laptop, desktop), defaults to "desktop" Provide the following functions for the class: A default constructor (constructor with no parameters)...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT