programming languages and paradigms object-oriented programming (part ii)

21
Programming Languages and Paradigms Object-Oriented Programming (Part II)

Upload: scot-david-ellis

Post on 31-Dec-2015

232 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Programming Languages and Paradigms Object-Oriented Programming (Part II)

Programming Languagesand Paradigms

Object-Oriented Programming(Part II)

Page 2: Programming Languages and Paradigms Object-Oriented Programming (Part II)

OOPSlide 2

Java and C++ Comparison(continued)

Arrays Constructors and destructors Operators Static vs Dynamic Binding Containers and Reuse GUI Programming

Page 3: Programming Languages and Paradigms Object-Oriented Programming (Part II)

OOPSlide 3

Arrays

int x[20]; Button b[20]; Valid declarations in C++, not in Java Creates 20 ints and 20 Button objects

In Java, Declaration and array creation separate For object arrays, individual object

creation necessary

Page 4: Programming Languages and Paradigms Object-Oriented Programming (Part II)

OOPSlide 4

Pointers and Arrays

In C++, there is a close relationship between pointers and arrays

Instead of int x[20]; can issueint *x; x = new int[20];to allow for dynamic allocation Usage of the array (e.g., x[3] = 5;)

identical in both cases To deallocate, use delete [] x;

Page 5: Programming Languages and Paradigms Object-Oriented Programming (Part II)

OOPSlide 5

Constructors in Java and C++

In Java, a constructor is invoked only through the new

keyword recall that all object variables are references

In C++, a constructor is called upon variable

declaration, or explicitly through new with pointers, or in other situations

other types of constructors

Page 6: Programming Languages and Paradigms Object-Oriented Programming (Part II)

OOPSlide 6

C++ Destructor

Special method whose signature is a ~ followed by the name of the class e.g., ~SomeClass();

Particularly if the class contains pointers and the constructor contains calls to new, a destructor needs to be defined e.g., SomeClass() { A = new int[20]; }

~SomeClass() { delete [] A; }

Page 7: Programming Languages and Paradigms Object-Oriented Programming (Part II)

OOPSlide 7

C++ Copy and Assignment

In C++, the semantics of “a = b” (assignment) can be specified by defining the copy-assignment operator

In C++, there is a copy constructor specifies what happens during object

copying, e.g., when function parameters are passed

There is more low-level control shallow copy vs deep copy

Page 8: Programming Languages and Paradigms Object-Oriented Programming (Part II)

OOPSlide 8

Operators

In C++, operators like =, +, *, ==, etc. can be defined, just like methods

Example: class Matrix {

// ... Matrix operator+(Matrix m) { … } // …}

c = a + b; // equiv to c = a.operator+(b);

Page 9: Programming Languages and Paradigms Object-Oriented Programming (Part II)

OOPSlide 9

Method Binding Let Teacher be a subclass of Employee

Also, suppose promote() is a method defined in both classes

Employee variables can refer to Teachers In Java, Employee e; … e = new Teacher(); In C++, Employee *e; … e = new Teacher;

e.promote() (or e->promote() ) calls which promote() method?

Page 10: Programming Languages and Paradigms Object-Oriented Programming (Part II)

OOPSlide 10

Static vs Dynamic Binding In C++, Employee’s promote() is called

Determined at compile time and deduced from the type of the variable (static binding)

In Java, Teacher’s promote is called Determined at run-time because the actual

type of the referred object is checked then (dynamic binding)

* C++ uses virtual functions for dynamic binding

Page 11: Programming Languages and Paradigms Object-Oriented Programming (Part II)

OOPSlide 11

Another exampleclass Employee{public: double salary() {return sal;} double computeRaise() {return 25;} Employee(double salary) {sal = salary;}private: double sal;};

class Manager: public Employee

{public: double computeRaise() {return 100;} Manager(double

Salary) : Employee (salary)

{}};

Page 12: Programming Languages and Paradigms Object-Oriented Programming (Part II)

OOPSlide 12

Sample continued

Driver Code:Manager * boss1 = new Manager(2000);double boss1Salary = boss1->salary(); // 2000Employee *boss2 = new Manager(2300);double *boss2Salary = boss2->salary(); //2300double boss1Raise = boss1->computeRaise(); //

100double boss2Raise = boss2->computeRaise(); // 25

Page 13: Programming Languages and Paradigms Object-Oriented Programming (Part II)

OOPSlide 13

C++ Run Time Binding

If the intent is for the selection of the function to be determined by the object’s class, not by the declaration of the pointer used to address it: Declare some base class members to be

virtual If virtual, the compiler will deposit the

“type field” of the class in the object

Page 14: Programming Languages and Paradigms Object-Oriented Programming (Part II)

OOPSlide 14

Virtual Functions

Base class usually defines a body for a virtual function.

Inherited by derived class as default if it chooses not to override the implementation

Virtual keyword in function declaration, not in definition

Page 15: Programming Languages and Paradigms Object-Oriented Programming (Part II)

OOPSlide 15

Containers Examples: Lists, Stacks, Files, etc. Structures that “contain” elements Often, the element’s type has little or

nothing to do with the containers’ operations

Possible room for re-use unified container code for a stack of

integers, a stack of webpages, a stack of strings, ...

Page 16: Programming Languages and Paradigms Object-Oriented Programming (Part II)

OOPSlide 16

Java and the Object Hierarchy

All classes extend the Object class: A variable of class Object can refer to any

Java object Example:

public class Stack { Object A[]; int top; // … void push(Object elt) // ...}

Page 17: Programming Languages and Paradigms Object-Oriented Programming (Part II)

OOPSlide 17

C++ and Templates Templates allow for a generic definition

parameterized definition, where the element type is the parameter

Example: template<class T>

class Stack<T> { T A[MAX]; int top; public: void push(T element) // …}

Page 18: Programming Languages and Paradigms Object-Oriented Programming (Part II)

OOPSlide 18

C++ Templates <class T> indicates that a template is being

declared T is the type name (can be a class) Usage example:

Stack <int> iStack; Stack <Cards> cStack;

where Cards is a user defined class A type used as a template argument must

provide the interface expected by the template

Page 19: Programming Languages and Paradigms Object-Oriented Programming (Part II)

OOPSlide 19

C++ Standard Containers vector : 1-D array of T list : double linked list of T dequeue : double-ended queue of T queue : queue of T stack : stack of T map : associative array of T set : set of T bitset : set of booleans

Page 20: Programming Languages and Paradigms Object-Oriented Programming (Part II)

OOPSlide 20

Java Generics

Recent addition to Java Similar to C++ templates Why are templates/generics better

than using the Object class as a parameter?

Page 21: Programming Languages and Paradigms Object-Oriented Programming (Part II)

OOPSlide 21

GUI Programming In Java, GUI is part of its development kit

java.awt.* is a collection of classes that support visual programming and graphics

visual objects (buttons, text fields, etc), layout managers, events, etc.

In C++ not part of the language libraries dependent on platform (e.g., MFCs

and Motif)