costructor

Views:
 
Category: Entertainment
     
 

Presentation Description

No description available.

Comments

By: Pratistha (1 week(s) ago)

i want this ppt

By: sanjit754 (8 month(s) ago)

please tell me why way download this presentation ?

By: sanjit754 (8 month(s) ago)

nice....!!!

By: harpreet.mavi (8 month(s) ago)

i want to download this presentation its very useful please make it public harpreet

By: hemangee (9 month(s) ago)

greattttttttt ppt of constructor and destructor

See all

Presentation Transcript

CONSTRUCTORSAND DESTRUCTORS : 

CONSTRUCTORSAND DESTRUCTORS

INTRODUCTION : 

INTRODUCTION The main aim of c++ is to create user defined Data Types such as class. This means that we should be able to initialize a Class type variable (Object) when it is declared and When a variable of built in types goes out of scope ,the compiler automatically destroys the variable. C++ provides the special member functions for the above purpose called Constructor and Destructor respectively

Constructors : 

Constructors It is a Special Member function with the same name as as its class which enables an Object to initialize itself when it is created.

Example of Constructor : 

Example of Constructor A constructor is defined and declared as follows: Class Student { int rollno,marks; Public: . . Student() //Constructor { Rollno=0; marks=0; } . . };

NEED FOR CONSTRUCTORS : 

NEED FOR CONSTRUCTORS 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 is called whenever a program creates an object of that class. Generally a constructor should be defined under the public sections of the class ,so that its objects can be created in any Function

TYPES OF CONSTRUCTORS : 

TYPES OF CONSTRUCTORS Default Constructor: A Constructor that accepts no arguments is called the Default Constructor. In the previous example Student::Student() is a default constructor. Parameterized Constructors:The constructors that can take arguments are called Parameterized Constructors.

Parameterized Constructors : 

Parameterized Constructors The constructors that can take arguments are called Parameterized Constructors. Class ABC { int i; Public: float j; char k; ABC(int a,float b,char c) //Parameterized Constructor { i=a; j=b; k=c; } . . };

Slide 8: 

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 function explicitly eg: integer int1=integer(0,100) 2) By calling the function implicitly integer int1(0,100);

COPY CONSTRUCTOR : 

COPY CONSTRUCTOR The copy constructor is used to declare and initialize and declare the object from another object For example : integer I2(I1); Would define the object I2 and at the same time initialize it to the values of I1.Another form of this statement is integer I2=I1;

DYNAMIC CONSTRUCTORS : 

DYNAMIC CONSTRUCTORS The constructors can also be used to allocate memory while creating objects .This will enable the system to allocate right amount of memory for each object when the objects are not of same memory. Allocation of memory to objects at the time of their construction is known as dynamic construction of objects. The memory is allocated with the new operator.

Characteristics of Constructors : 

Characteristics of Constructors They are invoked automatically when the objects are created. They do not have return types, not even void and therefore they cannot return values. They cannot be inherited, though a derived class can call the base class constructor. Like other c++ functions, they can have default arguments. They should be declared in the public section.

Destructors : 

Destructors A destructor is a member function which is used to destroy the objects that have been created by a constructor. Like a Constructor , The destructor is a member function whose name is the same as the class name but preceded by a tilde. For example ,the destructor for the class can be defined as shown below: ~clasname() { .. }

Example of Destructor : 

Example of Destructor A destructor is defined and declared as follows: Class sample { int i,j; Public: . . Sample(int a ,int b) //Constructor { i=a; j=b; } ~Sample() //Destructor { cout<<“Destructor at work”; } . };

NEED FOR DESTRUCTORS : 

NEED FOR DESTRUCTORS During the construction of an object by the constructor, resources are allocated for use e.g.,allocation of memory, opening of file etc. these allocated resources must be deallocated before the object is destroyed. A destructor is responsible for this task and performs all clean up jobs like closing a file, deallocating and releasing memory area automatically.

Order of invocation : 

Order of invocation Class A { int i,j; public: A() { cout<<”\nconstructor A”; } ~A() { Cout<<“\nDestructor A”; } }; Class B { int k,l; public: B() { cout<<“\nconstructor B”; } ~B() { Cout<<“\nDestructor B”; } }; Class C { A ob1,ob2l; B ob3; public: C() { cout<<“\nconstructor C”; } ~C() { Cout<<“\nDestructor C”; } }; Void main() { A oa; B ob; C ac; } OUTPUT constuctor A //oa constuctor B //ob constuctor A constuctor A oc constuctor B constuctor C Destructor C oc Destructor B Destructor A Destructor A Destructor B //ob Destructor A //oa

Characteristics of Destructors : 

Characteristics of Destructors Destructor functions are invoked automatically when the objects are destroyed. Destructor functions also, obey the usual access rules as other member functions do. No argument can be provided to a destructor, neither does it return any value. They can’t be inherited. A destructor may not be static. Member functions may be called from within a destructor.

Slide 17: 

Problem (1) Explain Constructors and Destructors with Example. (2) What do you understand by default constructor? What is its role? (3) Why is destructor function required in class? (4) What is a copy constructor? Define constructor overloading? Mr. M.L.MEENA PGT(Comp Sc.) K.V. VIKASPURI NEW DELHI THANK U