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

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

G52CPP C++ Programming Lecture 13

6.096 Introduction to C++ January (IAP) 2009

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

Interview Questions of C++

Constructors and Destructors. 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.

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

CSCE 110 PROGRAMMING FUNDAMENTALS

Constructors for classes

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

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

Ch. 12: Operator Overloading

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

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

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

Short Notes of CS201

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

CS201 - Introduction to Programming Glossary By

Object Oriented Design

Midterm Review. PIC 10B Spring 2018

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

Chapter 11. Abstract Data Types and Encapsulation Concepts

Multiple and Virtual Inheritance in C++

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

Instantiation of Template class

CS201 Some Important Definitions

CS32 - Week 4. Umut Oztok. Jul 15, Umut Oztok CS32 - Week 4

C++ Important Questions with Answers

Advanced Systems Programming

Inheritance, and Polymorphism.

G52CPP C++ Programming Lecture 14. Dr Jason Atkin

COMP6771 Advanced C++ Programming

Evolution of Programming Languages

COMP 2355 Introduction to Systems Programming

Constructor - example

CSE 303: Concepts and Tools for Software Development

Vector and Free Store (Vectors and Arrays)

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

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

CS105 C++ Lecture 7. More on Classes, Inheritance

CS250 Final Review Questions

Where do we go from here?

The issues. Programming in C++ Common storage modes. Static storage in C++ Session 8 Memory Management

G52CPP C++ Programming Lecture 16

Fast Introduction to Object Oriented Programming and C++

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

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

INHERITANCE: EXTENDING CLASSES

Declaration Syntax. Declarations. Declarators. Declaration Specifiers. Declaration Examples. Declaration Examples. Declarators include:

Introduction to Classes

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

Object-Oriented Programming, Iouliia Skliarova

Lecture 13: more class, C++ memory management

POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors

POLYMORPHISM 2 PART. Shared Interface. Discussions. Abstract Base Classes. Abstract Base Classes and Pure Virtual Methods EXAMPLE

CS250 Final Review Questions

Polymorphism Part 1 1

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

CGS 2405 Advanced Programming with C++ Course Justification

AN OVERVIEW OF C++ 1


Classes - 2. Data Processing Course, I. Hrivnacova, IPN Orsay

QUIZ How do we implement run-time constants and. compile-time constants inside classes?

엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University COPYRIGHTS 2015 EOM, HYEONSANG ALL RIGHTS RESERVED

Introduction Of Classes ( OOPS )

cout << "How many numbers would you like to type? "; cin >> memsize; p = new int[memsize];

COEN244: Class & function templates

Constants, References

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

2 ADT Programming User-defined abstract data types

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

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

CIS 190: C/C++ Programming. Classes in C++

Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat

CSE 374 Programming Concepts & Tools. Hal Perkins Fall 2015 Lecture 19 Introduction to C++

Increases Program Structure which results in greater reliability. Polymorphism

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?

Part VII. Object-Oriented Programming. Philip Blakely (LSC) C++ Introduction 194 / 370

INHERITANCE: CONSTRUCTORS,

An Introduction to C++

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

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

Object Oriented Programming. Solved MCQs - Part 2

Object-Oriented Programming. Lecture 4 Dr Piotr Cybula

Inheritance. A mechanism for specialization A mechanism for reuse. Fundamental to supporting polymorphism

Object Oriented Design

G52CPP C++ Programming Lecture 10. Dr Jason Atkin

STRUCTURING OF PROGRAM

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved. 1

C++ For Science and Engineering Lecture 27

Introduction to C++ Part II. Søren Debois. Department of Theoretical Computer Science IT University of Copenhagen. September 12th, 2005

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

Programming C++ Lecture 5. Howest, Fall 2013 Instructor: Dr. Jennifer B. Sartor

Special Member Functions

Object-Oriented Principles and Practice / C++

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

CPSC 427: Object-Oriented Programming

Function Overloading

Transcription:

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

What s s Not Inherited? The following methods are not inherited: Constructors Destructor Assignment operator We will see that Definition of these methods is in a sense recursive Overriding these methods is always a refinement

Reminder: The Default Constructor Constructor with no arguments Called automatically when an object is created without an explicit constructor call Recursion base: Empty for: Basic types: int, char, double,... Pointers to: basic types, classes, structures Generated automatically by the compiler if no constructor is defined by programmer Recursive step (compiler generated default constructor): Call default constructors of all base class subobjects Call default constructors of all data member subobjects Use the order they were declared in.

Constructors and Inheritance Constructor methods are not inherited Initialization List: The stuff between the constructor header and body Preceded with a colon : class Stack { int *buff, *sp; public: Stack(int n) : buff(new int[n]), sp(buff + n) { //... Defining constructors in a derived class: Strive to make constructor body empty Use initialization list Base class subobjects can only be initialized in initialization list If not initialized there, then default constructor is called const data members can only be initialized in initialization list Order of application: Same as that of default constructor

Construction Order Whenever a constructor is invoked: All base class subobjects must be constructed If in initialization list: invoke appropriate constructor Otherwise, apply default constructor All data member subobjects must be constructed If in initialization list: invoke appropriate constructor Otherwise, apply default constructor Body of user defined constructor is called Fixed construction order: Order of items in initialization list is irrelevant Construction order: Base class subobjects (in the order of their definition) Data member subobjects (in the order of their definition) Body of constructor: It is empty for compiler-generated constructors

Quiz: Order of Construction struct A { A(void) { cout << A ; struct B { B(void) { cout << B ; struct C { C(void) { cout << C ; What will be printed? struct X: public A { B b; X(void): b() { cout << X ; struct Y: public X { C c; Y(void): X() { cout << Y ; struct Z: public Y { X x; B b; Z(void): b(), x(), Y() { cout << Z ; z; Solution

Reminder: Destructor Called automatically when object is destroyed Doesn t have any arguments Recursion base: Empty for: Basic types: int, char, double,... Pointers to: basic types, classes, structures Generated automatically by the compiler if not defined by the programmer Recursive step: Call body of destructor: user defined/compiler generated Call destructor of all data member subobjects Reverse order of declaration Call destructor of all base class subobjects Order: reverse order of construction

Quiz: Order of Destruction struct A { ~A(void) { cout << A ; struct B { ~B(void) { cout << B ; struct C { ~C(void) { cout << C ; What will be printed? struct X: public A { B b; ~X(void) { cout << X ; struct Y: public X { C c; ~Y(void) { cout << Y ; struct Z: public Y { X x; B b; ~Z(void){ cout << Z ; z;

The Copy Constructor Reminder: Constructor of form Class::Class(const Class &X) Called automatically in: Initialization Call by value Function returning value Recursion base: Member-wise copy for basic types Generated automatically by the compiler if no copy constructor is defined by programmer Recursive step (for compiler generated copy constructors): Call copy constructor of all base class subobjects Call copy constructor of all data members Use the order they were defined in. Q: What s the recursion of user defined copy constructors?

The Assignment Operator Reminder: Must be defined as member function: class X { public: void operator = (const X& right); Recursion base: Empty for basic types and pointers. Generated automatically by the compiler if no assignment operator defined by programmer Recursive step (compiler defined assignment operators): Call assignment operators of all base class subobjects Call assignment operators of all data member subobjects Use the order they were declared in. Call body of assignment operator: user defined/compiler generated Q: What s the recursion of user defined assignment operators?