In the following example how does the code "know" whether to use the scope operator function body or the one above it in class. They have many overlapping parameters (it seems).I know this is a very basic question but I have also been questioning why we use the out-of-body functions in the first place.
// The scope you're in is usually determined by your {curly braces}
class Rectangle
{ // <- begin 'Rectangle' scope
// within these braces, we are inside the Rectangle class
// therefore, anything in these braces is assumed to be inside
// of "Rectangle" scope.
void func(); // <- Since this is inside the Rectangle braces, it is part of the
// Rectangle class, and has Rectangle's scope.
}; // <- end 'Rectangle' scope
// We are no longer in those braces. So anything we put out here will NOT
// be assumed to be inside the Rectangle class.
void func() {} // <- Therefore, this creates a GLOBAL function... it is NOT the Rectangle's
// function because it is not inside the Rectangle's braces. The fact that is has the same
// name of "func" doesn't matter -- it is a completely different function because the scope
// is different.
// So if we want to define Rectangle's function outside of the {braces}, how do we do it?
// The answer: Use the Scope operator ::
//
// Whatever you put to the left of the scope operator tells it what scope you want to be in.
// So if we are not inside the Rectangle {braces}, but still want to do something with the
// Rectangle scope, we can use the scope operator:
void Rectangle::func() {} // <- Since 'Rectangle' is to the left of the '::', this indicates
// that we are referring to the 'func' that is inside the Rectangle scope.
I have also been questioning why we use the out-of-body functions in the first place.
Typically the class goes in a header file and the function bodies go in .cpp files. That way whenever you make changes to one of the functions you don't need to recompile your entire program, you only need to recompile that one cpp file. It's not a big deal for small projects, but as projects get bigger, having to recompile the whole thing for every tiny little change is a huge waste of time. Compilation of larger projects can take several minutes if done from scratch.
I've taken in all of the instruction you gave me. Thx for your time -- the explanation written out so clearly really helped. To really help my understanding I coded a new project below and was wondering whether someone could help me bring this all together.
The present project just manipulates .h private and public vars. Sorry for the long initialization list -- I know its not 100% needed. 1 file 1 function.
public:
int aircraft_carrier = 0;
int destroyer = 0;
int submarine = 0;
int barge = 0;
int handle_private_x = x;
int handle_private_y = y;
int c = 0; int d = y; int a = 0; int b =0; int answer= 0;
int private_mult(){answer = a * b;};
I went ahead and just for giggles I made some scope::member functions which called from outside the class. It seemed to work except for cout<<"Ballooning"<<endl; .
.h
[code]
#ifndef SCOPING_H_
#define SCOPING_H_
class programming {
public:
void snippet(); //function declaration
};
class ballooning{
public:
void get_waffles();
int waffles = 50;
int more_waffles = 60;
};
#endif /* SCOPING_H_ */