remaining assignments - university of marylandto mimic java interfaces ... templates in c++ are...

14
Remaining Assignments HW #5 due tomorrow (Thurs) Project #2 due Sunday night Final exam on Monday © 2011 Fawzi Emad, Computer Science Department, UMCP 1

Upload: others

Post on 29-Sep-2020

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Remaining Assignments - University Of Marylandto mimic Java interfaces ... Templates in C++ are similar to Java generics, but are quite different: •In C++, the compiler will generate

Remaining Assignments

• HW #5 due tomorrow (Thurs)

• Project #2 due Sunday night

• Final exam on Monday

© 2011 Fawzi Emad, Computer Science Department, UMCP

1

Page 2: Remaining Assignments - University Of Marylandto mimic Java interfaces ... Templates in C++ are similar to Java generics, but are quite different: •In C++, the compiler will generate

More Inheritance Details…

• C++ has protected visibility

• Friendship is not inherited

• There is no “root” class (like Java’s Object)

• Unlike Java, you may reduce visibility of inherited member (violating “Is-A”)

© 2011 Fawzi Emad, Computer Science Department, UMCP

2

Page 3: Remaining Assignments - University Of Marylandto mimic Java interfaces ... Templates in C++ are similar to Java generics, but are quite different: •In C++, the compiler will generate

Example: ReducedVisibility

• Visibility of inherited member can be reduced

• No problem for static binding

• What happens for virtual method???

© 2011 Fawzi Emad, Computer Science Department, UMCP

3

Page 4: Remaining Assignments - University Of Marylandto mimic Java interfaces ... Templates in C++ are similar to Java generics, but are quite different: •In C++, the compiler will generate

Private Inheritance

class Foo : private Bar

Foo inherits all members from Bar, but they become

private. • Used to restrict API • Violates “Is-A” relationship Foo f;

Bar &b = f; // No longer works

Bar *p = &f; // No longer works

© 2011 Fawzi Emad, Computer Science Department, UMCP

4

Page 5: Remaining Assignments - University Of Marylandto mimic Java interfaces ... Templates in C++ are similar to Java generics, but are quite different: •In C++, the compiler will generate

Example: PrivateInheritance

• Used to “restrict” API for base class

• Internally, private members from base class are still available.

© 2011 Fawzi Emad, Computer Science Department, UMCP

5

Page 6: Remaining Assignments - University Of Marylandto mimic Java interfaces ... Templates in C++ are similar to Java generics, but are quite different: •In C++, the compiler will generate

Multiple Inheritance

Java: • Java has “interfaces” • A class may implement multiple interfaces • No multiple inheritance C++: • No Java-style interfaces • Abstract classes are used instead • Multiple Inheritance allowed and used frequently

to mimic Java interfaces

© 2011 Fawzi Emad, Computer Science Department, UMCP

6

Page 7: Remaining Assignments - University Of Marylandto mimic Java interfaces ... Templates in C++ are similar to Java generics, but are quite different: •In C++, the compiler will generate

Why is Multiple Inheritance Scary?

“The Diamond of Death!”

© 2011 Fawzi Emad, Computer Science Department, UMCP

7

(Next slide please…)

Page 8: Remaining Assignments - University Of Marylandto mimic Java interfaces ... Templates in C++ are similar to Java generics, but are quite different: •In C++, the compiler will generate

Baseball-Playing Chef

© 2011 Fawzi Emad, Computer Science Department, UMCP

8

It’s the same guy!!

Page 9: Remaining Assignments - University Of Marylandto mimic Java interfaces ... Templates in C++ are similar to Java generics, but are quite different: •In C++, the compiler will generate

The Diamond of Death! Chef has: • name • hat Baseball Player has: • name • hat By default, Baseball-Chef will inherit: • Two names (He should only have one) • Two hats (He needs them both) There is a messy mechanism that allows you to change the default setup so that a

Baseball-Chef has only one name (but still gets both hats). • Constructors become complicated • Issues remain with conflicting contracts

• Better idea: Avoid the Diamond of Death

© 2011 Fawzi Emad, Computer Science Department, UMCP

9

Page 10: Remaining Assignments - University Of Marylandto mimic Java interfaces ... Templates in C++ are similar to Java generics, but are quite different: •In C++, the compiler will generate

Template Overview

Basic idea: Code that works the same way for many different data types. (The type is a parameter.)

Templates in C++ are similar to Java generics, but are

quite different: • In C++, the compiler will generate many different

versions of the same code – one for each type that is encountered.

• Works with a class or a non-class function • Parameterized type can be object or primitive

© 2011 Fawzi Emad, Computer Science Department, UMCP

10

Page 11: Remaining Assignments - University Of Marylandto mimic Java interfaces ... Templates in C++ are similar to Java generics, but are quite different: •In C++, the compiler will generate

Function Templates

Syntax: template<typename T>

void foo(T)

{

}

What will compiler do with this? string s;

foo(s);

1. If this is the first time type string has been used to call foo, it creates a version of foo using string in place of “T”, and calls it here.

2. If string has been used previously, it calls the function previously generated.

© 2011 Fawzi Emad, Computer Science Department, UMCP

11

Page 12: Remaining Assignments - University Of Marylandto mimic Java interfaces ... Templates in C++ are similar to Java generics, but are quite different: •In C++, the compiler will generate

Example: FunctionTemplates

findMin revisited:

• Template used so we can find the minimum value of any vector.

• Works for objects or primitives

• How many versions of findMin will the compiler generate?

• What kinds of objects will cause a compilation error?

© 2011 Fawzi Emad, Computer Science Department, UMCP

12

Page 13: Remaining Assignments - University Of Marylandto mimic Java interfaces ... Templates in C++ are similar to Java generics, but are quite different: •In C++, the compiler will generate

Class Templates

• A bit tricky to separate interface from implementation

• Like function templates, compiler generates as many different copies of the class as it needs.

© 2011 Fawzi Emad, Computer Science Department, UMCP

13

Page 14: Remaining Assignments - University Of Marylandto mimic Java interfaces ... Templates in C++ are similar to Java generics, but are quite different: •In C++, the compiler will generate

Example: ClassTemplate

• More than one parameterized type can be used

• “friend” can be used

© 2011 Fawzi Emad, Computer Science Department, UMCP

14