Click to See Complete Forum and Search --> : how to do?
gamecocks
February 4th, 2005, 08:48 AM
int getID();
float getCost();
Class MyClass
{
MyClass();
MyClass(char*,char*);
~MyClass();
)
};
I like MyClass* pRealClass(char*,char*); how to use getID() and getCost() to feed these parameters? Thanks.
Arjay
February 4th, 2005, 11:15 AM
Your class definition looks incomplete as I don't see your class storing the char* data passed in the constructor. You probably want to store this data in the class, don't you?
Also, you need to name the params in the constructor if you wish to use them (i.e., MyClass(char*, char*) should be something like MyClass(char* szData1, char* szData2) or even more descriptive data names).
I'm not sure what your objective is regarding the global functions, are you trying to access the data from a class instance? If so, you'll probably want to make these class methods instead (and apply the appropriate conversions to convert from a string to the int or float).
Arjay
gamecocks
February 4th, 2005, 11:43 AM
I am sorry that I don't write clearly. I like to use constructior MyClass(char*, char*) to create object such as MyClass* pRealClass=new MyClass("1","1.0"); I like to use function's(int getID();float getCost())return value to replace "1" and "1.0" here. Thanks
Hobson
February 4th, 2005, 12:01 PM
You could try following:
Class MyClass
{
MyClass();
//MyClass(char*,char*); <<whats this for?
MyClass(int _id, float _cost){ID = _id; cost = _cost;} // is not this one better than above?
~MyClass();
int ID;
float cost;
int getID(){return ID};
float getCost(){return cost;}
};
Should work, if I got you right.
Hob
Andreas Masur
February 4th, 2005, 12:09 PM
Well...you would need to convert the returned numbers to a string somehow...take a look at the following FAQ (http://www.codeguru.com/forum/showthread.php?s=&threadid=231056)...
Hobson
February 4th, 2005, 12:20 PM
Ah, I got what you need. Try this then:
itoa(getID(),buff1,10);
sprintf(buff2,"%f",getCost());
MyClass* pRealClass(buff1,buff2)
It is one of many ways of doing it.
You need to allocate buff1 and buff2 before, or declare them as
char buff1[20], buff2[20];
I am sorry I posted wrong answer before, hope this one helps.
Hob
gamecocks
February 4th, 2005, 01:06 PM
I remember to use scanf(). It is long time not to use it. Thanks.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.