Page 1 :
Unit-1, Programming in C++, Chapter-5, CONSTRUCTOR AND DESTRUCTOR, Part-1, , Introduction, This chapter discusses the means for creating (initializing) and destroying of objects of a class. We will, learn about constructors and destructors, special member functions of a class for initializing and, destroying of objects belonging to that class., The objects of a class type have to be created and initialized separately as an object. An object is a, region of memory space created at run time. These named objects can only be used in the scopes in, which they are defined. A scope is a region of program text. A scope is the area of the program in which, a variable or a function is accessible., There are three kinds of scope:, •, , local scope – which include objects defined in blocks of code, , •, , file scope – which include objects defined in a file, outside of all blocks and classes, , •, , class scope – which include objects defined in a class, , Lifetime of an object: The lifetime of an object is determined by its storage class which may be:, •, , Automatic – Objects declared within a block are considered automatic. They are created and, initialized each time the flow of the program reaches the statements that define them and are, destroyed upon exit from the block., , •, , Static – These are objects prefixed with the keyword static. They exist and retained their values, during the entire execution of the program. Global objects are also considered to be static., Static objects are initialized to zero by default., , Constructors, Constructors is a special member function whose task is to initialize the object of its class. It is special, because its name is the same as the class name. It is called constructor because it constructs the values, of data members of the class. A constructor function is automatically called whenever an object of its, associated class is created., Characteristics of a constructor:, 1. They should be declared in the public section., 2. They are invoked automatically when objects of its associated class are created., 3. A constructor does not have any return data type., 4. They cannot be inherited., 5. We cannot refer to their addresses., , Prepared by Irungbam Robina
Page 2 :
Unit-1, Programming in C++, Chapter-5, CONSTRUCTOR AND DESTRUCTOR, Part-1, , 6. They can have default arguments., , A constructor is declared and defined as follows:, class integer, {, int m, n;, public:, integer( );, , //constructor declared, , ……, ……, };, integer :: integer( ), , //constructor defined, , {, m=0;, n=0;, }, When a class contains a constructor like the one defined above, it is guaranteed that an object created, by the class will be initialized automatically., , Types of constructors:, 1. Default constructors: A constructor that accepts no arguments is called the default arguments., 2. Parameterized constructor: The constructors that can take arguments are called parameterized, constructors., 3. Copy constructor: A copy constructor is used to declare and initialize an object from another, object., Default Constructor, A constructor that accepts no arguments is called the default arguments. The default constructor for, class A is A::A( )., , Prepared by Irungbam Robina
Page 3 :
Unit-1, Programming in C++, Chapter-5, CONSTRUCTOR AND DESTRUCTOR, Part-1, , Default constructor allow objects to be created without passing any parameters to the constructor. With, a default constructor, objects are created just the same way as variables of other types are created., For example,, A ob1;, will create the object ob1 of type A by invoking the default constructor., A default constructor initializes the data members of all objects with the same set of values., Parameterized constructor, Just like any other functions, we can also pass arguments to constructor functions when the objects are, created. Constructors that can take arguments are called parameterized constructors. A parameterized, constructor initializes the various data members of different objects with different values when they, are created., When a constructor has been parameterized, the object declaration statement such as integer int1; may, not work., We must pass the initial values as arguments to the constructor function when an object is declared., This can be done in two ways., 1. By calling the constructor implicitly – the constructor is invoked even when its name has not, been mentioned in the statement., integer int1(10,70);, This statement creates an object int1 of integer type and invoke the constructor (implicitly) of integer to, initialize int1 with values 10 and 70., This method, sometimes called the shorthand method, is used very often as it is shorter, easy and looks, better., 2. By calling the constructor explicitly – the name of the constructor is explicitly provided to invoke, it so that the object can be initialized., integer int1 = integer(10,70);, This statement creates an object int1 of integer type and invoke the constructor (explicitly) of integer to, initialize int1 with values 10 and 70., Example:, class integer, {, , Prepared by Irungbam Robina
Page 4 :
Unit-1, Programming in C++, Chapter-5, CONSTRUCTOR AND DESTRUCTOR, Part-1, , int m, n;, public:, integer(int x, int y);, void display(void), {, cout<<“m=“<<m<<“\n”;, cout<<“n=“<<n<<“\n”;, };, integer:: integer(int x, int y), { m=x;, n=y;, }, void main(), {, integer ob1(10,70);, , //implicit call, , integer ob2=integer(20,80); //explicit call, cout<<“\n Object1:”;, ob1.display();, cout<<“\n Object2:”;, ob2.display();, }, Output:, Object1, a=10, b=70, Object2, a=20, b=80, , Prepared by Irungbam Robina