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
- allows base class methods to be `replaced' (overridden)
by derived class (even when method is used in the base class)
- virtual only needed in base class
(the same methods in derived class are implicitly virtual)
- can't have virtual instance variables
- a class with a virtual method must
define the method (unless...)
Abstract Methods:
Created using the syntax:
virtual myMethod(...) = 0;
- allows base class to specify an interface
without implementing it.
- forces derived class to implement it
(unless they declare the method as abstract).
- all abstract base classes should have
a virtual destructor
to allow the subclass destructor to be called
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;
};
- used for optimization
- compiler can generate inline code instead of
a function call (if it wants to)
- for inlining to take effect, function definition
must occur before function is used.
Operator Overloading
- used to specify a method or function to call when
a specified operator is applied to specified types
- function syntax:
ret-type operator unary-op(lhs-type)
ret-type operator binary-op(lhs-type,
rhs-type)
- method syntax:
class cname {
...
ret-type operator unary-op();
ret-type operator binary-op(rhs-type);
...
};
The left hand side is an instance of the class.
- exception: unary postfix ++, --:
// Member function:
ret-type operator ++(int);
// Regular function:
ret-type operator ++(lhs-type, int);
the int argument is always 0 - a kludge to distinguish
prefix from postfix
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)
- commonly overloaded operators:
- =: to control object manipulation (eg, potential loss of memory)
- <<, >>: objects for which I/O is
reasonable
- +,-,*,/: arithmetic type
objects (e.g., coordinates, complex numbers, matrices)
- []: array like objects (e.g., vectors, matrices,
associative arrays)
- new, delete: when speed of allocation/deallocation is very important
- order of binary operator operands is important
- operators that can't be overloaded:
:: . .* : ?
(there are 42 that can)
- operator overloading can lead to loss of readability - should be
used carefully
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;
- static keyword used to declare
class methods and variables
- no instance needed to call a class method
(e.g., use Stuff::getNInstances())
- one copy of class variables, shared by all instances.
[Previous Lecture]
[Lecture Index]
[Next Lecture]