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.
Printable View
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.
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
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
You could try following:
Should work, if I got you right.Code:
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;}
};
Hob
Well...you would need to convert the returned numbers to a string somehow...take a look at the following FAQ...
Ah, I got what you need. Try this then:
It is one of many ways of doing it.Code:itoa(getID(),buff1,10);
sprintf(buff2,"%f",getCost());
MyClass* pRealClass(buff1,buff2)
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
I remember to use scanf(). It is long time not to use it. Thanks.