You are on page 1of 11

Constructor Function

• Constructor function is a member function of


a class having:
Same name as class name
No return type
Is called automatically at the time of creation of an
object.
Cannot be called like other member functions
Is called only once in the life time of an object i.e. at
the time of creation.
Is normally used to initialize the data members of an
object.
1. #include<iostream.h> 10. void show() {
2. #include<conio.h> 11. cout<<"x="<<x; }
3. class Example 12. };
4. { 13. void main()
5. private: int x; 14. {
6. public: 15. Example ob1, ob2;
7. Example() { 16. ob1.show();
8. x=0; 17. ob2.show();
9. } 18. getch();
19. }
Constructor Overloading

Two or more than two constructor


functions having different number of
arguments OR
same number of arguments with
different data types is called constructor
overloading
1. #include<iostream.h> 12. void show() {
2. #include<conio.h> 13. cout<<"x="<<x;
3. class Example
14. cout<<“y="<<y;
4. {
5. private: int x,y; 15. } };
6. public: 16. void main()
7. Example() { 17. {
8. x=0 18. Example ob1,
9. y=0; ob2(9), ob3(10,20);
10. } 19. ob1.show();
11. Example(int a) {
20. ob2.show();
12. x=a; y=a }
13. Example(int a, int b) { 21. ob3.show();
14. x=a; y=b } 22. getch(); }
1. Class Example
2. { 1. void get()
3. private: 2. {
4. int x; 3. cout<<“x=“ << x<< “y=“<< y);
5. float y; 4. }
6. public: 5. };
7. // the following function is no argument
constructor function
6. void main ()
8. Example()
7. {
9. {
8. Example ob1; // no argumnet constructor
10. x=0; function called for ob1
11. }
12. //the following function is one argument
constructor functionwith int argument 9. Example ob2(20); one argument constructor
function with int data type called
13. Example(int a)
14. {
10. Example ob3(2.9); one argument constructor
15. x=a; function withfloat data type called
16. y=a;
17. }
11. Example ob4(20,3.0); two argument
18. //the following function is one argument constructor function called
constructor functionwith one float argument
19. Example(float a)
12. Ob1.get();
20. {
13. Ob2.get();
21. x=a;
14. Ob3.get();
22. y=a;
15. Ob4.get);
23. }
16. }
24. //the following function is two argument
constructor functionwith one int and one float
argument

25. Example(int a, float b)


26. {
27. x=a;
28. y=b;
29. }
The Class Destructor:
A destructor is a special member
function of a class that is executed
whenever an object of it's class goes
out of scope or whenever the delete
expression is applied to a pointer to the
object of that class.

6
A destructor will have exact same name
as the class prefixed with a tilde (~)
and it can neither return a value nor
can it take any parameters.
Destructor can be very useful for
releasing resources before coming out
of the program like closing files,
releasing memories etc.
7
1. #include<iostream.h> 12. void createObject();
2. #include<conio.h> 13. void main()
3. class Example 14. {
4. { 15. clrscr();
5. public: 16. createObject();
6. Example() { 17. getch();
7. cout<<“object 18. }
created”<<endl;} 19. void createObject()
12. ~Example() { 20. {
13. cout<<“object 21. Example ob1, ob2;
destroyed”<<endl; } }; 22. }
#include <iostream.h> Line::~Line(void) {
#include<conio.h> cout << "Object is being
class Line { deleted" << endl; }
private: void Line::setLength( double len )
double length; {
public: length = len; }
void setLength( double len ); double Line::getLength( void ) {
double getLength( void ); return length; }
// Main function for the program
Line(); // This is the
constructor declaration void main( )
~Line(); // This is the {
destructor: declaration Line line;
}; // set line length
// Member functions definitions line.setLength(6.0);
including constructor cout << "Length of line : " <<
Line::Line(void) line.getLength() <<endl;
{ return 0;
cout << "Object is being getch(); }
created" << endl;
}
9
Function Overloading.

Two or more than two member


functions in class with the same name
but different number of arguments OR
same number of arguments with
different data types is called function
overloading.
Function overloading
1. void main()
1. class A 2. {
2. { 3. A ob1;
3. public: 4. ob1.set(2,8.3);
4. void set() 5. ob1.set();
5. { 6. ob1.set(2);
6. cout<<“no argument set”; 7. getch();
7. } 8. }
8. void set(int x)
9. {
10. cout<<“one argument set”;
11. }
12. void set(int x, double y)
13. {
14. cout<<“two argument set”;
15. }
16. };

You might also like