CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.

Slides:



Advertisements
Similar presentations
Object Oriented Programming with Java
Advertisements

CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
. Virtual Classes & Polymorphism. Example (revisited) u We want to implement a graphics system u We plan to have lists of shape. Each shape should be.
Polymorphism From now on we will use g++!. Example (revisited) Goal: Graphics package Handle drawing of different shapes Maintain list of shapes.
1 Inheritance Concepts n Derive a new class (subclass) from an existing class (base class or superclass). n Inheritance creates a hierarchy of related.
Inheritance Chapter 8.
DERIVED CLASSES AND INHERITANCE Moshe Fresko Bar-Ilan University Object Oriented Programing
Virtual Functions Junaed Sattar November 10, 2008 Lecture 10.
Abstraction: Polymorphism, pt. 1 Abstracting Objects.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Abstract Classes 1. Objectives You will be able to: Say what an abstract class is. Define and use abstract classes. 2.
1 Data Structures - CSCI 102 CS102 C++ Polymorphism Prof Tejada.
Pointer Data Type and Pointer Variables
Inheritance in C++ CS-1030 Dr. Mark L. Hornick.
CSE 425: Object-Oriented Programming II Implementation of OO Languages Efficient use of instructions and program storage –E.g., a C++ object is stored.
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.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Polymorphism &Virtual Functions 1. Polymorphism in C++ 2 types ▫Compile time polymorphism  Uses static or early binding  Example: Function and operator.
Overview of Previous Lesson(s) Over View  OOP  A class is a data type that you define to suit customized application requirements.  A class can be.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CS 106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CS 106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CS 11 C++ track: lecture 4 Today: More on memory management the stack and the heap inline functions structs vs. classes.
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
Programming With Java ICS201 University Of Hail1 Chapter 13 Interfaces.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
 Definition: Accessing child class methods through a parent object  Example: Child class overrides default parent class methods  Example: Child class.
CS 106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CS 106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
Programming Abstractions Cynthia Lee CS106X. Topics:  Binary Search Tree (BST) › Starting with a dream: binary search in a linked list? › How our dream.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Java Polymorphism. What we have learned Polymorphism is one of the three very powerful and useful mechanisms offered by OOP. What are the other two?
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.
Programming Abstractions Cynthia Lee CS106X. Inheritance Topics Inheritance  The basics › Example: Stanford GObject class  Polymorphism › Example: Expression.
© 2007 Lawrenceville Press Slide 1 Chapter 9 Inheritance  One class is an extension of another.  Allows a class to define a specialized type of an existing.
Overview of C++ Polymorphism
1 / 71 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 4 Programming Fundamentals using Java 1.
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
CSC241 Object-Oriented Programming (OOP) Lecture No. 17.
1 C++ Classes & Object Oriented Programming Overview & Terminology.
CSC 143 O 1 CSC 143 Inheritance and Object Oriented Design.
Programming Abstractions Cynthia Lee CS106B. Inheritance Topics Inheritance  The basics › Example: Stanford GObject class  Polymorphism.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
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.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
1 More About Derived Classes and Inheritance Chapter 9.
CMSC 202 Polymorphism.
Programming Abstractions
Advanced Program Design with C++
Expression Trees Eric Roberts CS 106B March 4, 2013.
Methods Attributes Method Modifiers ‘static’
Polymorphism.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Programming II Polymorphism A.AlOsaimi.
Overview of C++ Polymorphism
Chapter 14 Abstract Classes and Interfaces
Lecture 6: Polymorphism
Static Binding Static binding chooses the function in the class of the base class pointer, ignoring any versions in the class of the object actually.
Presentation transcript:

CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons Attribution-NonCommercial- ShareAlike 4.0 International License. Permissions beyond the scope of this license may be available at Bailey LeeCreative Commons Attribution-NonCommercial- ShareAlike 4.0 International Licensehttp://peerinstruction4cs.org

Today’s Topics  Basic inheritance in C++  Some help with Stanford Expression trees  Inheritance and the “virtual” keyword  Polymorphism 2

Inheritance in C++ The basics

class Circle { public: double myArea() { return * radius * radius; } void setLoc(Loc center) { centerx = center.x; centery = center.y; } Loc getLoc() { return Loc(centerx, centery); } private: double halfMoonArea() { return myArea / 2; } int radius, centerx, centery; }; class FilledCircle : public Circle { public: void setColor(std::string color) { this->color = color; } std::string getColor() { return color; } private: std::string color; }; If we add a new public method “ void doStuff() ” to FilledCircle, which of the following lines of code would not be legal to include in doStuff() ? A. int topedge = centery – radius; B. size_t colorstrlen = color.length(); C. double quarterarea = this->halfMoonArea()/4 + this->myArea()/8; D. Other/none/more than one

Stanford Walkthrough The Expression class

Walkthrough of Stanford  Excel-like spreadsheet  Among other things, it needs to parse expressions  We did something similar to this earlier in the quarter (see next slide)

