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

Slides:



Advertisements
Similar presentations
Outline 1 Introduction 2 Base Classes and Derived Classes 3 protected Members 4 Relationship between Base Classes and Derived Classes 5 Case Study: Three-Level.
Advertisements

Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
Derived Classes. C++ 2 Outline  Definition  Virtual functions  Virtual base classes  Abstract classes. Pure virtual functions.
Esempio Polimorfismo1 // Definition of abstract base class Shape #ifndef SHAPE_H #define SHAPE_H class Shape { public: virtual double area() const { return.
What is a copy constructor? A copy constructor is a special constructor for a class/struct that is used to make a copy of an existing instance during initialization.
CSE 1302 Lecture 8 Inheritance Richard Gesick Figures from Deitel, “Visual C#”, Pearson.
Inheritance Polymorphism Briana B. Morrison CSE 1302C Spring 2010.
Reusing Code Private or Protected inheritance. A cool class for array valarray class deals with numeric values, and it supports operation such as summing.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
CSE 332: C++ Classes From Procedural to Object-oriented Programming Procedural programming –Functions have been the main focus so far Function parameters.
CSE 332: C++ Overloading Overview of C++ Overloading Overloading occurs when the same operator or function name is used with different signatures Both.
1 Chapter 14-2 Object- Oriented Software Development Dale/Weems.
Introduction to Effective C++ Programming Kwanghee Ko Design Laboratory Department of Ocean Engineering Massachusetts Institute of Technology Day 3.
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 © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
CS (CCN 27241) Software Engineering for Scientific Computing Lecture 5: C++ Key Concepts.
BASE CLASSES AND INHERITANCE CHAPTER 4. Engineer Class.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
CSE 332: C++ templates This Week C++ Templates –Another form of polymorphism (interface based) –Let you plug different types into reusable code Assigned.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
Algorithm Programming Bar-Ilan University תשס"ח by Moshe Fresko.
Programming Pillars Introduction to Object- Oriented Programming.
Tuc Goodwin  Object and Component-Oriented Programming  Classes in C#  Scope and Accessibility  Methods and Properties  Nested.
1 Chapter 14 Object-Oriented Software Development Dale/Weems.
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.
Chapter 9 Questions 1. What are the difference between constructors and member functions? 2. Design and implement a simple class as you want, with constructors.
Module 10: Inheritance in C#. Overview Deriving Classes Implementing Methods Using Sealed Classes Using Interfaces Using Abstract Classes.
Methodology First and Language Second -A Way to Teach Object-Oriented Programming Haibin Zhu, PhD Department of Computer Science and Mathematics Nipissing.
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.
Department of Computer Science Data Structures Using C++ 2E Chapter 2 Object-Oriented Design (OOD) and C++  Learn about inheritance  Learn about derived.
CSC 143F 1 CSC 143 Constructors Revisited. CSC 143F 2 Constructors In C++, the constructor is a special function automatically called when a class instance.
Effective C++, 2nd Ed. By Scott Myers. Constructors, Destructors, and Assignment Operators 11.Define a copy constructor and an assignment operator for.
C++ Inheritance Data Structures & OO Development I 1 Computer Science Dept Va Tech June 2007 © McQuain Generalization versus Abstraction Abstraction:simplify.
Csi2172 class 5 Midterm: June 12. constructor Special method used to create objects of the class Never has a return type. Is called automatically upon.
Coming up: Inheritance
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
Constructor It is a special member of a class that has the following characteristic 1)It has the same name as of its class. 2)It don’t have an explicit.
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.
Learners Support Publications Constructors and Destructors.
©Copyrights 2016 Eom, Hyeonsang All Rights Reserved Computer Programming Object Oriented Programming & C++ 1 st Lecture 엄현상 (Eom, Hyeonsang) Department.
CS 342: C++ Overloading Copyright © 2004 Dept. of Computer Science and Engineering, Washington University Overview of C++ Overloading Overloading occurs.
 Virtual Function Concepts: Abstract Classes & Pure Virtual Functions, Virtual Base classes, Friend functions, Static Functions, Assignment & copy initialization,
 2006 Pearson Education, Inc. All rights reserved Templates.
