Constructors & Destructors

Similar documents
Initializing and Finalizing Objects

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

6.096 Introduction to C++ January (IAP) 2009

Object Oriented Design

Classes: Member functions // classes example #include <iostream> using namespace std; Objects : Reminder. Member functions: Methods.

Recharge (int, int, int); //constructor declared void disply();

Pointers, Dynamic Data, and Reference Types

Example : class Student { int rollno; float marks; public: student( ) //Constructor { rollno=0; marks=0.0; } //other public members };

Chapter 10 Introduction to Classes

C++ 8. Constructors and Destructors

THE NAME OF THE CONSTRUCTOR AND DESTRUCTOR(HAVING (~) BEFORE ITS NAME) FUNCTION MUST BE SAME AS THE NAME OF THE CLASS IN WHICH THEY ARE DECLARED.

Lecture 18 Tao Wang 1

Introduction Of Classes ( OOPS )

Fast Introduction to Object Oriented Programming and C++

Classes and Objects. Class scope: - private members are only accessible by the class methods.

AN OVERVIEW OF C++ 1

Constructor - example

Review: C++ Basic Concepts. Dr. Yingwu Zhu

COMS W3101 Programming Language: C++ (Fall 2015) Ramana Isukapalli

Constructors for classes

COMS W3101 Programming Language: C++ (Fall 2015) Ramana Isukapalli

Class. SSE2034: System Software Experiment 3, Fall 2018, Jinkyu Jeong

Abstraction in Software Development

C++ PROGRAMMING LANGUAGE: CLASSES. CAAM 519, CHAPTER 13

CS201 Some Important Definitions

public : int min, hour ; T( ) //here constructor is defined inside the class definition, as line function. { sec = min = hour = 0 ; }

Next week s homework. Classes: Member functions. Member functions: Methods. Objects : Reminder. Objects : Reminder 3/6/2017

Come and join us at WebLyceum

Lecture 8: Object-Oriented Programming (OOP) EE3490E: Programming S1 2017/2018 Dr. Đào Trung Kiên Hanoi Univ. of Science and Technology

Advanced Systems Programming

Composition I. composition is a way to combine or compose multiple classes together to create new class

Constants, References

Inheritance, and Polymorphism.

Function Overloading

Chapter 9 Classes : A Deeper Look, Part 1

Introduction to Classes

Object Oriented Programming. Assistant Lecture Omar Al Khayat 2 nd Year

C++ Addendum: Inheritance of Special Member Functions. Constructors Destructor Construction and Destruction Order Assignment Operator

Chapter 6: User-Defined Functions. Objectives (cont d.) Objectives. Introduction. Predefined Functions 12/2/2016

Programming, numerics and optimization

COMS W3101 Programming Language: C++ (Fall 2016) Ramana Isukapalli

Object Oriented Programming(OOP).

G52CPP C++ Programming Lecture 13

Make Classes Useful Again

Quiz Start Time: 09:34 PM Time Left 82 sec(s)

CS304 Object Oriented Programming Final Term

Comp151. Construction & Initialization

CS201 Latest Solved MCQs

Lecture 5. Function Pointers

Page. No. 1/15 CS201 Introduction to Programmming Solved Subjective Questions From spring 2010 Final Term Papers By vuzs Team

Intro to OOP Visibility/protection levels and constructors Friend, convert constructor, destructor Operator overloading a<=b a.

Programming in C++: Assignment Week 3

1. Describe History of C++? 2. What is Dev. C++? 3. Why Use Dev. C++ instead of C++ DOS IDE?

CS201- Introduction to Programming Current Quizzes

Topic 10: Introduction to OO analysis and design

IS0020 Program Design and Software Tools Midterm, Fall, 2004

Come and join us at WebLyceum

Classes and Pointers: Some Peculiarities


KOM3191 Object Oriented Programming Dr Muharrem Mercimek OPERATOR OVERLOADING. KOM3191 Object-Oriented Programming

See the CS 2704 notes on C++ Class Basics for more details and examples. Data Structures & OO Development I

CS 162, Lecture 25: Exam II Review. 30 May 2018

Introduction to C++ Introduction to C++ Dr Alex Martin 2013 Slide 1

Function Declarations. Reference and Pointer Pitfalls. Overloaded Functions. Default Arguments

Constructors and Destructors. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

cout << "How many numbers would you like to type? "; cin >> memsize; p = new int[memsize];

Miri Ben-Nissan (Kopel) (2017)

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

! A class in C++ is similar to a structure. ! A class contains members: - variables AND. - public: accessible outside the class.

A Deeper Look at Classes

Iterator: A way to sequentially access ( traverse ) each object in a container or collection. - Access is done as needed, not necessarily at once.

Chapter 13: Introduction to Classes Procedural and Object-Oriented Programming

CSCE 110 PROGRAMMING FUNDAMENTALS

cout<< \n Enter values for a and b... ; cin>>a>>b;

nptr = new int; // assigns valid address_of_int value to nptr std::cin >> n; // assigns valid int value to n

Object oriented programming with C++

C++: Const Function Overloading Constructors and Destructors Enumerations Assertions

Short Notes of CS201

CSE 143. Linked Lists. Linked Lists. Manipulating Nodes (1) Creating Nodes. Manipulating Nodes (3) Manipulating Nodes (2) CSE 143 1

W3101: Programming Languages C++ Ramana Isukapalli

INHERITANCE PART 2. Constructors and Destructors under. Multiple Inheritance. Common Programming Errors. CSC 330 OO Software Design 1

Modern C++ for Computer Vision and Image Processing. Igor Bogoslavskyi

Implementing Abstract Data Types (ADT) using Classes

Software Design and Analysis for Engineers

(5 2) Introduction to Classes in C++ Instructor - Andrew S. O Fallon CptS 122 (February 7, 2018) Washington State University

Topics. Topics (Continued) 7.1 Abstract Data Types. Abstraction and Data Types. 7.2 Object-Oriented Programming

CS201 - Introduction to Programming Glossary By

Ch. 11: References & the Copy-Constructor. - continued -

The Class. Classes and Objects. Example class: Time class declaration with functions defined inline. Using Time class in a driver.

More C++ : Vectors, Classes, Inheritance, Templates

! A class in C++ is similar to a structure. ! A class contains members: - variables AND. - public: accessible outside the class.

CSCI 123 Introduction to Programming Concepts in C++


SSE2034: System Software Experiment 3 Spring 2016

C++ Programming: From Problem Analysis to Program Design, Fifth Edition. Chapter 12: Classes and Data Abstraction

Lecture-5. Miscellaneous topics Templates. W3101: Programming Languages C++ Ramana Isukapalli

CONSTRUCTOR AND DESTRUCTOR

A brief introduction to C++

l A class in C++ is similar to a structure. l A class contains members: - variables AND - public: accessible outside the class.

Pointers. Developed By Ms. K.M.Sanghavi

Transcription:

Constructors & Destructors

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); void draw(); void posn(int, int); void move(int, int); // constructor // draw member function // position member function // 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); rc.draw(); 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;

class rectangle private: float height; float width; int xpos; int ypos; ; Composition: objects as members of classes (cont.) 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;

Destructor It is a member function which deletes an object. A destructor function is called automatically when the object goes out of scope: (1) the function ends (2) the program ends (3) a block containing temporary variables ends (4) a delete operator is called A destructor has: (i) the same name as the class but is preceded by a tilde (~) (ii) no arguments and return no values

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

Comments on destructors If you do not specify a destructor, the compiler generates a default destructor for you. When a class contains a pointer to memory you allocate, it is your responsibility to release the memory before the class instance is destroyed.

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 // use copy constructor rectangle rc2(rc1); 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...

#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? str2.print();

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; 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? str2.print(); Note: same results can be obtained by overloading the assignment operator.