Download presentation
1
OOP Languages: Java vs C++
CS 124
2
Outline Program Structure and Execution Encapsulation and Inheritance
Objects, Variables, and Arrays Constructors Methods, Operators, and Binding Containers and Reuse GUI Programming
3
Program Structure Class definition similar in Java and C++
Java: two types of programs application (with main() function) applet (typically embedded in a web page) C++ a program is (still) a collection of functions that may use objects and classes main() function serves as driver
4
Program Execution Java: Virtual Machine (VM)
programs: both compiled and interpreted compiler produces .class from .java VM loads .class file(s) as needed C++: compiled, linked, and loaded modules separately compiled linked to produce executable static vs dynamic libraries
5
Encapsulation Enforced through access keywords In Java In C++
public: for interface private: to make implementation inaccessible protected: access for subclasses only In Java each member is prefixed with a keyword another access level: package-access In C++ public, private, and protected sections friend keyword used to break encapsulation
6
Inheritance Feature that allows a class to be defined based on another class methods and attributes are inherited Java and C++ difference Java: public class A extends B { … } C++: class A: public B { … } (different types of inheritance) Multiple inheritance possible in C++, not in Java But in Java, one may implement several interfaces
7
Objects and Identity Questions: Difference between Java and C++
How/when are objects created? What is the relationship between a variable and an object? Difference between Java and C++ distinction between primitive (built-in) type variables and variables for objects reference relationship between variable and actual object
8
Variables for Built-in Types
Variables for built-in types (C++ and Java) int x; … x = 5; X X 5
9
Reference Variables (in Java)
Reference type variables Button x; … x = new Button(“click”); X Button Object “click” X
10
Variables That “hold” Objects (in C++)
Declaration of an object variable allocates space for the object Button x(“Click”); X “click”
11
Pointers (in C++) Variables can be explicitly declared as pointers to objects Button *x; … x = new Button(“click”); X Button Object “click” X
12
Disposing of Allocated Memory
In Java, garbage collection is automatic Memory allocated objects are reclaimed when no variables refer to them Need to set reference variables to null when the object is no longer needed In C++, object destruction is the programmers responsibility using the delete keyword
13
delete in C++ There should be a delete for every new Memory leak
SomeClass *x = new SomeClass(…); // … use object pointed to by x delete x; // done using object Memory leak Occurs when you forget to delete Wasted memory Can this occur in Java?
14
Object Construction Constructor Default Constructor
place where you include code that initializes the object Default Constructor no additional info required User-defined Constructor with parameters that specify values or sizes
15
Arrays int x[20]; Button b[20]; In Java,
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
16
Pointers and Arrays In C++, there is a close relationship between pointers and arrays Instead 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 To deallocate, use delete [] x;
17
Constructors in Java and C++
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
18
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; }
19
C++ Control Over 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
20
Methods Defines object behavior Static methods vs instance methods
Method overloading within class, two methods with the same name but different signatures Method overriding same signatures across different classes (subclass and superclass)
21
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);
22
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?
23
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
24
Another example class 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) {} };
25
Sample continued Driver Code: Manager * boss1 = new Manager(2000);
double boss1Salary = boss1->salary(); // 2000 Employee *boss2 = new Manager(2300); double *boss2Salary = boss2->salary(); //2300 double boss1Raise = boss1->computeRaise(); // 100 double boss2Raise = boss2->computeRaise(); // 25
26
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
27
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
28
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, ...
29
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) ... }
30
Templates Templates allow for a generic definition C++ Example:
parameterized definition, where the element type is the parameter C++ Example: template<class T> class Stack<T> { T A[MAX]; int top; public: void push(T element) // … } Java counterpart (as of Java 1.5): generics
31
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
32
Defining a Template When defining a template member outside of its class, it must be explicitly declared a template Example template <class T> Stack<T>::Stack()
33
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
34
GUI Programming In Java, GUI is part of its development kit In C++
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)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.