CSE 332: C++ Overloading Overview of C++ Overloading Overloading occurs when the same operator or function name is used with different signatures Both.
CSC 143 O 1 CSC 143 Inheritance and Object Oriented Design.
INTRODUCTION TO CLASSES & OBJECTS CREATING CLASSES USING C#
Object-Oriented Programming Review 1. Object-Oriented Programming Object-Oriented Programming languages vary but generally all support the following features:
CSE 332: C++ Exceptions Motivation for C++ Exceptions Void Number:: operator/= (const double denom) { if (denom == 0.0) { // what to do here? } m_value.
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
Passing from design to implementation
Programming with ANSI C ++
Class A { public : Int x; A()
Inheritance, Polymorphism, and Virtual Functions
More Design Patterns 1.
More Design Patterns 1.
Contents Introduction to Constructor Characteristics of Constructor
Inheritance, Polymorphism, and Virtual Functions
Overview of C++ Overloading
Constructors and destructors
Constructors and Destructors
Anatomy of Inheritance
Function Overloading.
CS 144 Advanced C++ Programming February 21 Class Meeting
Anatomy of Inheritance
Jim Fawcett CSE687 – Object Oriented Design Spring 2014
Presentation transcript:

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

Abstraction : Design of a Class Show how abstraction is made in a class design with an example. Example – Determine the price of various configurations of a computer Expansions : CD-ROM drive, Floppy drive, Network card Monitor : CRT, LCD

Abstraction : Design of a Class First Design class Card { public: virtual int price() = 0; virtual char *name() = 0; virtual int rebate(); }; class Network : public Card { public: int price(); char *name(); }; class CDRom : public Card { public: int price(); char *name(); int rebate(); }; class Floppy : public Card { public: int price(); char *name(); };

Abstraction : Design of a Class First Design class Monitor { public: virtual int price() = 0; virtual char *name() = 0; }; class CRT : public Monitor { public: int price(); char *name(); }; class LCD : public Monitor { public: int price(); char *name(); };

Abstraction : Design of a Class Class inheritance hierarchies Card NetworkCDRomFloppy Monitor CRTLCD

Abstraction : Design of a Class Find common abstractions and concentrate them in a base class. class Card { public: virtual int price() = 0; virtual char *name() = 0; virtual int rebate(); }; class Network : public Card { public: int price(); char *name(); }; class CDRom : public Card { public: int price(); char *name(); int rebate(); }; class Floppy : public Card { public: int price(); char *name(); };

Abstraction : Design of a Class class Monitor { public: virtual int price() = 0; virtual char *name() = 0; }; class CRT : public Monitor { public: int price(); char *name(); }; class LCD : public Monitor { public: int price(); char *name(); }; Find common abstractions and concentrate them in a base class.

Abstraction : Design of a Class class Card { public: virtual int price() = 0; virtual char *name() = 0; virtual int rebate(); }; class Monitor { public: virtual int price() = 0; virtual char *name() = 0; };

Abstraction : Design of a Class class Card { public: virtual int price() = 0; virtual char *name() = 0; virtual int rebate(); }; class Monitor { public: virtual int price() = 0; virtual char *name() = 0; }; class Component { public: virtual int price() = 0; virtual char *name() = 0; virtual int rebate(); int netPrice(); }; int Component::rebate() { return 0; }

Abstraction : Design of a Class Class inheritance hierarchies Card NetworkCDRomFloppy Monitor CRTLCD Component

Abstraction : Design of a Class What are the differences between classes? (only within this design) Card NetworkCDRomFloppy Monitor CRTLCD Component

Abstraction : Design of a Class What are the differences between classes? (only within this design) Card NetworkCDRomFloppy Monitor CRTLCD ComponentPrice Name Amount of rebate

Abstraction : Design of a Class What are the differences between classes? (only within this design) Card NetworkCDRomFloppy Monitor CRTLCD ComponentPrice Name Amount of rebate Variation in Value? Variation in Behavior?

Abstraction : Design of a Class What are the differences between classes? (only within this design) Card NetworkCDRomFloppy Monitor CRTLCD ComponentVariation in Value Use data members Variation in behavior Use virtual functions

Abstraction : Design of a Class class Component { int price_; char *name_; int rebate_; public: Component(int p,char *n, int r=0); int netPrice() int price() {return price_;} char *name() {return name_;} int rebate() {return rebate_;} }; Careful observation reveals that each component can be represented by different values of data members (price, name, rebate).

Abstraction : Design of a Class class Component { int price_; char *name_; int rebate_; public: Component(int p,char *n, int r=0); int netPrice() int price() {return price_;} char *name() {return name_;} int rebate() {return rebate_;} }; Careful observation reveals that each component can be represented by different values of data members (price, name, rebate).

Abstraction : Design of a Class Treatment of Nonzero Rebates – We need to reintroduce the classes Card and Monitor to provide constructors with appropriate rebate defaults other than zero. Card Monitor Component

Abstraction : Design of a Class class Card : public Component { public: Card(int p, char *n, int r=45):Component(p,n,r) {} }; A public derived class should be a specialization of its base class class Monitor : public Component { public: Monitor(int p, char *n, int r=0):Component(p,n,r) {} }; Different default rebates

General Guidelines Design class interfaces that are complete and minimal. Decide smartly between member, non- member and friend functions. Avoid data members in the public interface. Use ‘const’ as much as you can. Prefer pass-by-reference to pass-by-value

General Guidelines Don’t try to return a reference when you must return an object. Function overloading vs. parameter defaulting. Explicitly disallow use of implicitly generated member functions you don’t want. Use namespaces.