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

Similar documents
Padasalai.Net Half Yearly Exam Model Question Paper

SRE VIDYASAAGAR HIGHER SECONDARY SCHOOL

C++ 8. Constructors and Destructors

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

ntw;wp ngw;wpl kdkhw tho;j;jfpd;nwd;...

Darshan Institute of Engineering & Technology for Diploma Studies


Chapter-6 Classes and Objects. stud.execute(); getch();} Output:

OOP THROUGH C++(R16) int *x; float *f; char *c;

Padasalai.Net s Model Question Paper

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.

Constructor - example

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

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

SURA's Guides for 3rd to 12th Std for all Subjects in TM & EM Available MARCH [1]

CHAPTER 9 INHERITANCE. 9.1 Introduction

Data type of a pointer must be same as the data type of the variable to which the pointer variable is pointing. Here are a few examples:

Data Structures using OOP C++ Lecture 3

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

CSE202-Lec#4. CSE202 C++ Programming

Introduction to Programming Using Java (98-388)

DHANALAKSHMI COLLEGE OF ENGINEERING, CHENNAI DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING REWRAP TEST I CS6301 PROGRAMMING DATA STRUCTURES II

+2 Volume II OBJECT TECHNOLOGY OBJECTIVE QUESTIONS R.Sreenivasan SanThome HSS, Chennai-4. Chapter -1

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

Object Oriented Programming(OOP).

C++ : Object Oriented Features. What makes C++ Object Oriented

Functions. Lab 4. Introduction: A function : is a collection of statements that are grouped together to perform an operation.

Chapter-13 USER DEFINED FUNCTIONS

Chapter 3 Function Overloading


Short Notes of CS201

Constructors for classes

CS201 - Introduction to Programming Glossary By

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

Object Oriented Programming. Solved MCQs - Part 2

Some important concept in oops are 1) Classes 2) Objects 3) Data abstraction & Encapsulation. 4) Inheritance 5) Dynamic binding. 6) Message passing

MAN4A OBJECT ORIENTED PROGRAMMING WITH C++ Unit I - V

CS201 Some Important Definitions

CONSTRUCTORS AND DESTRUCTORS

4. C++ functions. 1. Library Function 2. User-defined Function

CHAPTER 4 FUNCTIONS. 4.1 Introduction

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

OBJECT ORIENTED PROGRAMMING. Ms. Ajeta Nandal C.R.Polytechnic,Rohtak

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

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

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

FORM 2 (Please put your name and form # on the scantron!!!!)

Faculty of Engineering Computer Engineering Department Islamic University of Gaza C++ Programming Language Lab # 6 Functions

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

COIMBATORE EDUCATIONAL DISTRICT

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of MCA

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

Lecture 18 Tao Wang 1

Chapter 10 Introduction to Classes

Bangalore South Campus

Module 1. C++ Classes Exercises

I BCS-031 BACHELOR OF COMPUTER APPLICATIONS (BCA) (Revised) Term-End Examination. June, 2015 BCS-031 : PROGRAMMING IN C ++

SRI SARASWATHI MATRIC HR SEC SCHOOL PANAPAKKAM +2 IMPORTANT 2 MARK AND 5 MARK QUESTIONS COMPUTER SCIENCE VOLUME I 2 MARKS

Functions. Introduction :

Tema 6: Dynamic memory

Kapi ap l S e S hgal P T C p t u er. r S. c S ienc n e A n A k n leshw h ar ar Guj u arat C C h - 8

6.096 Introduction to C++ January (IAP) 2009

Module Operator Overloading and Type Conversion. Table of Contents

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

2 2

CS304 Object Oriented Programming Final Term

Chapter 11: More About Classes and Object-Oriented Programming

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

Object-Oriented Programming

1. Answer the following : a) What do you mean by Open Source Software. Give an example. (2)

CSC 210, Exam Two Section February 1999

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

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

- HALF YEARLY EXAM ANSWER KEY DEC-2016 COMPUTER SCIENCE ENGLISH MEDIUM

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

