CITS2002 Systems Programming  
prev
next CITS2002 CITS2002 schedule  

Classes with methods defined externally, continued

Here, the class's methods are defined 'away' from their declarations in the header file. Thus, we need to preface each method name with the name of class to which it belongs. To ensure that the defintions are type-compatible with the class's declarations, we use the preprocessor to include the class definition:

Here, we can think of Point as the namespace to which the methods belong:

#include <iostream>
#include "myvector.h"

// myvector.cpp - method implementation

void Point::offset(double offsetX, double offsetY)
{
    x += offsetX; y += offsetY;
}

void Point::print(void)
{
    cout << "(" << x << "," << y << ")";
}

void MyVector::offset(double offsetX, double offsetY) 
{
    start.offset(offsetX, offsetY);

    end.offset(offsetX, offsetY);
}

void MyVector::print(void)
{
    start.print();
    cout << " -> ";
    end.print();
    cout << endl;
}

 


CITS2002 Systems Programming, Lecture 23, p7, 17th October 2023.