Question

In main.cpp, write a templated function more which takes in two variables of the same type...

In main.cpp, write a templated function more which takes in two variables of the same type and returns whichever variable is greater than (>) the other.

You can and may need to add something to the food class in order for more to be able to be called properly.

//food.h

#ifndef _FOOD_H

#define _FOOD_H

#include <iostream>

#include <string>

using namespace std;

class Food {

private:

string name;

int quantity;

public:

Food();

void setName(string newName);

void setQuantity(int newQuantity);

string getName();

int getQuantity() const;

};

#endif


//food.cpp

#include <iostream>

#include <string>

using namespace std;

#include "food.h"

Food::Food() {

name = "food";

quantity = 0;

}

void Food::setName(string newName) {

name = newName;

}

void Food::setQuantity(int newQuantity) {

quantity = newQuantity;

}

string Food::getName() {

return name;

}

int Food::getQuantity() const {

return quantity;

}

void increaseQuantity(Food * food) {

(*food).setQuantity((*food).getQuantity() + 1);

}


//main.cpp

#include <iostream>

#include <string>

using namespace std;

#include "food.h"

//

// Write a templated function `more` which takes in two variables of the same

// type and returns whichever variable is greater than (`>`) the other.

//

int main() {

Food apples, oranges, greater;

apples.setName("apples");

apples.setQuantity(5);

oranges.setName("oranges");

oranges.setQuantity(3);

greater = more<Food>(apples, oranges);

cout << "We have more " << greater.getName() << "." << endl;

return 0;

}

Homework Answers

Answer #1

Hi, Please find my implementation.

Please let me know in case of any issue.

//food.h
#ifndef _FOOD_H
#define _FOOD_H

#include <iostream>
#include <string>
using namespace std;

class Food {
private:
string name;
int quantity;
public:
Food();
void setName(string newName);
void setQuantity(int newQuantity);
string getName();
int getQuantity() const;
Food more(const Food &) const;
};

#endif

//food.cpp
#include <iostream>
#include <string>
using namespace std;

#include "food.h"

Food::Food() {
name = "food";
quantity = 0;
}

void Food::setName(string newName) {
name = newName;
}

void Food::setQuantity(int newQuantity) {
quantity = newQuantity;
}

string Food::getName() {
return name;
}

int Food::getQuantity() const {
return quantity;
}

void increaseQuantity(Food * food) {
(*food).setQuantity((*food).getQuantity() + 1);
}

Food Food::more(const Food &other) const{
   if(quantity > other.quantity)
       return *this;
   else
       return other;
}

//main.cpp
#include <iostream>
#include <string>
using namespace std;

#include "food.h"

//
// Write a templated function `more` which takes in two variables of the same
// type and returns whichever variable is greater than (`>`) the other.
template <typename T>
T const more(T const& a, T const& b) {
return a.more(b);
}



int main() {
Food apples, oranges, greater;
apples.setName("apples");
apples.setQuantity(5);
oranges.setName("oranges");
oranges.setQuantity(3);
greater = more<Food>(apples, oranges);
cout << "We have more " << greater.getName() << "." << endl;
return 0;
}

