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 Paradigm | Java | C++ |
---|---|---|---|
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 |
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:
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.
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 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:
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.
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.
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) |
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.)scores.length
in a Java program but not in a C++ program.