C++ Lecture 6

[Previous Lecture] [Lecture Index] [Next Lecture]

MyPoint.h

#ifndef MY_POINT_H
# define MY_POINT_H

# include "point.h"

class MyPoint : public Point {
  public:
    double abs() {
        return sqrt(x * x + y * y);
    }
};

#endif /* MY_POINT_H */

Virtual Methods


Abstract Methods:

Created using the syntax:
    virtual myMethod(...) = 0;

Inline Functions and Methods

An inline function:
    // Inline keyword needed
    inline int add10(int b) { return b + 10; }
An inline method:
    class Abc {
    public:
	// ...
	// Inline keyword not needed
	int add10() { return b + 10; }
	// ...
    private:
	// ...
	int b;
    };

Operator Overloading


point2.h

#ifndef POINT_H
# define POINT_H

class Point {
  public:
    Point(int x, int y);
    int getX() const;
    int getY() const;
    //...
    Point operator +(const Point &p2) const;
    //...
  private:
    int x, y;
};

ostream &operator<<(ostream &os,
                    const Point &p);

#endif /* POINT_H */

point2.cc

// binary + operator method
Point
Point::operator +(const Point &p2) const
{
    return Point(x + p2.getX(), y + p2.getY());
}

// output function
ostream &
operator<<(ostream &os,
           const Point &p)
{
    return os << '(' << p.getX() << ','
                     << p.getY() << ')';
}

Operator Overloading (Cont'd)


Class Methods & Class Variables

    class Stuff {
    public:
	Stuff() : i(3) {
	    nInstances++;
	}
	// ...
	static int getNInstances();
    private:
	// ...
	int i;
	static int nInstances;
    };
    int
    Stuff::getNInstances()
    {
	return nInstances;
    }

    int Stuff:nInstances = 0;

[Previous Lecture] [Lecture Index] [Next Lecture]