Know the answer?
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
Similar Questions
This code it's not working, fix it for me please #include <iostream> using namespace std; class...
This code it's not working, fix it for me please #include <iostream> using namespace std; class People {    string name;    double height; public:    void setName(string name)    {        this->name = name;    }    void setHeight(double height)    {        this->height = height;    }    double getHeight() {        return height;    }    string getName()    {        return name;    } }; int main() {    const int size...
Utilize the code from last week Add a default, full, and copy constructor. Also add a...
Utilize the code from last week Add a default, full, and copy constructor. Also add a constructor that allows you to specify only the name of the video game with no high score or times played specified. Adjust your code to demonstrate use of all 4 constructors and output of the resulting objects. #include <iostream> #include <string> #include <iomanip> using namespace std; class VideoGame { private:     string name;     int highScore;     int numOfPlays; public:     VideoGame() {        ...
Menu.cpp isn't working. C++  utilize inheritance to create a banking app. The architecture of the app is...
Menu.cpp isn't working. C++  utilize inheritance to create a banking app. The architecture of the app is up to you as long as you implement some form of inheritance. Below is my code however the credit and the actual menu isn't working. Can i get some help on getting the menu to work properly. // BankAccount.h #ifndef ACCOUNT_H #define ACCOUNT_H #include <string> #include <iostream> using namespace std; class BankAccount { protected : int noOfWithdrawls; private: // Declaring variables    float balance;...
Write a program (O(n), where n is the number of words) that takes as input a...
Write a program (O(n), where n is the number of words) that takes as input a set of words and returns groups of anagrams for those words. Complete your code here - For example, if the list is "debitcard", "elvis", "silent", "badcredit", "lives", "freedom", "listen", "levis", the output should be silent listen debitcard badcredit elvis lives levis Note that freedom has no anagram, and hence is not printed. ///////////////////////////////////////////////$%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% anagram.cpp #include <iostream> #include <unordered_map> #include <vector> #include <algorithm> using namespace...
Write a program containing a function, reverseDigit, that takes an integer as a parameter and returns...
Write a program containing a function, reverseDigit, that takes an integer as a parameter and returns the number with its digits reversed, then printout the return result. For example, the value of reverseDigit(12345) is 54321; the value of reverseDigit(5600) is 65; the value of reverseDigit(7008) is 8007; and the value of reverseDigit(-532) is -235.    Modify the following program to make a correct output. /* // Name: Your Name // ID: Your ID // Purpose Statement: ~~~ */ #include <iostream>...
C++ See the provided specification files. Complete the implementation for each as a separate file. void...
C++ See the provided specification files. Complete the implementation for each as a separate file. void seen(std::string); If there is already a Word object in the Words list, then the number of occurrences for this word is incremented. If there is no Word object for this word already, create a new word object with occurrence =1, and insert this object into the list of Word objects. std::string getNextWord(); Returns the next word of the list and sets the currentItem pointer...
read a text file into two parrall arrays. The file has a name and a grade...
read a text file into two parrall arrays. The file has a name and a grade here is what I am doing not working #include < iostream> #include < fstream> #include <string> usind namespace std; int main() { int const size =2; string names[size]; int age[size]; ifstream inputFile; inputFile.open("text.txt"); for (int i = 0; i <size; i++) { inputFile >> names[i]; inputFile>> age[i]; cout << age[i] << endl; cout << name[i] << endl; } inputFile.close(); return 0; }
Complete the missing code for the constructors as indicated in the comments in all three header...
Complete the missing code for the constructors as indicated in the comments in all three header files. C++ mainDriver.cpp #include <string> #include "Address.h" #include "Date.h" #include "Person.h" using namespace std; int main() {    Person p1;    Person p2("Smith", "Bobby", "[email protected]", 111, "Main St", "Clemson",            "SC", 29630, 1, 31, 1998);    cout << endl << endl;    p1.printInfo();    p2.printInfo();    return 0; } Person.h #ifndef PERSON_H #define PERSON_H #include <iostream> #include <string> using namespace std; class...
/* Write a function that looks for a particular person in their respective linked list The...
/* Write a function that looks for a particular person in their respective linked list The only place you need to write code is the "find" method in the linked list class */ #include <bits/stdc++.h> using namespace std; string ltrim(const string &); string rtrim(const string &); #define BUFFLEN 10 /* Each "person" is defined by their name, zipcode, and their pet's name. Persons are hashed by their zipcode. */ //---------------------------------------------------------------------------- /* function declarations ------------------------*/ int computeKey(int); void add_to_buffer(string,int,string); void find_in_buffer(int);...
Getting the following errors: Error 1 error C2436: '{ctor}' : member function or nested class in...
Getting the following errors: Error 1 error C2436: '{ctor}' : member function or nested class in constructor initializer list on line 565 Error 2 error C2436: '{ctor}' : member function or nested class in constructor initializer list on line 761 I need this code to COMPILE and RUN, but I cannot get rid of this error. Please Help!! #include #include #include #include using namespace std; enum contactGroupType {// used in extPersonType FAMILY, FRIEND, BUSINESS, UNFILLED }; class addressType { private:...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT