Concepts of Constructors and Its Types

Slides:



Advertisements
Similar presentations
Constructors and Destructors. Constructor Constructor—what’s this? Constructor—what’s this? method used for initializing objects (of certain class) method.
Advertisements

Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
Introduction to Programming Lecture 39. Copy Constructor.
F UNCTION O VERLOADING Chapter 5 Department of CSE, BUET 1.
Short C++ Review CS 302 – Data Structures. Call by value/reference.
Structures/Classes CS 308 – Data Structures. What is a structure? It is an aggregate data type built using elements of other types. Declaring a structure.
Constructors & Destructors Review CS 308 – Data Structures.
Introduction to C++ Programming
Operator Overloading CS 308 – Data Structures What is operator overloading? Changing the definition of an operator so it can be applied on the objects.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
C++ Review CS 302 – Data Structures Review Topics Calling functions by value or reference Pointers and reference variables Static and dynamic arrays.
CONSTRUCTORS AND THEIR TYPES. Prepared by. MURLI MANOHAR. PGT (COMP
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
Object Oriented Programming (OOP) Lecture No. 8. Review ► Class  Concept  Definition ► Data members ► Member Functions ► Access specifier.
CSC241 Object-Oriented Programming (OOP) Lecture No. 4.
More C++ Features True object initialisation
Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses.
Csi2172 class 5 Midterm: June 12. constructor Special method used to create objects of the class Never has a return type. Is called automatically upon.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Programming II Array of objects. this Using the this Pointer this Objects use the this pointer implicitly or explicitly. – this is – this is used implicitly.
Constructors & Destructors, Proxy Classes, Friend Function and example of static member.
CS1201: Programming Language 2 Classes and objects.
Constructors Initializing New Objects #include “fraction.h” int main() { float x; float y = 6.7; float z(7.2); Fraction.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Object Oriented Programming(Objects& Class) Classes are an expanded concept of data structures: like.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Destructors The destructor fulfills the opposite functionality. It is automatically called when an object.
Classes II Lecture 7 Course Name: High Level Programming Language Year : 2010.
Constructors, Copy Constructors, constructor overloading, function overloading Lecture 04.
Constructors And Destructors. 2 Constructor Special Member Function used for Initialization -- Same Name as the Class Name Special Member Function used.
1 Class 19 Chapter 13 – Creating a class definition.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Introduction to C++ programming Recap- session 1 Structure of C++ program Keywords Operators – Arithmetic – Relational – Logical Data types Classes and.
Object Oriented Programming (OOP) Lecture No.2 Downloaded From:
Chapter 2 Objects and Classes
Constructors and Destructors
Pointer to an Object Can define a pointer to an object:
Pointers and Dynamic Arrays
Access Functions and Friend Functions
Object-Oriented Programming (OOP) Lecture No. 15
Submission Example May 14, 2018
Overloading Operator MySting Example
Object Oriented Programming (OOP) Lecture No. 8
CONSTRUCTORS & DESTRUCTORS
Constructors & Destructors.
Object-Oriented Programming (OOP) Lecture No. 17
Chapter 5 Classes.
Constructor & Destructor
Constructors & Destructors
14.4 Copy Constructors.
LinkedList Class.
This pointer, Dynamic memory allocation, Constructors and Destructor
group work #hifiTeam
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Chapter 15 Pointers, Dynamic Data, and Reference Types
CS212: Object Oriented Analysis and Design
Pointers, Dynamic Data, and Reference Types
Constructor Spl member fn auto ini of object
Constructors and destructors
Constructors and Destructors
Constructors and Destructors
9-10 Classes: A Deeper Look.
Introduction to Classes and Objects
CS410 – Software Engineering Lecture #5: C++ Basics III
A Deeper Look at Classes
Review: C++ class represents an ADT
Destructors, Copy Constructors & Copy Assignment Operators
Pointers, Dynamic Data, and Reference Types
Destructors, Copy Constructors & Copy Assignment Operators
Constructors & Destructors
9-10 Classes: A Deeper Look.
Presentation transcript:

Concepts of Constructors and Its Types

What is a constructor? It is a member function which initializes a class. A constructor has: (i) the same name as the class itself (ii) no return type

class rectangle { private: float height; float width; int xpos; int ypos; public: rectangle(float, float); // constructor void draw(); // draw member function void posn(int, int); // position member function void move(int, int); // move member function };   rectangle::rectangle(float h, float w) { height = h; width = w; xpos = 0; ypos = 0; }

Comments on constructors A constructor is called automatically whenever a new instance of a class is created. You must supply the arguments to the constructor when a new instance is created. If you do not specify a constructor, the compiler generates a default constructor for you (expects no parameters and has an empty body).

Comments on constructors (cont.) void main() { rectangle rc(3.0, 2.0);   rc.posn(100, 100); rc.draw(); rc.move(50, 50); } Warning: attempting to initialize a data member of a class explicitly in the class definition is a syntax error.

Overloading constructors You can have more than one constructor in a class, as long as each has a different list of arguments.   class rectangle { private: float height; float width; int xpos; int ypos; public: rectangle(float, float); // constructor rectangle(); // another constructor void draw(); // draw member function void posn(int, int); // position member function void move(int, int); // move member function };

Overloading constructors (cont.) rectangle::rectangle() { height = 10; width = 10; xpos = 0; ypos = 0; }   void main() rectangle rc1(3.0, 2.0); rectangle rc2(); rc1.draw(); rc2.draw();

Composition: objects as members of classes A class may have objects of other classes as members.   class properties { private: int color; int line; public: properties(int, int); // constructor }; properties::properties(int c, int l) { color = c; line = l; }

Composition: objects as members of classes (cont.) class rectangle { private: float height; float width; int xpos; int ypos; properties pr; // another object public: rectangle(float, float, int, int ); // constructor void draw(); // draw member function void posn(int, int); // position member function void move(int, int); // move member function };

Composition: objects as members of classes (cont.) rectangle::rectangle(float h, float w, int c, int l):pr(c, l) { height = h; width = w; xpos = 0; ypos = 0; };   void main() rectangle rc(3.0, 2.0, 1, 3); C++ statements; }

class string { private: char *s; int size; public: string(char *); // constructor ~string(); // destructor };   string::string(char *c) { size = strlen(c); s = new char[size+1]; strcpy(s,c); } string::~string() delete []s;

What is a copy constructor? It is a member function which initializes an object using another object of the same class. A copy constructor has the following general function prototype:  class_name (const class_name&);

class rectangle { private: float height; float width; int xpos; int ypos; public: rectangle(float, float); // constructor rectangle(const rectangle&); // copy constructor void draw(); // draw member function void posn(int, int); // position member function void move(int, int); // move member function };

rectangle::rectangle(const rectangle& old_rc) { height = old_rc.height; width = old_rc.width; xpos = old_rc.xpos; ypos = old_rc.ypos; }   void main() rectangle rc1(3.0, 2.0); // use constructor rectangle rc2(rc1); // use copy constructor rectangle rc3 = rc1; // alternative syntax for // copy constructor  C++ statements;

Defining copy constructors is very important In the absence of a copy constructor, the C++ compiler builds a default copy constructor for each class which is doing a memberwise copy between objects. Default copy constructors work fine unless the class contains pointer data members ... why???

#include <iostream.h> #include <string.h>   class string { private: char *s; int size; public: string(char *); // constructor ~string(); // destructor void print(); void copy(char *); }; void string::print() { cout << s << endl; }

void string::copy(char *c) { strcpy(s, c); }   void main() string str1("George"); string str2 = str1; // default copy constructor str1.print(); // what is printed ? str2.print(); str2.copy("Mary"); str1.print(); // what is printed now ?

Defining a copy constructor for the above example: class string { private: char *s; int size; public: string(char *); // constructor ~string(); // destructor string(const string&); // copy constructor void print(); void copy(char *); };

string::string(const string& old_str) size = old_str.size; { size = old_str.size; s = new char[size+1]; strcpy(s,old_str.s); }   void main() string str1("George"); string str2 = str1; str1.print(); // what is printed ? str2.print(); str2.copy("Mary"); str1.print(); // what is printed now ? Note: same results can be obtained by overloading the assignment operator.

Defalut Constructor A default constructor is a constructor that either has no parameters, or if it has parameters, all the parameters have default values. If no user-defined constructor exists for a class A and one is needed, the compiler implicitly declares a constructor A::A(). This constructor is an inline public member of its class. The compiler will implicitly define A::A() when the compiler uses this constructor to create an object of type A. The constructor will have no constructor initializer and a null body. The compiler first implicitly defines the implicitly declared constructors of the base classes and nonstatic data members of a class A before defining the implicitly declared constructor of A. No default constructor is created for a class that has any constant or reference type members. A constructor of a class A is trivial if all the following are true: •It is implicitly defined •A has no virtual functions and no virtual base classes •All the direct base classes of A have trivial constructors •The classes of all the nonstatic data members of A have trivial constructors Contd…

if no constructor was declared, then compiler generates the default constructor (constructor with no arguments) of an empty body, for T class: T::T() { } if we declare any constructor (even constructor with args) then compiler does not generate the default constructor.