classes

Pages: 12
Hi, i know a program could be written like so:

#include<iostream>

using namespace std;

class Example
{
blahh
}

main()

.................

whats the difference between defining the a class outside of the main function and in the main function?

is just a general rule that classes are not defined in function?
plus, can you give values to data members inside classes?
and again, plus...

i dont fully understand how using 'void' works, on certain things in my code, it only wants to work when i put void in front of it
*cough* *cough*
Sorry. I'm just suffering from a slight cold here, so sorry if my head's a little bit foggy tonight. I'm not getting nearly enough oxygen.

First, I'm assuimg blahh is just a placeholder and not what you actually put in your class. :)

Second, you need to end class declarations with a ;. I just thought I'd pick at that.

Third, you shouldn't be defining classes inside a function, but for educational purposes, let's consider that. What's the difference when you declare a variable outside a function and inside a function?

Fourth, you can give default values to data members inside a class when you make an instance of it, yes. Such a thing is done with a default constructor (<- Google term, hint hint).

Fifth, void is to be used in place of a type, stating that a function doesn't return anything with a type void example() {return 0;}//error . It can also be used to create an extremely dangerous form of pointer (void*).

Happy coding! Does this explain it well enough?

-Albatross
thanks for your reply...

you didnt explain "What's the difference when you declare a variable outside a function and inside a function?"

and

hm i kinda understand void, so why in my class do i HAVE to put:

void Greet()
{
blahh
}

and not

Greet()
{
blahh
}





Unless Greet is the name of your class, it's the same reason you can't (or at least shouldn't) define a function without a type. All functions must return something, and if they don't you need to let the compiler know. main()'s the only exception I can think of, but that's not standards compliant, hehe.

You don't know the answer to "What's the difference when you declare a variable outside a function and inside a function"? Alright, that's something you really need to know. When you declare something outside of any functions, namespaces, classes, or the like, it's declared in the global namespace; in other words it's a perfectly valid symbol in any bit of code below it.

Examples:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int a; //This variable is in the global namespace.
void example1();
{
    int b; //This is a local variable. It only exists inside example1().
    b = 0;
    a = 0;
    return;
}
void example2();
{
    b = 0; //Error. b isn't defined here.
    a = 0; //But a is!
    return;
}


Does that help now?

-Albatross
my reply to the 1st part:

ah ok, i wondered why i didnt have to use void with the main function. Can you give me an example of a function type please, because i havnt come across function types before as im a beginner. I assume the types are int, char and so on, but i dont understand why a function has to have a type, seeing as its not a variable

my reply to the 2nd part:

thanks, i understand you.
Last edited on
The "type" of a function is the type of what it returns with the return statement. main() is supposed to return an int, by all rights. If a function returns an int, then you declare in advance that it returns an int int example(), and if it's an std::string*, declare that in advance instead std::string* example(). If it returns nothing, use void. As far as I know you can't declare two different return types like int char example();, although if you're clever there's a way to do something similar. Basically, the template to use is:
returntype name(args)

Have fun!

-Albatross
Ok, thanks, but i dont understand the whole point about functions returning values, is it so that if the function returns a value then we know that the function has worked correctly
Ok, thanks, but i dont understand the whole point about functions returning values, is it so that if the function returns a value then we know that the function has worked correctly
Suppose you want a function to calculate the square of a number. You pass a number as the argument and get the square as a return value:

1
2
3
double square(double n) {
    return n * n;
}

There are many cases in which you'll want to pass a function some data, have it process the data and return some meaningful value for you to use. In fact, whenever a function alters program state instead of just returning a value, it is said to produce side effects.
"It is said to produce side effects"? What side effects would these be? Sometimes you want a function to simply perform a set of instructions or to change variables. You don't always want it to return a value. And you might want to use that function more than once, which means you can just call that function instead of rewriting the code within the function again.
This returning values is not sinking in, ill have a look on the internet to find a description which gets through lol :)
Last edited on
Metallon wrote:
"It is said to produce side effects"? What side effects would these be? Sometimes you want a function to simply perform a set of instructions or to change variables. You don't always want it to return a value. And you might want to use that function more than once, which means you can just call that function instead of rewriting the code within the function again.

Indeed. That means you want a function to produce side effects only. If you're interested: http://en.wikipedia.org/wiki/Side_effect_(computer_science)
Ok, so far i have found out that "A return from the function allows you to assign the function to a variable"...ok so when i return a value to a function it allows me to assign that value to a variable...ok

but what is the point in this, instead of putting return a; so that it can be put into a variable, why not just put char exampleVariable = a; ?
ahh, when we return the value we are returning it to the function, so the function is kinda a variable in a way, it has a value.. right? and thats it? we return values so that the function can hold a value and use it in some way?
Last edited on
???
If you have code which is repeated many times, you can put it in a function so that it is only written once and can be called whenever you need it. I don't think you understand functions yet though, they're certainly not variables. Take a look at the code example below, the numbers show the order that things happen.

1
2
3
4
5
6
7
8
9
10
11
12
13
int Multiply(int x, int y)
{
    return ( x * y ); // 3 - the function calculates the value of x multiplied by y and 'returns' the result
}

int main()
{
    int num; // 1 - declares a variable
    num = Multiply(2,3); // 2 - the function is called - 4 - num is assigned the value returned by the function    
    return 0;
}

two different things happen on line 9, and the flow of the program skips into the function until it returns a value, then back to the place where the function was called
Last edited on
" I don't think you understand functions yet though, they're certainly not variables"

I do know what functions are, i have used them before, i said "kinda" a variable, by this i mean, when we put say return a; does that mean that the function then holds the value a which we put into a variable? and use...
???
Pages: 12