Presentation is loading. Please wait.

Presentation is loading. Please wait.

Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 3 is due Sunday, the 8 th at 7pm. Problems with assn 3? Discuss at your team meeting tonight.

Similar presentations


Presentation on theme: "Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 3 is due Sunday, the 8 th at 7pm. Problems with assn 3? Discuss at your team meeting tonight."— Presentation transcript:

1 Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 3 is due Sunday, the 8 th at 7pm. Problems with assn 3? Discuss at your team meeting tonight. Today: –Lots of OOP terminology! How to code for polymorphism and how to create abstraction. –Namespaces (if we have time).

2 Fall 2015CISC/CMPE320 - Prof. McLeod2 Polymorphism and virtual Prototypes To allow the polymorphic process of dynamic binding to work, name the function to be shared in parent and child classes as virtual (before the return type in the prototype). Note that once a function is declared virtual in a parent class all child class versions of the same function are automatically virtual – but it is considered good form to still name them as virtual.

3 Aside – C++11 override Keyword You can add this to the end of a function prototype that is overriding a virtual parent prototype. Then the compiler will check to make sure you have done the overriding properly. (Like the @override annotation in Java). Fall 2015CISC/CMPE320 - Prof. McLeod3

4 Aside – C++ final Keyword Used at the end of the prototype to prevent a virtual function from being overridden in a child class. A class can also be declared final, in which case it cannot be used as a parent class. (Same as Java). Fall 2015CISC/CMPE320 - Prof. McLeod4

5 Overriding, Cont. If you don’t use a virtual function in the Parent class, then the Child class just redefines the function when it uses the same signature. If you make the Parent class’ function virtual then you are overriding the function in the Child class, and you can use polymorphism. With a virtual function ownership is determined by the object’s actual type not the type of the pointer used to refer to the object. Fall 2015CISC/CMPE320 - Prof. McLeod5

6 Fall 2015CISC/CMPE320 - Prof. McLeod6 Overriding, Cont. Polymorphism only works with pointers, not the actual objects (this latter use is called static binding). If the child class does not override the Parent’s virtual function it just inherits it.

7 Inheritance, Cont. – virtual Destructors We have not talked about destructors, yet (we will!). They are responsible for deleting memory used on the heap. The parent class should declare a virtual destructor even if it does nothing. This will make sure that the all destructors get invoked. They will get invoked as a “chain reaction”. Fall 2015CISC/CMPE320 - Prof. McLeod7

8 More Notes on the Use of virtual Don’t use this keyword in the implementation file. You do not *have* to override a virtual function in a child class, but then you won’t have polymorphism. (As we will see shortly – you *must* implement a pure virtual function if your child class is to be non-abstract.) If you redefine a non-virtual function from the parent class in the child then Eclipse will say you are “shadowing” the function. Fall 2015CISC/CMPE320 - Prof. McLeod8

9 Fall 2015CISC/CMPE320 - Prof. McLeod9 Pure Virtual Member Functions (This is just like abstract in Java.) This is when you have a virtual prototype in the parent class, but cannot implement it in the parent class. You avoid implementation by setting the function prototype to zero: virtual double getArea() const = 0; getArea() might be in a Shape parent class, but cannot calculate an area, for example.

10 Fall 2015CISC/CMPE320 - Prof. McLeod10 Pure Virtual Member Functions, Cont. If a class has at least one pure virtual member then it is termed an abstract class. An abstract class can contain normal members and other virtual members, as usual. You cannot instantiate an abstract class. But you can use the abstract class as a type to enable polymorphism. If a child class is not to be abstract, as well, then it must implement the pure virtual member function.

11 Shapes Example – (Almost) No Heap… Look at the Shapes hierarchy. This is a simplified version of one that uses the heap and has virtual destructors. Look for the effects of virtual, pure virtual, overriding, inheritance and shadowing. Fall 2015CISC/CMPE320 - Prof. McLeod11

12 More Function Binding Examples Static binding is determined at compilation. Dynamic binding is determined at run time. See: –TestStaticBinding.cpp –TestDynamicBinding.cpp Fall 2015CISC/CMPE320 - Prof. McLeod12

13 Name Hiding The designers of C++ decided that overloaded functions cannot be inherited in a child class. The child class version of the overloaded function “hides” the parent class version. This is a good way to avoid some tricky ambiguities, apparently. See TestHiding.cpp Fall 2015CISC/CMPE320 - Prof. McLeod13

14 Name Hiding, Cont. You can get around this by using “ using ” in the child class as shown in the demo. However, this is considered poor practice. It is less confusing to avoid trying to overload inherited functions and just use different function names instead. Fall 2015CISC/CMPE320 - Prof. McLeod14

15 Fall 2015CISC/CMPE320 - Prof. McLeod15 Namespaces We’ve been using namespace std; for a while now. Without it, you would need to write: std::cout << something << std::endl; A simple mechanism to avoid naming conflicts. A class can have the same name as long as it is in a different namespace.

16 Fall 2015CISC/CMPE320 - Prof. McLeod16 Namespaces, Cont. To place your class definitions in a namespace, simply name it: namespace alpha { class dingdong { … }; } Now dingdong can be defined in another namespace (like std ) and there will not be a conflict.

17 Fall 2015CISC/CMPE320 - Prof. McLeod17 Namespaces, Cont. Particularly useful when you are building a library of classes. You will want to make sure your namespace’s name is unique. You can use an alias to make this easier: namespace alpha = Alpha_Software_Company; Now the user of your library, just needs to type: alpha::someThing();

18 Fall 2015CISC/CMPE320 - Prof. McLeod18 Namespaces, Cont. The only namespace that you should identify with the using keyword is std. All other namespaces should be named in the statement, like alpha::. Then you won’t conflict with anything in the std namespace. (I didn’t do this in the Shapes hierarchy, however…)


Download ppt "Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 3 is due Sunday, the 8 th at 7pm. Problems with assn 3? Discuss at your team meeting tonight."

Similar presentations


Ads by Google