Object Oriented Design

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

C++_ MARKS 40 MIN

C++ (classes) Hwansoo Han

PESIT Bangalore South Campus

Introduction To C#.NET

Solved Exercises from exams: Midterm(1)

Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat

Programming in C and C++

Programming, numerics and optimization

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

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

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION (Autonomous) (ISO/IEC Certified) Subject Name: Object Oriented Programming

A506 / C201 Computer Programming II Placement Exam Sample Questions. For each of the following, choose the most appropriate answer (2pts each).

Computer Science - I Code - [A]

PROGRAMMING IN C++ COURSE CONTENT

C++ C and C++ C++ fundamental types. C++ enumeration. To quote Bjarne Stroustrup: 5. Overloading Namespaces Classes

Selected Questions from by Nageshwara Rao

Type Conversion. and. Statements

Initializing and Finalizing Objects

Abstract Data Types (ADTs) 1. Legal Values. Client Code for Rational ADT. ADT Design. CS 247: Software Engineering Principles

Conversions and Overloading : Overloading

Distributed Real-Time Control Systems. Lecture 17 C++ Programming Intro to C++ Objects and Classes

CS201- Introduction to Programming Current Quizzes

CSC1322 Object-Oriented Programming Concepts

Transcription:

CHAPTER 8 CONSTRUCTORS AND DESTRUCTORS 8.1 Introduction When an instance of a class comes into scope, a special function called the constructor gets executed. The constructor function initializes the class object. When a class object goes out of scope, a special function called the destructor gets executed. The constructor function name and the destructor have the same name as the class tag. Both the functions return nothing. They are not associated with any data type. 8.2 Constructors // Program - 8.1 // to determine constructors and destructors #include<iostream.h> #include<conio.h> class simple private:int a,b; public: simple() a= 0 ; b= 0; cout<< \n Constructor of class-simple ; ~simple() cout<< \n Destructor of class simple.. ; void getdata() ; cout<< \n Enter values for a and b... ; cin>>a>>b; void putdata() cout<< \nthe two integers.. <<a<< \t << b; cout<< \n The sum of the variables.. << a+b; void main() simple s; s.getdata(); s.putdata(); 185

When the above program is executed, constructor simple() is automatically executed when the object is created. Destructor ~ simple() is executed, when the scope of the object s is lost, i.e., at the time of program termination. The output of the program will be as follows: Constructor of class - simple.. Enter values for a & b 5 6 The two integers.. 5 6 The sum of the variables.. 11 Destructor of class - simple 8.3 Functions of constructor 1) The constructor function initializes the class object 2) The memory space is allocated to an object 8.4 Constructor overloading Function overloading can be applied for constructors, as constructors are special functions of classes. Program - 8.2 demonstrates constructor overloading. The constructor add() is a constructor without parameters(non parameterized). It is called as default constructor. More traditionally default constructors are referred to compiler generated constructors i.e., constructors defined by the computers in the absence of user defined constructor. A non- parameterized constructor is executed when an object without parameters is declared. 186

// Program - 8.2 // To demonstrate constructor overloading # include<iostream.h> #include<conio.h> class add int num1, num2, sum; public: add() cout<< \n Constructor without parameters.. ; num1= 0; num2= 0; sum = 0; add ( int s1, int s2 ) cout<< \n Parameterized constructor... ; num1= s1; num2=s2; sum=null; add (add &a) cout<< \n Copy Constructor... ; num1= a.num1; num2=a.num2; sum = NULL; void getdata() cout<< Enter data... ; cin>>num1>>num2; void addition() sum=num1+num2; void putdata() cout<< \n The numbers are.. ; cout<<num1<< \t <<num2; cout<< \n The sum of the numbers are.. << sum; ; void main() add a, b (10, 20), c(b); a.getdata(); a.addition(); b.addition(); c.addition(); cout<< \n Object a : ; a.putdata(); cout<< \n Object b : ; b.putdata(); cout<< \n Object c.. ; c.putdata(); 187

