The Command Line, Classes

Tom Kelliher, CS 320

Mar. 10, 2000

Administrivia

Announcements

Don't forget \\ phoenix.

Jill's environment class???

Save copies of files before making changes.

Make a few changes; test.

Assignment

Implement stack class, as defined by stack.h. Develop a set of tests for the class.

From Last Time

Outline

  1. Using command line arguments.

  2. Classes: why, sum, stack.

Coming Up

Maze design.

The Command Line

  1. Setting them from the IDE:
    Project | Settings | Debug | Program arguments
    
    Example: 20 30
    No commas.

  2. Accessing command line args from main():
    int main(int argc, char *argv[])
    {
       /* ... */
    
       return 0;   /* "Normal" return value. */
    }
    
    argc, argv (argv[0] --- program name)?

  3. atoi() --- stdlib.h.

Classes

Why?

  1. Abstract data types. Improvement over the primitive types.

  2. Data hiding.

  3. Implementation hiding.

Sum

sum.h:

#ifndef SUM_H
#define SUM_H

class sum
{
 private:   // Default protection.  Also protected.

   int theSum;

 public:

   sum(int, int);
   int get(void);
};

#endif

sum.cpp:

#include "sum.h"

sum::sum(int a, int b)
{
   theSum = a + b;
}

int sum::get(void)
{
   return theSum;
}

main.cpp:

#include <iostream.h>
#include "sum.h"

int main()
{
   sum s(12, 34);
   sum t(-90, 85);

   cout << "sum s: " << s.get() << endl;
   cout << "sum t: " << t.get() << endl;

   return 0;
}

Implement, build, and run.

Stack

stack.h:

#ifndef STACK_H
#define STACK_H

class stack
{
 private:

   int* s;
   int size;
   int tos;

   void die(char *);

 public:

   stack(int);
   ~stack();
   void push(int);
   int pop(void);
   int full(void);
   int empty(void);
};

#endif



Thomas P. Kelliher
Fri Mar 10 10:21:00 EST 2000
Tom Kelliher