Object-Oriented Programming C++
Types of Constructor
Size: 760.46 KB
Language: en
Added: Apr 14, 2018
Slides: 19 pages
Slide Content
</> G.H PATEL COLLEGE OF ENGINEERING AND TECHNOLOGY TOPIC : CONSTRUCTOR AND ITS TYPES SUBJECT : OBJECT ORIENTED PROGRAMMING WITH C++ (2140705 ) PREPARED BY : TEJOY VACHHRAJANI (160110116057) BHAVIK VASHI (160110116061) KHYATI VALERA (160110116059) SUBMITTED TO : PROF. BHARGESH PATEL CLASS : B.E. IT (2 ND YEAR) BATCH : 1D16
2 CONSTRUCTOR FUNCTION
Ever thought that what kind of problems can an object create if it’s not initialised. I t ’ s si m i l ar t o g i v i n g y ou r v eh i cl e the final gear just during the startup.
The Object What if user didn’t assign me any value initially (by chance he forget) and finally ask me my value? I’ll be helpless. So to make a program robust the object should be initialised every time. And in class we do this by constructors...
Well! It is a special member function that is used for initialization of objects (data members). A constructor function is called whenever an object is created. Constructors are also called when an object is created as a part of another object. What is “ Constructor ” !!!
Constructor Functions have “ same name ” as that of class name . They do not have return types, not even void . May or may not have parameters. C++ has default constructors that are called whenever a class is declared even if no function with the same name exists Some of it’s characteristics Mind It!
They should be declared in “ public section” . They are invoked “ automatically ” when objects are created . We cannot call it for the already constructed object. Some of it’s characteristics
THERE ARE 3 TYPES OF CONSTRUCTORS:- 1. DEFAULT CONSTRUCTOR 2. PARAMETER CONSTRUCTOR 3. COPY CONSTRUCTOR TYPES OF CONSTRUCTORS
DEFAULT CONSTRUCTOR Because in C++ default constructor is there. This constructor has no arguments in it. Default Construc t or is a l so c a l l e d a s no argument constructor.
DEFAULT CONSTRUCTOR Default argument is an argument to a function that a programmer is not required to specify . C++ allow the programmer to specify default arguments that always have a value, even if one is not specified when calling the function . Fo r exa m p l e, in the following function declaration: int MyFunc ( int a, int b, int c = 12 ) ;
PARAMETER CONSTRUCTOR A parameterized constructor is just one that has parameters specified in it. We can pass the arguments to constructor function when object are created. A constructor that can take arguments are called parameterized constructors.
COPY CONSTRUCTOR Co p y C o nst r u c tor is us e d to decl a r e and initialize an object from another object. For example the statement: abc c2(c1); would define the object c2 and at the same time initialize it to the value of c1. The process of initializing through a copy constructor is known as copy initialization.
SOMETHING IMPORTANT Au t omat i c a l l y call e d a n o b j e ct is wh e n created. We can define our own constructors A constructor takes the same name as the class name. W e c a n’t defin e a co n s t ruct o r in the private section.