C++ examples for Class:Constructor
Constructors and Destructors
#include <cstdlib> #include <iostream> using namespace std; class MyClass /*from w w w .j a v a 2s . com*/ { public : MyClass(); ~MyClass(); void SetI(int iValue); int GetI(); void SetF(float fValue); float GetF(); private: int i; float f; }; MyClass ::MyClass() { cout << "Entering constructor." << endl; i = 0; f = 0.0; } MyClass ::~MyClass() { cout << "Entering destructor." << endl; } void MyClass ::SetI(int iValue) { i = iValue; } int MyClass ::GetI() { return (i); } void MyClass ::SetF(float fValue) { f = fValue; } float MyClass ::GetF() { return (f); } int main(int argc, char *argv []) { MyClass anObject, anotherObject; anObject .SetI(10); anObject .SetF(3.14159); anotherObject .SetI(-10); anotherObject .SetF(0.123456); cout << "anObject .i = " << anObject .GetI() << endl; cout << "anObject .f = " << anObject .GetF() << endl; cout << "anotherObject .i = " << anotherObject .GetI() << endl; cout << "anotherObject .f = " << anotherObject .GetF() << endl; return (0); }