Classes in C++

Class Definitions

Object-Oriented C++ Basics

Classes vs. Structs

Organization of program

Object oriented (OO) programming language features

OO Abstraction & modelling

Class Components

Information hiding of class members

Member functions

Syntax for class interface (in *.h file)

class classname {
     // complete member list
};   // semicolon is very important

Constructors and destructors

Constructors are used to initialize objects and each class can have many:

C++ automatically provides a default constructor and a (shallow) copy constructor for every class if you don’t explicitly create any constructors. However, if you define any constructor, default ones are not created automatically for the class.

Copy constructors are invoked

Initializer lists for data members

Constructors using default values

Point p1(3, 4); // use both
Point p2(3);    // use x; y defaults to 0
Point p3;       // both default to 0

Destructors

Other C++ class definition features

Inline function definitions

Instance variables (data members)

The this pointer to current object

Constant member functions

static class members

Class composition - objects in classes

Friend classes

    // in Point.h
            private x, y;            
            friend class Rectangle;

    // in Rectangle.cpp
            Rectangle :: Rectangle (int x1, int y1, int x2, int y2)
            {
                  p1.x = x1;   p1.y = y1; // allowed due to friend declaration above
                  p2.x = x2;   p2.y = y2;
            }

Remember: Friends violate the principle of data encapsulation - use as little as possible!!

Misc points about classes

Operators = and == on objects:

Explicit constructor call creates constant temporary object:

More on storage, lifetimes, scope and references

If a function returns non-reference value, it is a temporary object. If a function returns reference value, it is a name for something else that must have a lifetime outside the function. This list shows what could and could not be returned through a reference return type, with an example below.

Example:

#include <iostream>
int globalint;

int & mult(int & x, int y)
{
    int z;
    static int s;
    
    z = x * y;
    s = z;
    x = z;
    y = z;
    globalint = z;
    
    // return z; // bad, would be a reference to (popped) stack element
    //  return y;  // bad, same reason
    // return x * y; // bad
    
    return x;  // ok, was a reference argument so not popped
    return s;  // ok, static so not on stack
    return globalint;  // ok, also not on stack
}

int main ()
{
    int m, x = 3, y = 5;
    
    m = mult(x, y);
    globalint = 0;
    std::cout << "m=" << m << "  gi=" << globalint << std::endl;
}

Could have function/method return something by reference but constant so that it can’t be changed - particularly useful for classes:

class Point {
public:
    void display() const;
        void setX(int);
...
};

class Rectangle {
public:
    const Point & getTL() { return topleft; };

private:
    Point topleft;
};

// in main

Rectangle r1(1,1,4,4);
Point   p1;

r1.getTL().setX(3);  // not allowed
p1 = r1.getTL();     // ok
r1.getTl().display();  // ok because display is const