------------------------------------------------------------------------------
MC logo
Copy v. Reference Assignment
[^] Semantics
------------------------------------------------------------------------------
[Ch. 1: Overview and History] [Syntax] [Names and Scope] [Types and Type Systems] [Semantics] [Functions] [Memory Management] [Imperitive Programs and Functional Abstraction] [Modular and Class Abstraction] [Functional Programming] [Logic Programming]
[Copy v. Reference Assignment] [Return Value and Exceptional I/O]
#include <iostream>

class Joe {
private:
        int val;
public:
        Joe(int v = 0) { val = v; }
        void set(int v) { val = v; }
        int get() { return val; }
};

int main(int argc, char **argv)
{
        Joe j1(1), j2, j3;
        j2 = j1;
        j3 = j1;

        j1.set(10);
        j2.set(20);
        j3.set(30);
        
        std::cout << "j1: " << j1.get() 
                  << ", j2: " << j2.get()
                  << ", j3: " << j3.get() 
                  << std::endl;
}
class Joe {
    private int val;
    public Joe() { val = 0; }
    public Joe(int v) { val = v; }
    public void set(int v) { val = v; }
    public int get() { return val; }
}

class Corr {
    public static void main(String[] argv)
    {
        Joe j1 = new Joe(1), j2, j3;
        j2 = j1;
        j3 = j1;

        j1.set(10);
        j2.set(20);
        j3.set(30);
        
        System.out.println("j1: " + j1.get() + 
                           ", j2: " + j2.get() + 
                           ", j3: " + j3.get());
    }
}
Output: j1: 10, j2: 20, j3: 30 Output: j1: 30, j2: 30, j3: 30