Constructor
-
Upload
poonamchopra7975 -
Category
Documents
-
view
2.273 -
download
0
description
Transcript of Constructor
![Page 1: Constructor](https://reader033.fdocuments.us/reader033/viewer/2022051313/54b3f0664a795950688b45d9/html5/thumbnails/1.jpg)
1
Constructor/DestructorFunctions
![Page 2: Constructor](https://reader033.fdocuments.us/reader033/viewer/2022051313/54b3f0664a795950688b45d9/html5/thumbnails/2.jpg)
OBJECTIVES:After this presentation we shall be able to answer following questions:
What is a CONSTRUCTOR Function?Characteristics of ConstructorDefault ConstructorParameterized ConstructorConstructor OverloadingConstructor with Default ArgumentsCopy ConstructorGuidelines for implementing ConstructorsDestructor Function
![Page 3: Constructor](https://reader033.fdocuments.us/reader033/viewer/2022051313/54b3f0664a795950688b45d9/html5/thumbnails/3.jpg)
What is a CONSTRUCTOR Function?
Special member function used for initialization of objects (data members).A constructor function is called whenever an object is created.Constructors are also called when an object is created as a part of another object.
![Page 4: Constructor](https://reader033.fdocuments.us/reader033/viewer/2022051313/54b3f0664a795950688b45d9/html5/thumbnails/4.jpg)
A Constructor is declared and defined as follows:
class number{ int a, b ; public:
number( void); // ** Constructor Function Declared
----- -----
};number :: number( ) // ** Constructor Function Defined{ a=0; b=0;}
![Page 5: Constructor](https://reader033.fdocuments.us/reader033/viewer/2022051313/54b3f0664a795950688b45d9/html5/thumbnails/5.jpg)
Characteristics of Constructors
Constructor Functions have same name as that of class name.
They do not have return types, not even voidMay have parametersC++ has default constructors that are called
whenever a class is declared even if no function with the same name exists
They should be declared in public section.They are invoked automatically when objects
are created.
![Page 6: Constructor](https://reader033.fdocuments.us/reader033/viewer/2022051313/54b3f0664a795950688b45d9/html5/thumbnails/6.jpg)
C++
The default constructor takes no arguments .If no such constructor is defined, then compiler supplies a default constructor.For Example
X x ; The default constructor for class X is X::X( )The statement X x ; invokes the default constructor of the compiler to create the
object x.
Default Constructor
![Page 7: Constructor](https://reader033.fdocuments.us/reader033/viewer/2022051313/54b3f0664a795950688b45d9/html5/thumbnails/7.jpg)
C++ Parameterized Constructor
class number{ int a, b ; public:
number( int x, int y); // Constructor Func. Declared ----- };
number :: number(int x, int y) // Constructor Func Defined{ a=x; b=y; }
number ob1; // ***** WRONG OBJECT CREATION number ob1( 0,100 ); // ********** * Implicit Call
Known as shorthand method Easy to implement and looks better
number ob1 = number( 0, 100 ); // ** Explicit Call
![Page 8: Constructor](https://reader033.fdocuments.us/reader033/viewer/2022051313/54b3f0664a795950688b45d9/html5/thumbnails/8.jpg)
C++ : Constructor Overloading
class number{ int a, b ; public:
number( ){a=0; b=0; }// Constructor 1 number( int x, int y); // Constructor 2 ----- };
number :: number(int x, int y) // Constructor 2 Defined{ a=x; b=y; }
![Page 9: Constructor](https://reader033.fdocuments.us/reader033/viewer/2022051313/54b3f0664a795950688b45d9/html5/thumbnails/9.jpg)
C++ : Constructor with Default Arguments
class number{ int a, b ; public:
number( int x, int y=5 ); // Constructor ----- };
number :: number(int x, int y) // Constructor Defined{ a=x; b=y; } number ob1(10);number ob2(0 , 0 );
![Page 10: Constructor](https://reader033.fdocuments.us/reader033/viewer/2022051313/54b3f0664a795950688b45d9/html5/thumbnails/10.jpg)
Example of Constructor
class sum{public: sum();private: int sum1,sum2;};
![Page 11: Constructor](https://reader033.fdocuments.us/reader033/viewer/2022051313/54b3f0664a795950688b45d9/html5/thumbnails/11.jpg)
C++
void main (){sum obj1; //constructor is called at
this //timecout<<“end of main”<<endl;return;}
![Page 12: Constructor](https://reader033.fdocuments.us/reader033/viewer/2022051313/54b3f0664a795950688b45d9/html5/thumbnails/12.jpg)
C++ Sample Functions
What is the output??
![Page 13: Constructor](https://reader033.fdocuments.us/reader033/viewer/2022051313/54b3f0664a795950688b45d9/html5/thumbnails/13.jpg)
C++ Functions
sum::sum (){ sum1=0; sum2=10; cout<<sum1<<“ “<<sum2<<endl;}
![Page 14: Constructor](https://reader033.fdocuments.us/reader033/viewer/2022051313/54b3f0664a795950688b45d9/html5/thumbnails/14.jpg)
the answer
0 10
end of main
![Page 15: Constructor](https://reader033.fdocuments.us/reader033/viewer/2022051313/54b3f0664a795950688b45d9/html5/thumbnails/15.jpg)
Example of Constructor withArguments
class sum{public: sum(int,int);private: int sum1,sum2;};
![Page 16: Constructor](https://reader033.fdocuments.us/reader033/viewer/2022051313/54b3f0664a795950688b45d9/html5/thumbnails/16.jpg)
C++
void main (){sum obj1 (10,20);
//constructor is // called at this timecout<<“end of main\n”;return;}
![Page 17: Constructor](https://reader033.fdocuments.us/reader033/viewer/2022051313/54b3f0664a795950688b45d9/html5/thumbnails/17.jpg)
C++ Functions
sum::sum (int x,int y)){ sum1=x; sum2=y; cout<<sum1<<“ sum1”; cout<<sum2<<“
sum2”<<endl;}
![Page 18: Constructor](https://reader033.fdocuments.us/reader033/viewer/2022051313/54b3f0664a795950688b45d9/html5/thumbnails/18.jpg)
C++ Sample Functions10 sum120 sum2
end mainWhat is the output??sum::sum (int x,int y)){ sum1=x; sum2=y; cout<<sum1<<“ sum1”; cout<<sum2<<“
sum2”<<endl;}
![Page 19: Constructor](https://reader033.fdocuments.us/reader033/viewer/2022051313/54b3f0664a795950688b45d9/html5/thumbnails/19.jpg)
C++ : DESTRUCTOR FUNCTION
A special function which also has same name as that of class but is preceded with a tilde (~) sign eg., ~ number( );Does not have a return type (not even void)Cannot have parametersCalled automatically when the class object is destroyedDe-allocates storage allocated to a classShould be public (or protected)
![Page 20: Constructor](https://reader033.fdocuments.us/reader033/viewer/2022051313/54b3f0664a795950688b45d9/html5/thumbnails/20.jpg)
public:
~sum (); // DESTRUCTOR FUNCTION
sum::~sum ( )
{
close (infile);
close (outfile);
}
C++ DESTRUCTOR FUNCTION