CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: how to do?

  1. #1
    Join Date
    Jan 2005
    Posts
    54

    Question 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.

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    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

  3. #3
    Join Date
    Jan 2005
    Posts
    54

    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

  4. #4
    Join Date
    Dec 2004
    Location
    Poland
    Posts
    1,165

    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 &#91;code] [/code] tags!

    Did YOU share your photo with us at CG Members photo gallery ?

  5. #5
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Re: how to do?

    Well...you would need to convert the returned numbers to a string somehow...take a look at the following FAQ...

  6. #6
    Join Date
    Dec 2004
    Location
    Poland
    Posts
    1,165

    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 &#91;code] [/code] tags!

    Did YOU share your photo with us at CG Members photo gallery ?

  7. #7
    Join Date
    Jan 2005
    Posts
    54

    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
  •  





Click Here to Expand Forum to Full Width

Featured