We can play the same game with non-alpha characters as keys: What does this print? void traverse(Node *node) { if (node != NULL) { traverse(node->left); traverse(node->right); cout key << " "; } A. * + / B. * / 2 C * 8 / 2 D / * E. Other/none/more * +/ 3428

Evaluation of CompoundExp  This is in the implementation of CompoundExp —let’s take a look at the.h file to see what op, lhs, and rhs are

C++ and the “virtual” keyword

Expression (base class)  Note: you cannot actually create an Expression object  These methods are never implemented (note the “ = 0 ”)  Expression exists solely to provide a base class to others  “pure virtual”

Another Derived class: DoubleExp

Another Derived class: IdentifierExp

Inheritance and the “is a” relationship  To write “class B is derived from class A”:  class B : public class A { … }  This means that:  An object of type B “is a” A  Any public methods of class A are inherited by class B, and become public methods of class B  Any private methods and data of class A are not accessible by class B  B may add some of its own public/private methods and data in the {} braces

class Square : public ________________ { public: virtual double myArea() { return side*side; } private: int length; }; class Rectangle : public ________________ { public: virtual double myArea() { return length*width; } private: int length, width; }; class Shape { public: virtual double myArea() = 0; }; How should we fill in the blanks so that the class relationships match our understanding of geometry? (note: ignore ordering of declarations) A. Square, Rectangle B. Shape, Shape C. Rectangle, Shape D. Other/none/more than one

This diagram shows a CompoundExp object as the root of a tree, and the children are the LHS and RHS (private member variables of type Expressio*). LHS points to an object of type DoubleExp, with value 3.7. RHS points to an object of type IdentifierExp with name “foo”. Note that it is ok for the RHS and LHS pointers to point to DoubleExp and IdentifierExp objects, because these are derived classes of the base class Expression. Stanford eval  CompoundExp, DoubleExp, and IdentifierExp can all be lhs/rhs of CompoundExp, which is type Expression*  Because of the “is a” relationship

This diagram shows a CompoundExp object as the root of a tree, and the children are the LHS and RHS (private member variables of type Expressio*). LHS points to an object of type DoubleExp, with value 3.7. RHS points to an object of type IdentifierExp with name “foo”. Note that it is ok for the RHS and LHS pointers to point to DoubleExp and IdentifierExp objects, because these are derived classes of the base class Expression. Stanford eval  CompoundExp doesn’t want to care exactly what type its lhs and rhs are  Just calls eval() on whatever they are and gets the right value

class Mammal { public: virtual void makeSound() = 0; string toString() { return “Mammal”; } }; class Cat : public Mammal { public: string toString() { return “Cat”; } }; class Siamese : public Cat { public: virtual void makeSound() { cout << “meow” << endl; } }; How many of these compile? (A) 0-1 (B) 2-3 (C) 4 (D) I don’t know! Siamese * s = new Siamese; Mammal * m = new Cat; Cat * c = new Siamese; Siamese * s = new Cat;

Rules for “virtual”: pure virtual  If a method of a class looks like this:  virtual returntype method() = 0;  then this method is a called “ pure virtual ” function  and the class is called an “ abstract class ”  Abstract classes are like Java interfaces  You cannot do “= new Foo();” if Foo is abstract (just like Java interfaces)  ALSO, you cannot do “= new DerivedFoo();” if DerivedFoo extends Foo and DerivedFoo does not implement all the pure virtual methods of Foo

Rules for “virtual”: runtime calls BaseType * obj = new DerivedType();  If we call a method like this: obj.method(), two different things could happen: 1. If method is not virtual, then BaseType’s implementation of method is called 2. If method is virtual, then DerivedType’s implementation of method is called DerivedType * obj = new DerivedType();  If we call a method like this: obj.method(), only one thing could happen: 1. DerivedType’s implementation of method is called

class Mammal { public: virtual void makeSound() = 0; string toString() { return “Mammal”; } }; class Cat : public Mammal { public: virtual void makeSound() { cout << “rawr” << endl; } string toString() { return “Cat”; } }; class Siamese : public Cat { public: virtual void makeSound() { cout << “meow” << endl; } string toString() { return “Siamese”; } }; What is printed? S iamese * s = new Siamese; cout toString(); (A)“Mammal” (B)“Cat” (C)“Siamese” (D)Gives an error (E)Other/none/more

class Mammal { public: virtual void makeSound() = 0; string toString() { return “Mammal”; } }; class Cat : public Mammal { public: virtual void makeSound() { cout << “rawr” << endl; } string toString() { return “Cat”; } }; class Siamese : public Cat { public: virtual void makeSound() { cout << “meow” << endl; } string toString() { return “Siamese”; } }; What is printed? Cat * c = new Siamese; cout toString(); (A)“Mammal” (B)“Cat” (C)“Siamese” (D)Gives an error (E)Other/none/more

class Mammal { public: virtual void makeSound() = 0; string toString() { return “Mammal”; } }; class Cat : public Mammal { public: virtual void makeSound() { cout << “rawr” << endl; } string toString() { return “Cat”; } }; class Siamese : public Cat { public: virtual void makeSound() { cout << “meow” << endl; } string toString() { return “Siamese”; } }; What is printed? Cat * c = new Siamese; c->makeSound(); (A)“rawr” (B)“meow” (C)“Siamese” (D)Gives an error (E)Other/none/more

This diagram shows a CompoundExp object as the root of a tree, and the children are the LHS and RHS (private member variables of type Expressio*). LHS points to an object of type DoubleExp, with value 3.7. RHS points to an object of type IdentifierExp with name “foo”. Note that it is ok for the RHS and LHS pointers to point to DoubleExp and IdentifierExp objects, because these are derived classes of the base class Expression. How does this relate to Stanford 1-2-3? eval() must be virtual, or we wouldn’t call the eval() implementations specific to the derived classes like DoubleExp and IdentifierExp

Which best explains good design of destructors in polymorphic classes? A. Destructors are specific to each class, so we don’t need to apply virtual to them B. Destructors should be virtual or we will cause a memory leak in cases like this: “ DerivedType obj = new DerivedType(); delete obj; ” C. Destructors should be virtual or we will cause a memory leak in cases like this: “ BaseType obj = new DerivedType(); delete obj; ” D. Both B and C E. Other/none