Post on 03-Jun-2020
transcript
Three Properties of Object-Oriented Languages:
I Encapsulation
I Inheritance
I Dynamic method binding (polymorphism)
2/19
Encapsulation
I Data and functions bound together into a single object.
I “Data hiding” – hide implementation details from user.
I More accurately, control access to data using public and privatevariables and methods.
3/19
Inheritance
I Hierarchy of classes and objects.
I Shared behaviors and data – code re-use.
I Static polymorphism: one interface for many kinds of object.
4/19
Multiple Inheritance
I In Java, classes and subclasses form a tree – a class may havemany subclasses, but each subclass extends exactly one parentclass. This is called the class hierarchy.
I Java does not permit “multiple inheritance.”
5/19
Multiple Inheritance
I On the other hand, the C++ language allows classes to inheritfrom several different parent classes: multiple inheritance.
I For example, consider the following set of classes:
6/19
Multiple Inheritance in C++
class Person { ... };
class Student : public Person { ... };
class Employee : public Person { ... };
class StudentEmployee : public Student, public Employee
{...};
In C++, constructors for subclasses can invoke the constructors oftheir parent classes, e.g.,Student(string name, int year, double gpa): Person(name)...
7/19
Multiple Inheritance in C++
In our example, StudentEmployee can invoke the constructors ofboth parents:
8/19
Multiple Inheritance in C++
I In our example, assume name is an instance variable in the classPerson, with accessor method getName().
I Then both Student and Employee will inherit this variable aswell as the getName method.
I Now consider the class StudentEmployee. It inherits name andgetName from both Student and Employee.
9/19
Multiple Inheritance in C++
I In our example, assume name is an instance variable in the classPerson, with accessor method getName().
I Then both Student and Employee will inherit this variable aswell as the getName method.
I Now consider the class StudentEmployee. It inherits name andgetName from both Student and Employee.
9/19
Multiple Inheritance in C++
I In our example, assume name is an instance variable in the classPerson, with accessor method getName().
I Then both Student and Employee will inherit this variable aswell as the getName method.
I Now consider the class StudentEmployee. It inherits name andgetName from both Student and Employee.
9/19
Multiple Inheritance
I This is called the “diamond problem” (a.k.a. “Diamond ofDeath”).
I In C++, one way to avoid the error on the previous page is tosimply choose one of the “getName()” methods and ignore theother one.
10/19
Multiple Inheritance
Can we gain the benefits of multiple inheritance in Java?
I Sort of ... in Java we can create “interfaces”.
I They are similar to classes, but an interface has no instancevariables and contains only abstract methods.
I A class can implement more than one interface.
I It is not quite the same as multiple inheritance, but yields manyof the same benefits.
12/19
Dynamic Method Binding
An object’s methods are determined at runtime rather thancompilation time, since subclasses can override methods and can beused wherever the superclass is allowed.
13/19
Dynamic Binding
DynamicDemo.java in the shared repo.
At runtime, Java determined the correct class of the parameter andinvoked ys talk method. This is dynamic method binding.
14/19
Dynamic Binding
DynamicDemo.java in the shared repo.
At runtime, Java determined the correct class of the parameter andinvoked ys talk method. This is dynamic method binding.
14/19
Dynamic Binding
DynamicDemo.java in the shared repo.
At runtime, Java determined the correct class of the parameter andinvoked ys talk method. This is dynamic method binding.
14/19
Static vs. Dynamic Binding
I See program staticbind.cpp in the shared repo.
I There is a parent class Super and a child class Sub, each with aget() method.
Sub a(10,20);
Super b = a;
cout << a.get() << endl; // Which "get"?
cout << b.get() << endl; // Which "get"?
Both will use Super’s “get()” method!
15/19
Static vs. Dynamic Binding
I By default, C++ uses static binding.
I However, you can still obtain the same behavior as dynamicbinding by using virtual methods and pointers as shown inprogram dynamicbind.cpp
16/19
Overloading
I When two methods in a class have the same name but differentparameters, we say that the method name is “overloaded.”
I This is familiar from Java (where, for instance, we have twodifferent “substring” methods for the String class or multipleconstructor methods).
I In C++ we can even overload symbolic operators like “+” and“*” (really, any operator).
17/19
Overloading
I When two methods in a class have the same name but differentparameters, we say that the method name is “overloaded.”
I This is familiar from Java (where, for instance, we have twodifferent “substring” methods for the String class or multipleconstructor methods).
I In C++ we can even overload symbolic operators like “+” and“*” (really, any operator).
17/19
Overloading an Operator in C++Overload.cpp
class Pirate {
public:
Pirate(string name) { this->name = name;}
Pirate operator +(Pirate p) {
return Pirate(p.getName() + name);
}
... some code omitted ...
...
Pirate x("Fred");
Pirate y("Mary");
Pirate a = x + y; // Creates a Pirate named "MaryFred"
See overload.cpp and overload1.cpp in the shared repo. 18/19
Overloading an Operator in C++
I There are many aspects of operators that we must worry about:precedence, associativity, etc.
I C++ avoids these by forcing overloaded operators to have thesame precedence and associativity that the original operatorshad.
I See program overload2.cpp in the shared repo.
19/19