Introduction to Effective C++ Programming Kwanghee Ko Design Laboratory Department of Ocean Engineering Massachusetts Institute of Technology Day 3.

Slides:



Advertisements
Similar presentations
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
Advertisements

Constructors and Destructors. Constructor Constructor—what’s this? Constructor—what’s this? method used for initializing objects (of certain class) method.
Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
Introduction to Programming Lecture 39. Copy Constructor.
F UNCTION O VERLOADING Chapter 5 Department of CSE, BUET 1.
Inheritance, Polymorphism, and Virtual Functions
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
Chapter 4 Inheritance Bernard Chen Spring Objective IS-A relationships and the allowable changes for derived classes The concept of polymorphism.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists.
1 Classes- Inheritance Multiple Inheritance It is possible to derive a new class from more than one base class. This is called Multiple Inheritance. Under.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Introduction to Effective C++ Programming Kwang Hee Ko December 6, 2010 Modeling and Simulation Laboratory School of Mechatronics Gwangju Institute of.
Chapter 11: Inheritance and Composition. Objectives In this chapter, you will: – Learn about inheritance – Learn about derived and base classes – Redefine.
CSE 332: C++ templates This Week C++ Templates –Another form of polymorphism (interface based) –Let you plug different types into reusable code Assigned.
Inheritance Joe Meehean. Object Oriented Programming Objects state (data) behavior (methods) identity (allocation of memory) Class objects definition.
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
CS212: Object Oriented Analysis and Design Lecture 12: Operator Overloading-II.
Overloading Binary Operators Two ways to overload –As a member function of a class –As a friend function As member functions –General syntax Data Structures.
CS212: Object Oriented Analysis and Design Lecture 7: Arrays, Pointers and Dynamic Memory Allocation.
1 Inheritance. 2 Why use inheritance?  The most important aspect of inheritance is that it expresses a relationship between the new class and the base.
Data Structures Using C++ 2E Chapter 3 Pointers. Data Structures Using C++ 2E2 Objectives Learn about the pointer data type and pointer variables Explore.
1 Recall Definition of Stack l Logical (or ADT) level: A stack is an ordered group of homogeneous items (elements), in which the removal and addition of.
Object Oriented Programming Elhanan Borenstein Lecture #3 copyrights © Elhanan Borenstein.
C/C++ 3 Yeting Ge. Static variables Static variables is stored in the static storage. Static variable will be initialized once. 29.cpp 21.cpp.
More C++ Features True object initialisation
OOP in C++ CS 124. Program Structure C++ Program: collection of files Source (.cpp) files be compiled separately to be linked into an executable Files.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
Effective C++, 2nd Ed. By Scott Myers. Constructors, Destructors, and Assignment Operators 11.Define a copy constructor and an assignment operator for.
What happens... l When a function is called that uses pass by value for a class object like our dynamically linked stack? StackType MakeEmpty Pop Push.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14: More About Classes.
Introduction to Effective C++ Programming Kwanghee Ko Design Laboratory Department of Ocean Engineering Massachusetts Institute of Technology Day 2.
Chapter 11: Inheritance and Composition. Introduction Two common ways to relate two classes in a meaningful way are: – Inheritance (“is-a” relationship)
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
Class Inheritance Inheritance as an is-a relationship Public derive one class from another Protected access Initializer lists in constructor Upcasting.
Pointers in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Learners Support Publications Constructors and Destructors.
CSC 143 O 1 CSC 143 Inheritance and Object Oriented Design.
Abstract classes only used as base class from which other classes can be inherit cannot be used to instantiate any objects are incomplete Classes that.
Constructors and Destructors
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Learning Objectives Pointers as dada members
The C++ programming language
7. Inheritance and Polymorphism
Programming with ANSI C ++
Class A { public : Int x; A()
Inheritance, Polymorphism, and Virtual Functions
Inheritance: hiding methods
C++, OBJECT ORIENTED PROGRAMMING
Class: Special Topics Copy Constructors Static members Friends this
Memberwise Assignment / Initialization
CSC 253 Lecture 8.
CSC 253 Lecture 8.
Inheritance, Polymorphism, and Virtual Functions
Constructors and destructors
Constructors and Destructors
Copy Assignment CSCE 121 J. Michael Moore.
Indirection.
CISC/CMPE320 - Prof. McLeod
The C++ programming language
COP 3330 Object-oriented Programming in C++
A Deeper Look at Classes
Class: Special Topics 2 For classes using memory allocation
ENERGY 211 / CME 211 Lecture 30 December 5, 2008.
Copy Assignment CSCE 121.
C++ support for Object-Oriented Programming
SPL – PS3 C++ Classes.
Presentation transcript:

Introduction to Effective C++ Programming Kwanghee Ko Design Laboratory Department of Ocean Engineering Massachusetts Institute of Technology Day 3

General Guidelines Avoid returning “handles” to internal data. Avoid member functions that return non- const pointers or references to members less accessible than themselves. Never return a reference to a local object or to a dereferenced pointer initialized by new with in the function (memory leakage). Postpone variable definitions as long as possible.

General Guidelines Public inheritance models : “isa” relation? – Ex. A bus is a vehicle? Never redefine an inherited non-virtual function. Never redefine an inherited default parameter value. Avoid cast down the inheritance hierarchy. Model “has-a” or “is-implemented-in- terms-of” through layering.

General Guidelines Use multiple inheritance judiciously.

Ambiguities under Multiple Inheritance Case 1 Base 1 f(); g(); Base 2 f(); h(); Derived j(); k(); Derived d; d.g(); // OK d.h(); // OK d.f(); // Ambiguous!!! d.Base1::f(); // OK d.Base2::f(); // OK

Ambiguities under Multiple Inheritance Case 2 Left int y; Right int z; Bottom int a Top int x;

Ambiguities under Multiple Inheritance Case 2 Left int y; Right int z; Bottom int a Default inheritance mechanism -> maintains separate copies of the data members inherited from all base classes. Top int x;

Ambiguities under Multiple Inheritance Case 2 (Non-virtual Base class) Left int y; Right int z; Bottom int a Top int x; Top int x; Top int x; Top int x; Left int y; Right int z; Bottom int a

Ambiguities under Multiple Inheritance Case 2 : Virtual Base Class Left int y; Right int z; Bottom int a Top int x; virtual class Left::public virtual Top{…} class Right::public virtual Top{…} Top int x; Left int y; Right int z; Bottom int a

Ambiguities under Multiple Inheritance Case 2 : Virtual Base Class Left int y; Right int z; Bottom int a Top int x; virtual Inherently ambiguous!!! Ex) Bottom b; b.x -> b.Left::x? b.Right::x? b.Top::x?

Ambiguities under Multiple Inheritance Case 2 : Virtual Base Class Left int y; Right int z; Bottom int a Top int x; virtual Assignment for Top::x happens twice. - Bottom->Left->Top - Bottom->Right->Top

Ambiguities under Multiple Inheritance Case 2 : Virtual Base Class Left int y; Right int z; Bottom int a Top int x; virtual Assignment for Top::x happens twice. - Bottom->Left->Top - Bottom->Right->Top Solution???

General Guidelines Use multiple inheritance judiciously. Before using virtual base classes, understand them thoroughly. – Use an experimental program to understand its behavior. If a public base class does not have a virtual destructor, no derived class should have a destructor. If a multiple inheritance hierarchy has any destructors, every base class should have a virtual destructor.

General Guidelines Declare a copy constructor and an assignment operator for classes with dynamically allocated memory. Prefer initialization to assignment in constructors. List members in an initialization list in the order in which they are declared. Make sure base classes have virtual destructors. Have operator= return a reference to *this. Assign to all data members in operator=. Check for assignment to self in operator=. Overloading vs. default argument.