Thread: Default Constructors w/Classes as Parameters

  1. #16
    Registered User GrNxxDaY's Avatar
    Join Date
    Jul 2002
    Posts
    140
    You are awesome! I took out all the parameters for the default constructors for Room and Monster like you said -- works like a charm
    you're good
    Last edited by GrNxxDaY; 07-31-2002 at 07:04 AM.
    AOL: GrNxxDaY
    IDE: Dev-C++ Beta 5 (v4.9.4.1)
    Project: Eye of Sahjz (text-RPG)
    If you think I may need help, please IM me.

  2. #17
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    The default Room constructor:

    Code:
    Room::Room( ) 
    {
    	r = c = 0;  //unsure about what this does so I'll just set it to 0
    	N = E = S = W = false;
    	name = descr = NULL;
    }
    Remember these create useless rooms, no names, descriptions, exits, monsters or anything. Same with the default Monster constructor.

    Overloaded = operators are required so that you can do things like this safely:
    Code:
    Monster m1, m2;
    m1.changeSomething( );
    m2 = m1;   //= used here
    Just working on a bit of the overloaded = then I'll post that
    Couldn't think of anything interesting, cool or funny - sorry.

  3. #18
    Registered User GrNxxDaY's Avatar
    Join Date
    Jul 2002
    Posts
    140
    hermm.. try not to change TOO much to the point where it is unreadable by me, a n00b (see the avatar?). If you do change something, do you think you could explain? I just started C++ 3 weeks ago!.
    AOL: GrNxxDaY
    IDE: Dev-C++ Beta 5 (v4.9.4.1)
    Project: Eye of Sahjz (text-RPG)
    If you think I may need help, please IM me.

  4. #19
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    OK but you will need it if you are going to use strings represented as char*'s. Here is part of the overloaded assignment, if its too complex say so and you should probably use string objects instead.

    Code:
    Monster& Monster::operator =( Monster&  rvalue )
    {
    	//routine assignent of values
    	alive = rvalue.alive;
    	maxHP = rvalue.maxHP;
    	curHP = rvalue.curHP;
    	str = rvalue.str;
    	agi = rvalue.agi;
    	gold = rvalue.gold;
    	xp = rvalue.xp;
    
    	//create space for name attribute
    	name = new char[ strlen( rvalue.name )+1 ];
    	if( name == NULL )
    	{
    		cout << "Insufficient memory, exiting....";
    		exit( 0 );
    	}
    	else
    	{
    		strcpy( name, rvalue.name );  //copying name
    	}
    	return *this;
    	//UNFINISHED
    }
    The compiler creates a default operator for your classes but that would just copy rvalue.name into name leaving 2 objects pointing at the same space to represent name. Therefore if you change 1 then the other would also change, not good.

    By the way, your doing much better than I was after 3 weeks - I wasn't even on to pointers until week 4!
    Last edited by endo; 07-31-2002 at 07:28 AM.
    Couldn't think of anything interesting, cool or funny - sorry.

  5. #20
    Registered User GrNxxDaY's Avatar
    Join Date
    Jul 2002
    Posts
    140
    EEP - yes, that all looks very complex and the only part I understand is:
    Code:
    cout << "Insufficient memory, exiting....";
    I see you have *this pointer.. I can't get that far in my C++ book cause the author expects me to already understand everything.
    & I'm hesitant to use the new[] thingy because I dont understand the benefits of it or what dynamic memory really is...

    Since i'm too n00bish, i should use string types instead of char*?
    Last edited by GrNxxDaY; 07-31-2002 at 07:36 AM.
    AOL: GrNxxDaY
    IDE: Dev-C++ Beta 5 (v4.9.4.1)
    Project: Eye of Sahjz (text-RPG)
    If you think I may need help, please IM me.

  6. #21
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    you could do, but you could also use char arrays. Just declare name as char name[ 20 ] or something then treat them as char*'s after that. That way you will know exactly how much space you have every time.

    or you could declare them as string objects, which takes care of most of that stuff for you.

    string name;
    string attack;
    etc.
    etc.

    If you do that the set them to an empty string in the constructor:

    name = "";

    Hope that helps, the char arrays are more educational but string objects are simpler to use.
    Couldn't think of anything interesting, cool or funny - sorry.

  7. #22
    Registered User GrNxxDaY's Avatar
    Join Date
    Jul 2002
    Posts
    140
    thanks for helping with those constructors - you saved me hours of pressing the re-build button.
    i have no idea what i'm gonna do about those char*'s now though. I'd like to learn why I need all that overloaded operator stuff - but i'm too stupid to comprehend it =P.

    Thanks again for spending the time to help!
    AOL: GrNxxDaY
    IDE: Dev-C++ Beta 5 (v4.9.4.1)
    Project: Eye of Sahjz (text-RPG)
    If you think I may need help, please IM me.

  8. #23
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    No problem I remember how tough it is when you are starting. I'd suggest using string objects for now though, they are nice and versatile
    Last edited by endo; 07-31-2002 at 07:52 AM.
    Couldn't think of anything interesting, cool or funny - sorry.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array members copied by the default copy constructors?
    By cyberfish in forum C++ Programming
    Replies: 4
    Last Post: 02-18-2008, 12:46 AM
  2. Default Constructors
    By pritin in forum C++ Programming
    Replies: 5
    Last Post: 03-27-2007, 06:38 AM
  3. A question about constructors...
    By Wolve in forum C++ Programming
    Replies: 9
    Last Post: 05-04-2005, 04:24 PM
  4. Inherited constructors with parameters
    By dave74 in forum C++ Programming
    Replies: 3
    Last Post: 11-20-2003, 11:27 AM
  5. Default Constructors
    By MethodMan in forum C++ Programming
    Replies: 2
    Last Post: 10-25-2002, 06:30 PM