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 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. Example: class Student { private: int rollno; float marks; public: : Student ( ) { rollno=0; marks=0.0; } : }; //constructor //other public members Example : class rectangle { private: float height; float width; int xpos; int ypos; 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; } Need For Constructor A structure or an array in C++ can be initialized at a time of their declaration. struct Student { int rollno; 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. Example class Student { private: int rollno; 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. Declaration And Definition class x { int i; public: int j,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. TYPES OF CONSTRUCTOR 1. 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. Example: For Default Constructor class Test { int ant; public: Test ( ) { ant = 0; } . . }; int main( ) { Test t1; . . . } 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. Example : Parameterized Constructor • class Test { int ant; public: Test(int i) { ant=i; } . . }; int main() { Test objects1(45); Test objects2(75); } 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. • 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 : }; 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 ; sample S2 =S1 ; //default constructor used //copy constructors used Example : Copy Constructor • class sample { public: int I,j; 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”; } . . }; When is a copy constructor called? When an object is passed by value void afunc (Sample); //Sample is a class 2. When a function returns an object Sample afunc( ); // Sample is a class and here it is return type of afunc( ) 1. 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. Special Characteristics of Constructors 1. 2. 3. 4. 5. 6. 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. Contd.Default constructors and copy constructors are generated where needed. 8. Like other C++ functions, constructors can also have default arguments. 9. It is not possible to take the address of a constructor. 10. Member functions may be called from within a constructor. 7. THANKS