It appears you have not yet registered with our community. To register please click here.

Origin XT RPG Network Home







XT Project Introduction
Blah blah intro to this place. To be composed later.

C++ - Tutorial, Lesson 5, Functions, Classes, and OOP


Mar 26 2007, 04:51 PM (Post #1)
Well why can't we do the shuffle?!
* * * * * *
Posts: 1,679
Cash: 183,455 / 0
Group: Nobility
Joined: 7/12/03 08:49 PM

Lesson 5 - Functions, Classes, and OOP


Congrats to all who have made it this far, you've gotten through all the boring stuff and have arrived at the meat of C++ programming. This is where things start getting interesting!

Functions—What are they?


Put simply, a function is a block of code that is programmed to do something. Remember int main() ? That's a function, it is the main function and is the starting point of every program. That is a required function, but you can make other functions as well! Pretty cool huh? Well, let's make some! But before that, I have to tell you a few things about functions.

Function Types


We'll also use int main() as an example. I bet you're wondering why it's "int" main()? It just means that the main function will return a value that is an integer. But you can also declare your functions as other data types (char, double, float, string, etc.) But one you have not seen is void. All void does is if you do not want your function to return a value to the main function or to another function, you just declare the function as void.

Input Parameters


Say you have a function like: int function() Notice how there is always nothing inside of those paranthesis. Well, there can be! It could look like this: int function(int integer, int integer2). When we code the functions, I will go over, in detail, how to use these.

Ok, now we're ready to start making some functions. Let's get to it.

Example One


CODE

#include <iostream>
using namespace std;

int adderFunction(int, int); //this is a function signature
//the meat of the function in this program will be
//declared after the main function
//notice the input parameters, all we're doing is
//telling the function that it will be accepting two
//parameters of the data type int
//it will also return a value of int

int main()
{
int numberOne;
int numberTwo;
int numberThree;

cout<<"Input a number"<<endl;
cin>>numberOne;
cout<<"Input another number"<<endl;
cin>>numberTwo;

numberThree = adderFunction(numberOne, numberTwo);

//thats where the magic is my friends!
//this is a function call, and what it does is it calls the adderFunction
//sends numberOne and numberTwo to the function to be used
//then returns the value that it generates to numberThree!
//now the code in the function, even though the function comes after main
//will be executed before the rest of the code in the main function

cout<<"The Result: "<<numberThree<<endl;
system("PAUSE");
return 0;
}

int adderFunction(int numberOne, int numberTwo)
{
//the parameters in the above line can have different names if you want
//but for simplicity, I kept them the same

return numberOne + numberTwo;
//just returns the value of numberOne + numberTwo to the main function
//the value will be held in numberThree
}

And that, my friends, is how you can use functions. Try creating your own and practice with them before you move on through this lesson, because now we're going to get into classes and object oriented programming.

OOP (Object Oriented Programming) with Classes


As I mentioned before, for all of you JAVA people, C++ code does not have to be wrapped up in a class, however it can be. It has just as much power with OOP (Object Oriented Programming) as JAVA does, but it is completely optional. So let's take a look at how to use classes in C++ after I explain a couple of things.

Scope of Access


There are ways of limiting access to an internal function or variable (internal meaning that it's encapsulated in a class). Programmers often use these features for security reasons; if everything is public it makes the program easier to hack. However, these kind of things do not matter in these tutorials, so I'll use public a lot. Also, on larger, more complex programs, using private more often can make debugging much easier.

Public


All variables and classes declared under public can be accessed by all other functions within the program.

Private


All variables and functions declared under private can only be accessed by other functions within the same class. Used for security purposes.

"Friend" functions


I find it to be useless crap because these functions just retrieve values from private variables anyway.

Example Two


CODE

#include <iostream>
using namespace std;

class ourClass
{
private:
int A;
int B;
int C;

public:
void setA(int); //this is coupled with the get function to edit a, because it is
//private
int getA();

void setB(int);
int getB();
void setC(int);
int getC();

int adderFunction(int, int, int); //our signatures for functions go in a class
}; //this semi-colon is important!

void ourClass::setA(int pA)
{
//notice the ourClass::setA
//this just tells the compiler that the function, setA is in the class ourClass
//it takes an input parameter of pA, which will temporarily hold the value of
//A, so it can be put in the real variable

A = pA;
}

int ourClass::getA()
{
return A;
}

void ourClass::setB(int pB)
{
B = pB;
}

int ourClass::getB()
{
return B;
}

void ourClass::setC(int pC)
{
C = pC;
}

int ourClass::getC()
{
return C;
}

//now are set and gets are done, we can focus on the adder function

int ourClass::adderFunction(int A, int B, int C)
{
return A + B + C;
}

int main()
{
//so, how do we access all these functions from the main function?
//we use object oriented programming and declare ourselves a variable
//of the data type class like this:

ourClass ourVariable;

int a;
int b;
int c;
int finalAddition;


cout<<"Please input a value for A."<<endl;
cin>>a;

ourVariable.setA(a);

cout<<"Please input a value for B."<<endl;
cin>>b;

ourVariable.setB(b);

cout<<"Please input a value for C."<<endl;
cin>>c;

ourVariable.setC(c);

finalAddition = ourVariable.adderFunction(a, b, c);

cout<<"The answer is: "<<finalAddition<<endl;

system("PAUSE");
return 0;
}

Afterword


That's pretty much it for this lesson, everyone! Keep coming here for lessons!

Next Lesson:
Lesson 6 - Making header files, constructors, destructors

~Nesticles~
Post Options

 
Apr 24 2007, 06:14 PM (Post #2)
Well why can't we do the shuffle?!
* * * * * *
Posts: 1,679
Cash: 183,455 / 0
Group: Nobility
Joined: 7/12/03 08:49 PM
Update:
I might not be able to post another lesson for a while. The thing is I have to learn two new languages. I have to learn ASP and C#, because we're going to be
making GUI's in C#. It's gonna be cool. But I've been kinda busy but hopefully it won't take too long for me to get another lesson up. My finals start in about
2 1/2 weeks, so after that I'll have some time. Thanks guys.
Post Options

Apr 25 2007, 04:26 PM (Post #3)
Not Odd anymore
* * * * * * * * * *
Posts: 45,875
Cash: 1,915,578 / 1,817,041,051
Group: Administrator
Joined: 7/10/02 09:48 PM
That's not a problem ssmile.gif I thank you for the contributions you've already made.
Post Options