Reputation: 9
I want the Account class to hold a person's first, last name and their balance
The Produce class hold information about a type of fruit or vegetable
I'd like the SellProd function in the Account class to accept a Produce object so that it can add the price of that Produce to the person's balance. As of now, the function gives me an error saying "Unknown type name 'Produce'
Any tips to get this to work??
#include <iostream>
#include <string>
using namespace std;
class Account
{
friend class Produce;
private:
double funds;
string ownerfirst, ownerlast;
void addToFunds(double p) { funds += p;};
public:
Account( string first, string last) { ownerfirst = first; ownerlast = last; funds = 0;};
void printAccount()
{
cout<<"Account Holder: "<<ownerfirst<<" "<<ownerlast<<endl;
cout<<"Account Balance: "<<funds<<endl;
};
void sellProd(Produce a)
{
cout<<"Selling: "<<kind<<" | Classified as: "<<type<<endl;
cout<<"Current Balance: "<<funds<<endl<<"Sell price: "<<price<<endl;
addToFunds(price);
cout<<"New Balance: "<<funds<<endl;
};
};
class Produce
{
private:
string kind;
string type; //fruit or vegetable
double price;
public:
void printProd()
{
cout<<"Type of "<<type<<": "<<kind<<endl;
cout<<"Selling for: "<<price<<"$"<<endl;
};
Produce( string k, string t, double p) { kind = k; type = t; price = p;};
};
int main()
{
Account myAccount("John", "Doe");
myAccount.printAccount();
Produce prod1("Tomato", "Fruit", 2.99);
Produce prod2("Apple", "Fruit", 0.99);
Produce prod3("Carrots", "Vegetable", 1.49);
Produce prod4("Potato", "Vegetable", 1.29);
prod1.printProd();
myAccount.printAccount();
}
Upvotes: 0
Views: 62
Reputation: 661
It seems like you would want your Account class to contain a Produce object. In my example, I am assuming that you have moved your Produce class to separate .h, and .cpp files and included some getter functions in your implementation of that class. It could look something like this:
#include <iostream>
#include <string>
#include "Produce.h" // including the assumed files mentioned above
using namespace std;
class Account
{
friend class Produce;
private:
double funds;
string ownerfirst, ownerlast;
void addToFunds(double p) { funds += p;};
Produce a; //creating a produce object for your account class
public:
Account( string first, string last) { ownerfirst = first; ownerlast = last; funds = 0;};
void printAccount()
{
cout<<"Account Holder: "<<ownerfirst<<" "<<ownerlast<<endl;
cout<<"Account Balance: "<<funds<<endl;
};
void sellProd() //since Produce a is a private data member of your
//class, you do not need to include it in functions that your
//class implements.
{
cout<<"Selling: "<<a.getKind()<<" | Classified as: "<<a.getType()<<endl;
cout<<"Current Balance: "<<funds<<endl<<"Sell price: "<<a.getPrice()<<endl;
addToFunds(a.getPrice());
cout<<"New Balance: "<<funds<<endl;
};
A getter function for your Produce class could be as simple as (this is only an example of the implementation, or the .cpp file):
using namespace std;
class Produce(typeInput,priceInput,kindInput)
Produce::Produce() {
type=typeInput;
price=priceInput;
kind=kindInput;
}
Produce::~Produce(){}
int Produce::getPrice(){
return price;
}
Upvotes: 0
Reputation: 4637
Produce
is defined after Account
is, so when the compiler sees the definition of the function, it hasn't seen what Produce
is. Just switch around the definitions so that Produce
comes before Account
.
Upvotes: 1