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;
}
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;
}
Get Answers For Free
Most questions answered within 1 hours.