CS 2150 Roadmap

Data Representation

Program Representation

 
 
string
 
 
 
int x[3]
 
 
 
char x
 
 
 
0x9cd0f0ad
 
 
 
01101011
vertical red double arrow  
Objects
 
Arrays
 
Primitive types
 
Addresses
 
bits
           
 
Java code
 
 
C++ code
 
 
C code
 
 
x86 code
 
 
IBCM
 
 
hexadecimal
vertical green double arrow  
High-level language
 
Low-level language
 
Assembly language
 
Machine code

Background: virtual method tables

class A {
    virtual void foo()
               { ... }
}

class B : public A {
    virtual void foo()
               { ... }
}
void main () {
    int which = rand() % 2;
    A *bar;
    if ( which )
        bar = new A();
    else
        bar = new B();
    bar->foo();
    return 0;
}
  • Which foo() is called?
  • It's not known until run-time!

Virtual method table example

  • In main():
    Person *p = new Student();
  • First the Person constructor is called...
    • And creates a regular Person object...
    • Then sets up the virtual method table (VMT)...
  • Then the Student constructor is called...
    • And updates the object...
    • Then updates the VMT...
spacer virtual method table image 1 virtual method table image 2 virtual method table image 3 virtual method table image 4

Uniform initialization

class Timer {
  public:
    Timer();
};
class TimeKeeper {
   public:
     TimeKeeper(const Timer& t);
     int get_time();
};

int main() {
  // Look at the 'Timer()' in 
  // the next line...
  TimeKeeper time_keeper(Timer());
  return time_keeper.get_time();
}
  • Is the indicated part:
  1. A variable definition for time_keeper that takes in a constructed Timer object
  2. A time_keeper function that takes in a function as a parameter?
  • Most people want the first; the compiler insists on the second

Uniform initialization

class Timer {
  public:
    Timer();
};
class TimeKeeper {
   public:
     TimeKeeper(const Timer& t);
     int get_time();
};

int main() {
  // Look at the 'Timer()' in 
  // the next line...
  TimeKeeper time_keeper(Timer());
  return time_keeper.get_time();
}
  • The new standard will use curly-brackets for both constructor calls and initialization lists (e.g., arrays)
     
  • So we use the following:
TimeKeeper
  time_keeper
  {Timer()};