lec 9: classes

administrivia

late days - you don’t have to ask or tell us

common error from Monday: scope & lifetime

hw2 out, due in two weeks

sections tomorrow: C++ I - const, references, inlining

read “C++ Primer” for details—we won’t have time in class to cover everything useful

today’s plan

operator overloading

references

classes

hello 42

#include <iostream>
#include <cstdlib>
int main(int argc, char **argv) {
  const char *s = "hello";
  int x = 42;
  std::cout << s << " " << x << std::endl;
  return EXIT_SUCCESS;
}
  • C++ standard headers
  • namespace std
  • operator <<, cout, endl

q: how does g++ implement this?

exercise: hello 42 from scratch

#include <stdio.h>

struct stream {
  FILE *fp;
};

stream print_int(stream os, int x) {
  ...
}

int main(void) {
  stream out = {stdout};
  print_int(out, 42);
}

example

pointers revisited

#include <iostream>

void swap(int *x, int *y) {
  int t = *x;
  *x = *y;
  *y = t;
}

int main(int argc, char **argv) {
  int a, b;
  std::cin >> a >> b;
  if (!std::cin) {
    std::cerr << "invalid input!" << std::endl;
    return -1;
  }
  swap(&a, &b);
  std::cout << a << " " << b << std::endl;
}

pass-by-reference

#include <iostream>

void swap(int &x, int &y) {
  int t = x;
  x = y;
  y = t;
}

int main(int argc, char **argv) {
  int a, b;
  std::cin >> a >> b;
  if (!std::cin) {
    std::cerr << "invalid input!" << std::endl;
    return -1;
  }
  swap(a, b);
  std::cout << a << " " << b << std::endl;
}

pass-by-reference sum-up

& (ampersand): address-of or reference

client passes in an argument with normal syntax

function uses reference parameters with normal syntax

modifying a reference parameter modifies the caller’s argument

one more reference

1   int x = 5, y = 10;
2   int &z = x;
3   z += 1;
4   x += 1;
5   z = y;
6   z += 1;

q: what are the values of x, y, and z at each line?

q: how does g++ implement this?

style: which do you prefer?

bool CalcArea(int width, int height, int *area) {
  if (width < 0 || height < 0) return false;
  *area = width * height;
  return true;
}
...
int w = 10, h = 20, a;
CalcArea(w, h, &a);
bool CalcArea(int width, int height, int &area) {
  if (width < 0 || height < 0) return false;
  area = width * height;
  return true;
}
...
int w = 10, h = 20, a;
CalcArea(w, h, a);

see also: C#’s ref and out

reference sum-up

a reference acts like an alias for some other variable

alias: another name that is bound to the aliased variable

mutating a reference is mutating the referenced variable

classes

class declaration syntax (often in a .h file)

class Name {
 private:
  members;
 public:
  members;
};

class member definition syntax (in a .cc file)

returntype classname::methodname(parameters) {
  statements;
}

You can name your .cc/.h files anything

exercise: point

#pragma once

class Point {
 public:
  Point(const int x, const int y);       // constructor
  int get_x() const { return x_; }       // inline member function
  int get_y() const { return y_; }       // inline member function
  double Distance(const Point &p) const; // member function
private:
  int x_;  // data member
  int y_;  // data member
}; // class Point

Point operator +(Point a, Point b);

q: what does each of the 8 consts mean?

finish point.cc

struct vs class

  • C: only struct
    • a struct contains only fields; no methods
    • does not have public/private/protected
  • C++: struct and class are (almost) the same
    • both can contain fields & methods
    • both can have public/private/protected
    • struct: default public, class: default private

self-exercise 1

Write a C++ program that

  • has a class representing a 3-dimensional point
  • has the following methods:
    • return the inner product of two 3d points
    • return the distance between two 3d points
    • accessors and mutators for the x, y, z coordinates

self-exercise 2

Write a C++ program that:

  • has a class representing a 3-dimensional box
    • use your exercise 1 class representing 3d points to store the coordinates of the verticies that define it
    • assume the box has right-angles only and its faces are parallel to the axes, so you only need two vertices to define it
  • has the following methods:
    • test if one box is inside another box
    • return the volume of a box
    • handles <<, =, and a copy constructor
    • uses const in all the right places

soln

see you on Friday

MapReduce