PDA

View Full Version : Help With Structures and Classes (C++)




ghall
Dec 11, 2007, 01:33 PM
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.



toddburch
Dec 11, 2007, 03:14 PM
What are you stumped on? I see no attempt at a structure or class definition.

Todd

ghall
Dec 11, 2007, 03:14 PM
What are you stumped on? I see no attempt at a structure or class definition.

Todd

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.

toddburch
Dec 11, 2007, 03:15 PM
Also, it's looking pretty much (exclusively) like C code, not C++ code.

Todd

toddburch
Dec 11, 2007, 03:17 PM
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.)

ghall
Dec 11, 2007, 03:24 PM
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.)

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.

toddburch
Dec 11, 2007, 03:27 PM
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

ghall
Dec 11, 2007, 03:29 PM
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

Right now, I just want the part of speech to be a variable (noun, verb, etc.).

toddburch
Dec 11, 2007, 03:31 PM
OK. Grab your C book and let's see your first attempt at defining a structure.

Todd

ghall
Dec 11, 2007, 03:35 PM
OK. Grab your C book and let's see your first attempt at defining a structure.

Todd

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. :(

#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);
}

ChrisBrightwell
Dec 11, 2007, 03:40 PM
Right now, I just want the part of speech to be a variable (noun, verb, etc.).

If you want to use a class or struct, then, it would look something like this:

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();
}

toddburch
Dec 11, 2007, 04:49 PM
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

iSee
Dec 11, 2007, 07:47 PM
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

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

ChrisBrightwell
Dec 13, 2007, 03:49 PM
Chris, I wasn't aware you could define constructors and destructors with structures. 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).

BTW, he's sticking with C, so that C++ example doesn't help him. (him?)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.