Sunday, September 12, 2010

Once again ... C/C++

Today I am going to talk about C++ instead of basic C. C++ adds several new ideas to C, the biggest of which is Object Oriented Programming. Objects are variables (called classes) that contain other variables and/or functions (called members). There are two ways to create an Object: class and struct

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:
Account anAcct;
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;
   }
};

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

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.

21 comments:

  1. Damn, I'm a Java programmer and I don't really understand any of this.

    Showing love ;)

    ReplyDelete
  2. the only programming i do is html based, so i guess that doesn't count.
    but, i'll tip my dad off too this blog, cause he does programming shit.

    ReplyDelete
  3. Once again, great info bro, keep it up.

    ReplyDelete
  4. Too tired to understand this. lol
    I'll check this out later.

    ReplyDelete
  5. im a designer so i dont need to understand.
    But great work mate many will learn basic programming from u.

    Support ur ideas

    ReplyDelete
  6. Hey Great Post Man! Just Passing By Your Blog To Show Some Support Take Care !

    ReplyDelete
  7. good old c++

    this is why I did not take programming class in highschool

    ReplyDelete
  8. Wish I had the time to sit down and learn C / C++. =(

    ReplyDelete
  9. Hey great info! i"m going to stop by again try to learn it!

    ReplyDelete
  10. very interesting, as a music major i suck with programming, but this shit is very very interesting man.

    ReplyDelete
  11. Awesome information man, im really happy ive stumbled across your blog, helped me out alot

    ReplyDelete
  12. I 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.

    ReplyDelete
  13. This 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!

    ReplyDelete
  14. wish i had this when i was a freshman. cool stuff bro

    ReplyDelete