Presentation is loading. Please wait.

Presentation is loading. Please wait.

OOP in Java and C++ CS 123/CS 231. Outline zProgram Structure and Execution zEncapsulation and Inheritance zObjects, Variables, and Arrays zConstructors.

Similar presentations


Presentation on theme: "OOP in Java and C++ CS 123/CS 231. Outline zProgram Structure and Execution zEncapsulation and Inheritance zObjects, Variables, and Arrays zConstructors."— Presentation transcript:

1 OOP in Java and C++ CS 123/CS 231

2 Outline zProgram Structure and Execution zEncapsulation and Inheritance zObjects, Variables, and Arrays zConstructors zMethods, Operators, and Binding zContainers and Reuse zGUI Programming

3 Program Structure zClass definition similar in Java and C++ zJava: two types of programs Õapplication (with main() function) Õapplet (typically embedded in a web page) zC++ Õa program is (still) a collection of functions that may use objects and classes Õmain() function serves as driver

4 Program Execution zJava: Virtual Machine (VM) Õprograms: both compiled and interpreted Õcompiler produces.class from.java ÕVM loads.class file(s) as needed zC++: compiled, linked, and loaded Õmodules separately compiled Õlinked to produce executable Õstatic vs dynamic libraries

5 Encapsulation zEnforced through access keywords Õpublic: for interface Õprivate: to make implementation inaccessible Õprotected: access for subclasses only zIn Java Õeach member is prefixed with a keyword zIn C++ Õpublic, private, and protected sections

6 Breaking Encapsulation zPossible in C++ through the friend keyword zA method or class may be declared as a friend of an existing class zAllows access to private members “A friend is someone who has access to your private parts.”

7 Inheritance zFeature that allows a class to be defined based on another class Õmethods and attributes are inherited zJava and C++ difference ÕJava: public class A extends B { … } ÕC++: class A: public B { … } (different types of inheritance) zMultiple inheritance possible in C++, not in Java ÕBut in Java, one may implement several interfaces

8 Objects and Identity zQuestions: ÕHow/when are objects created? ÕWhat is the relationship between a variable and an object? zDifference between Java and C++ Õdistinction between primitive (built-in) type variables and variables for objects Õreference relationship between variable and actual object

9 Variables for Built-in Types zVariables for built-in types (C++ and Java) int x; … x = 5; 5 X X

10 Reference Variables (in Java) zReference type variables Button x; … x = new Button(“click”); X X “click” Button Object

11 Variables That “hold” Objects (in C++) zDeclaration of an object variable allocates space for the object Button x(“Click”); “click” X

12 Pointers (in C++) zVariables can be explicitly declared as pointers to objects Button *x; … x = new Button(“click”); X X “click” Button Object

13 Arrays zint x[20]; Button b[20]; ÕValid declarations in C++, not in Java ÕCreates 20 ints and 20 Button objects zIn Java, ÕDeclaration and array creation separate ÕFor object arrays, individual object creation necessary

14 Pointers and Arrays zIn C++, there is a close relationship between pointers and arrays zInstead of int x[20]; can issue int *x; x = new int[20]; to allow for dynamic allocation ÕUsage of the array (e.g., x[3] = 5;) identical in both cases

15 Object Construction zConstructor Õplace where you include code that initializes the object zDefault Constructor Õno additional info required zUser-defined Constructor Õwith parameters that specify values or sizes

16 Constructors in Java and C++ zIn Java, Õa constructor is invoked only through the new keyword Õrecall that all object variables are references zIn C++, Õa constructor is called upon variable declaration, or explicitly through new with pointers, or in other situations Õother types of constructors

17 C++ Control Over Copy and Assignment zIn C++, the semantics of “a = b“ (assignment) can be specified Õby defining the copy-assignment operator zIn C++, there is a copy constructor Õspecifies what happens during object copying, e.g., when function parameters are passed zThere is more low-level control Õshallow copy vs deep copy

18 Methods zDefines object behavior zStatic methods vs instance methods zMethod overloading Õwithin class, two methods with the same name but different signatures zMethod overriding Õsame signatures across different classes (subclass and superclass)

19 Operators zIn C++, operators like =, +, *, ==, etc. can be defined, just like methods zExample: Õclass Matrix { //... Matrix operator+(Matrix m) { … } // … } Õc = a + b; // equiv to c = a.operator+(b);

20 Method Binding zLet Teacher be a subclass of Employee ÕAlso, suppose promote() is a method defined in both classes zEmployee variables can refer to Teachers ÕIn Java, Employee e; … e = new Teacher(); ÕIn C++, Employee *e; … e = new Teacher; ze.promote() (or (*e).promote() ) calls which promote() method?

21 Static vs Dynamic Binding zIn C++, Employee’s promote() is called ÕDetermined at compile time and deduced from the type of the variable (static binding) zIn 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

22 Containers zExamples: Lists, Stacks, Files, etc. zStructures that “contain” elements zOften, the element’s type has little or nothing to do with the containers’ operations zPossible room for re-use Õunified container code for a stack of integers, a stack of webpages, a stack of strings,...

23 Java and the Object Hierarchy zAll classes extend the Object class: ÕA variable of class Object can refer to any Java object zExample: Õpublic class Stack { Object A[]; int top; // … void push(Object elt) //... }

24 C++ and Templates zTemplates allow for a generic definition Õparameterized definition, where the element type is the parameter zExample: Õtemplate class Stack { T A[MAX]; int top; public: void push(T element) // … }

25 GUI Programming zIn 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. zIn C++ Õnot part of the language Õlibraries dependent on platform (e.g., MFCs and Motif)


Download ppt "OOP in Java and C++ CS 123/CS 231. Outline zProgram Structure and Execution zEncapsulation and Inheritance zObjects, Variables, and Arrays zConstructors."

Similar presentations


Ads by Google