Oo 0
Oo 0
OO Languages: Preliminaries
(a.k.a. Under the Hood of Java/C++)
2
Class and Object
Class is a type and includes data and
operations together.
An object is an instance of class.
Objects are declared to be of a particular class
exactly as variables are declared to be of a
particular type in C.
3
Fields and Methods
Member variables / fields
Instance variables: each object has its own
copy of instance variables
Class (static) variables: across all objects of a
class – similar to global variables
Each object includes a set of functions called
methods.
Similar to ordinary procedures and functions
Can automatically access the object’s data.
Calling/invoking a method of an object is
sometimes called sending the object a
message.
4
Example: Complex Number
5
Example: Complex Number (Java)
public class Complex
{ private double re, im;
public Complex()
{ re = 0; im = 0;}
public Complex (double realpart, double imagpart)
{ re = realpart; im = imagpart; }
public double realpart()
{ return re;}
public double imaginarypart()
{ return im;}
public Complex add (Complex c)
{ return new Complex(re + c.realpart(), im + c.imaginarypart());}
public Complex multiply (Complex c)
{ return new Complex(re * c.realpart() - im * c.imaginarypart(),
re * c.imaginarypart() + im * c.realpart()); }
} 6
Example: Complex Number (Java)
import java.io.*;
public class Main{
public static void main(String[] args){
Complex z, w;
z = new Complex (1,2);
w = new Complex (-1,1);
z = z.add(w);
System.out.println(z.realpart());
System.out.println(z.imaginarypart());
z = z.add(w).multiply(z);
System.out.println(z.realpart());
System.out.println(z.imaginarypart());
}
}
7
What To Remember
An object is a run-time instance of a class. Each
class can have many instances. All objects from the
same class share the methods, but each of them
have different “internal” states, stored in the fields.
Instantiation is achieved by the new expression.
Constructors are used here, so that an object can
be created with initial values. Constructors do not
have return values. Each class may have multiple
constructors, a case of overloading.
Different objects are “hooked up” together via
messaging (method invocation).
The entire program starts from the main method,
and only one class can have it.
Object types are class names.
8
Heaps and Object References
When a new expression is evaluated, an
object (i.e. its fields and auxiliary data
structures) is allocated on the heap.
The value passed back to programmers from
the new expression however is not the
object on the heap itself, but a reference to
it, just like pointers in C.
A reference can still uniquely identify an
object.
When assignments happen (such as local
assignments or via parameter passing), only
the reference gets passed around.
9
An Example in C++
A list that consists of integers
class IntList {
private:
int elem;
IntList *next;
public:
IntList(int first) { … };
~IntList() { … };
void insert(int i) { … };
int getval() { … };
IntList *getNext() {…};
};
10
Similar to Java, But…
I won’t differentiate between those superficial
terminology distinctions: methods in Java are
called member functions in C++, etc – when you
get to learn a dozen OO languages, these terms
will get confusing anyways.
The name C++ suggests it has inherited some of
the C language features:
Different treatment of the value of “objects”. In
Java it always means reference, but in C++, there
are two possibilities because the language has
decided to follow the C tradition -- both a struct
and a pointer to the struct visible to programmers.
An explicit destructor.
11
Object Values in C++
class IntList {
private:
int elem;
IntList *next;
Whatever whatever;
public:
IntList(int first) { … };
~IntList() { … };
void insert(int i) { … };
int getval() { … };
IntList *getNext() {…};
};
12
Destructors in C++
A *x = new A();
...
delete x;
13