1 / 21

What is a constructor?

CONSTRUCTORS AND THEIR TYPES Prepared by MURLI MANOHAR PGT ( COMP. SCIENCE) KV,B.E.G ., PUNE. What is a constructor?. It is a member function which initializes a class. A constructor has: ( i ) the same name as the class itself (ii) no return type.

Download Presentation

What is a constructor?

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.


Presentation Transcript


  2. What is a constructor? • It is a member function which initializes a class. • A constructor has: (i) the same name as the class itself (ii) no return type

  3. Constructor : Def. A constructor is a member function of a class that is automatically called, when an object is created of that class. The constructor has the same name as that of the class’s name and its primary job is to initialize the object to a legal initial value for the class.

  4. Example: class Student { private: introllno; float marks; public: : Student ( ) //constructor { rollno=0; marks=0.0; } : //other public members };

  5. Example : - class rectangle { private: float height; float width; intxpos; intypos; public: rectangle(float, float); // constructor void draw(); // draw member function void posn(int, int); // position member function void move(int, int); // move member function }; rectangle::rectangle(float h, float w) { height = h; width = w; xpos = 0; ypos = 0; }

  6. Need For Constructor A structure or an array in C++ can be initialized at a time of their declaration. struct Student { introllno; float marks; }; int main ( ) { Student s1 = { 0, 0.0} int a[5] = { 1,2,3,4,5}; . . } But such initialization does not work for a class because the class members have their associated access specifiers. They might not be available to the outside world.

  7. Example class Student { private: introllno; float marks; public: // public members : }; int main() { Student senior = {0,0.0}; //illegal ! Data member are not accessible : }; A constructor for a class is needed so that the compiler automatically initializes an object as soon as it is created. A class constructor, if defined, is called whenever a program creates an object of that class.

  8. Declaration And Definition class x { inti; public: intj,k; x( ) // Constructor { i=j=k=0; } . . // other members . }; Generally, a constructor should be defined under the public section of a class, so that its objects can be created in any function.

  9. TYPES OF CONSTRUCTOR1. Default Constructors • A constructor that accepts no parameter is called the default constructor. • When user-defined class does not contain an explicit constructor ,the compiler automatically supplies a default constructor, having no arguments. This implicitly-declared default constructor is an inline public members of its class.

  10. Example: For Default Constructor class Test { int ant; public: Test () { ant = 0; } . . }; int main( ) { Test t1; . . . }

  11. 2. Parameterized Constructor • Like other functions you can also write constructors that can accept parameters. • Such constructors are called Parameterized constructors. Once you declare a constructor with arguments, the default constructor becomes hidden. • A constructor that accepts parameters for its invocation is known as parameterized constructor.

  12. Example : Parameterized Constructor • class Test { int ant; public: Test(int i) { ant=i; } . . }; int main() { Test objects1(45); Test objects2(75); }

  13. Significance Of Default Constructors • The default constructors are very useful when you want to create object without having to type the initial values or want to initialize object every time with prespecified initial values or if you want to create array of object of your class type.

  14. Class test { int a ; char b; public : test () //this is default constructors { cout<<“default constructors being called \n” ; } test (int I , char j) //constructor with argument { a= i ; b = j ; cout <<“constructor with argument called \n”; } } ; int main () { test tarry [5] ; //ok now as array can be created because default constructors is available test newjob (31 , ‘z’) ; // constructor with argument will be called for this : } ;

  15. 3. COPY CONSTRUCTOR A copy constructor is a constructor of the form classname (classname &) . The complier will use the copy constructor whenever you initialize an instance using values of another instance of same types. • Examples sample S1 ; //default constructor used sample S2 =S1 ; //copy constructors used

  16. Example : Copy Constructor • class sample { intI,j; public: sample (int a, int b) // Constructor { i=a; j=b; } sample (sample &s) // Copy Constructor { j = s.j; i = s.j; cout<<“\n Copy constructor working \n”; } void print (void) { cout<<i<<j<<“\n”; } . . };

  17. When is a copy constructor called? • When an object is passed by value void afunc (Sample); //Sample is a class • When a function returns an object Sample afunc( ); // Sample is a class and here it is return type of afunc( )

  18. Three types of constructors supported in c++ • Default Constructor • Parameterized Constructor • Copy Constructor The standard c++11 has introduced a new type of constructor namely Move Constructor , Which is used to move the content of objects class type.

  19. Special Characteristics of Constructors • Constructor functions are invoked automatically when the objects are created. • If a class has a constructor, each object of that class will be initialized before any use is made of the object. • Constructor functions obey the usual access rules. Only the function that have access to the constructor of a class, can create an object of the class. • No return type can be specified for a constructor . • They cannot be inherited, though a derived class can call the base class constructor • A Constructor may not be static.

  20. Contd.- • Default constructors and copy constructors are generated where needed. • Like other C++ functions, constructors can also have default arguments. • It is not possible to take the address of a constructor. • Member functions may be called from within a constructor.

  21. THANKS

More Related