-2

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.

Ryan
  • 35
  • 5

2 Answers2

2

The varA you define on top of test.cpp is different than the one in the test class. It is also never used, because inside the various member functions, the member varA has priority.

If you want to initialize test::varA to 10, do it in the constructor:

test::test()
    : varA(10)
{
    cout << "this is the test main class" << endl;
}

You can get rid of the global varA in test.cpp completely.

isanae
  • 3,253
  • 1
  • 22
  • 47
1

This line:

int varA = 10;

creates a new variable named varA which is independent of its homonym in class test instances.

Primitive types, such as your int varA in the definition of class test are not initialized by default, hence the -858993460 value you obtain. You can set an initial value in the constructor of test:

test::test()
   : varA(10)
{}

Or inside the class declaration in C++11:

class test
{
    // …
    int varA = 10;
}
Émilien Tlapale
  • 903
  • 5
  • 11
  • `homonyme` is a no good expression –  Jun 01 '15 at 18:35
  • 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 return10. however, if i want to use the same variable in the header, 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'. Because what I want to do is, make varA in test.cpp, and refer/use it inside of ConsoleApplication1.cpp. – Ryan Jun 01 '15 at 21:24
  • Each `test` instance has its own copy, with a different value, of `varA`. If you *really* want them to have the same value, you can use a global variable, or a `static` variable. For the latter, you can declare its storage and set a default value with `int test::varA = 10;` in you test.cpp. – Émilien Tlapale Jun 01 '15 at 21:29