1.3. Object-Oriented Programming Review: C++ vs. Java

Time: 00:06:08 | Download: Large, Large (CC), Small | Streaming, Streaming (CC)| Slides (PDF)

Although our initial study of C++ focuses on the procedural programming paradigm, we will utilize some object-oriented C++ features. Even though there are a few syntactic differences between Java and C++, your experience with Java in CS 1400 should be sufficient to carry you through the first few chapters. The following table reviews some concepts and terminology as used in the text or as you might see in other places.

Term or Concept Object-Oriented ParadigmJavaC++
Supported Paradigm Object-Oriented Object-Oriented Object-Oriented
Procedural
class class class class
object object, instance object, instance object, instance
Create an object from a class instantiate instantiate instantiate
Data not associated with a class N/A N/A variable
Operation not associated with a class N/A N/A function
Data associated with or stored in a class attribute instance variable,
instance field
data member,
member variable
Operation associated with a class behavior, operation method member function
Object-Oriented Paradigm: Concepts and Terms. At the conceptual level, object orientation is language-independent (sometimes called language agnostic). However, the object-oriented paradigm and various object-oriented programming languages often use different terms to name the same concept. The table compares the concept names common in pure object orientation with those used in C++ and Java. (Note that C++ is a hybrid language supporting the object-orientated and the older procedural programming paradigms.)

Class vs. Object

From its name, it seems like object-oriented programming is all about objects. Yet, when we write an object-oriented program, our code defines a feature called a "class," and both C++ and Java include class as a keyword - but neither has object as a keyword. We previously used a cookie-cutter and cookies metaphor to illustrate the relationship between a class and the objects instantiated from it.

In the cookie-cutter metaphor, the cutter plays the role of the class while the cookies are the objects. A cookie-cutter describes a cookie's shape and size but is inedible and contains no cookie ingredients. Alternatively, we make cookies from flour, sugar, and butter. Once we make the cookie dough, we can stamp out as many cookies as we want with the cookie cutter. Classes and objects are fundamentally connected, so we often use them interchangeably. Nevertheless, the two concepts are distinct.

So, we can say that a class is a description that describes three characteristics of the objects that are created or instantiated from it:

  1. The class name becomes a new data type that we may use throughout the program and is the type of objects instantiated from it.
  2. Each class forms an aggregate data type, which means that it can hold many smaller variables. The class specifies the names and types of all the variables in the objects instantiated from the class.
  3. Classes also contain functions or methods that can operate on the data or variables stored in the object. The class specifies the name of each function, its return type, and the number and type of each argument.

Just as cookies have ingredients, so do objects. Memory is the "raw ingredient" programmers use to make objects. One of the most striking differences between C++ and Java is where and how programmers allocate that memory. The following figures illustrate the differences, which we revisit in detail in chapter 4.

C++ and Java Syntax

The object-oriented paradigm requires three main characteristics: encapsulation, inheritance, and polymorphism. We explore the latter two characteristics later in the semester. We achieve encapsulation by packaging data and the operations that use that data together into an entity called an object. Java and C++ use different terms to name class variables and operations. Java calls them instance variables or fields and methods, respectively, which denotes that they "belong" to instances or objects. C++ calls them member variables and member functions because they are class members. The following syntax focuses on creating objects, accessing data, and calling operations.

Constructors

Constructors are just functions (C++) or methods (Java), but they are special in one significant way. Each time a program instantiates a new object, it automatically calls a constructor that builds, initializes, or constructs the object, making it ready for use by the program. Constructors can be simple or complex. Simple constructors may only initialize the variables in an object by transferring the values stored in the constructor arguments to the variables. Complex constructors may perform more complicated operations.

Creating or instantiating a new object in C++ is similar to instantiating an object in Java, but there are some critical differences. Suppose that we have a class named Foo, and we wish to instantiate a Foo object. Assume that class Foo has a constructor that does not take any arguments. The instantiation statements look like this:

