|
-
February 4th, 2005, 09:48 AM
#1
how to do?
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.
-
February 4th, 2005, 12:15 PM
#2
Re: how to do?
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
-
February 4th, 2005, 12:43 PM
#3
Re: how to do?
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
-
February 4th, 2005, 01:01 PM
#4
Re: how to do?
You could try following:
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;}
};
Should work, if I got you right.
Hob
B+!
'There is no cat' - A. Einstein
Use [code] [/code] tags!
Did YOU share your photo with us at CG Members photo gallery ?
-
February 4th, 2005, 01:09 PM
#5
Re: how to do?
Well...you would need to convert the returned numbers to a string somehow...take a look at the following FAQ...
-
February 4th, 2005, 01:20 PM
#6
Re: how to do?
Ah, I got what you need. Try this then:
Code:
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
B+!
'There is no cat' - A. Einstein
Use [code] [/code] tags!
Did YOU share your photo with us at CG Members photo gallery ?
-
February 4th, 2005, 02:06 PM
#7
Re: how to do?
I remember to use scanf(). It is long time not to use it. Thanks.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|