0

I learn C++ at the moment and as far as I know instance variables should be declared in a Header file. An example header (.hpp) looks like:

class myClass {
private:
    int i;
    std::ifstream file;
    anotherClass aClassObj;
public:
    //methods
}

I would like to initialize the variables in my source file (.cpp). For int it's only:

i = 4;

How would I do it for instance variables? In Java it would be variableName = new Classname(ConstructorParam1, ConstructorParam2);

However this is not possible in C++ because file = new ifstream("filename"); results in a type mismatch as new <...>; returns a pointer. Should I do a file.open("filename"); instead or something else? How can I initialize my variable aClassObj, I would like to call a constructor with parameters?

Kind regards

PSaR
  • 23
  • 1
  • 4
  • The first place to start is a good book, or at least the differences between Java and C++. – Weak to Enuma Elish Nov 22 '15 at 17:44
  • In C++ is easier you wouldn't have to provide `new`. But you would have to define a user defined constructor, where you'd initialize your member variables. – 101010 Nov 22 '15 at 17:45

2 Answers2

1

First off, you'll need to create constructor for you class (possibly multiple overloaded ones) as you would do in Java. Since C++ is based on values rather than pointers as Java (mostly; some fundamental types like integers are treated as values in Java), you normally don't need to use new in C++ unless you faff about with dynamic polymorphism. A constructor for your class could look, e.g., like this (obviously, it needs to be declared in the class definition of myClass):

myClass::myClass(int i, std::string const& name, AParam1 p1, AParam2 p2)
    : i(i)
    , file(name.c_str()) // if you use C++11 or later you can omit .c_str())
    , aClassObj(p1, p2) {
}

You didn't specify what kind of arguments the constructor of the class anotherClass takes. The code above assumes two arguments of type AParam1 and AParam2. Most likely you don't need to get all parameters from constructor parameters but have values ready. For example, you might always want to initialize the i member with the value 0 in which case you wouldn't need a parameter and just use the constant.

The keyword you want to look-up in your C++ tutorial is member initializer lists: this is how members are initialized in C++.

Dietmar Kühl
  • 150,225
  • 13
  • 225
  • 380
0

Instead of calling a constructor with parameters, you could initialize the instance with no parameters and then set the attributes of the object . For instance, in the header file;

#include "anotherClass.h"    
class Myclass{
      public:
        MyClass();
        ~MyClass();
        int a;
        int b;
        anotherClass aClassObj;
      };

then in the constructor of MyClass.cpp file;

MyClass::MyClass(){
   aClassObj.setParams(a, b);
}

Meanwhile, your "anotherClass" has this format in the header;

class anotherClass{
   public:
      anotherClass();
      ~anotherClass();
      int x;
      int y;
      void setParams(int a, int b);
};

and the .cpp file has this constructor and the set method;

anotherClass::anotherClass(){
}
void anotherClass::setParams(int a, int b){
    x = a;
    y = b;
}