Constructor, Destructor, Accessibility and Virtual Functions



Similar documents
13 Classes & Objects with Constructors/Destructors

CIS 190: C/C++ Programming. Polymorphism

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

C++ INTERVIEW QUESTIONS

OBJECT ORIENTED PROGRAMMING AND DATA STRUCTURES CONSTRUCTORS AND DESTRUCTORS

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

C++FA 5.1 PRACTICE MID-TERM EXAM

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

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

C++ Overloading, Constructors, Assignment operator

BCS2B02: OOP Concepts and Data Structures Using C++

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

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

Advanced Data Structures

Ch 7-1. Object-Oriented Programming and Classes

Description of Class Mutation Mutation Operators for Java

1. Polymorphism in C++...2

EP241 Computer Programming

Curriculum Map. Discipline: Computer Science Course: C++

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

CISC 181 Project 3 Designing Classes for Bank Accounts

Object Oriented Software Design II

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.

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) {

Java Interview Questions and Answers

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

El Dorado Union High School District Educational Services

Glossary of Object Oriented Terms

An Internet Course in Software Development with C++ for Engineering Students

Compile-time type versus run-time type. Consider the parameter to this function:

Embedded SQL. Unit 5.1. Dr Gordon Russell, Napier University

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

Appendix K Introduction to Microsoft Visual C++ 6.0

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

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

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

Class 16: Function Parameters and Polymorphism

Object-Oriented Programming Lecture 2: Classes and Objects

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

Chapter 5 Functions. Introducing Functions

Konzepte objektorientierter Programmierung

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

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

Function Overloading I

OpenCL Static C++ Kernel Language Extension

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

Course notes Standard C++ programming

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

C++ Programming Language

The C Programming Language course syllabus associate level

Linked List as an ADT (cont d.)

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

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

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

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

CEC225 COURSE COMPACT

Chapter 9 Delegates and Events

Comp151. Definitions & Declarations

Brent A. Perdue. July 15, 2009

COSC 181 Foundations of Computer Programming. Class 6

Chapter 4 OOPS WITH C++ Sahaj Computer Solutions

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

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

Basics of C++ and object orientation in OpenFOAM

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

Object Oriented Software Design II

C++FA 3.1 OPTIMIZING C++

Operator Overloading. Lecture 8. Operator Overloading. Running Example: Complex Numbers. Syntax. What can be overloaded. Syntax -- First Example

Copyright 2001, Bill Trudell. Permission is granted to copy for the PLoP 2001 conference. All other rights reserved.

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

Syllabus OBJECT ORIENTED PROGRAMMING C++

PHP Object Oriented Classes and objects

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

Data Structures using OOP C++ Lecture 1

ATM Case Study OBJECTIVES Pearson Education, Inc. All rights reserved Pearson Education, Inc. All rights reserved.

INTRODUCTION TO OBJECTIVE-C CSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGN LECTURE 12 09/29/2011

CS 101 Computer Programming and Utilization

6. Control Structures

Computer Programming C++ Classes and Objects 15 th Lecture

Passing 1D arrays to functions.

Binary compatibility for library developers. Thiago Macieira, Qt Core Maintainer LinuxCon North America, New Orleans, Sept. 2013

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

Compiler Construction

Introduction to Programming Block Tutorial C/C++

Object Oriented Software Design II

SE 360 Advances in Software Development Object Oriented Development in Java. Polymorphism. Dr. Senem Kumova Metin

Binary storage of graphs and related data

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

Java Classes. GEEN163 Introduction to Computer Programming

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

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

Basics of I/O Streams and File I/O

Transcription:

Constructor, Destructor, Accessibility and Virtual Functions 182.132 VL Objektorientierte Programmierung Raimund Kirner Mitwirkung an Folienerstellung: Astrit Ademaj

Agenda Constructor Destructors this pointer Inheritance / Accessibility Virtual functions

Class A class consists of: member data (usually private) member functions (usually public) the simplest class shall have at least two member functions: set data show data member data can be public, and member functions can be private

Member data initialization After an object is created: Standard ways for data initialization: Setting default constants Reading input from user (using cin >>)

Member data initialization - example class circle private: int xc, yc, radius ; void set (int x, int y, int r) xc = x; yc = y; radius = r; void get () cout << \Enter x coordinate: ; cin >> xc; cout << \Enter y coordinate: ; cin >> yc; cout << \Enter radius : ; cin >> radius; void draw () draw_circle(xc, yc, radius); int main() circle c1, c2; c1.set (15, 17, 8); c2.get (); c1.draw(); c2.draw()

Member data initialization during the object instantiation use a special member function called CONSTRUCTOR (ctor)

Constructor Constructor is a member function that has the same name as the class To use a constructor it has to be declared public Constructor can be used to initialize data members of a class Is called automatically when an object of the class is created (instantiated)

Constructor - example class circle private: int xc, yc, radius ; circle() xc = 25 ; yc = 25 ; radius = 5; void set (int x, int y, int r) xc = x; yc = y; radius = r; void get () cout << \Enter x coordinate: ; cin >> xc; cout << \Enter y coordinate: ; cin >> yc; cout << \Enter radius : ; cin >> radius; void draw () draw_circle(xc, yc, radius); int main() circle c1, c2; c1.set (15, 17, 8); c1.draw(); c2.draw()

Constructor Last example shows the constructor - i.e, the constructor that takes no arguments either due to All parameters have default values (showed in the example), or No parameters are required Constructor does not return a value (void) to whom should it!? int main() circle c1; // default ctor will be executed int main() circle c1(); /* declaration of function c1() that has a circle-object as return value */

Constructor If no constructor is defined, a compiler generates one: default constructor if you don t care how data are being initialized If user defines a no-argument constructor member data are initialized using the no-argument constructor

Initializer List class circle private: int xc, yc, radius ; circle() : xc(25), yc(25), radius(5) /* empty body */ void set (int x, int y, int r) xc = x; yc = y; radius = r; void get () cout << \Enter x coordinate: ; cin >> xc; cout << \Enter y coordinate: ; cin >> yc; cout << \Enter radius : ; cin >> radius; void draw () Initialization takes place before the function body Members initialized in the initializer list and initialized before the constructor is executed Why not initialize members in constructor body? draw_circle(xc, yc, radius); const member data initialization

Constructor with parameters class circle private: int xc, yc, radius ; circle( int x, int y, int r) : xc (x), yc(y), radius(r) /* empty body */ void set (int x, int y, int r) xc = x; yc = y; radius = r; void get () cout << \Enter x coordinate: ; cin >> xc; cout << \Enter y coordinate: ; cin >> yc; cout << \Enter radius : ; cin >> radius; void draw () draw_circle(xc, yc, radius); int main() circle c1(15,25,10); c1.draw();

Constructor overloading A class can have more than 1 constructor Overloaded constructors shall have different parameter lists circle(); circle (int x, int y, int r);

Constructor overloading class circle private: int xc, yc, radius ; circle() : xc(25), yc(25), radius(5) circle( int x, int y, int r) : xc (x), yc(y), radius(r) void set (int x, int y, int r) xc = x; yc = y; radius = r; void get () cout << \Enter x coordinate: ; cin >> xc; cout << \Enter y coordinate: ; cin >> yc; cout << \Enter radius : ; cin >> radius; void draw () draw_circle(xc, yc, radius); overloading Only one default constructor is allowed int main() circle c1(15,25,10); circle c2; c1.draw(); c2.draw();

Member function overloading Non-constructor member functions can also be overloaded set (int x, int y, int r) set (int x, int y) Must have unique parameter lists (as with constructors)

Member function overloading class circle private: int xc, yc, radius ; circle() : xc(25), yc(25), radius(5) void set (int x, int y, int r) xc = x; yc = y; radius = r; void set (int x, int y) xc = x; yc = y void draw () draw_circle(xc, yc, radius); overloading int main() circle c1; c1.set(15,15,10); c1.draw(); c1.set(20,20); c1.draw();

Data Initialization No-argument constructor - initialize with constant Constructor with arguments Useful feature Initialize the data with another object of the same type Default Copy Constructor need not to be created (its for free ) a constructor which takes one object as argument

Copy Constructor class circle private: int xc, yc, radius ; circle() : xc(25), yc(25), radius(5) circle( int x, int y, int r) : xc (x), yc(y), radius(r) void set (int x, int y, int r) xc = x; yc = y; radius = r; void get () cout << \Enter x coordinate: ; cin >> xc; cout << \Enter y coordinate: ; cin >> yc; cout << \Enter radius : ; cin >> radius; void draw () draw_circle(xc, yc, radius); int main() circle c1(15,25,10); circle c2(c1); circle c3 = c1; c1.draw(); c2.draw(); c3.draw();

Destructors Public member function automatically called when an object is deleted (e.g., when the program finishes) Destructor name is ~classname, e.g., ~circle It has no return type (to whom?) and takes no arguments Only 1 destructor is allowed per class it cannot be overloaded, as it does not take arguments

Destructors class circle private: int xc, yc, radius, ID *ncolour; ~circle () delete ncolour; cout << Deleting circle with ID: << ID << \n ; void set (int x, int y, int r, int circlecolour) xc = x; yc = y; radius = r; *ncolour= circlecolour circle( int x, int y, int r, int id) : xc (x), yc(y), radius(r), ID(id) ncolour = new int; void draw () draw_circle(xc, yc, radius, ncolour); int main() circle c1(15,25,10,1); circle c2(15,15,10,2); cout << -- end ---\n ; --- end --- Deleting circle with ID: 2 Deleting circle with ID: 1

Copy Constructors Take care when a class uses pointers Copy constructor initializes the pointers ending up pointing into the same memory (defined by the old class) When the old class destroys the memory by using the destructor the new class will still have a pointer to that memory. Solution: create its own copy constructor and allocate the memory as required.

Copy constructor class circle private: int xc, yc, radius, ID, *ncolour; ~circle () delete ncolour; cout << Deleting circle with ID: << ID << \n ; int GetColour() return * ncolour ; circle( int x, int y, int r, int id) : xc(x), yc(y), radius(r),id(id) ncolour = new int; void draw () draw_circle(xc, yc, radius, ncolour); circle (circle & tmpcircle ) ncolour = new int; ncolour = tmpcircle.getcolour();

Copy constructor Copy constructor required to use reference parameters, and therefore it will have access to their argument s data The purpose is to make a copy of the argument, no reason the constructor should modify the argument s data Thus, specify the keyword const in the parameter list circle (const circle & tmpcircle )

this pointer The this pointer points to the class object instance by the member function. Hidden object parameter for each member function All member data and member functions can be accessed using the this pointer

this pointer Can be used to compare a particular object with the member function of the object being executed. Can be used to access members that may be hidden by parameters with the same name.

this pointer class circle private: int xc, yc, radius ; circle() : xc(25), yc(25), radius(5) /* empty body */ void set (int xc, int yc, int radius) this->xc = xc; this->yc = yc; this->radius = radius; void get () cout << \Enter x coordinate: ; cin >> this-> xc; cout << \Enter y coordinate: ; cin >> this-> yc; cout << \Enter radius : ; cin >> radius; void draw () draw_circle(xc, yc, radius); No difference between the cin >> this-> xc; and cin >> xc;

Accessibility Private - only within the class Protected - within the class and the derived class Public open (not protected) and accessible from everywhere in the program

Inheritance class grafobject protected: int xc, yc; grafobject() xc(25), yc(25) cout << GRAFOBJECT constructor ; void set (int x, int y) xc = x; yc = y; class circle : public grafobject private: int radius ; circle () radius(5) cout << CIRCLE constructor ; class cannot invoke private members from base class derived base class class Inheritance don t work in reverse. Base class do not know about the members from the classes that are derived from the base class.

Inheritance: Constructor and Destructors class grafobject protected: int xc, yc, ncolour; grafobject():xc(25), yc(25) cout << GRAFOBJECT constructor ; ~grafobject() cout << GRAFOBJECT destructor ; void set (int x, int y) void linecolour (int ncolour) cout << Set GRAFOBJECT colour ; class circle : public grafobject private: int radius ; circle (): radius(5) cout << CIRCLE constructor ; ~circle () cout << CIRCLE destructor ; void draw() void linecolour (int ncolour) cout << Set Circle colour ; int main() circle c1 ; c1.draw(); cout << -- end ---\n ; GRAFOBJECT constructor CIRCLE constructor --- end --- CIRCLE destructor GRAFOBJECT destructor

Derived class constructor class grafobject protected: int xc, yc; grafobject() : xc(15), yc(15) grafobject(int x, int y) : xc(x), yc(y) class circle : public grafobject privat: int radius; circle() : grafobject() radius = 0; Overloaded constructors in the base class impose overloaded constructors in the derived class Calling constructor from the initialization list. circle (int x, int y, int r) : grafobject(x,y) radius = r;

Calling the base method from overridden method class grafobject protected: int xc, yc, ncolour; grafobject():xc(25), yc(25) cout << GRAFOBJECT constructor ; ~grafobject() cout << GRAFOBJECT destructor ; void set (int x, int y) void setlinecolour (int ncolour) cout << Set GRAFOBJECT colour ; class circle : public grafobject private: int radius ; circle (): radius(5) cout << CIRCLE constructor ; ~circle () cout << CIRCLE destructor ; void draw() void setlinecolour (int ncolour) cout << Set Circle colour ; Override (overwrite): crate a method in a derived class that has the same name and the same signature as the method in the base class. int main() circle c1; c1.setlinecolour(255); c1.draw(); c1.grafobject::setlinecolour(125); cout << --- end ---\n ;

Pointers to member function class Base void show() cout << Show Base class ; class Derived1 : public Base void show() cout << Show Derived 1 class ; class Derived2 : public Base void show() cout << Show Derived 2 class ; int main() Derived1 d1; Derived2 d2; Base *pobj pobj = & d1; *pobj->show(); pobj = & d2; *pobj->show(); Show Base class Show Base class Compiler does not complain because the member functions are type-compatible and selects the function that matches the type.

Virtual Functions Used to override member functions Calling a function in one class may call a function in another class Enables an execution of different functions by using the same function name - POLIMORPHYSMUS

Virtual Functions class Base virtual void show() cout << Show Base class ; class Derived1 : public Base void show() cout << Show Derived 1 class ; class Derived2 : public Base void show() cout << Show Derived 2 class ; int main() Derived1 d1; Derived2 d2; Base *pobj pobj = & d1; *pobj->show(); pobj = & d2; *pobj->show(); Show Derived 1 class Show Derived 2 class Compiler selects the function that matches the content (and not only the type)

Virtual Destructors Usually destructors are used to free memory (delete) If destructor is not declared virtual - only the memory of the base class is destroyed Base class destructors should be virtual The destructors of the derived class are called

Non-Virtual Destructors class Base virtual void show() cout << Show Base class ; ~Base () cout << Base destroyed ; class Derived1 : public Base void show() cout << Show Derived 1 class ; ~ Derived1 () cout << Derived 1 destroyed ; int main() Base *pbase = new Derived delete pbase ; return 0; Base destroyed

Virtual Destructors class Base virtual void show() cout << Show Base class ; virtual ~Base () cout << Base destroyed ; class Derived1 : public Base void show() cout << Show Derived 1 class ; ~ Derived1 () cout << Derived 1 destroyed ; int main() Base *pbase = new Derived delete pbase ; return 0; Derived 1 destroyed Base destroyed

Literature for further reading: Object-oriented programming in C++ Robert Lafore Teach yourself C++ in 21 Days Jasse Liberty C++ Master Class Andre Willms