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

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

Friend Functions. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Static Members. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Namespaces. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Object Oriented Design

Constructor - example

Data Structures using OOP C++ Lecture 3

Object Oriented Programming. C++ 6 th Sem, A Div Ms. Mouna M. Naravani

Constructors for classes


Module - 3 Classes, Inheritance, Exceptions, Packages and Interfaces. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Cpt S 122 Data Structures. Introduction to C++ Part II

Lecture 18 Tao Wang 1

Data Types, Variables and Arrays. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

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

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

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

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

C++ 8. Constructors and Destructors

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

Chapter 10 Introduction to Classes

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING OBJECT ORIENTED PROGRAMMING CLASS : THIRD SEMESTER CSE

Simple Java Programs. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

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.

Object Oriented Programming(OOP).

Introduction Of Classes ( OOPS )

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

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

Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat Chapter 6 Inheritance Extending a Class

内存管理. Memory management

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

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

An Introduction to C++

CS201 Some Important Definitions

10. Abstract Data Types

Fundamentals of Programming Session 12

Object Oriented Programming COP3330 / CGS5409

Cpt S 122 Data Structures. Course Review Midterm Exam # 2

Chapter 11. Abstract Data Types and Encapsulation Concepts

Function Overloading

Developed By Strawberry

Module 5 The Applet Class, Swings. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

OBJECT ORIENTED PROGRAMMING USING C++

Object-Oriented Programming

Implementing Abstractions

Program vector.cc (continued)

Short Notes of CS201

Ch02. True/False Indicate whether the statement is true or false.

6.096 Introduction to C++ January (IAP) 2009

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

CS201 - Introduction to Programming Glossary By

CS304 Object Oriented Programming Final Term

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

Initializing and Finalizing Objects

Starting Savitch Chapter 10. A class is a data type whose variables are objects. Some pre-defined classes in C++ include int,

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.

Department of Information Technology Object Oriented Programming. Two Mark Questions

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

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

Chapter 13: Copy Control. Overview. Overview. Overview

Object-Oriented Principles and Practice / C++

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

QUIZ on Ch.5. Why is it sometimes not a good idea to place the private part of the interface in a header file?

G52CPP C++ Programming Lecture 13

Module 5 The Applet Class, Swings. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

OBJECT-ORIENTED PROGRAMMING CONCEPTS-CLASSES IV

Implementing Subprograms

Programming in C ++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Intermediate Programming & Design (C++) Classes in C++

CONSTRUCTOR AND DESTRUCTOR

C Programming for Engineers Functions

Classes and Pointers: Some Peculiarities

C++ Basic Syntax. Constructors and destructors. Wojciech Frohmberg / OOP Laboratory. Poznan University of Technology

Object-Oriented Programming

JAYARAM COLLEGE OF ENGINEERING AND TECHNOLOGY Pagalavadi, Tiruchirappalli (An approved by AICTE and Affiliated to Anna University)

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


QUIZ Friends class Y;

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

Interview Questions of C++

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

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Padasalai.Net s Model Question Paper

Jayaram college of Engineering and Technology, Pagalavadi. CS2203 Object Oriented Programming Question Bank Prepared By: S.Gopalakrishnan, Lecturer/IT

Scope. Scope is such an important thing that we ll review what we know about scope now:

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

Class, Variable, Constructor, Object, Method Questions

CPSC 427: Object-Oriented Programming

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

Chapter 10. Pointers and Dynamic Arrays. Copyright 2016 Pearson, Inc. All rights reserved.

Fundamentals of Programming Session 24

2 ADT Programming User-defined abstract data types

MaanavaN.Com CS1203 OBJECT ORIENTED PROGRAMMING DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Classes: A Deeper Look

Memory management COSC346

IS0020 Program Design and Software Tools Midterm, Fall, 2004

C How to Program, 6/e, 7/e

Exercise 6.2 A generic container class

Midterm 2. 7] Explain in your own words the concept of a handle class and how it s implemented in C++: What s wrong with this answer?

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

Transcription:

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

A constructor guarantees that an object created by the class will be initialized automatically.

Ex: create an object integer int1; Object int1 is created, and also initializes its data members m and n to zero. There is no need to write any statement to invoke the constructor function. If a normal member function is defined for zero initialization, we would need to invoke this function for each of the objects separately. This would be very inconvenient, if there are a large number of objects.

Default Constructor Accepts no parameters. Also called as Zero Constructor. If no such constructor is defined, then the compiler supplies a default constructor.

Parameterized Constructor A constructor that takes arguments or parameters are called Parameterized constructors. We can pass the arguments to constructor function when object are created.

We must pass the initial values as arguments to the constructor function when an object is declared. This can be done in two ways: By calling the constructor explicitly. By calling the constructor implicitly. By calling the constructor explicitly integer int1 = integer(10, 100); This statement creates an integer object int1 and passes the values 10 and 100 to it. By calling the constructor implicitly integer int1(10, 100); Also called as short hand method, shorter, better and easy to implement.

Copy Constructor Copy Constructor is used to declare and initialize an object from another object. Ex: integer I2 ( I1 ); This would define an object I2 and at the same time initialize it to the values of I1. Another form: integer I2 = I1;//it simply assigns the values of I1 to I2, member-by-memeber The process of initializing through a copy constructor is known as copy initialization. A copy constructor takes a reference to an object of the same class as itself as an argument.

Multiple Constructors in a Class class integer { int m, n; public: integer() { m=0; n=0; } integer(int a, int b) { m=a; n=b; } integer(integer & i) { m = i.m; n=i.n; }; } //Default Constructor //Parameterized Constructor //Copy Constructor 1. Object declarations: integer I1; - invokes default constructor and set both m and n of I1 to 0. 2. Object declarations: integer I2(20, 40); - invokes parameterized constructor and set both m and n of I2 to 20 and 40 respectively. 3. Object declarations: integer I3(I2); - invokes copy constructor which copies the values of I2 into I3.

The actual parameter, when specified, overrides the default value.

Default Constructor A :: A() Default Argument Constructor A :: A(int = 0) The default argument constructor can be called with either one argument or no arguments. When called with no arguments, it becomes a default constructor. When both these forms are used in a class, it causes ambiguity for a statement such as: A a; The ambiguity is whether to call A::A() or A::A(int = 0)

Destructors Used to destroy the objects that have been created by a constructor. The destructor is a member function whose name is the same as the class name but is preceded by a tilde( ~). Destructor of class integer can be defined as; ~integer() { } Destructor never takes any argument nor does it return any value. It will be invoked implicitly by the compiler upon exit from the program to clean up storage that is no longer accessible. (No need to call it explicitly) Destructors releases memory space for future use. Destructors destroy the objects in the reverse order of creation.

Whenever new is used to allocate memory in the constructors, we should use delete to free that memory. This is required because when the pointers to objects go out of scope, a destructor is not called implicitly.

Constructors.cpp Destructors.cpp DestructoroutofScope.cpp beforedestructor.cpp CopyConstructors.cpp OverloadedConstructors.cpp ParameterizedConstructors.cpp

References Sourav Sahay, Objected Oriented Programming with C++ E Balagurusamy, Objected Oriented Programming with C++ P. B. Kotur, Objected Oriented Programming with C++