Passing classes question

This is a discussion on Passing classes question within the C++ Programming forums, part of the General Programming Boards category; I have the following, rather stupid question I made a parent class called "Space" with a name (eg. sun) and ...

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    12

    Passing classes question

    I have the following, rather stupid question

    I made a parent class called "Space" with a name (eg. sun) and a weight (float).
    Then i made a child class called "Planet" with a datamember "Space turnaround" (example, the earth-planet turns around the sun-space)

    I made a constructor :

    Code:
    Planet(char*="",float=0.0,Space);
    a getTurnaround function :

    Code:
    Space & getTurnaround (void) const;
    now my problem is, i don't know how I should write the Planet-constructor. I was thinking something like :

    Code:
    Planet::Planet(d):Space(n,f)
    {
          name= new char[strlen (n) +1];
          strcpy(name,n);
          weight= f;
          turnaround= d; //would requere a friend operator= if im not mistaking
    }
    and for the getTurnaround :
    Code:
    Space Planet::getTurnaround(void) const
    {
           return turnaround;
    }
    But I expect a lot of mistakes in this, since this is my first experience with passing classes.

    Hope someone can help.
    Last edited by Daggie; 04-07-2003 at 09:48 AM.

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    60

    What is name*=""?

    I've heard of char and LPSTR. What is name*=""? Besides, default-parametres must be assigned from right to left, I think.

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    12
    was a typo

    I was translating it from Dutch and apparantly I made a mistake there

    hope this makes more sence

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    You can try something like this. I don't use initialization lists when using constructors that contain strings because I always get confused as to how to do it. I write it out like this:

    Code:
    struct Space
    {
       char name[80];
    };
    
    struct Planet
    {
        Planet(Space, char *, float);
        //you need a default and copy constructor if you declare a 
        //non default constructor
        float weight;
        char name[80];
        Space space
    };
    
    //default null string for _name and default 0.0 for wt, no default 
    //for _space
    Planet::Planet(Space _space, char _name[80] = {'\0'}, float wt = 0.0)
    {
       stcpy(name, _name);
       space = _space;
       weight = wt;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 05:30 PM
  2. Question about passing & receiving parameters
    By TCB in forum C Programming
    Replies: 9
    Last Post: 04-11-2006, 07:08 AM
  3. QUESTION!! parameter passing
    By jave in forum C Programming
    Replies: 8
    Last Post: 10-21-2005, 01:50 PM
  4. Question about Classes
    By WildFire in forum C++ Programming
    Replies: 8
    Last Post: 06-18-2004, 08:57 PM
  5. question about passing arg in constructor??
    By smd in forum C++ Programming
    Replies: 4
    Last Post: 08-20-2003, 08:31 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21