Help With Structures and Classes (C++)

Discussion in 'Mac Programming' started by ghall, Dec 11, 2007.

  1. ghall macrumors 68040

    ghall

    Joined:
    Jun 27, 2006
    Location:
    Rhode Island
    #1
    So I am continuing my C++ studies. A while ago, I wrote a Madlibs program as an assignment for my C++ tutors, and my new assignment is to modify the code to use structures and/or classes, and I'm stumped. Can somebody give me a few ideas. The code is about 150 lines long, so I've uploaded it as an attachment.

    Feel free to compile and play around with it too if you so desire.
     

    Attached Files:

  2. toddburch macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #2
    What are you stumped on? I see no attempt at a structure or class definition.

    Todd
     
  3. ghall thread starter macrumors 68040

    ghall

    Joined:
    Jun 27, 2006
    Location:
    Rhode Island
    #3
    I have no idea where to begin. I know what I want to do, but I don't know how I should go about it.
     
  4. toddburch macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #4
    Also, it's looking pretty much (exclusively) like C code, not C++ code.

    Todd
     
  5. toddburch macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #5
    Well, decide if you want C code with Structures, or C++ code with Structures and/or Classes first.

    Can you verbalize what you want to do?

    Todd
    (I know what to do - but you need to show you know.)
     
  6. ghall thread starter macrumors 68040

    ghall

    Joined:
    Jun 27, 2006
    Location:
    Rhode Island
    #6
    I guess I need to use structures. I'm trying to create a structure for blank spaces and an structure for parts of speech. I'm confused at the order I should do them, and how to implement them in the main code.

    I'm not even really sure what I'm doing. I'm having a tough time grasping classes and structures.
     
  7. toddburch macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #7
    OK, so a structure of group of related data, made up of individual data types and/or other structures (basically). Think of a structure as a programmer defined data type.

    What variables do you have now that you want to group together into a structure?
    Todd
     
  8. ghall thread starter macrumors 68040

    ghall

    Joined:
    Jun 27, 2006
    Location:
    Rhode Island
    #8
    Right now, I just want the part of speech to be a variable (noun, verb, etc.).
     
  9. toddburch macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #9
    OK. Grab your C book and let's see your first attempt at defining a structure.

    Todd
     
  10. ghall thread starter macrumors 68040

    ghall

    Joined:
    Jun 27, 2006
    Location:
    Rhode Island
    #10
    I don't really have a book, I'm just being taught off a white board. Anyway here's my first program using structures. I got some help with it, but I can't seem to grasp the concept of structures. It's so frustrating, I was doing so well until I reached this point in my lessons. :(

    Code:
    #include <stdio.h>
    
    //this structure represents walls in a room
    struct wall{
    	int height;
    	int length;
    };
    //this structure represents a room
    struct room{
    	wall north,south,east,west,updn;
    };
    
    //this function calculates the area of a wall
    int area (wall mw){
    	return mw.height*mw.length;
    }
    
    //this function calculates the area of the floor and ceiling
    int updown (room mc){
    	return mc.north.length*mc.east.length;
    }
    
    //this function calculates the total area of a room
    int roomarea (room mr){
    	return area(mr.north)+area(mr.south)+area(mr.east)+area(mr.west)+updown(mr)+updown(mr);
    }
    
    //this function calculates the area of the walls and the ceiling of a room
    int paintarea (room ma){
    	return area(ma.north)+area(ma.south)+area(ma.east)+area(ma.west)+updown(ma);
    }
    
    //this function calculates the total area of the floor
    int carpetarea (room mp){
    	return updown(mp);
    }
    
    
    int main()
    {
    	room r;
    	
    	//take in the room height and assign it to the nort south west and east walls
    	printf ("Height\n");
    	scanf ("%d", &r.north.height);
    	r.south.height = r.north.height;
    	r.west.height = r.north.height;
    	r.east.height = r.north.height;
    	
    	
    	//take in the room length and assign it to the north and south walls
    	printf ("Length\n");
    	scanf ("%d", &r.north.length);
    	r.south.length = r.north.length;
    	
    	//take in the room width and assign it to the east and west walls
    	printf ("Width\n");
    	scanf ("%d", &r.west.length);
    	r.east.length = r.west.length;
    	
    	//assign the functions roomarea paintarea and carpetarea to integers
    	int myarea = roomarea (r);
    	int mypaintarea = paintarea (r);
    	int mycarpetarea = carpetarea (r);
    	
    	//output the results
    	printf ("%d foot area\n%d paint area\n%d carpet area\n", myarea, mypaintarea, mycarpetarea);
    }
    
     
  11. ChrisBrightwell macrumors 68020

    ChrisBrightwell

    Joined:
    Apr 5, 2004
    Location:
    Huntsville, AL
    #11
    If you want to use a class or struct, then, it would look something like this:

    Code:
    struct BlankSpace
    {
      private:
        String type; // adj., noun, adv., etc.
        String value; // value given by user.
    
      public:
        BlankSpace(); // default constructor
        BlankSpace(const BlankSpace&) // copy constructor
        ~BlankSpace(); // default destructor
        BlankSpace& operator= (const BlankSpace& bs);
    
        void setType(String s);
        void setValue(String s);
        String getType();
        String getValue();
    }
     
  12. toddburch macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #12
    Chris, I wasn't aware you could define constructors and destructors with structures.

    BTW, he's sticking with C, so that C++ example doesn't help him. (him?)

    Todd
     
  13. iSee macrumors 68040

    iSee

    Joined:
    Oct 25, 2004
    #13
    Under C++, structs and classes are the same thing, the only difference being that members are public by default for structs, and private by default for classes.

    (But as a style thing, I'd recommend only using structs in the traditional way.)
     
  14. ChrisBrightwell macrumors 68020

    ChrisBrightwell

    Joined:
    Apr 5, 2004
    Location:
    Huntsville, AL
    #14
    Structs and Classes in C++ are basically the same. They differ in the default access of their members (structs default to public, classes to private).

    I'm not a C guru, by any means, but I don't see why it wouldn't work. Swap String for a char array, maybe, but that's probably the only diff.
     

Share This Page