Task 1 The purpose of this practical is to examine more complex types of operator overloading in the context of two classes.
2.1.1 Ingredient
The ingredient class consists of two files: ingredient.h and ingredient.cpp.
The class has the following definition:
ingredient
-name:string
-price:double
-weight:int
----------------
+ingredient(n:string,p:double,w:int)
+~ingredient()
+friend operator<<(output:ostream &,t:const ingredient &):ostream &
+getName():string
+getPrice():double
+getWeight():int
The class variables are as follows:
• name: the name of the ingredient
• price: the base price for each ingredient
• weight: the weight of a single ingredient
The methods have the following behaviour:
• ingredient(n:string,p:double,w:int): The class constructor that instantiates an ingredient based on the input variables
• ∼ingredient: The class destructor. It prints out the message ”X removed” with a new line at the end when the class is destroyed. X refers to the name of the ingredient.
• operator<<: An overload of the << operator. It will print out the ingredient when used with the following format. Note that each line should have a new line at the end.
Name: 5-Spice
Cost: 100
Weight: 0.05
• getWeight,getName,getPrice: getters for the class variables.
2.1.2 Recipe
The recipe class consists of two files: recipe.h and recipe.cpp.
The class has the following definition:
recipe
-ingredients: ingredient**
-cost:double
-weight:double
-size:int
---------------------
+recipe(s:int)
+~recipe()
+friend operator<<(output:ostream &,t:const recipe &):ostream &
+operator+(add:ingredient*):recipe &
+calculateCost():double
+calculateWeight():double
The class variables are as follows:
• ingredients: A dynamic array that will be used to hold ingredients
• cost: The total cost of the entire recipe.
• weight: The total weight of the entire recipe.
• size: The number of ingredients that make up the recipe.
The methods have the following behaviour: 3
• recipe(s:int): The class constructor. It receives the number of ingredients in the recipe and instantiates its ingredients variable accordingly. All ingredients should be set to null.
• recipe(): The class destructor. It deallocates all assigned memory.
• operator<<: An overload of the << operator. This will display all of the ingredients in the recipe as well as the total cost and weight. Each line out output as a new line at the end. It should conform to this format(2 ingredients being an example):
Name: Bread
Cost: 25
Weight: 0.5
Name: Jam
Cost: 12
Weight: 0.09
Total Weight: 0.59
Total Cost: 37
• operator+(add:ingredient*): This is an overload of the + operator. It adds an ingredient into the recipe. The new ingredient should be added at the first non-null opening. If the recipe is full, nothing should happen.
• calculateCost(): This calculates the total cost of the recipe.
• calculateWeight(): This calculates the total weight of the recipe. You will have a maximum of 10 uploads for this task. Your submission must contain recipe.h, recipe.cpp, ingredient.cpp, ingredient.h and main.cpp. You will only need the iostream library for this practical.
//C++ code
//ingredient.h
#ifndef __INGREDIENT
#define __INGREDIENT
#include<iostream>
#include<string>
using namespace std;
class ingredient
{
private:
string name;
double price;
double weight;
public:
string getName();
double getPrice();
double getWeight();
/*The class constructor that instantiates
an ingredient based on the input variables*/
ingredient(string n, double p, double w);
/*The class destructor. It prints out the
message
”X removed” with a new line at the end when
the class is destroyed.
X refers to the name of the ingredient.*/
~ingredient();
/*
An overload of the << operator.
It will print out the ingredient when
used with the following format. Note that
each line should have a new line at the end.*/
friend ostream & operator<<(ostream
&output, const ingredient &t);
};
#endif
//ingredient.cpp
#include"ingredient.h"
ingredient::ingredient(string n, double p, double w)
{
name = n;
price = p;
weight = w;
}
ingredient::~ingredient()
{
cout << name << " removed\n";
}
ostream & operator<<(ostream &output, const
ingredient &t)
{
output << "Name: " << t.name <<
"\nCost: " << t.price << "\nWeight: " << t.weight
<< endl;
return output;
}
string ingredient::getName()
{
return name;
}
double ingredient::getPrice()
{
return price;
}
double ingredient::getWeight()
{
return weight;
}
//recipe.h
#ifndef __RECIPE
#define __RECIPE
#include"ingredient.h"
class recipe
{
private:
//A dynamic array that will be used to hold
ingredients
ingredient **ingredients;
//The total cost of the entire recipe.
double cost;
//The total weight of the entire recipe.
double weight;
//The number of ingredients that make up the
recipe.
int size;
public:
recipe(int s);
~recipe();
friend ostream & operator<<(ostream
&output, const recipe &t);
recipe& operator+(ingredient *add);
double calculateWeight();
double calculateCost();
};
#endif
//recipe.cpp
#include"recipe.h"
/*
The class constructor. It receives the number
of ingredients in the recipe and instantiates
its ingredients variable accordingly.
All ingredients should be set to null.
*/
recipe::recipe(int s)
{
ingredients = new ingredient*[s];
for (int i = 0; i < s; i++)
{
ingredients[i] = new
ingredient("",0,0);
}
}
/*
The class destructor.
It deallocates all assigned memory.
*/
recipe::~recipe()
{
delete[] ingredients;
}
/*
This will display all of the
ingredients in the recipe as well as the total
cost and weight. Each
line out output as a new line at the end.
*/
static int i = 0;
ostream & operator<<(ostream &output, const recipe
&t)
{
for (int j = 0; j < i; j++)
output << *(t.ingredients[j])
<< endl;
output <<"\nTotal Weight:
"<<t.weight<<"\nTotal Cost: " << t.cost <<
endl;
return output;
}
/*
This is an overload of the + operator.
It adds an ingredient into the recipe.
The new ingredient should be added at the
first non-null opening.
If the recipe is full, nothing should happen.
*/
recipe &recipe:: operator+(ingredient *add)
{
ingredients[i] = add;
i++;
return *this;
}
/*4
This calculates the total cost of the recipe.
*/
double recipe::calculateWeight()
{
for (int j = 0; j < i; j++)
{
weight +=
ingredients[j]->getWeight();
}
return weight;
}
/*
This calculates the total weight of the recipe.
*/
double recipe::calculateCost()
{
for (int j = 0; j < i; j++)
{
cost +=
ingredients[j]->getPrice();
}
return cost;
}
//main.cpp
#include"recipe.h"
//main() function
int main()
{
recipe rec(2);
ingredient *in1= new ingredient("Bread", 25,
0.5);
ingredient *in2 = new ingredient("Jam", 12,
0.09);
rec = rec +(in1);
rec = rec + (in2);
rec.calculateCost();
rec.calculateWeight();
cout << rec << endl;
system("pause");
return 0;
}
//output

