Typically, a class contains both variables and functions and a struct contains only variables, though in newer versions of C++ the two are treated the same. Lets create a struct:
struct Account
{
int Number;
float Balance;
};
Notice the semicolon " ; " after the ending bracket is required. To access the two variables I created, I would, first, create a variable of type "Account" then you can access the members of the class with the period operator " . ". Like this:{
int Number;
float Balance;
};
Account anAcct;
anAcct.Number = 123567;
anAcct.Balance = 0.0f; //"f" tells the compiler it is a float type. use "c" for char.
anAcct.Number = 123567;
anAcct.Balance = 0.0f; //"f" tells the compiler it is a float type. use "c" for char.
When you create a class, you can specify a "constructor" and a "destructor" that are automatically processed when the variable is created and destroyed respectively. Both are named the same as the class with no type defined though the destructor has a tilde " ~ " before its name. For instance:
class Account
{
int Number;
float Balance;
Account()
{
Number = 0;
Balance = 0.0f;
}
~Account()
{
}
void SetBal(float B)
{
Balance = B;
}
};
{
int Number;
float Balance;
Account()
{
Number = 0;
Balance = 0.0f;
}
~Account()
{
}
void SetBal(float B)
{
Balance = B;
}
};
To access the functions inside a class is the same as accessing a variable. I.E. I could call "anAcct.SetBal(1300.45f);". If you were to create a pointer to an "Account" object, you could access the members with the "arrow" operator " -> ". Anything referenced in such a way is treated as the real object and not a pointer. For Instance:
Account anAccount;
Account *pAcct;
pAcct = &AnAccount;
pAcct->Number = 9023;
pAcct->SetBal(10.0f);
Account *pAcct;
pAcct = &AnAccount;
pAcct->Number = 9023;
pAcct->SetBal(10.0f);
Inside a class you can even tell the compiler how to handle operators (such as +, -, =, ==, etc.) using the operator keyword.
I've included two files to show the operator keyword in use:
DataTypes.h - the file that defines the classes.
DataTypes.cpp - the file that has the actual code.
And, finally for today, for an object to create a pointer to itself, it can use the keyword this.
Damn, I'm a Java programmer and I don't really understand any of this.
ReplyDeleteShowing love ;)
the only programming i do is html based, so i guess that doesn't count.
ReplyDeletebut, i'll tip my dad off too this blog, cause he does programming shit.
Once again, great info bro, keep it up.
ReplyDeleteToo tired to understand this. lol
ReplyDeleteI'll check this out later.
cool software shit...
ReplyDeletefollowing
im a designer so i dont need to understand.
ReplyDeleteBut great work mate many will learn basic programming from u.
Support ur ideas
Hey Great Post Man! Just Passing By Your Blog To Show Some Support Take Care !
ReplyDeletegood old c++
ReplyDeletethis is why I did not take programming class in highschool
Wish I had the time to sit down and learn C / C++. =(
ReplyDeleteHey great info! i"m going to stop by again try to learn it!
ReplyDeletevery interesting, as a music major i suck with programming, but this shit is very very interesting man.
ReplyDeleteAwesome information man, im really happy ive stumbled across your blog, helped me out alot
ReplyDeletethis was a very interesting post.
ReplyDeleteI did some C/C++ back in my first year of university, forgot most of it by now. Last time I did programming it was in Python.
ReplyDeleteThis is greek to me, I'm afraid, but after browsing through the blog, I see it's not just programming posts, so I'll keep checking back!
ReplyDeletewish i had this when i was a freshman. cool stuff bro
ReplyDeleteAwesome
ReplyDeletenice dude
ReplyDeleteThis is sweet!
ReplyDeletethis is never easy
ReplyDeletevery useful
ReplyDelete