03 - Functions and Methods
03 - Functions and Methods
OBJECT-ORIENTED PROGRAMMING
1
FUNCTIONS AND METHODS
pass by reference
Java:
pass by value (of object reference)
function (method) overloading
2
FUNCTION DECLARATIONS
Prototype (signature):
void credit(int anAmount)
Implementation (C++):
class Account
public:
void credit(int anAmount) {
theBalance += anAmount;
}
};
3
PASSING ARGUMENTS IN C++
pass by reference
4
PASSING A PRIMITIVE TYPE ARGUMENT BY VALUE
5
PASSING A PRIMITIVE TYPE ARGUMENT BY POINTER
6
PASSING A PRIMITIVE TYPE ARGUMENT BY REFERENCE
7
PASSING A CLASS TYPE ARGUMENT BY VALUE
8
PASSING A CLASS TYPE ARGUMENT BY POINTER
any changes made to the object inside the called function can become visible
in the calling function
void main() {
User* p = new User("Xeno", 89); //(A)
g(p); //(B)
cout << p->name << " " << p->age << endl; // Xeno 89
h(p); //(C)
cout << p->name << " " << p->age << endl; // Yuki 200
}
void g(User* q) { //(D)
q = new User("Yuki", 200); //(E)
}
void h(User* q) { //(F)
q->name = "Yuki"; //(G)
q->age = 200; //(H)
}
9
PASSING A CLASS TYPE ARGUMENT BY REFERENCE
10
PASSING ARGUMENTS IN JAVA
11
PASSING A PRIMITIVE TYPE ARGUMENT BY VALUE
12
PASSING A CLASS TYPE ARGUMENT BY VALUE OF
OBJECT REFERENCE
different from passing a class type argument by value in C++
similar to the case of passing a class type argument by pointer in C++
class Test {
public static void main(String [] args) {
User u = new User( "Xeno", 89); //(A)
g(u); //(B)
System.out.println(u.name + " " + u.age); // Yuki 200
}
static void g(User v) { //(C)
v.name = "Yuki"; //(D)
v .age = 200; //(E)
}
}
13
PASS-ARGUMENT-BY-VALUE-OF-OBJECT-REFERENCE IN
JAVA VS. PASS-ARGUMENT-BY-REFERENCE IN C++
the argument passing mode in Java does not at all work like the pass-by-
reference mode in C++
class Test {
public static void main(String[] args) {
User u1 = new User("Xeno", 95); //(A)
User u2 = new User("Yuki", 98); //(B)
swap(u1, u2); //(C)
System.out.println(u1.name); // Xeno
System.out.println(u2.name); // Yuki
}
static void swap(User s, User t) { //(D)
User temp = s;
s = t;
t = temp;
}
}
14
PASS-ARGUMENT-BY-VALUE-OF-OBJECT-REFERENCE IN
JAVA VS. PASS-ARGUMENT-BY-REFERENCE IN C++ (CONT.)
void main() {
User u1("Xeno", 95); //(A)
User u2("Yuki", 98); //(B)
swap(u1, u2); //(C)
cout << u1.name << endl; // Yuki
cout << u2.name << endl; // Xeno
return 0;
}
void swap(User& s, User& t) { //(D)
User temp = s; s = t; t = temp;
}
The references s and t in line (D) become aliases for the objects
u1 and u2 of main
15
SUMMARY: DIFFERENCES BETWEEN C++ AND JAVA
16
FUNCTION OVERLOADING IN C++
use the same name with a different number and/or types of arguments
class Account {
string theNumber;
int theBalance;
public:
Account() {
theNumber = “ACB123”;
theBalance = 0;
}
Account(string number, int balance) {
theNumber = number;
theBalance = balance;
}
}
17
FUNCTION OVERLOADING IN JAVA
class Account {
private String theNumber;
private int theBalance;
public Account() {
theNumber = “ACB123”;
theBalance = 0;
}
public Account(String number, int balance) {
theNumber = number;
theBalance = balance;
}
}
18
OBJECT DESTRUCTION
When objects go out of scope in C++, they are
automatically destroyed by the invocation of
their destructors
A destructor is given the name of the class
prefixed with a ~
If no variables in a Java program are holding
references to an object, that object becomes a
candidate for what is known as garbage
collection
19