dificulty with scope basics

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
using namespace std;

class Rectangle {
    int width, height;
  public:
    void set_values (int,int);
    //cout<<"Break-------->"<<endl;
    int area() {return width*height;}
};

void Rectangle::set_values (int x, int y)
{
  width = x;
  height = y;
}

int main () {
  Rectangle rect;
  rect.set_values (3,4);
  cout << "area: " << rect.area();
  return 0;
}
Line 7 is a member function declaration
Line 12 is a member function definition

I don't understand your question
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// 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.
Last edited on
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.

scoping.h file [code]
#ifndef SCOPING_H_
#define SCOPING_H_

class Ships
{
private:
int x = 0; int y = 0;

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


Ships () : aircraft_carrier (12), destroyer(18), a(9500), b(4500),submarine (4), c(9), d (90), barge(3),handle_private_x (7),handle_private_y (7), x(20), y(30) {};

~Ships (){}; // destructor

};

#endif /* SCOPING_H_ */

[code\]
scoping.cpp
[code]
#include <iostream>
#include "scoping.h"

using namespace std;



int main()
{
Ships ships;
cout<<ships.destroyer<<endl;
cout<<ships.handle_private_x<<endl;
cout<<ships.handle_private_y<<endl;
cout<<ships.c<<endl;
cout<<ships.d<<endl;
cout<<"Retreiving product of 2 private class integers"<<endl;
ships.private_mult();
cout<<ships.answer;
return 0;

}
[code\]

Thx


Last edited on
Where in this code would a scoped operator make sense?
Where in this code would a scoped operator make sense?


Nowhere. Aside from 'private_mult' not returning a value, this code compiles just fine without the scope operator.
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_ */

[code/]
[code]
/*
scope.cpp
Jul 17, 2015
Technologist
*/
#include <iostream>
#include "scoping.h"
using namespace std;

void programming::snippet() {
cout << "Function defined outside the class.\n";
}

void ballooning:: get_waffles() {
cout<<"Ballooning"<<endl;
}

int main() {
programming x;
x.snippet();
ballooning z;
cout<<z.waffles<<endl;;
cout<<z.more_waffles;

return 0;
}

[code/]
this wont print thoughno error no Ballooning
void ballooning:: get_waffles() {
cout<<"Ballooning"<<endl;
Last edited on
Topic archived. No new replies allowed.