First, what you are asking about is called "windows programming". Windows programs have a very different structure than console type programs, and therefore there is a separate forum for windows programming.
It seems though that C++ requires you to instantiate the object at the same time as declaring it
Yes, that's true, but you can use pointers:
Obj* p;
...
...
//sometime later:
p = new Obj(1, 2, 3);
I think pointers and java references are essentially the same thing. The key aspect for both is that when multiple variables refer to the same object, and you change that object, then all the variables change.
In Java this would be easy. I could declare the object as an attribute of the program's class and then instantiate it when the program started.
What's the difference:
Java:
Code:
class Obj
{
String color;
Obj(String c)
{
color = c;
}
Obj()
{
color = "";
}
}
class Program
{
Obj obj;
int size;
Program(String c, int b)
{
obj = new Obj(c);
size = b;
}
};
class DemoProgram
{
public static void main(String[] args)
{
Program my_program = new Program("red", 10);
System.out.println(my_program.obj.color);
}
}
C++:
Code:
#include<iostream>
#include<string>
using namespace std;
class Obj
{
public:
string color;
Obj(string c)
{
color = c;
}
Obj()
{
color = "";
};
};
class Program
{
public:
Obj obj;
int size;
Program(string c, int b)
{
obj = Obj(c);
size = b;
}
};
int main()
{
Program my_program("red", 10);
cout<<my_program.obj.color<<endl;
return 0;
}