Foo myFoo1 = new Foo();		// Java
Foo* myFoo2 = new Foo;		// C++
Foo myFoo3;			// C++
Foo* myFoo2 = new Foo();	// C++, new
Foo myFoo3();			// C++, new
Instantiating an object with a zero-argument constructor. Java provides one way of creating an object, while C++ provides two ways. C++ requires more symbols or operators - a more extensive syntax - than Java because it has more options. When a C++ program uses the new keyword, it must also use the asterisk, *, with the class name. The asterisk modifies the variable myFoo2, making it a pointer, which we explore in Chapter 4. The options make C++ more flexible but at the expense of having a more complex syntax. C++ does not require parentheses when calling a constructor without arguments and previously disallowed them. But the newer standards allow both illustrated versions.
Foo myFoo1 = new Foo(5);	// Java
Foo* myFoo2 = new Foo(5);	// C++
Foo myFoo3(5);   		// C++
Instantiating an object with a one-argument constructor. Adding arguments to constructors does not change the number of options for instantiating objects: one for Java and two for C++, but the parentheses are now required. The figure illustrates the options with a one-argument constructor.

General Object-Oriented Method and Function Calls

Now let's suppose that the Foo class has a method or function named doSomething. C++ has two ways of creating an object, which requires two ways to use the object, which in turn requires an operator not found in Java. The new operator, called arrow, is formed by the minus and greater than symbols together, without a space between them, and is used whenever the variable to the left is a pointer. Otherwise, both languages use the dot operator to make the call.

myFoo1.doSomething();		// dot operator: C++ and Java
myFoo2->doSomething();		// arrow operator: C++
Calling a method or function defined in a class. Collectively, we call dot, ., and arrow, ->, selection operators because they select functions from classes. The left-hand argument is an object (i.e., a class instance), and the right-hand argument is a function (or, as we'll see later, a member variable).

Many Java textbooks claim that Java doesn't have pointers. Nevertheless, Java programs always manage all objects with pointers (technically references). However, Java only has one way of handling objects and can dispense with the pointer operators, Chapter 4's topic.

Arrays

Strictly speaking, arrays are not an object-oriented construct. However, Java implements them as objects, while C++ treats them as a primitive or fundamental data type. The difference between the two languages warrants a brief comparison, which should be sufficient until we formally study arrays in chapter 7.

Conceptually and functionally, an array is a collection of variables managed by a common name. Individual variables, called elements, within the array are selected by an index or subscript. Arrays have a size: the number of elements or variables they can hold. Programs determine the size of each array when they create them. C++ and Java implement zero-indexed arrays, which means that valid array index values range from 0 to the size of the array - 1. All variables in the array must be the same data type (e.g., int or double). The following figure demonstrates how programmers create arrays in C++ and Java. The figure also shows an abstract representation of an array in computer memory.

(a)
double[] scores = new double[8];
(b)
double scores[8];
double* scores = new double[8];
(c) An array represented as a large rectangle subdivided into eight smaller rectangles. The smaller rectangles represent the eight array elements. The illustration includes the array index values, 0, 1, 2, ..., 7 adjacent to the smaller element rectangles.
Creating arrays in Java and C++. Arrays are a common data structure that most programming languages support. The illustration shows an array capable of holding eight double values.
  1. In Java, an array is an instance of an unnamed class that the program always creates with the new operator. (The java.util.Arrays class contains a collection of static methods useful when working with arrays but is not involved with creating them.)
  2. C++ can create arrays in two different ways. The second way is very similar (in appearance and the underlying concepts) to Java. We explore the difference between the two statements in greater detail in Chapters 4 and 7.
  3. Arrays created in both languages have a similar appearance. The illustrated array has eight double variables or elements accessed as scores[0], scores[1],scores[2], ..., scores[7].
Java arrays have a length attribute, which is possible because Java arrays are objects. However, C++ arrays are NOT objects and do NOT have a length attribute. So, for example, based on Figure 5(a), you may write scores.length in a Java program but not in a C++ program.