INHERITANCE: CONSTRUCTORS,

Similar documents
C++ STREAMS; INHERITANCE AS

Object Oriented Design

Introduction to Classes

Computer Programming Inheritance 10 th Lecture

6.096 Introduction to C++ January (IAP) 2009

PASCAL - OBJECT ORIENTED

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

1. the base class s (default) constructor is executed first, 2. followed by the derived class s constructor

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

C++ (Non for C Programmer) (BT307) 40 Hours

Data Structures and Other Objects Using C++

Lecture 18 Tao Wang 1

And Even More and More C++ Fundamentals of Computer Science

QUIZ. How could we disable the automatic creation of copyconstructors

CSCI 123 Introduction to Programming Concepts in C++

Object-Oriented Design (OOD) and C++

Chapter 9 Classes : A Deeper Look, Part 1

G52CPP C++ Programming Lecture 9

Short Notes of CS201

CS201 - Introduction to Programming Glossary By

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

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

Lecture #1. Introduction to Classes and Objects

QUIZ. How could we disable the automatic creation of copyconstructors

CPSC 427: Object-Oriented Programming

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

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

Abstract Class. Lecture 21. Based on Slides of Dr. Norazah Yusof

Lecture 5. Function Pointers

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

Make Classes Useful Again

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

PIC 10A. Lecture 17: Classes III, overloading

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

Inheritance, and Polymorphism.

Chapter 10 Introduction to Classes

Intermediate Programming, Spring 2017*

What is Polymorphism? Quotes from Deitel & Deitel s. Why polymorphism? How? How? Polymorphism Part 1

Extending Classes (contd.) (Chapter 15) Questions:

Abstraction in Software Development

The mechanism that allows us to extend the definition of a class without making any physical changes to the existing class is called inheritance.

Object Oriented Design

Polymorphism Part 1 1

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

Chapter 6: Inheritance

Lecture 13: more class, C++ memory management

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

Praktikum: Entwicklung interaktiver eingebetteter Systeme

Abstract Data Types. Lecture 23 Section 7.1. Robb T. Koether. Hampden-Sydney College. Wed, Oct 24, 2012

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

class Polynomial { public: Polynomial(const string& N = "no name", const vector<int>& C = vector<int>());... };

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

CS24 Week 3 Lecture 1

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

Slide Set 14. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

CMSC202 Computer Science II for Majors

More C++ : Vectors, Classes, Inheritance, Templates. with content from cplusplus.com, codeguru.com

Fundamental Concepts and Definitions

Preface... (vii) CHAPTER 1 INTRODUCTION TO COMPUTERS

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

ITI Introduction to Computing II

Inheritance and Polymorphism

ITI Introduction to Computing II

the gamedesigninitiative at cornell university Lecture 7 C++ Overview

Chapter 14. Inheritance. Slide 1

1.1 The hand written header file

Lecture 10: building large projects, beginning C++, C++ and structs

CS201 Some Important Definitions

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

Macros and Preprocessor. CGS 3460, Lecture 39 Apr 17, 2006 Hen-I Yang

Common Misunderstandings from Exam 1 Material

Constructors for classes

G52CPP C++ Programming Lecture 13

Learning Objectives. C++ For Artists 2003 Rick Miller All Rights Reserved xli

Welcome to Teach Yourself Acknowledgments Fundamental C++ Programming p. 2 An Introduction to C++ p. 4 A Brief History of C++ p.

CSCE 110 PROGRAMMING FUNDAMENTALS

2.1 Introduction UML Preliminaries Class diagrams Modelling delegation... 4

Intermediate Programming, Spring 2017*

XII- COMPUTER SCIENCE VOL-II MODEL TEST I

KLiC C++ Programming. (KLiC Certificate in C++ Programming)

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

Lab 2: ADT Design & Implementation

Chapter 1: Object-Oriented Programming Using C++

Function Overloading

Software Design and Analysis for Engineers

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

Ministry of Higher Education Colleges of Applied Sciences Final Exam Academic Year 2009/2010. Student s Name

6.096 Introduction to C++

CS304 Object Oriented Programming Final Term

UNIVERSITY OF SWAZILAND

#include <iostream> #include <cstdlib>

Introduction to Programming session 24

Chapter 19 C++ Inheritance

Lecture 7. Log into Linux New documents posted to course webpage

Operator overloading

CPSC 427: Object-Oriented Programming

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

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

CSE 303: Concepts and Tools for Software Development

Programming, numerics and optimization

Transcription:

INHERITANCE: CONSTRUCTORS, DESTRUCTORS, HEADER FILES Pages 720 to 731 Anna Rakitianskaia, University of Pretoria

