C++ constructor introduction

C++ constructor introduction

in the last tutorials we in the all cases we have used member functions such as getvalue() and setvalue() to provide initial values to the private member variables. for example The following statement

a.input();

invokes the member function input(), which assigns the initial values to the data items of object A.

similarly the statement

a.getdata(100,99.5);

passes the initial values as arguments to the function getdata(), where these values are assigned to the private variables of object a. All these function call statements are used with the appropriate objects that have already created. These functions can’t be used to initialize the member variables at the time of create of their objects.

providing the initial values as described above does not conform with the philosophy of C++ language. we started earlier that one of the aims of c++ is to create user defined data types such as class, That behave very similar to the built in types. This means that we should be able to initialize a class type variables(object) when it is declared, much the same way as initialize of an ordinary variable for example

int m=20;
float x=6.56;

above are valid initialization statements for basic data types

similarly when a variable of built in types goes out scope, the compiler automatically destroy the variable. But it has not happened with the objects we have so far studied. it is therefore clear that some more features of classes need to be explored that would enable us to initialize the objects when they are created and destroy(delete) them when their presence is no longer necessary.

C++ provides a special member function called the constructor which enables an object itself when it is created, This is known as automatically initialization of objects, it also provides another member function called the destructor that destroys the object when they are no longer required.

CONSTRCUTOR:

A constructor is a special member function whose task is to initialize the objects of its class it is very special because its name is same as the class name.The constructor is automatically invoked whenever an object of its associated class is created. it is called constructor because it constructors the value of data member of the class.

A constructor is declared in the below example and defined as follows

// class with a constructor
class interger
{
int m,n;
public:
integer(); // declared constructor
……..
…….
};
integer :: integer() // define constructor
{
m=0;
n=0;
}

when a class contains a constructor like the one defined above. it is guaranteed that an object created by class will be initialized automatically, for example The declaration

integer int1; // object int1 created

no only created the object int1 of type integer but also initializes its data member m and n to 0. There is no need to write any statement to invoke the constructor function (as we do with the normal member functions). if a normal member function is defined for 0 initialization, we would need to invoke this function for each of the objects separately. This would be very inconvenient, if there are a large number of objects

A constructor that accepts no parameters(arguments) is called the default constructor. A default constructor for class A is A::A(). if no such constructor is defined then the compiler create a default constructor. therefore a statement such as
A a;
invokes the default constructor of the compiler to create the object a.

The constructor functions have some special characteristic these are:

1. they should be declared in the public (scope)section.
2. they are involved automatically when the object are created .
3. they do not have return type, not even void and therefore and they cannot return values.
4. They cannot be inherit, through a derived class can call the base class constructor.
5. Like other functions in C++ language, they can have default arguments.
6. we can not refer to their address.
7. Constructor can not be virtual.
8. They make implicit calls two operators new and delete when memory allocation is required.
9. an object with a constructor (or destructor) cannot be used as a member of a union.

  • C++ Dynamic Initialization of Objects
  • C++ Copy Constructor
  • C++ Dynamic Constructor
  • C++ Destructors
  • C++ Exercise
  • C++ Operator Overloading
  • C++ Overloading Unary Operators
  • Const pointer in C
  • Void pointer in c
  • C++ Overloading Binary Operators
  • C++ Overloading Binary Operators Using Friends
  • C++ Manipulation String Using Operators
  • C++ Rules for overloading operators
  • C++ Exercise
  • C++ Basic To class Type
  • C++ Class TO Basic Type
  • C++ One class To another class type
  • C++ Exercise
  • C++ Inheritance introduction
  • C++ Single Inheritance
  • C++ Multiple Inheritance
  • C++ Ambiguity Resolution in inheritance
  • C++ Hierarchical Inheritance
  • C++ Hybrid Inheritance
  • C++ Virtual Base Classes
  • C++ Exercise
  • C++ abstract class
  • C++ nesting of classes
  • C++ Exercise
  • C++ polymorphism
  • C++ Exercise
  • C++ pointers
  • C++ Pointers TO object
  • C++ this pointer
  • C++ Pointer to Derived class
  • C++ Virtual functions
  • C++ Exercise
  • C++ streams
  • C++ unformatted I/O operations
  • C++ Put() and get()
  • C++ getline() and write()
  • C++ Formatted console I/O
  • C++ Manipulators
  • C++ Exercise
  • C++ file handling
  • C++ file stream classes
  • C++ Open and closing file
  • C++ open using constructor
  • C++ open using open()
  • C++ Detecting End of file
  • C++ File modes
  • C++ File pointers and Manipulators
  • C++ Sequential I/O
  • C++ Reading and Writing
  • C++ Updating a File
  • C++ Error handling In File
  • C++ Command Line Arguments
  • C++ Exercise
  • C++ Template introduction
  • C++ Class Templates with multiple Parameters
  • C++ Function templates
  • C++ Function templates with multiple parameters
  • C++ member function Template
  • C++ Exercise
  • C++ Exception handling
  • C++ Basics of Exception Handling
  • C++ Exception Handling Mechanism
  • C++ Throwing Mechanism
  • C++ Catch Mechanism
  • C++ Catch all Exceptions
  • C++ Re-Throwing An Exception
  • C++ Specifying Exceptions
  • C++ Exercise