What's the difference between enumerators, structs and classes? [closed] - c++

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
So i've been learning c++ through www.learncpp.com/c++ primer and through the power of google for a while now and my goal has always been to make an RPG of some sort, even if it's a text based one.
Currently with my limited knowledge i'm trying to make a system that stores items/weapons/characters/spells/stats/etc.
I know that structs have public variables by
default and classes have private variables by default.
I also know that they all store data
Only thing i don't know is what's the difference between all 3 of them and when i should be using each one.

Modern C++ treats structs and classes the same, the only difference is that structs default to public while classes default to private. They are basically collections of data with associated functionality.
For example
class Player
{
private:
int health;
int attack;
public:
void calculateHealth(int healthChange);
}
Enums are basically named numbers, for example:
enum Months {January=1, February=2 ...}
So now instead of saying if currentMonth == 3 you can say if currentMonth == Months.March
Enums make the code easier to follow.

Related

C++ how to handle class types when writing a scripting language [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I've been stumped by this for some time although I believe it may be quite simple. I am writing a simple programming language in c++ and I can't figure out the best way to handle variable types withing my language.
For example I may have eight different variable types including string, number, bool, table, etc! I need to know the best way to store these within c++ because they are all different types, which means all different classes! I will have many more than eight types though. The code is line based with a compiler. Please help!
In general, you want to use a technique some call "type boxing". PHP for example uses something along these lines (more C-ish):
enum class ScriptVarType {
STRING,
NUMBER,
};
struct ScriptVar {
ScriptVarType type;
union {
std::string str;
int num;
};
};
You can also used derived classes if all your types are classes. They can share a ScriptVar base class, and you can use RTTI (or similar) to obtain the actual type, such as ScriptVarString.
User definable types just need to have members to define all the user parts.

Style for naming a function that is setting an argument by reference? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
So pretend I'm developing a car class and I want one of the car class's functions to return a list of passengers, except I'd like to put a list reference as an argument and just set that list instead of returning a list.
void GetPassengerList(PassengerList &passengerList); //sets the list
I don't know if I should call it GetPassengerList or SetPassengerList, or something else. I feel like using the words get / set make it seem like there is a private variable that is being manipulated like the typical getter / setter methods. What's a good naming convention to use here?
In our team for input/output arguments we either use
void AdjustPassengerList(PassengerList&);
or
void AddPassengersTo(PassengerList&);
Depending on the use-case. For example the first one could be used if you want a list created from more than one car. The second usually reads well in code, something like:
car.AddPassengersTo(list);

C++ simple text RPG game, type of creature and quantity [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have just started writing simple RPG game in C++. There will be a class (schema, not code)
class creature
string name;
int hp, strength...
There will also be quantity of them. But if I want to create two groups of same type creature, example: ('a', 100, 10)', it would be wasting of time and memory to make them separately, writing name and all other parameters two times, with only quantity being different. Do you know any method to implement quantity of creatures stacks, without necessity to repeat data?
Because I have just started working, I will be trying to do something and tell you about results.
How about wrapping it in a class that represents quantity as well?
What I mean is
class CreatureGroup {
Creature creature;
int quantity;
};

how to create custom byte level datatype? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I'd like to know how to create my own byte level datatype (like int, short, long, etc.). I know some will ask "Why!!? this is crazy!!" let's say I don't wanna use classes or structs. And I wanna know how to create that kind of datatype for C/C++ even if I have to use asm code for that (which I guess I'll have to use). Or is there a book that could help?
Can anyone please help? thank you.
If you exclude all the methods C and C++ give you to create new types then you can't create new types.
It's simple with a class:
Make a class that has some sort of storage. If you insist on doing everything manually then just make this an array of bytes. Overload all the operators you want to support.
class my_datatype
{
public:
my_datatype();
// overload operators
private:
uint8_t[sizeof_my_datatype] data;
};
Other than classes and structs, the only way to "make" a new datatype in C and C++ is a typedef. That's nothing more than an alias, though.

how to define a class using c++ [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I need to define a class to hold stock data in c++. At minimum it needs a method for "meanvalue", a method for "vairance", and properties for "trading volume" and other historical data.
Here's a start:
class Stock
{
public:
Stock();
~Stock();
int MeanValue();
int Variance();
private:
int mMeanValue;
int mVariance;
};
I'll leave the implementation of the methods in this class up to you.
You'll probably want to have a name property, and perhaps an exchange property, method to handle updates to specific things like volume, quotes, trade prices, etc. You can do this with key/value pairs or define methods for each.
For things like variance and history, you will have to have methods that are triggered by the updates so you can change the calculated values.
You really should consider showing us what you have, then we'll be more likely to help...