Tuesday, July 28, 2009

C++ adding constructor and destructor help?

Am using visual studio. I created a class and I forgot to click the virtual destructor checkbox. I need to add a constructor and destructor to the class I have already created. Is there anyway to do this or do I have to create the class all over again and copy the codes? If there is a way, can you explain how? Thanks

C++ adding constructor and destructor help?
Here follows a brief explanation about constructors and destructors. You can go skip to the example if you want.





Constructors and Destructors are methods used in their class' objects to perform initialization and clean up respectively. Constructors are used to initialize an object into a stable working state while destructors, clean-up and free resources used by an object, back to the system so they can be reused.





In C++ the class destructor is a method whose name is the same of the class precedded by a tilde character (~); it has no return type (not even void), nor paramters. A class can have only one destructor.





Similary, a constructor is a method whose name is the same of the class; it has no return type (not even void), but different from a destructor it can have parameters. A constructor with no parameters is called a default constructor and is called every time an object of the class is create. Other specialized constructors can be created specifying different parameters thus overloading the class constructor. A constructor cannot be virtual.





Example:





class CMyClass


{


public:





// constructors


CMyClass()


{cout %26lt;%26lt; "Default constructor";};





// you can supress this constructor if you don't need it


CMyClass(int iNumber)


{cout %26lt;%26lt; "Overloaded constructor " %26lt;%26lt; iNumber;};





// destructor (this is declared as virtual)


virtual ~CMyClass()


{cout %26lt;%26lt; "Destructor";};





};





If you need more C++ pre written code or classes, you can visit my website


http://negyware.110mb.com


No comments:

Post a Comment