Moving a variable outside its scope

I'm new to C++ and I am having a problem with a programming assignment I have. I have a constructor and a destructor, and my constructor takes char name. In my constructor I define char name as "default". My assignment wants me to code the destructor so it prints out char name, which would be "default". I don't know how to get char name read by the destructor and the only way I know how to print variables is by doing this: cout << variable << endl; Any help is greatly appreciated.
I am still learning C++ myself, but you may try to say in your destructor

exampleClass::~exampleClass(){

cout << this -> nameOfYourCharVariable << endl; //Prints to the console " Default "

//cout << "Isthan has great advice!!" <<endl;

}//end destructor for exampleClass class

"this" is a special pointer referring to the object calling the function. By using the pointer notation ( -> ) , you are accessing the CONTENTS of what the pointer points to. So the object calling the function, which is being destructed, has data members you've mentioned that can be accessed.
Last edited on
Of course nameOfYourCharVariable should be a member of your class, if that 'char name' is just the parameter of the constructor, the destructor can't access it
Topic archived. No new replies allowed.