//if you need any help regarding this solution .......... please leave a comment .......... thanks
Task 1 The purpose of this practical is to examine more complex types of operator overloading...
C++ Class and Operator Overloading part 1 The source file contains a C++ program that declares and initializes an array of Circles. The program is using a for loop statement to find the largest circle in the array and display it on the output screen. However, the program is currently not working. The problem is the program uses logical operator ' > ' for comparison of circles and it is not working unless we overload such operator for the class...
Prompt: In this stepping stone lab assignment, you will build a Recipe class, getting user input to collect the recipe name and serving size, using the ingredient entry code from Stepping Stone Lab Four to add ingredients to the recipe, and calculating the calories per serving. Additionally, you will build your first custom method to print the recipe to the screen. Specifically, you will need to create the following: The instance variables for the class (recipeName, serving size, and...
C++ CODE: The matrix class consists of two files: matrix.h and matrix.cpp. The class has the following definition: matrix -size:int -mat:int** ---------------- +matrix(file:string) +~matrix() +operator +(add:matrix*):matrix & +operator-(sub:matrix*):matrix & +friend operator<<(out:ostream &, t:const matrix&):ostream & +displayRow(r:int):void The class variables are as follows: • size: the number of rows and columns in a square matrix • mat: the integer matrix that contains the numeric matrix itself. The methods have the following behaviour: 2 • matrix(file:string): The default constructor. It receives the...
Hi. Could you help me with the code below? I need to validate the code below, using expressions that can carry out the actions or that make appropriate changes to the program’s state, using conditional and iterative control structures that repeat actions as needed. The unit measurement is missing from the final output and I need it to offer options to lbs, oz, grams, tbsp, tsp, qt, pt, and gal. & fl. oz. Can this be added? The final output...
-----------------------------------
public class Customer
{
String firstName,lastName;
//constructor
public Customer(String firstName,String lastName)
{
this.firstName=firstName;
this.lastName=lastName;
}
//override toString() method
public String toString()
{
return this.lastName+", "+this.firstName;
}
boolean equals(Customer obj)
{
if(this.firstName.equals(obj.firstName) &&
this.lastName.equals(obj.lastName))
return true;
else
return false;
}
}
-----------------------------------
public class Ingredient
{
String ingredientName;
int amount;
double price;
public Ingredient(String ingredientName,int amount,double
price)
{
this.ingredientName=ingredientName;
this.amount=amount;
this.price=price;
}
double getCost()
{
return amount/1000.0*price;
}
public String toString()
{
return this.ingredientName+", "+this.amount+" mls,
$"+this.price+"/L";
}
}
-----------------------------------...
1. Write a new class, Cat, derived from PetRecord – Add an additional attribute to store the number of lives remaining: numLives – Write a constructor that initializes numLives to 9 and initializes name, age, & weight based on formal parameter values 2. Write a main method to test your Cat constructor & call the inherited writeOutput() method 3. Write a new writeOutput method in Cat that uses “super” to print the Cat’s name, age, weight & numLives – Does...
I need help implementing 3 function prototypes with using operator overloading in a class. I have bolded the section I need help with since the cpp file is bigger than I expected. Any comments throughout would be extremely helpful. Time.cpp file #include "Time.h" #include #include #include // The class name is Time. This defines a class for keeping time in hours, minutes, and AM/PM indicator. // You should create 3 private member variables for this class. An integer variable for...
The task is to write Song.cpp to complete the implementation of the Song class, as defined in the provided header file, Song.h, and then to complete a program (called sales) that makes use of the class. As in Project 1, an input data file will be provided containing the song name and other information in the same format as in Program 1: Artist Song_Title Year Sales Medium As before, the program (sales) which are individual copies, will use this information to compute the gross...
Lab 1: InheritanceTest Write a program called InheritanceTest1.java to support an inheritance hierarchy for class Point–Square–Cube. Use Point as the superclass of the hierarchy. Specify the instance variables and methods for each class. The private data of Point should be the x-y coordinates, the private data of Square should be the sideLength, and the private data of Cube should be depth. Provide applicable accessor methods, mutator methods, toString() methods, area() method, and volume() method to all classes. Write a program...
IN c++ i post this question 5 times. hope this time somebody answer.. 16.5 Homework 5 OVERVIEW This homework builds on Hw4(given in bottom) We will modify the classes to do a few new things We will add copy constructors to the Antique and Merchant classes. We will overload the == operator for Antique and Merchant classes. We will overload the + operator for the antique class. We will add a new class the "MerchantGuild." Please use the provided templates...