CONSTRUCTORS Constructors are used to create objects Object creation = initialising member variables persontype::persontype(string first, string last) firstname = first; lastname = last; } Both derived and base (child and parent) classes need constructors

CONSTRUCTORS OF DERIVED AND BASE CLASSES Child class inherits member variables of the parent class Problem: private variables of the parent class are not directly accessible from the child class Inherited private variables still have to be initialised!

CONSTRUCTORS OF DERIVED AND BASE CLASSES Can we use a constructor to initialize all member variables, including the private inherited ones? Yes! However, parent constructors are not inherited We can write child class constructor such that it executes the constructor of the parent class

CONSTRUCTORS OF DERIVED AND BASE CLASSES We can write child class constructor such that it also executes the constructor of the parent class Parent constructor: rectangetype::rectangetype(double l, double w) setdimension(l, w); } Child constructor: boxtype::boxtype(double l, double w, double h) : rectangletype(l, w) height = h; }

CONSTRUCTORS OF DERIVED AND BASE CLASSES Line 1: executes the parent class constructor Line 2: executes the parent class constructor with values 6.0, 5.0, then executes the child class constructor In order: parent constructors precede child constructors

CONSTRUCTOR WITH DEFAULT PARAMETERS AND INHERITANCE HIERARCHY Child classes can also have default constructors, and constructors with default parameters Constructors of child classes can pass default parameters to the parent class constructors Declare constructor with default parameters: boxtype(double l = 0, double w = 0, double h = 0); Implement the constructor: boxtype::boxtype(double l, double w, double h) : rectangletype(l, w) height = h; }

DESTRUCTORS IN A DERIVED CLASS Destructors deallocate dynamic memory (i.e. pointers) boxtype::~boxtype() // deallocate memory! } Both child and parent classes may have destructors When a child class object goes out of scope (out of } where it was defined), it automatically invokes a destructor When the destructor of the child class executes, it automatically invokes the destructor of the parent class No explicit reference from child destructor to parent destructor is necessary Reverse order: child destructors precede parent destructors

HEADER FILE OF A DERIVED CLASS When we define a new class, we create a new header file (.h) To create child classes that inherit from previously defined parent classes, header files of the new classes should contain commands (#include) that specify where to look for the definitions of the base classes:

MULTIPLE INCLUSIONS OF A HEADER FILE The preprocessor command (#include) is used to include a header file in a program The preprocessor processes the program before it is compiled

MULTIPLE INCLUSIONS OF A HEADER FILE Consider the following code: Both testa.h and headertest.cpp include test.h thus, test.h is included twice This will result in compile errors due to multiple definitions of ONE and TWO Solution: use identifiers in the headers (next slide)

MULTIPLE INCLUSIONS OF A HEADER FILE #ifndef H_test means if not defined H_test #define H_test means define H_test #endif means end if Now, ONE and TWO are declared with a unique preprocessor identifier H_test

PUTTING EVERYTHING TOGETHER Box inheriting from Rectangle: a complete (constructor) example boxtype -height: double +boxtype(double l, double w, double h) +getheight() const: double +area() const: double +volume() const: double +setdimension(double l, double w, double h): void +print() const: void rectangletype -length: double -width: double +rectangletype(double l, double w) +getlength() const: double +getwidth() const: double +area() const: double +perimeter() const: double +setdimension(double l, double w): void +print() const: void

CONSTRUCTORS: RECTANGLETYPE HEADER #ifndef RECTANGLETYPE_H #define RECTANGLETYPE_H class rectangletype private: double length; double width; public: rectangletype(double l = 0, double w = 0); double getlength() const; double getwidth() const; double area() const; double perimeter() const; void setdimension(double l, double w); void print() const; }; #endif

RECTANGLETYPE IMPLEMENTATION #include rectangletype.h rectangletype::rectangletype(double l, double w) setdimension(l, w); } void rectangletype::setdimension(double l, double w) if(l >= 0) length = l; else length = 0; } if(w >= 0) width = w; else width = 0; // etc

BOXTYPE HEADER #ifndef BOXTYPE_H #define BOXTYPE_H #include rectangletype.h class boxtype : public rectangletype private: double height; public: boxtype(double l = 0, double w = 0, double h = 0); double getheight() const; double area() const; double volume() const; void setdimension(double l, double w, double h); void print() const; }; #endif

BOXTYPE IMPLEMENTATION #include boxtype.h boxtype::boxtype(double l, double w, double h) : rectangletype(l, w) height = h; } void boxtype::setdimension(double l, double w, double h) rectangletype::setdimension(l, w); } if(h >= 0) height = h; else height = 0; // etc

QUESTION TIME Next lecture: C++ stream classes Inheritance as public, protected or private Composition Stay tuned!