Presentation is loading. Please wait.

Presentation is loading. Please wait.

Prepared by Puttalakshmi H.R PGT(CS) KV Minambakkam Chennai-27.

Similar presentations


Presentation on theme: "Prepared by Puttalakshmi H.R PGT(CS) KV Minambakkam Chennai-27."— Presentation transcript:

1 Prepared by Puttalakshmi H.R PGT(CS) KV Minambakkam Chennai-27

2 CONSTRUCTOR A member function with the same name as its class is called Constructor and it is used to initialize the objects of that class type with a legal initial value.

3 Declaration and Definition
class x { int i; public: int j,k; X() { i=j=k=0; } }; OR class x { int i; Public: X(); } x::x()

4 Special characteristics of constructors
1.These are called automatically when the objects are created. 2.All objects of the class having a constructor are initialized before some use. 3.These should be declared in the public section for availability to all the functions . 4.Return type(not even void) cannot be specified for constructors . 5.These cannot be inherited, but a derived class can call the base class constructor. 6.These cannot be static. 7.Default and copy constructors are generated by the compiler wherever required. Generated constructors are public. 8.These can have default arguments 9.The address of a constructor cannot be taken. 10.An object of a class with a constructor cannot be used as a member of a union. 11.A constructor can call member functions of its class.

5 Default Constructor A constructor that accepts no parameter is called the default constructor.It simply allocates memory to data members of objects. Eg: class Travelplan { int no_of_travellers; int no_of_buses; char place[40]; public: travelplan() →default constructor no_of_travellers=50; no_of_buses=2; strcpy(place,”Agra”); } };

6 Parameterized Constructors
A constructor that takes arguments are called parameterized constructors..The parameterized constructors allow to initialize the various data elements of different objects with different values when they are created. Eg: class xyz { float a; public: int b; char c: xyz(float x,int y,char ch)-----parameterized constructor a=x; b=y; c=ch; }………… …………//remaining members; };

7 Temporary instance or Temporary object
A temporary instance or temporary object is created when we make an explicit call.A temporary object resides in the memory as long as the object is referenced and after that it dies.

8 Copy constructor It is of the form classname(classname &)and used for the initialization of an object from another object of same type. A copy constructor may be called in the following situations: 1.When an object is defined and initialized with other object of the same class. Eg: sample s1; Sample s2=s1; 2.When we pass an object by value.

9 3.In case a function returns an object.
class fun { float x,y; public: fun(float a,float b) x=a; y=b; } fun(fun &f) cout<<”\ncopy constructor at work\n”; x=f.x; y=f.y; void display(void) cout<<x<<””<<y<<endl; };

10 Constructor Overloading
A constructor overloading is similar to function overloading as it can also be overloaded for various combinations of parameter types.The only difference is that the overloaded function can return a value while the overloaded constructor cannot return a value.

11 Eg: class figure { private: float radius,side1,side2,side3; char shape[10]; public: figure(float r) radius=r; strcpy(shape,”circle”); } figure(float s1,float s2) side=s1; side2=s2; side3=radius=0.0; strcpy(shape,”rectangle”); };

12 Destructors A destructor is also a member function whose name is the same as the class name but is preceded by tilde(~).A destructor is used to destroy the objects that have been created by a constructor. The syntax for declaring the constructor is: ~ name_of _the_class() { .. }

13 Eg: class add { private: int num1,num2,num3; public: add(int n1,int n2) num1=n1; num2=n2; num3=0; } void sum() num3=num1+num2; ~add() num1=num2=num3=0; };

14 Special Characteristics of Destructors
1.Destructors are invoked automatically when the objects are destroyed. 2.These de_initialize each object before the objects goes out of scope. 3.No arguments and return type(even void) are permitted with destructors. 4.Destructors follow the usual access rules as other member functions. 5.These cannot be inherited. 6.Static destructors are not allowed. 7.Address of a destructor cannot be taken. 8.A destructor can call member function of its class. 9.An object of a clsss having a destructor cannot be a member of a union.

15 Sample Questions Theory questions
1.Differentiate between a constructor and a destructor function. 2.What do you understand by constructor and destructor functions used in classes ? How are these functions different from other member functions? 3.What is copy constructor?What do you mean by constructor overloading? 4.What is a constructor?What is its need?Explain with example. 5.Why is a destructor function required in a class?Illustrate with the help of an example. 6.What is the importance of constructor in object oriented programming?Explain with the help of an example. 7.List some special charecteristics of constructor function and destructor function. 8.What is the order of a constructor and destructor invocation ?Explain giving a suitable example.

16 Sample Problems 1. class readbook { Public: Readbook() //function-1 {cout<<”open the book”<<endl;} void readchapter() //function-2 {cout<<”reading chapter-1”<<endl;} ~readbook() //function-3 {cout<<”close the book”} }; i)In object oriented programming,what is function- 1reffered as and when does it get invoked? ii) In object oriented programming,what is function -3 reffered as and when does it get invoked?

17 2.Answer the questions(i) and(ii) after goig through the following class:
Class exam { Int marks; Char subject[20]; Public: Exam() Marks=0; Strcpy(subject,”computer”); } Exam (char s[]) Strcpy(subject,”s”); Exam(int m) Marks=m; Exam (char s[],int m) Strcpy(subject,s); }; i)Which statement in c++ that would execute function3 and function 4 of class exam. ii)Which feature of object oriented programming is demonstrated using function 1,function 2,function 3 and function 4 in the above class exam?

18 3.Find the syntax errors if any.
class abc { Int x=10; Float y; abc() Y=5; } ~(){} }; void main() abc a1,a2;


Download ppt "Prepared by Puttalakshmi H.R PGT(CS) KV Minambakkam Chennai-27."

Similar presentations


Ads by Google