OUTPUT: Constructor without parameters. Parameterized Constructor... Copy Constructors Enter data.. 5 6 Object a: The numbers are 5 6 The sum of the numbers are.. 11 Object b: The numbers are 10 20 The sum of the numbers are 30 Object c: The numbers are 10 20 The sum of the numbers are.. 30 The constructor add ( int s1, int s2) is called as parameterized constructor.to invoke this constructor, the object should be declared with two integer constants or variables. 188

Note: char, float double parameters can be matched with int data type due to implicit type conversions For example: add a ( 10, 60 ) / add a ( ivar, ivar ) The constructor add (add &a ) is called as copy constructor. A copy constructor is executed: 1) When an object is passed as a parameter to any of the member functions Example void add::putdata( add x); 2) When a member function returns an object For example, add getdata(); 3) When an object is passed by reference to constructor For example, add a; b(a); The following program 2 demonstrates as to when a copy constructor is executed? 189

// Program 8.3 // To demonstrate constructor overloading # include<iostream.h> #include<conio.h> class add int num1, num2, sum; public: add() cout<< \n Constructor without parameters.. ; num1= \0 ; num2= \0 ; sum = \0 ; add ( int s1, int s2 ) cout<< \n Parameterized constructor... ; num1= s1; num2=s2; sum=null; add (add &a) cout<< \n Copy Constructor... ; num1= a.num1; num2=a.num2; sum = NULL; void getdata() cout<< Enter data... ; cin>>num1>>num2; void addition(add b) sum=num1+ num2 +b.num1 + b.num2; add addition() add a(5,6); sum = num1 + num2 +a.num1 +a.num2; ; void putdata() cout<< \n The numbers are.. ; cout<<num1<< \t <<num2; cout<< \n The sum of the numbers are.. << sum; 190

void main() clrscr(); add a, b (10, 20), c(b); a.getdata(); a.addition(b); b = c.addition(); c.addition(); cout<< \n Object a : ; a.putdata(); cout<< \n Object b : ; b.putdata(); cout<< \n Object c.. ; c.putdata(); ouput of the above program Constructor without parameters.. Parameterized constructor... Copy Constructor... Enter data... 2 3 Copy Constructor... Parameterized constructor... Parameterized constructor... Object a : The numbers are..2 3 The sum of the numbers are.. 35 Object b : The numbers are..0 1494 The sum of the numbers are.. 0 Object c.. The numbers are..10 20 The sum of the numbers are.. 41 Have you noticed as to how many times copy constructor is executed? Copy constructor is for the following statements of Program 8.3. 191

In the above example the function addition is also overloaded. So, primarily functions declared anywhere within the program can be overloaded. 8.5 Rules for constructor definition and usage 1) The name of the constructor must be same as that of the class 2) A constructor can have parameter list 3) The constructor function can be overloaded 4) The compiler generates a constructor, in the absence of a user defined constructor 5) The constructor is executed automatically 8.6 Destructors A destructor is a function that removes the memory of an object which was allocated by the constructor at the time of creating a object. It carries the same name as the class tag, but with a tilde ( ~) as prefix. 192

Example : class simple public : ~simple()... 8.7 Rules for destructor definition and usage 1) The destructor has the same name as that of the class prefixed by the tilde character ~. 2) The destructor cannot have arguments 3) It has no return type 4) Destructors cannot be overloaded i.e., there can be only one destructor in a class 5) In the absence of user defined destructor, it is generated by the compiler 6) The destructor is executed automatically when the control reaches the end of class scope 193

Exercises I Complete the following table Constructor Destructor 1. Should be declared under - scope - scope 2. overloading is - - 3. Is executed when an object is The function of a II. Why do the following snippets throw error? Class simple private : int x; simple() x = 5; ; Class simple private : int x; public : simple(int y) x = y; ; void main() simple s; Class simple private : int x; public : simple(int y) x = y; simple(int z = 5) x = z; ; void main() simple s(6); 194