Classes in C++

Posts 1–4 of 4 · Page 1 of 1
Classes in C++
Well this is more of a question on Programming style ,I think....

In every example Of OO (with classes and what not) Programming I see with C++, i've noticed a trend. The class functions(methods?) were always written outside of the class definition. I don't know why but this irks me to no end. Is there anything in particular that is wrong with writing all of the class methods within the class itself? This just makes more sense to me and annoys me less.

It's the one thing, visually, that i like better about Java than C++


For example:
Why is it like this:
Code:
class CRectangle {
    int x, y;
  public:
    void set_values (int,int);
    int area ();
};

void CRectangle::set_values (int a, int b) {
    x = a;
    y = b;
}

int CRectangle::area(){    
    return (x*y);
}

int main () {
  CRectangle rect, rectb;
  rect.set_values (3,4);
  rectb.set_values (5,6);
  return 0;
}
Instead of this:
Code:
class CRectangle {
    int x, y;
  public:
    void set_values (int,int){
        x = a;
        y = b;
    }

    int area (){
        return (x*y);
    }
};


int main () {
  CRectangle rect, rectb;
  rect.set_values (3,4);
  rectb.set_values (5,6);
  return 0;
}
The former(first one) Just seems to create more clutter.
Basically i'm asking, are there any specific reasons it is done this way?
If there are, are the reasons coding style, organization of some sort, or some practical reason that i haven't noticed.

This question applies to the constructor/deconstructors as well as the methods

Thanks for your answers. If i get any.
I'm guessing you don't know what inline functions are but basically when you compile your program a normal function call will look like
push para3
push para2
push para1
call function
this function would look like function(x, x, x)

now if this was inline instead of calling the function the compiler will put the function body(everything from the first { to the last }) in place of the call.

So now when you write a function like that in a class its an inline function. With functions that simple it can be more efficient than making the compiler call it. If you have a big function your performance will drop and if you call that function a lot you will get a way bigger program size.
Quote Originally Posted by sgtmattbaker View Post
I'm guessing you don't know what inline functions are but basically when you compile your program a normal function call will look like
push para3
push para2
push para1
call function
this function would look like function(x, x, x)

now if this was inline instead of calling the function the compiler will put the function body(everything from the first { to the last }) in place of the call.

So now when you write a function like that in a class its an inline function. With functions that simple it can be more efficient than making the compiler call it. If you have a big function your performance will drop and if you call that function a lot you will get a way bigger program size.
Thanks a lot, that makes perfect sense =). Is there anyway to define the function in the class without it being an inline function? And is it frowned upon to do that too much if it is possible?

P.S. I did know what inline functions are, but thanks for explaining it anyway =)
hey beats me. Haven't got into classes yet in C++.
Posts 1–4 of 4 · Page 1 of 1
This thread is closed for replies.

Similar Threads

Tags for this Thread

Talk with us