1-2) Simple Objects and Classes

revisions:
Sept. 17,2007 modified code example to compile with Visual C++ 2005 (changed .h and .cpp and includes structure)



Here is the world's simplest C++ program using classes. We will use this as a starting place to explore more and more advanced concepts in C++ and better and better ways to write C++ code.

In this program there is no heap storage yet and everything is pass by value, except the output stream operator.
This example illustrates the creation of classes and a simple container class.


Part 1) Simple Classes and Objects

1) Create a user defined type such as BankAccount with some attributes
(e.g. owner, title, balance).
 

2)Create a Vector class that supports the following
operations.

BankAccount elementAt(int index)
    //return  element at location index.
addElementAt(BankAccount b, int index)
    //add b to vector a location index

3) Create an experiment (main  program) add some BankAccounts
to your Vector then print out the contents of the Vector.

4) Modify your main program to determine if the original object is
retrieved or if a copy retrieved. (If the original object and the
one retrieved have the same address they are they same)

if(&v.elementAt(i) == &b1)
  //they are the same object
else
  //they are different objects

Advanced
5) Can you modify your Vector so that the original object is
indeed returned when an element is retrieved


Sample Code

#include <string.h>

const int MAX_CHAR = 10;

class BankAccount {
public:
        BankAccount ( char* name ="unknown",
                      char * mr = "Miss", float y=0.0)
        {
          title = mr;
          strcpy(owner, name);
          balance = y;
        }
        void print(ostream& o)
        {  o <<"who:"<< title << " "
             << owner<<" how much="<< balance<<"\n"; }

private:
     char * title;
     char owner[MAX_CHAR];
     float balance;
};

//notice use of references -to be discussed later
ostream& operator<< (ostream& o, BankAccount &b)  //notice pass by reference
{
 b.print(o);
 o << endl;
 return o;
}



//The container class

typedef BankAccount T;
class  Vector {
 public:
  Vector (int len = 100) {
       cout << "Vector(int)\n";
                mysize = len;
                buffer = new  T[len];
        }
  ~Vector (void) { delete  [] buffer; }
  int capacity (void) { return mysize; }
  void addElementAt(T x, int index)
    { if(index < mysize) buffer[index] = x;}
  T elementAt(int index)
    { return buffer[index] ;}

 private:
  int mysize;
  T *buffer;
};



#include <iostream>

using namespace std;


#include "bankaccount.h"
#include "vector.h"

const int MAX_BANK_ACCTS = 80;

int main (void)
{
 Vector v (MAX_BANK_ACCTS);
 BankAccount b1("Sue", "Miss", 100.00);
 BankAccount b2("Lou", "Mr", 500.00);
 v.addElementAt(b1, 0);
 v.addElementAt(b2, 1);
 for(int i = 0; i<2; i++)
    cout << v.elementAt(i);

 if (&v.elementAt(0) == &b1) cout << "the same!!!\n";
   else cout << "the objects are copied\n";

 // BankAccount b3("Mary", 1000.00); would be an error
 BankAccount b3("Mary");
 v.elementAt(1) = b3;
 v.elementAt(2) = BankAccount("Sam", "Mr", 300.00);
 for(i = 0; i<2; i++)
    cout << v.elementAt(i);
 

 return 0;
}



Program Output
Vector(int)
who:Miss Sue how much=100

who:Mr Lou how much=500

the objects are copied
who:Miss Sue how much=100

who:Mr Lou how much=500


1) Modify the Container so that same original object is returned
Here all we have to do is change the typedef to hold pointers instead.
//The container class

typedef BankAccount* T;
class  Vector {
 public:
  Vector (int len = 100) {
       cout << "Vector(int)\n";
                mysize = len;
                buffer = new  T[len];
        }
   ~Vector (void) { delete  [] buffer; }
   int capacity (void) { return mysize; }
   void addElementAt(T x, int index)
   { if(index < mysize) buffer[index] = x;}
   T elementAt(int index)
   { return buffer[index] ;}

 private:
  int mysize;
  T *buffer;
};



#include <iostream.h>

using namespace std;
#include "bankaccount.h"
#include "vector.h"

const int MAX_BANK_ACCTS = 80;

int main (void)
//Notice this code now works in terms of pointers
{
 Vector v (MAX_BANK_ACCTS);
 BankAccount b1("Sue", "Miss", 100.00);
 BankAccount b2("Lou", "Mr", 500.00);
 v.addElementAt(&b1, 0);
 v.addElementAt(&b2, 1);
 for(int i = 0; i<2; i++)
    cout << *v.elementAt(i);

 if (v.elementAt(0) == &b1) cout << "the same!!!\n";
   else cout << "the objects are copied\n";

 // BankAccount b3("Mary", 1000.00); would be an error
 BankAccount b3("Mary");
 *v.elementAt(0) = b3;
 *v.elementAt(1) = BankAccount("Sam", "Mr", 300.00);
 for(i = 0; i<2; i++)
    cout << *v.elementAt(i);
 

 return 0;
}

/* OUTPUT

Vector(int)
who:Miss Sue how much=100

who:Mr Lou how much=500

the same!!!
who:Miss Sue how much=100

who:Miss Mary how much=0

*/