OBJECT ORIENTED PROGRAMMING AND DATA STRUCTURES CONSTRUCTORS AND DESTRUCTORS



Similar documents
13 Classes & Objects with Constructors/Destructors

Chapter 4 OOPS WITH C++ Sahaj Computer Solutions

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

C++FA 5.1 PRACTICE MID-TERM EXAM

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

Constructor, Destructor, Accessibility and Virtual Functions

Multichoice Quetions 1. Atributes a. are listed in the second part of the class box b. its time is preceded by a colon. c. its default value is

Subtopics - Functions Function Declaration Function Arguments Return Statements and values. By Hardeep Singh

CS193D Handout 06 Winter 2004 January 26, 2004 Copy Constructor and operator=

Polymorphism. Problems with switch statement. Solution - use virtual functions (polymorphism) Polymorphism

C++ Overloading, Constructors, Assignment operator

CpSc212 Goddard Notes Chapter 6. Yet More on Classes. We discuss the problems of comparing, copying, passing, outputting, and destructing

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

EP241 Computer Programming

IS0020 Program Design and Software Tools Midterm, Feb 24, Instruction

Ch 7-1. Object-Oriented Programming and Classes

Classes and Objects in Java Constructors. In creating objects of the type Fraction, we have used statements similar to the following:

1.1.3 Syntax The syntax for creating a derived class is very simple. (You will wish everything else about it were so simple though.

Laboratory Assignments of OBJECT ORIENTED METHODOLOGY & PROGRAMMING (USING C++) [IT 553]

Comp151. Definitions & Declarations

COMPUTER SCIENCE 1999 (Delhi Board)

CISC 181 Project 3 Designing Classes for Bank Accounts

Answers to Review Questions Chapter 7

Lecture 3. Arrays. Name of array. c[0] c[1] c[2] c[3] c[4] c[5] c[6] c[7] c[8] c[9] c[10] c[11] Position number of the element within array c

Subject Name: Object Oriented Programming in C++ Subject Code:

Many applications consist of one or more classes, each containing one or more methods. If you become part of a development team in industry, you may

1 bool operator==(complex a, Complex b) { 2 return a.real()==b.real() 3 && a.imag()==b.imag(); 4 } 1 bool Complex::operator==(Complex b) {

C++ INTERVIEW QUESTIONS

Debugging. Common Semantic Errors ESE112. Java Library. It is highly unlikely that you will write code that will work on the first go

CSCI 253. Object Oriented Programming (OOP) Overview. George Blankenship 1. Object Oriented Design: Java Review OOP George Blankenship.

Chapter One Introduction to Programming

An Incomplete C++ Primer. University of Wyoming MA 5310

J a v a Quiz (Unit 3, Test 0 Practice)

While Loop. 6. Iteration

Syllabus OBJECT ORIENTED PROGRAMMING C++

VB.NET - CLASSES & OBJECTS

Data Structures Using C++ 2E. Chapter 5 Linked Lists

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

C++ Programming Language

Object Oriented Software Design II

1. Use the class definition above to circle and identify the parts of code from the list given in parts a j.

Advanced Data Structures

Class 16: Function Parameters and Polymorphism

Course notes Standard C++ programming

Practical Programming Methodology. Michael Buro. Class Inheritance (CMPUT-201)

MAHALAKSHMI ENGINEERING COLLEGE B TIRUCHIRAPALLI

El Dorado Union High School District Educational Services

C++ Crash Kurs. C++ Object-Oriented Programming

Basics of C++ and object orientation in OpenFOAM

C++ Outline. cout << "Enter two integers: "; int x, y; cin >> x >> y; cout << "The sum is: " << x + y << \n ;

For the next three questions, consider the class declaration: Member function implementations put inline to save space.

3 Representation in C++.

Chapter 5 Functions. Introducing Functions

Basic Object-Oriented Programming in Java

Software Engineering Concepts: Testing. Pointers & Dynamic Allocation. CS 311 Data Structures and Algorithms Lecture Slides Monday, September 14, 2009

Compiler Construction

Object Oriented Software Design II

CSE 1020 Introduction to Computer Science I A sample nal exam

Short Notes on Dynamic Memory Allocation, Pointer and Data Structure

Brent A. Perdue. July 15, 2009

The C Programming Language course syllabus associate level


Conversion Constructors

PRI-(BASIC2) Preliminary Reference Information Mod date 3. Jun. 2015

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

More C++ Concepts. Operator overloading Friend Function This Operator Inline Function

7.7 Case Study: Calculating Depreciation

C++ DATA STRUCTURES. Defining a Structure: Accessing Structure Members:

Conditions & Boolean Expressions

CS 241 Data Organization Coding Standards

MS Visual C++ Introduction. Quick Introduction. A1 Visual C++

Parameter Passing. Parameter Passing. Parameter Passing Modes in Fortran. Parameter Passing Modes in C

Moving from CS 61A Scheme to CS 61B Java

Accessing the Real Time Clock Registers and the NMI Enable Bit

Computer Programming C++ Classes and Objects 15 th Lecture

Introduction to Programming Block Tutorial C/C++

PROBLEM SOLVING SEVENTH EDITION WALTER SAVITCH UNIVERSITY OF CALIFORNIA, SAN DIEGO CONTRIBUTOR KENRICK MOCK UNIVERSITY OF ALASKA, ANCHORAGE PEARSON

Experiment # 1 AIM: Study and practice of Internal & External DOS commands.

Data Structures Using C++ 2E. Chapter 5 Linked Lists

Java Classes. GEEN163 Introduction to Computer Programming

Calling the Function. Two Function Declarations Here is a function declared as pass by value. Why use Pass By Reference?

11 November

Appendix K Introduction to Microsoft Visual C++ 6.0

Course Name: ADVANCE COURSE IN SOFTWARE DEVELOPMENT (Specialization:.Net Technologies)

Linked List as an ADT (cont d.)

Common Beginner C++ Programming Mistakes

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

Java CPD (I) Frans Coenen Department of Computer Science

Basics of I/O Streams and File I/O

explicit class and default definitions revision of SC22/WG21/N1582 = and SC22/WG21/ N

Boolean Expressions 1. In C++, the number 0 (zero) is considered to be false, all other numbers are true.

Moving from C++ to VBA

Object Oriented Programming With C++(10CS36) Question Bank. UNIT 1: Introduction to C++

CS107L Handout 02 Autumn 2007 October 5, 2007 Copy Constructor and operator=

Transcription:

CONSTRUCTORS AND DESTRUCTORS Constructors: A constructor is a member function whose name is same as class name and is used to initialize data members and allocate memory dynamically. A constructor is automatically executed whenever objects are created. Class Class-Name Function-Name ( ); //Variable and Function Declaration //Constructor (Class name and Function should be as same) Class integer integer( ); //constructor Characteristics of Constructor: 1. Constructors should be declared in public section. 2. They are involved automatically when the objects are created. 3. They do not have return types. 4. They cannot be inherited. TYPES OF CONSTRUCTORS 1. Parameterized Constructors 2. Constructor with default Arguments 3. Copy Constructors 4. Dynamic Constructors M.EZHILVENDAN [AP/IT] [Jawahar Engineering College - Chennai] Page 1

1. Parameterized Constructors: The constructor with arguments is called parameterized constructor. Example : Class integer //Class Class-Name int m,n; //Variable Declaration integer(int x,int y) //Function Definition with some parameter m=x; n=y; ; 1.1. By calling the constructor explicitly: Class-Name Object-Name = Function-Name (Arguments); integer int1=integer(0,100); 1.2. By calling the constructor implicitly: Class-Name Object-Name(Arguments); integer int1(0,100); 2. Constructor with default Arguments The constructor with no arguments is called default constructor Class integer int m,n; Public: Integer( );. ; // Class Class-Name // Variable Declaration; //Function Declaration (Function not having arguments) M.EZHILVENDAN [AP/IT] [Jawahar Engineering College - Chennai] Page 2

integer::integer( ) //default constructor No parameter or Arguments Pass //Function Definition outside the Class m=0; n=0; 3. Copy Constructors A copy constructor is used to declare and initialize an object from another object. It takes a reference to an object of the same class as an argument. class code //Class Class-Name int id; //Variable Declaration code(); //Default Constructor code(int a)id=a; //Parameter Constructor code(code &x) //Copy Constructor Constructor Argument call as an another constructor id=x.id; ; 4. Dynamic Constructors It is used for allocation of memory to a variable at run time. M.EZHILVENDAN [AP/IT] [Jawahar Engineering College - Chennai] Page 3

Example Program For Constructor // Constructor Types #include <iostream.h> class Distance int feet, int inch; Distance() // Default constructor feet = 0; inch = 0; Distance (int n, int d = 0) // Parameterized constructor feet = n; inch = d; Distance (const Distance &a) // Copy constructor feet = a.feet; inch = a.inch; void print() cout << feet: <<feet << " ' "<< /t << inch: <<inch << " "<< \n"; ; int main() Distance d1, d2(4), d3(22,7); Distance d4(d2); d1.print(); d3.print(); d4.print(); Output: Feet: 0 inch: 0 Feet: 22 inch: 7 Feet: 4 inch: 0 M.EZHILVENDAN [AP/IT] [Jawahar Engineering College - Chennai] Page 4

Destructors A destructor is used to destroy objects when they go out of scope. Like constructor, destructor has the same name as class, but proceeded by a tilde ~. A destructor never takes any arguments nor does it return. There can be only one destructor for a class. Any memory allocated dynamically using constructor should be released using delete in the destructor. ~mystring(); // Constructor and Destructor #include<iostream.h> #include<conio.h> class add int a,b,c; add() cout<<"enter the value of A: "; cin>>a; cout<<"enter the value of B: "; cin>>b; c=a+b; ; ~add() cout<<"the value of A is: "<<a<<endl; cout<<"the value of B is: "<<b<<endl; cout<<"the sum of A and B is: "<<c<<endl; M.EZHILVENDAN [AP/IT] [Jawahar Engineering College - Chennai] Page 5

void main() clrscr(); add a1; getch(); Output Enter the value of A: 12 Enter the value of B: 15 The value of A is: 12 The value of B is: 15 The sum of A and B is: 27 M.EZHILVENDAN [AP/IT] [Jawahar Engineering College - Chennai] Page 6