Presentation is loading. Please wait.

Presentation is loading. Please wait.

CISC/CMPE320 - Prof. McLeod

Similar presentations


Presentation on theme: "CISC/CMPE320 - Prof. McLeod"— Presentation transcript:

1 CISC/CMPE320 - Prof. McLeod
Winter 2013 CISC/CMPE320 2/23/2019 CISC/CMPE320 Assignment 4 on using the heap is due next Friday at 7pm. I’ll talk more about week 12 team presentations and peer grading next lecture. Fall 2018 CISC/CMPE320 - Prof. McLeod Prof. Alan McLeod

2 CISC/CMPE320 - Prof. McLeod
Today Using the Heap, Cont. – Run and interpret the results of the MyString demo program. Memory leak demo using the sample solution for assn 4. Start C++ Inheritance. Fall 2018 CISC/CMPE320 - Prof. McLeod

3 MyString Class Example, Cont.
See String.h, String.cpp and TestString.cpp. Note that simple messages have been added to constructors and the destructor in this class so we can see when they are invoked. Check out when constructors are invoked. Then, check out the use of the copy constructor. Note that our MyString object is mutable! Fall 2018 CISC/CMPE320 - Prof. McLeod

4 MyString Class Example, Cont.
Create a MyString on the heap and then delete it. What is the difference between testD and testG in how they are stored? If I did not delete testG as I did, would it be destroyed when main completes? (see lastOne). What happens if I try to delete testG twice? Fall 2018 CISC/CMPE320 - Prof. McLeod

5 CISC/CMPE320 - Prof. McLeod
Assignment Operator MyString& MyString::operator=(const MyString& right) { if (this != &right) { // Check to see if assigning to self delete[] buffer; // Get rid of old buffer len = right.length(); buffer = new char[len + 1]; // Using heap for (int i = 0; i < len; i++) buffer[i] = right[i]; buffer[len] = '\0'; } return *this; // Return ref to self Fall 2018 CISC/CMPE320 - Prof. McLeod

6 Assignment Operator, Cont.
Almost exactly the same as the copy constructor. But since assignment is carried out on an existing object, it must clean up the heap first. Note how aliasing is prevented here, as well. Just in case you are dumb enough to assign a variable to itself you need to check so that this operation does not delete the buffer prematurely. Fall 2018 CISC/CMPE320 - Prof. McLeod

7 MyString Class Example, Cont.
Note concatenation operations provided by the overloaded += and + operators. Note that when an int or char is supplied that you get a call to the conversion constructor followed by a call to the destructor for the temporary, unnamed variable holding the converted value. Fall 2018 CISC/CMPE320 - Prof. McLeod

8 CISC/CMPE320 - Prof. McLeod
Memory Leak Demo Remove any heap deletion code from the destructor in the assignment 4 sample solution. Watch memory consumption in the task manager as the program runs! Fall 2018 CISC/CMPE320 - Prof. McLeod

9 Inheritance and Software Engineering
Encapsulation helps to maintain the integrity of an object’s data. Inheritance avoids code repetition and allows you to lessen the impact that making changes in one class has on other classes. The only way to get polymorphism, which allows you to ensure common behaviour at runtime to any object that is part of the hierarchy (late binding). Software that uses polymorphism does not have to worry about the specifics of objects it is using, which makes extensibility of those objects easier. Fall 2018 CISC/CMPE320 - Prof. McLeod

10 Inheritance & Software Engineering, Cont.
Polymorphism usually removes the need for switch statement structures and leads to code that is shorter and easier to read. The use of abstract classes with pure virtual functions imposes design specifications on child classes. Use private attributes whenever possible! Fall 2018 CISC/CMPE320 - Prof. McLeod

11 Inheritance & Polymorphism
You are already familiar with this OOP concept from Java course(s). (Right?) The extension syntax: class Child : public Parent { } C++ does not have a single base class that works like the Object class in Java. Fall 2018 CISC/CMPE320 - Prof. McLeod

12 Aside - Protected Access
Until now, we have only used private and public specifiers inside the class’ definition. In addition, protected means the member is only accessible to a child class – so this specifier is only relevant in hierarchies. protected members are private outside the hierarchy. Otherwise the child class only inherits the public members of the parent class. Note that friend functions (and friend classes too!) can access both private and protected members from arguments. Fall 2018 CISC/CMPE320 - Prof. McLeod

13 Base Class Access Specifier
class Child : public Parent { } public means no change to inherited access. protected changes access of public parent members to protected in child class. private changes both protected and public parent members to private in child class. private in parent is never accessible to child. this thing Fall 2018 CISC/CMPE320 - Prof. McLeod

14 CISC/CMPE320 - Prof. McLeod
Inheritance, Cont. The child class must invoke the constructor of the parent class. You can do this in the initializer list: Child::Child(const double aNum, const int aVal) : Parent(aNum), myNum(aVal) {} Fall 2018 CISC/CMPE320 - Prof. McLeod

15 Aside - C++11 Inheriting Constructors
If the constructor in the Child class is the same as the parent class’ constructor (it “mimics” it), then you can inherit the constructor from the parent class, too. Then you don’t have to write one in the child! Do this by using the “using” keyword anywhere in the Child class: using Parent::Parent; C++11 Fall 2018 CISC/CMPE320 - Prof. McLeod

16 CISC/CMPE320 - Prof. McLeod
Multiple Inheritance Multiple Inheritance: You can extend more than one class (separate them with commas), if you really want to… How to deal with ambiguities? Fall 2018 CISC/CMPE320 - Prof. McLeod

17 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. Fall 2018 CISC/CMPE320 - Prof. McLeod

18 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 annotation in Java). C++11 Fall 2018 CISC/CMPE320 - Prof. McLeod

19 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 2018 CISC/CMPE320 - Prof. McLeod

20 CISC/CMPE320 - Prof. McLeod
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 2018 CISC/CMPE320 - Prof. McLeod

21 CISC/CMPE320 - Prof. McLeod
Overriding, Cont. Polymorphism only works with pointers and references, 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. Fall 2018 CISC/CMPE320 - Prof. McLeod

22 Inheritance, Cont. – virtual Destructors
The parent class should declare a virtual destructor even if it does nothing. This will make sure that all destructors get invoked. They will get invoked as a “chain reaction”. Fall 2018 CISC/CMPE320 - Prof. McLeod

23 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 you are “shadowing” or “hiding” the function. Fall 2018 CISC/CMPE320 - Prof. McLeod

24 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. Fall 2018 CISC/CMPE320 - Prof. McLeod

25 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. Fall 2018 CISC/CMPE320 - Prof. McLeod


Download ppt "CISC/CMPE320 - Prof. McLeod"

Similar presentations


Ads by Google