I NEED HELP UNDERSTANDING CLASSES [RM2K3]

Posts

Pages: 1
So I found this online...

#include <iostream>

using namespace std;

class Box
{
public:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
};

int main( )
{
Box Box1; // Declare Box1 of type Box
Box Box2; // Declare Box2 of type Box
double volume = 0.0; // Store the volume of a box here

// box 1 specification
Box1.height = 5.0;
Box1.length = 6.0;
Box1.breadth = 7.0;

// box 2 specification
Box2.height = 10.0;
Box2.length = 12.0;
Box2.breadth = 13.0;
// volume of box 1
volume = Box1.height * Box1.length * Box1.breadth;
cout << "Volume of Box1 : " << volume <<endl;

// volume of box 2
volume = Box2.height * Box2.length * Box2.breadth;
cout << "Volume of Box2 : " << volume <<endl;
return 0;
}

I don't know what the difference between a union, struct, and class are. Basically what I want to do is use DynRPG code to basically "name" the object, for instance, I want to be able to type in "Box1" into the first (the toughest part), then link that object to the .height and other stuff (I'm probably not using it to measure volume so maybe different variables).

I think I want to make a plugin that I can set string and ints inside a class, so I can do stuff like turn on switches if a certain character is female.
In C++, classes and structs are almost the same thing. The big difference is that a struct has public members by default, whereas with a class they are private by default.


struct Box {
// ...
};
Is basically the same as:

class Box{
public:
// ...
};

In a few rare cases, you would then need to add `struct' before that type name (I think this is only the case in certain uses of a struct type inside templates). So don't worry about that unless you are using templates.

A union is sort of a sum type. You can only use a single member of it at a time, they exist in the same memory location, whereas a struct or class has a series of members that are all unique. In general, you probably shouldn't be using unions very often at all. They can be useful, but they are tricky to use.

A possible example would be:


// A struct because it's just plain old data
struct Shape{
unsigned number_sides;
union{
double radius;
double side_length;
} dimensions;
};
//...

// A circle of radius 10 units
struct Shape circle = { 0 };
circle.dimensions.radius = 10.0;

// A square with side length 17 units
struct Shape square = { 4 };
square.dimensions.side_length = 17.0;

Note that the members side_length and radius don't need to be the same type. If they are the same type, then it wouldn't matter which one you read from, but if they aren't (for instance, one was an integer and one was a floating point), you'd get nonsense reading from one the one you didn't set.



union {
int A;
float B;
} data;

data.A = 10;
float C = data.B;
// C now contains nonsense (definitely NOT 10.0). data.A is still 10.
So could you help me with this? I basically have DynRPG code where I can store a name as a string. I would then want to name the (I guess it would then be struct) thing within the Box.

Something like a name for Box where I define the name externally (using OnComment from DynRPG).

Like:

(name = comment parameter 0)
Box (name here)
(name here).height = (parameter 1)
...

Is this doable?
Do you want to store a number boxes with different string names, and then refer to them later by those names?

You probably want to use a std::map<std::string, struct Box> in that case. That would let you do this:


#include <map>

struct Box{
int height;
// ...
};

static std::map<std::string, struct Box> boxes;

void someSetterCallback(std::string name, int height){
boxes[name].height = height;
}

int someGetterCallback(std::string name){
return boxes[name].height;
}
Hi,

I'm a programmer and let me try to explain a little bit about the concept.

In your code you have the "class Box"

The class is the specification.

Think that is the concept of the Box.

it has attributes on it concept:

double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box

when it running the Main it's executing the program itself

Box Box1; // Declare Box1 of type Box
Box Box2; // Declare Box2 of type Box

Box1 and Box2 are objects of type box. Think the object as that you create a Box (physically using the concept of the Class Box), however, this box still useless since there is no value on it so the box is NULL (how a box can have null lxbxh)

So now is the time to set it to be able to make some meaning (set the attributes of the object that has inherited from class


// box 1 specification
Box1.height = 5.0;
Box1.length = 6.0;
Box1.breadth = 7.0;

// box 2 specification
Box2.height = 10.0;
Box2.length = 12.0;
Box2.breadth = 13.0;

I hope that it can help you to understand the concept
Pages: 1