Page 1 of 6
Page 1 of 6
ion. The main operation of constructor is to allocate the required resources such as memory and initialize the object of its class. The constructor of a class is the first member function to be executed automatically when an object of the class is created. Similar to other members, constructor can be declared either within, or outside the body of the class.
Constructor (Inside Class) Syntax class classname { public: classname( ) { //constructor body } };
Constructor (Outside Class) Syntax class classname { public: classname( ); }; classname::classname( ) { //constructor body }
Page 1 of 6
Characteristics of a Constructor: It has the same name as that of the class to which it belongs. It is executed automatically whenever the class is initiated. It does not have any return type. It is normally used to initialize the data members of the class. It should have public access within the class. It should not be declared as static or const. Constructor cannot be virtual. Default Constructor Definition: Constructors that do not have any arguments are called a default constructor. Syntax class classname { public: classname( ) { //constructor body } }; Example
#include<iostream.h>
#include<conio.h> class sample { private: int a; public: sample() // default constructor { a=10; } void print() { cout<<a; } }; void main() { sample ob; ob.print(); }
Page 2 of 6
Parameterized Constructor Definition: - A constructor that can be invoked with arguments is called a parameterized constructor. - The argument list can be specified within braces similar to argument-list in the function, constructor with arguments are called parameterized constructor. Syntax class classname { public: classname(parameter-list) { //constructor body } }; Example #include<iostream.h> #include<conio.h> class sample { private: int a; public: sample(int x)//parameterized constructor { a=x; } void print() { cout<<a; } }; void main() { sample ob(10); ob.print(); }
Page 3 of 6
DYNAMIC CONSTRUCTOR Constructor with Dynamic Allocation: - If the memory space for the constructor (memory variable) is allocated during the constructor calling, then it is called a Dynamic Constructor. - This is done with the help of operator new. - Depending upon the size of the values in the variable, the memory occupation of the object varies. Example #include<iostream.h> #include<conio.h> #include<string.h> class exam { private: int rollno,mark1,mark2; char *name; public: exam(int a,char *b,int c,int d) { rollno=a; mark1=c; mark2=d; name=new char[strlen(b)+1]; name=b; } void print() { cout<<rollno<<name<<mark1<<mark2<<endl; } }; void main() { clrscr(); exam e1(1,"shankar",80,90); exam e2(2,"arun",70,77); e1.print(); e2.print(); getch(); }
Page 4 of 6
DYNAMIC INITIALIZATION THROUGH CONSTRUCTORS Objects data members can be dynamically initialized during runtime, even after their creation. One advantage is that it supports different initialization formats using overloaded constructors. This provides flexibility of using different forms of data at runtime depending upon the users need. Example: #include<iostream.h> #include<conio.h> class sample { private: int a,b,c; public: sample() { a=b=c=0; } sample(int x) { a=x; b=0;c=0; } sample(int x,int y) { a=x;b=y;c=0; } sample(int x,int y,int z) { a=x;b=y;c=z; } void print() { cout<<a<<endl<<b<<endl<<c; } }; void main() { sample ob1,ob2,ob3,ob4; ob2=sample(2); ob3=sample(2,4); ob4=sample(2,4,6); ob1.print(); ob2.print(); ob3.print(); ob4.print(); getch(); }
Page 5 of 6
COPY CONSTRUCTORS Definition: A copy constructor is used to declare and initialize an object from another object. Syntax: Example class classname { public: classname(classname &object) { //constructor body } }; #include<iostream.h> #include<conio.h> class sample { private: int a; public: sample(int x)// parameterized constructor { a=x; } sample(sample &ob) // copy constructor { a=ob.a; } void print( ) { cout<<a; } }; void main( ) { sample ob1(10); ob1.print( ); sample ob2(ob1); ob2.print(); }
Page 6 of 6