Object-oriented Programming in C++
C++
- A superset of C
-
with some powerful new features
- Exceptions
-
to signal errors in a standardized way
- Class templates
-
a type parameterization mechanism
- Operator overloading
-
a technique for defining standard operators (like ==)
for user-defined types
Exceptions
Class templates
- A powerful mechanism for code reuse.
- A parameterized skeleton for a class.
- Parameter values are types.
- A template defines a family of classes:
one for each parameter value.
Operator overloading
Allows syntax of builtin operators
to be applied to user-defined types.
Definition
int operator==(C& lhs,C& rhs)
{
// compare lhs and rhs here
// return 1 if they are the same; 0 otherwise
}
Use
C c0,c1;
if (c0 == c1) // invokes code above
...
Examples
- IntSet
-
simple class with exceptions
- Set
-
simple class template
- OpIntSet
-
IntSet with operator==
IntSet.h
const int MAXSIZE = 100;
// exception classes
class DuplicateExc {};
class FullExc {};
class NotFoundExc {};
class IntSet {
public:
IntSet();
void add(int);
void remove(int);
int isMember(int) const;
int size() const;
protected:
int findPos(int) const;
int s[MAXSIZE];
int curSize;
};
IntSet.C
#include "IntSet.h"
IntSet::IntSet()
{
curSize = 0;
}
void IntSet::add(int x)
{
if (findPos(x) >= 0) {
throw DuplicateExc();
} else if (curSize == MAXSIZE) {
throw FullExc();
}
s[curSize++] = x;
}
IntSetDriver.C
#include <iostream.h>
#include "IntSet.h"
int main()
{
IntSet s;
s.add(1);
s.add(2);
s.add(3);
cout << "isMember(2): " << s.isMember(2) << endl;
s.remove(2);
cout << "isMember(2): " << s.isMember(2) << endl;
try {
s.remove(2);
}
catch (NotFoundExc) {
cout << "NotFoundExc caught" << endl;
}
}
Set.h
const int MAXSIZE = 100;
// exception classes
class DuplicateExc {};
class FullExc {};
class NotFoundExc {};
template class Set {
public:
Set();
void add(Element);
void remove(Element);
int isMember(Element) const;
int size() const;
protected:
int findPos(Element) const;
Element s[MAXSIZE];
int curSize;
};
Set.C
template Set::Set()
{
curSize = 0;
}
template void Set::add(Element x)
{
if (findPos(x) >= 0) {
throw DuplicateExc();
} else if (curSize == MAXSIZE) {
throw FullExc();
}
s[curSize++] = x;
}
SetDriver.C
#include <iostream.h>
#include "Set.h"
int main()
{
Set s;
s.add(1.4142);
s.add(1.732);
s.add(3.1416);
cout << "isMember(1.732): " << s.isMember(1.732) << endl;
s.remove(1.732);
cout << "isMember(1.732): " << s.isMember(1.732) << endl;
}
OpIntSet.h
#include "IntSet.h"
class OpIntSet : public IntSet {
public:
friend int operator==(const OpIntSet&,const OpIntSet&);
};
OpIntSet.C
#include "OpIntSet.h"
int operator==(const OpIntSet& lhs,const OpIntSet& rhs)
{
if (lhs.curSize != rhs.curSize)
return 0;
for (int i = 0; i < lhs.curSize; i++)
if (rhs.findPos(lhs.s[i]) < 0)
return 0;
return 1;
}
OpIntSetDriver.C
#include <iostream.h>
#include "OpIntSet.h"
int main()
{
OpIntSet s0,s1;
s0.add(1); s1.add(1);
s0.add(2); s1.add(2);
s0.add(3); s1.add(3);
cout << "s0 == s1: " << (s0 == s1) << endl;
s0.remove(2);
cout << "s0 == s1: " << (s0 == s1) << endl;
}