1234567891011
#include <iostream> #include <cstdlib> #include "main.h" using namespace std; int main() { Oreo fox; fox.div(); fox.mod(); }
123456789101112131415161718
#include <iostream> #include <cstdlib> #include "main.h" using namespace std; Oreo goose; int Oreo::div() { alpha = 3; return 0; } int Oreo::mod() { alpha = 4; // The function in question. goose.div(); cout << alpha; return 0; }
#ifndef MAIN_H #define MAIN_H class Oreo { public: int alpha = 1; int div(); int mod(); }; #endif // MAIN_H
12345678910
// global goose.alpha == 1 int main() { Oreo fox; // fox.alpha == 1 fox.div(); // fox.alpha == 3 fox.mod(); // fox.alpha == 4 // but this also calls goose.div(), so goose.alpha == 3 }
12345678
int Oreo::mod() { alpha = 4; // The function in question. goose.div(); cout << alpha; return 0; }
1234567
int Oreo::mod() { alpha = 4; // The function in question. div(); cout << alpha; return 0;
12345678910111213141516171819
int Oreo::mod() // assuming 'this' is 'fox' { alpha = 4; // fox.alpha = 4 // The function in question. goose.div(); // calls goose.div() .... goose.alpha = 3 cout << alpha; // print fox.alpha: 4 return 0; } //... int Oreo::mod() // assuming 'this' is 'fox' { alpha = 4; // fox.alpha = 4 // The function in question. div(); // calls fox.div() .... fox.alpha = 3 cout << alpha; // print fox.alpha: 3 return 0;
this