| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
|
|
Ajax Application Generator Generate database and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!
|
|
#1
|
|||
|
|||
|
Please help with this merchant class.
Okay, i've built the class, anyone who can help me understand how to implement the actual functions. Also, if there is anything wrong with my class so far.
..and for the balance function, are the objects the actual variables that store the int of a merchant? or do i have to make those? does every merchant have 1000? thanks alot. heres what I have to do: Define a class ‘merchant’ for handling merchant-like objects. The class should have a public interface with the following members. merchant — a parameterless constructor initializing to 1000 the number of items each merchant possesses. sell — a two-parameter function transferring a specified number of items to a specified merchant. The function should not put any restriction on the order of its input values. buy — a two-parameter function transferring a specified number of items from a specified merchant. The function should not put any restriction on the order of its input values. balance — a parameterless function which writes out the number of items in the possession of its merchant. Code:
class merchant
{
public:
merchant(); //merchant constructor
~merchant(); //merchant destructor
int sell(int x, int y);
int buy (int p, int q);
void balance ();
};
merchant::merchant()
{
//need to initialize to 1000???
}
merchant::~merchant()
{
//destructor accepting no arguments.
}
int merchant::sell (int x, int y)
{
///should implement here, ..calls the sell member function which will sell a certain amount of what it has to the other merchant which is what?
///// return something?
}
int merchant::buy (int p, int q)
{
///should implement here, could be similiar to the sell member function.
// return something?
}
void merchant::balance()
{
// this should probably be an integer return type, but how??
}
int main(){
merchant a, b, c;
a.sell(50,b);
a.sell(c,50);
c.buy(70,b);
a.balance();
b.balance();
c.balance();
return 0;
}
Last edited by rcmango : May 6th, 2008 at 12:51 AM. Reason: added stuff. |
|
#2
|
|||
|
|||
|
I'll do a few changes, and highlight them.
Code:
class merchant
{
private:
int stock;
public:
merchant(); //merchant constructor
~merchant(); //merchant destructor
void sell(int x, merchant& y);
void sell(merchant& y, int x) { sell(x,y); }
// Same needs to happen with buy
int buy (int p, int q);
void balance() { std::cout << stock; }
};
merchant::merchant()
{
//need to initialize to 1000???
stock=1000;
}
The specification is for the public interface. What happens "further in", specifically in your functions and in the private section of the class, is totally up to you, so long as you get it working. That does mean you have to fill it out with something. So far you've probably just been told to write this piece of code like this and that piece of code like that. Now you're being let loose to decide how you want to implement it. |
|
#3
|
|||
|
|||
|
hey thanks for this help muhaja,
well had a question about this part: Quote:
i see how your actually just creating the function right inside the class there right? with the brackets? also, i see how the type for y is merchant& which is a reference. Can this only happen inside the class, just a little confused here about that part, i may need to read another class tut on that part. |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Please help with this merchant class. |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|