Reputation: 169
I am using fried functions for the very time and was assigned to complete an incomplete code using friend functions as below.
//CODE GIVEN IN THE QUESTION NOT MEANT TO BE EDITED
#include<iostream>
using namespace std;
class store_keeper;
class item
{
char prod_name[30];
char prod_code[10];
float prod_price;
int stock_In_Hand;
public:
void get();
void print()const;
friend class store_keeper;
};
class store
{
int num_Of_Items;
item items[20];
public:
void get_details();
void print_details() const;
friend class store_keeper;
};
class store_keeper
{
char name[30];
char id[10];
public:
void get();
void print();
void stock_mgmt(store &);
};
//MY CODE
void item::get()
{
cin>>prod_name>>prod_code>>prod_price>>stock_In_Hand;
}
void item::print() const
{
cout<<prod_name<<prod_code<<prod_price<<stock_In_Hand;
}
void store::get_details()
{
cin>>num_Of_Items;
for(int i=0;i<num_Of_Items;i++)
{
items[i].get();
}
}
void store::print_details() const
{
for(int j=0;j<num_Of_Items;j++)
{
items[j].print();
}
}
void store_keeper::stock_mgmt(store &s)
{
for(int k=0;k<s.num_Of_Items;k++)
{
if(items[k].stock_In_Hand<10)
{
s.print_details();
}
}
}
//CODE GIVEN IN THE QUESTION NOT MEANT TO BE EDITED
main()
{
store s;
store_keeper sk;
s.get_details();
sk.stock_mgmt(s);
}
I had to display the details of the item for which the stock in hand is less than 10.I am getting an error that num_Of_Items was not declared in this scope and suggest any edits if any required.Thanks.
Upvotes: 0
Views: 928
Reputation: 49986
There are few problems with your code, and they are all located in this function:
void store_keeper::stock_mgmt(store &s)
^ ~~~~~~ 1
{
for(int k=0;k<s.num_Of_Items;k++)
{ ^^^^^^^^^^^^^^~~~~~~~~~~~~~ 2
if(s.items[k].stock_In_Hand<10)
{ ^^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 3
s.items[k].print();
} ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 4
}
}
1 - you need to give name to this parameter as it is needed inside your function
2 - when compiler see store_keeper::num_Of_Items
it thinks you want to access a static variable of name num_Of_Items
inside store_keeper
class, but there is no such variable. What you want here is to use s.
to read num_Of_Items
from s
which is of type store
. You have befriended store_keeper
with store
so this is legal
3 and 4 - items
is a field in store
class, which is provided to your function as a parameter s
, so use s.
to access it.
And finally item
has a print
and not print_details
This will allow your code to compile, but probably more work is needed to make it work as expected.
Upvotes: 1