Expand|Select|Wrap|Line Numbers
- //code created and tested in VS 2003
- #include <iostream>
- using namespace std;
- class TestClass
- {
- public:
- TestClass();
- ~TestClass();
- void ModifyVariables(int, int);
- void PrintVariables();
- private:
- int Variable1;
- int Variable2;
- };
- TestClass::TestClass()
- {
- Variable1 = 100;
- Variable2 = 100;
- }
- TestClass::~TestClass()
- {
- Variable1 = 0;
- Variable2 = 0;
- }
- void TestClass::ModifyVariables(int Mod1, int Mod2)
- {
- Variable1 += Mod1;
- Variable2 += Mod2;
- }
- void TestClass::PrintVariables()
- {
- cout<<Variable1<<" "<<Variable2<<endl;
- }
- int main()
- {
- TestClass TestObject;
- TestObject.PrintVariables();
- TestObject.ModifyVariables(20,45);
- TestObject.PrintVariables();
- //TestObject.TestClass(); //can't do
- TestObject.~TestClass(); //works just fine.
- TestObject.PrintVariables();
- TestObject.ModifyVariables(120, 145);
- TestObject.PrintVariables();
- return 0;
- /* OUPUT:
- 100 100
- 120 145
- 0 0
- 120 145
- */
- }
How can i call the constructor to reinit the variables to 100? or do i have to write a separate function?