Loading

Basic Classes

Simple Classes


Example 1

#include <iostream>

// A simple 2D point class with only plain old data
Class CPoint
{
public:
    double mdX;
    double mdY;
};

int main() {
    // Create with built-in default constructor
    // that does no initialization
    CPoint qMyPoint;
    // Assign values to members
    qMyPoint.mdX = 7.4;
    qMyPoint.mdY = 2.6;
    
    // Output point coordinates
    std::cout << qMyPoint.mdX << ", " << qMyPoint.mdY << std::endl;
    return 0;
}

Example 2

#include <iostream>

// A simple 2D point class with only plain old data
Class CPoint
{
public:
    double mdX;
    double mdY;
};

int main() {
    // Assigns values to members with an initialization list
    CPoint qOrigin = {0.0, 0.0};
    
    // Initialize a via built-in assignment
    CPoint qOriginCopy1 = qOrigin;
    
    // Initialize via built-in copy constructor
    CPoint qOriginCopy2(qOrigin);
    
    // Create with built-in default constructor
    CPoint qOriginCopy3;
    // Copy via assignment operator
    qOriginCopy3 = qOrigin;
    
    // Output point coordinates for verification
    std::cout << qOrigin.mdX << ", " << qOrigin.mdY << std::endl;
    std::cout << qOriginCopy1.mdX << ", " << qOriginCopy1.mdY << std::endl;
    std::cout << qOriginCopy2.mdX << ", " << qOriginCopy2.mdY << std::endl;
    std::cout << qOriginCopy3.mdX << ", " << qOriginCopy3.mdY << std::endl;
    return 0;
}

Constructors and Destructors


Example 1

#include <iostream>

// A simple 2D point class with a few constructors
Class CPoint
{
public:
    // Default constructor
    CPoint() : mdX(0.0), mdY(0.0) {}
    // Copy constructor
    CPoint(const CPoint& qrPoint) : mdX(qrPoint.mdX), mdY(qrPoint.mdY) {}
    // Constructor using coordinates
    CPoint(double dX, double dY) : mdX(dX), mdY(dY)  {}
    double mdX;
    double mdY;
};

int main() {
    // Instantiate a point with the default constructor (0.0, 0.0)
    CPoint qDefault;
    // Instantiate a point with coordinate values
    CPoint qCoord(7.4, 2.6)
    // Instantiate a point with the copy constructor
    CPoint qCopy(qCoord);
    
    // Output point coordinates for verification
    std::cout << qDefault.mdX << ", " << qDefault.mdY << std::endl;
    std::cout << qCoord.mdX << ", " << qCoord.mdY << std::endl;
    std::cout << qCopy.mdX << ", " << qCopy.mdY << std::endl;
    return 0;
}

Example 2

#include <iostream>

// A simple 2D point class with an explicit copy constructor
Class CPoint
{
public:
    // Default constructor
    CPoint() : mdX(0.0), mdY(0.0) {}
    // Copy constructor—"explicit" disables built-in assignment
    explicit CPoint(const CPoint& qrPoint) {
        // Members assigned values internally
        mdX = qrPoint.mdX;
        mdY = qrPoint.mdY;
    }
    double mdX;
    double mdY;
};

int main() {
    // Instantiate a point with the default constructor
    CPoint qMyPoint;
    // Assign new values to coordinates
    qMyPoint.mdX = 7.4;
    qMyPoint.mdY = 2.6;
    // Instantiate a point with the copy constructor
    CPoint qCopy(qMyPoint);
    
    // Output point coordinates for verification
    std::cout << qMyPoint.mdX << ", " << qMyPoint.mdY << std::endl;
    std::cout << qCopy.mdX << ", " << qCopy.mdY << std::endl;
    return 0;
}

Example 3

#include <iostream>

// A simple int array class with a constructor and destructor
Class CIntArray
{
public:
    // Constructor the takes a size parameter
    explicit CIntArray(const unsigned int kuiArraySize) {
        // Allocate memory for array
        mipArray = new int[kuiArraySize];
    }
    // Destructor
    ~CIntArray() {
        // Deallocate memory
        delete []mipArray;
    }
    int mipArray;
};

int main() {
    // Instantiate an array object
    CIntArray qMyArray(3);
    // Access array elements to assign them values
    qMyArray.mipArray[0] = 60;
    qMyArray.mipArray[1] = 4;
    qMyArray.mipArray[2] = -52;
    
    // Output the entries for verification
    for (unsigned int uiIndex = 0; uiIndex < 3; ++uiIndex) {
        std::cout << qMyArray.mipArray[uiIndex] << "   ";
    }
    std::cout << std::endl;
    return 0;
}

Member Functions


Example 1

#include <iostream>

// A simple 2D point class with private members
Class CPoint
{
public:
    // Constructor using coordinates
    CPoint(double dX, double dY) : mdX(dX), mdY(dY)  {}
    // Constant member function
    void Print() const {
        std::cout << mdX << ", " << mdY << std::endl;
    }
    void SetX(const double kdX) {
        mdX = kdX;
    }
    void SetY(const double kdY) {
        mdX = kdY;
    }
private:
    // Private members only accessible by member functions
    double mdX;
    double mdY;
};
int main() {
    // Instantiate a point with coordinate values
    CPoint qMyPoint(7.4, 2.6);
    // Set the coordinates to new values by caling set functions
    qMyPoint.SetX(5.3);
    qMyPoint.SetY(9.3);
    // Output the coordinates
    qMyPoint.Print();
    // Instantiate a point with coordinate values
    const CPoint qConstPoint(3.5, 7.2);
    // Output the coordinates
    // This call is valid since Print() is a constant function
    qConstPoint.Print();
    return 0;
}

Example 2

#include <iostream>
// A simple 2D point class with private members
Class CPoint
{
public:
    // Constructor using coordinates
    CPoint(double dX, double dY);
    // Inlined and constant function
    inline void Print() const;
    // Non-constant functions
    void SetX(const double kdX);
    // Including the implementation code here is equivalent to
    // adding the keyword inline as we did above for Print().
    void SetY(const double kdY) {
    mdX = kdY;
}
private:
    // Private member only accessible by member functions
    mutable double mdX;
    mutable double mdY;
};
int main() {
    // Instantiate a point with coordinate values
    CPoint qMyPoint(7.4, 2.6);
    // Set the coordinates to new values by calling set functions
    qMyPoint.SetX(5.3);
    qMyPoint.SetY(9.3);
    // Output the coordinates
    qMyPoint.Print();
    // Instantiate a point with coordinate values
    const CPoint qConstPoint(3.5, 7.2);
    // Set the coordinates to new values by calling set functions
    // This is okay even though the object is constant, since the
    // members mdX and mdY are now mutable
    qMyPoint.SetX(3.14);
    qMyPoint.SetY(2.71);
    // Output the coordinates
    qConstPoint.Print();
    // Dynamically allocate a point object
    CPoint* qpNewPoint = new CPoint(3.6, 8.3);
    // Use the arrow operator with pointers
    QpNewPoint->Print();
    // Deallocate the memory when we are done
    delete [] qpNewPoint;
    return 0;
}
// Implementation
// Constructor using coordinates
CPoint::CPoint(double dX, double dY)
    : mdX(dX), mdY(dY)
{}
// The keyword inline is a suggestion to the compiler
inline void CPoint::Print() const {
    std::cout << mdX << ", " << mdY << std::endl;
}
void CPoint::SetX(const double kdX) {
    mdX = kdX;
}