CS253

CS253: Software Development with C++

Spring 2009

Alpha Op Lab

Alphabetic Operators Lab

Description

The files for this lab are in ~cs253/pub/Labs/AlphaOpLab.

As you know, C++ operators can be overloaded. However, you can’t create any new operators. You can’t create a statement like this:

    x = y $ z;

because there is no operator $. There just isn’t, and you can’t create it. However, one can create new operators, well, sort of.

infix.cc

Consider the file infix.cc. Compile it (avoid -Wall) and execute it.

Look at main(). It uses <MAX> and <zap> as if they were operators, like + or -. Clearly, this is impossible, yet it works.

Look at class MyMax and class Purge. These are functor classes. What do their functions do?

Now, look at these lines:

    AlphaOp<MyMax> MAX;
    AlphaOp<Purge> zap;

The first declares an object called MAX, of type AlphaOp<MyMax>.

AlphaOp.h

Now, consider AlphaOp.h. Figure it out.

Exercises

  1. Add a functor of your own design to infix.cc, bind it to <foo>, and make it work.

For Extra Fame & Glory

Get this to work using -Wall.

Modified: 2009-04-27T13:25

User: Guest

Check: HTML CSS
Edit History Source
Apply to CSU | Contact CSU | Disclaimer | Equal Opportunity
Colorado State University, Fort Collins, CO 80523 USA
© 2015 Colorado State University
CS Building