Question
C++ Please use two .cpp files and and one .h file forthe code . Please follow the directions below. What if we could add colors
C++
Please use two .cpp files and and one .h file forthe code. Please follow the directions below.
What if we could add colors like this: newColor = red + blue; Orwhat if we could compare athletes like this: if (Tyson > Ali)With OOP, we can make our code do some unusual things such as useoperators like the plus sign to “add” things that are not numbers!Let’s see how this is done.
We are going to write a class that will allow using the plussign to “add” two breeds of dogs and get a new breed!
Mutt |
- breed : string |
+Mutt() //default constructor; setbreed to “unknown” +Mutt(brd : string) +getBreed() : string +setBreed(brd : string) : void +operator+(Mutt brd2) : Mutt //overload theadd operator to combine dog breeds +operator<<( ostream &output, const Mutt &brd) :friend ostream& //overload the output stream operator; codegiven below |
STEP 1 – Create New Project
For this project, we will create a separate header filefor our class named, Mutt.h. The code for the member functions willbe placed in a .cpp file named, Mutt.cpp. We will also add a .cppfile for the main function.
In Visual Studio, use the Project menu and select, Add Class. Awindow will pop up where you select a C++ class and click the Addbutton. Another window asks for the name of your class. Enter:Mutt. You will see that Mutt.h and Mutt.cpp will be created soclick Finish. The files are created with some default starting codefor your class such as a default constructor and destructor.
Add another source file (.cpp) file to your project with a nameof your own choice for the main function; now we have 3 filesshowing in the Solution Explorer window.
STEP 2 – Coding the Class
Begin by creating the class in the Mutt.h file. At the top ofthe file, you should already see:
#pragma once
which prevents redefinition errors. Under this, put:
#include
#include
using namespace std;
Start your class definition. Use the UML above to declare theprivate data member. Then begin a public section. In that section,we will put the member function prototypes only; the code will beput in the Mutt.cpp file. Add prototypes for the two constructors,the setter and getter and the overloaded operators (follow theUML).
In the Mutt.cpp file, keep the #include for Mutt.h; this willalso give you the #include’s from the header file so no need torepeat those.
Code the constructors, setter and getter. Remember to put theclass name and scope resolution operator in front of the functionnames. Example:
Mutt::Mutt() {breed = “unknown”;}
STEP 3 – Overloading the Plus Operator
Let’s understand the overloaded plus operator. We want to beable to add two Mutt objects to get a new Mutt. What does thismean? We want to be able to add poodle and labrador and getlabradoodle.
So how can we add Mutt objects? First let’s look at how C++handles the statement:
brd3 = brd1 + brd2; //these are 3 Muttobjects
Suppose we create three such objects in our main function likethis:
Mutt brd1, brd2, brd3; //three objects with breed setto “unknown”
Now we want to use the setters to give data to the first two.Let’s say we want brd1 to be a poodle and brd2 to be a labrador(retriever):
brd1.setName(“poodle”);
brd2.setName(“labrador”);
If we overload the plus operator, we could add these Muttobjects to get a combination breed called a “labradoodle” likethis:
brd3 = brd1 + brd2;
cout << “The new breed is a “ << brd3 << endl;//Expect brd3 to be labradoodle
IMPORTANT TO UNDERSTAND: Now C++ won’t know how to add Muttobjects without coding the overloaded operator function for theplus sign. C++ also doesn’t know how to print out a Mutt objectwithout overloading the << operator. We could use getBreed()but it’s “cooler” to be able to write the code shown above.
The way C++ handles the first statement is this: it uses theoperator+ function for the brd1 object. The parameter passed to thefunction is the brd2 object. The object returned by the function isthe brd3 object.
Begin your overloaded plus sign operator function with theinformation shown in the UML above using the return type andparameter shown and adding the class name and scope resolutionoperator:
As we see, the function needs to return a Mutt object so wedeclare a local Mutt object in our function; let’s say we name thisobject: newBreed:
Mutt newBreed;
Now we do the magic. We want to set the name of the “new” breedbased on the current object’s breed and the parameter’s breed.Right now, let’s just handle poodles and labradors to makelabradoodles. So the object being passed in can be poodle orlabrador and it is either the same or opposite of the currentobject’s name. So we will use an if statement to handle thesepossibilities:
if (this->breed == brd2.breed)
newBreed.breed = this->breed; //both Breeds are thesame so nothing new created
else if (this->breed == “poodle” && brd2.breed ==“labrador”)
newBreed.breed = “labradoodle”;
else if (this->breed == “labrador” && brd2.breed ==“poodle”)
newBreed.breed = “labradoodle”;
Now all we have to do is return the new object:
return newBreed;
} //end overloaded + function
NOTE: if other breeds are “added”, the newBreed will stay“unknown”. We have to add more combinations to the if statement toget our class to handle other dog breeds as we will do in Step7.
STEP 4 – Overloading the Output streamoperator
Overloading the output stream operator is a bit tricky, so youwill be given the code, but let’s go over it so we understand whatis happening here. Here is the prototype and the code:
prototype in Mutt.h header file:
friend ostream &operator<<(ostream &output, constMutt &brd);
code for the Mutt.cpp file:
ostream &operator<<(ostream &output, const Mutt&brd)
{
output << brd.breed;
return output;
}
In the prototype, we declare the function a “friend” of theostream class so that it can be called without creating an object.The “friend” word is not repeated when you code the function; it isonly inside the class definition in the header file.
In coding the function, we see that there are two parameters.The first one is the address of the output stream (a pointer) andthe second is the Mutt object we are printing. We have to make thisa constant so it cannot be changed. Notice that we don’t put theclass name with the scope resolution operator in front of thefunction name! The actual code is just the usual type of textstream we would use with cout. When we return this output pointer,it can be displayed to the console window. Thus we can print anyMutt object like this:
cout << brd1 << endl;
STEP 5 – Main Function
Declare a main function with return type void or int; if you useint, be sure to return a number such as zero. At the bottom of thefunction, put in a system(“pause”); so the console window willremain to show the output.
At the top of your Source file, be sure you includedocumentation and:
#include “Mutt.h”
First, declare three Mutt objects as in the example above; youcan use variable names of your choice.
Now use the setter to make one object “poodle” and one object“labrador”. Don’t give a value to the third one. Be sure to spellthese names exactly the same as they are spelled in your ifstatement!
Now add the “poodle” and “labrador” objects together to get thethird object’s breed just as in the example above; use the plussign.
Display the breeds of all three objects using the <
cout << “First object: “ << brd1 << endl;
Get your program running and capture the results. Here is anexample:
STEP 6: More Mutts?
Now let’s go back to the overloaded plus operator function andadd to the if statement. For example, add code to add pugs tobeagles and get a puggle (or any combination of your choice!)Remember to add to conditions to the if statement so you can addpug to beagle or beagle to pug.
Then add code to main to change the names of your first twoobjects to your two new breeds and then “add” them with a plusoperator and display the result using the overloaded <
Step by Step Solution
3.49 Rating (149 Votes )
There are 3 Steps involved in it
Step: 1
To implement the class Mutt as described you will need to c...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started