Can someone explain class in c++ to somebody who doesn't know much about c++?

Why (and how) do you use class in c++ code? Can someone could explain it and/or give me an example of when to use it?

Thanks?

you try google it? there are a lot of good examples explain it.

I have googled it but all I can find are complicated definitions. I’ll try again though.

K this is how I understand it but i am self taught so correct me if I am wrong.

C++ classes are the base architecture of the program, so when using them your adding stuff straight to the build level rather than adding assets to the project.

Why do this? well its as close to total freedom as your gonna get. While using blueprints and other visual scripting tools your bound to the nodes and tools they have for you, with c++ if something isnt there you can make it :smiley:

Sorta like why you would use Uscript vs Kismet in UE3.

now for how C++ classes work. Classes need 2 files, the header file, refered to as the .h file and the script file or cpp. Now your header file is where all the declaration happens, this is so that the project knows of it. Sorta like a guide to the project or introduction, like hey this is me and I have this.
Then the cpp file is the one that handles what to do. So if in your header file you declare a variable and function say a float Age and a function GetAge, the cpp then can assign (or store) the value of age, then the GetAge function can return its value.

Variable Declaration:
This is how you tell the system to hold a value. It is usually done in this matter:
VatiableType VariableName;
EX:
float Age;

Here are some variable types:
float //A number with decimal values
int //A number without decimal values
FVector //A collection of 3 values representing a point in space, X value on the x plane, y value of y plane, and z value of the z plane. Note that Unreal works with a X fowards, Y Rightside, and Z and Upwards while some other programs use Y as the upwards.
FRotator //Like a vector but instead it holds the rotation, it uses Pitch, Yaw, and Roll values in degrees rather than units.
AActor //An actor reference, anything that is visible in the game is an actor of some sort, your pawn for instance is a child class of actor.
Class //Refers to the type of object, this is useful to say spawn a class, since you would need to know what “kind” of thing you want to create.

Here are some examples of declarations:
float HealthPercent;
int Ammo;
FVector Location;
FRotator AimDirection;
AActor Weapon;
Class AWeapon* WeaponClass;

now function declarations are similar but a little tricker:
void DoSomething();

Thats a basic declaration, something you would find in the header file for intance. This just lets the system know it exists but it wont do anything.

but if you add this to the cpp file
void MyClass::DoSomthing()
{
Age += 1;
}

it would add 1 to the age variable. Now to understand why you write it like that.

You first need a return type, meaning does this function return a value, if it doesnt, then you can use void to tell the system this function just does stuff but doesnt get anything back.
Then the function name, something usually that describes what it does like GetHealth, TakeDamage, and so on.
then if you need to give that variable values, you put those in the (), those will be the “Parameters” of the function. Like:

void GetAge(float Since);

Then you can call it like GetAge(1990)

I could keep going but im already spending a lotta time on this. Hope this helps you understand some basics and I may add more to this some other time.

Great books on C++, which you can buy in tree form or download for free, are Bruce Eckel’s Thinking in C++ Volume One and Two. You can find them here http://www.mindview.net/Books/TICPP/ThinkingInCPP2e.html

It covers object oriented programming concepts (including classes) really well. Definitely more work than anything we’d fit in a forum post, but will give you much more breadth and depth than you’ll get in here.

Hope it helps.

Cheers,

Alan.

Adding to @wilc4rd:

Think a class as an abstraction of a real-world object, that has it’s own methods (functions) and properties (variables).

Like a NPC inside a game. It’s an Object, it has functions (move, attack, talk…), and data (variables, life).

When I was a beginner, comparisons of classes to real world objects always got me a bit confused. A class can, but often isn’t such a representation. I found it much more useful just to think about a classes on an abstract level. It’s a custom type that has a set of variables (that can be either primitive like float, int etc. or other classes), and a set of functions that act upon those variables. Nothing less, nothing more.