I'm relatively new to coding,I know the basics of Java and C++ so far, but I am trying to organize my cod a bit more and speed up compile time a little. So far I've only figured out how to use functions from other classes. I'm self-learning everything so if anyone could show me an example using the code I've posted, that would be perfect. Feel free to explain it like you're talking to a child because these Header files are VERY confusing to me.
test.h:
#pragma once
class test
{
public:
//functions
test();
void part2();
//variables
int varA;
};
test.cpp
#include "stdafx.h"
#include <iostream>
using namespace std;
#include "test.h"
int varA = 10;
test::test()
{
cout << "this is the test main function" << endl;
}
void test::part2(){
cout << "you got to part 2, also, Variable A is " << varA << " when it's in test.cpp" << endl;
}
ConsoleApplication1
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
#include "test.h"
int main()
{
test objectA;
objectA.part2();
cout << "variable A is " << objectA.varA << "when it's in CA1.cpp" << endl;
system("pause");
return 0;
}
here is the console's output
this is the test main funtion
you got to part 2, also, Variable A is -858993460 when it's in CA1.cpp
variable A is -858993460 in CA1.cpp
Press any key to continue . . .
EDIT: So can I only get the varA that I'm declaring in the header in the test::test constructor? I tested this way, and it worked, it did return 10. however, if i want to use the same variable in the header/class, in a different function apart from the constructor, it gives me the gibberish again. The only way I can seem to use it, is if I create a global variable named something else, and use that. this wouldn't be practical if I needed to refer to it later like in an 'if-statement'.
What I'm trying to do is, make varA in test.cpp, and refer/use it inside of ConsoleApplication1.cpp.