Introduction: Classes and Objects

Objects in C - struct

Objects in C - struct

  • Self defined “composite” types

  • Copy is supported by the compiler - but nothing else

    • Explicit assignment

    • Parameter passing

    • Function return value

struct point
{
    int x;
    int y;
};
struct point add_points(
    struct point p1,
    struct point p2)
{
     struct point ret;
     ret.x = p1.x + p2.x;
     ret.y = p1.y + p2.y;
     return ret;
}

Example: struct point

Definition

Usage

struct point
{
    int x;
    int y;
};
struct point add_points(
    struct point rhs,
    struct point lhs);
void add_to_point(
    struct point *rhs,
    struct point lhs);
struct point A = {1,2},
             B = {2,4};
struct point C;

C = add_points(A, B);
add_to_point(&A, B);

struct point - Criticism

Is ``struct`` good enough?

  • Members are public

    • ⟶ Bugs are only a matter of time

    • Counter argument: “Real programmers don’t write bugs”

  • Function just hang around

  • Clean/consistent initialization wanted

    • Contructor (and Destructor)

    • Error checking

  • Self defined operators - e.g. addition of struct point using the addition operator “+”

  • Methods on Objects, like moving a point: A.move(1,2);

Example: class point

Definition

Usage

class point
{
public:
    point(int x, int y);
    int x() const;
    int y() const;
    point& operator+=(
        point addend);
private:
    int _x;
    int _y;
};
point operator+(
    point lhs,
    point rhs);
point A(1,2), B(2,4);
// C++11 init syntax: A{1,2}

point C = A + B;
A += B;

class point, analyzed (1)

Access Specifier

Definition

Usage

class point
{
private:
    int _x; // the underscore is only
            // a stylistic matter
};

This code won’t compile:

int x = A._x;
  • Compiler error: “Access to private member …”

  • Access Specifier: specifies who can call a method or access a member

    • public: access allowed from everywhere

    • private: access only from withni methods of the same class

    • protected: access only from within methods of same or derived class (⟶ inheritance)

class point, analyzed (2)

Access Specifier and Access Methods

Definition

Usage

class point
{
public:
    int x() const { return _x; }
private:
    // ...
};
int x = A.x();
  • Public Access ⟶ compiler does not complain

  • Access Specifier: matter of taste (“Design”)

    • Public Member Access: everybody could modify everything ⟶ C

    • Access Methods: read-only member access ⟶ inline

  • const: x() does not modify the object ⟶ excellent type system

class point, analyzed (3)

Constuctors

Definition

Usage

class point
{
public:
    point(int x, int y);
};
point A(1,2);
  • Constuctor: initializes the object

  • Here: initialization of the members x and y

  • Multiple constuctors possible

class point, analyzed (4)

Operators

Definition

Usage

class point
{
public:
    point& operator+=(point addend);
};
A += B;
C = A += B;
  • Operator Overloading

  • A += B has the value of A after assignment

class point, analyzed (5)

Operators

Definition

Usage

class point
{
    // ...
};
point operator+(point lhs, point rhs);
C = A + B;
  • Operator “+=” modifies an object (left hand side) ⟶ member (defined in class scope)

  • Operator “+” creates a new object ⟶ global (defined in global scope)

Terminology

One says:

class point
{
    // ...
};
  • point is a type …

  • … a class of objects

point A(1,2), B(3,4);
  • A and B are instances of class point

  • Instance is a synonym for object