Constructors and Destructors
Constructors and Destructors
4/30/12
Constructors
some facts about constructors - 1 1.they are functions 2.they are functions declared in the public section of a class 3. they are functions declared in the public section of a class but cannot be called using dot operator 4. they are functions declared in the public section of a class. They cannot 4/30/12
Constructors
5.they can be called without using dot operator and it can be done so even outside the class definition 6. they can be called without using dot operator outside the class definition explicitly 7. they can be called without using dot operator outside the class definition explicitly and they also get 4/30/12 called implicitly
Constructors
some more facts about constructors 3 1.they are functions 2.they are functions which cannot return anything 3.they are functions which cannot return anything including void
4/30/12
Constructors-Example-1
int main() class ABC { ABC abc; //ABC() int var1; }public: 1.1,1.2,1.7,1.8,2.2,3.3 ABC(void); //constructor functions(1.1) void fun(void); declared in public section(1.2) }; get :: ABC() ABCcalled implicitly(1.7)
called implicitly
4/30/12
Constructors-Example-2
int main() class ABC { ABC abc = ABC(); //explicit call int var1; }public: 1.4,1.5,1.6 //constructor ABC(void); can be called without using dot void fun(void); operator(1.4) }; can beABC() from outside the class ABC :: called
4/30/12
Constructors
some more facts about constructors 4 1.they are functions 2.they are functions which can take arguments 3.they are functions which can take arguments including default arguments
4/30/12
Constructors-Example-3
int main() class ABC { ABC abc(10); //now ABC() will be called int var1; }public: 4.2 ABC(int); //constructor constructors can take arguments void fun(void); (4.2) }; ABC :: ABC(int a)
4/30/12
Constructors-Example-4
void ABC classABC :: fun() { cout<<hai2;} int main() int var1; { int var2; ABC abc(10); //now ABC() will be called public: } ABC(int x,int y=2); 4.3 void fun(void); constructors can take default };
4/30/12
ABC::ABC() caution while using constructors with default= 1;} { var1 arguments class:: ABC(int x) ABC ABC { var1=x; } { int var1; int main() public: {
Constructors why?
one of the aims of C++ is to create user-defined data types such as class, that behave very similar to the built-in types built-in types gets initialized when declared we want such behavior in our userdefined data types
4/30/12
Hence constructors
constructors can take arguments (parameterized constructors) constructors can take arguments including default arguments (constructors with default arguments) constructors can take arguments that are reference to it`s own class (copy constructors)
4/30/12
Parameterized constructors
parameterized constructors : constructors that take parameters parameters of a constructor can be of any type except that of the class to which it belongs but a constructor can accept a reference to its own class as a parameter. such constructors are called copy constructors
4/30/12
int main() class ABC { ABC abc(10); int var1; }public: no ABC(int x); error compilation }; ABC :: ABC(int a) { var1=a; }
4/30/12
ABC XYZ class:: ABC(XYZ xyz) { var1=xyz.var1; } int int var2; main() { public: XYZ xyz; int var1; ABC abc(xyz); void fun(void); } }; no compilation error class ABC
4/30/12
ABC ABC class:: ABC(ABC abc1) { var1=abc1.var1; } int main() int var2; { public: ABC abc2; int var1; ABC abc(abc2); ABC(ABC abc1); } }; compilation error because abc2 is of
4/30/12
ABC constructor: constructor that copy:: ABC(ABC &abc1) can accept a reference to it`s own { var1=abc1.var1; } class as a parameter int main() class ABC { { ABC abc2; int var2; ABC abc(abc2); public: } int var1;
4/30/12
ABC constructor: copy copy:: ABC(ABC &abc1)constructor gets called when initialization is part { var1=abc1.var1; } of declaration int main() class ABC { { ABC abc2; int var2; ABC abc = abc2; public: } int var1;
4/30/12
Copy constructors
When no copy constructor is defined , the compiler supplies it`s own copy construtor
4/30/12
ABC ABC class:: ABC(ABC abc1) { var1=abc1.var1; } ABCint ABC(int a) :: var1; { var1=a; } public: int main() ABC(ABC abc1); { ABC(int a); ABC abc(abc2); }; ABC abc2(10);
4/30/12
Destructors
one of the aims of C++ is to create user-defined data types such as class, that behave very similar to the built-in types built-in data types gets destroyed when they go out of scope we want such behavior in our userdefined data types
4/30/12
Hence destructors
Destructors
destructor is a member function destructor is a member function whose name is same as the class name destructor is a member function whose name is same as the class name but is preceded by a tilde
4/30/12
Destructors Example-1
int main() class ABC { { int a; public:abc(10); ABC } ABC(int b); cout<<deleted; ~ABC(); } }; o/p would be, ABC::ABC(int b) { a = b; }
4/30/12
Destructors Example-2
int main() destructors can be used to destroy objects that have not been created { using constructors { class ABC ABC abc; { } int a; cout<<deleted; public: } ~ABC(); 4/30/12 would be, o/p
END
4/30/12