Week 4 Lecture Summary for CSC 309

These notes are not intended to be complete. They serve as an outline to the class and as a supplement to the text.

The Stack vs The Heap

Example

int *count = new int;
*count = 7;
delete count;

double *score = new double;
*score = 76.6;
score = new double;  // memory leak;


Dynamic Arrays


Classes and Objects


Defining a Class

Consider an ADT used to create Rectangle Objects

class Rectangle 
{
   // data declarations and operations go here
};

Defining an Object

You define an object of your new type similar in form to the built in types.

int count;
string name;
Rectangle room;
Rectangle yards[100]; // creates 100 rectangle objects.

The class definition must come before the creation of any objects.


The Complete Rectangle ADT

class Rectangle 
{
    private:
        double length;
        double width;
        
    public: 
        // Constructors
        Rectangle(){
            length = 0.0;
            width = 0.0;
        }
        Rectangle(double l, double w) {
            length = l;
            width = w;
        }
        // Accessors
        double getLength(){
            return length;
        }
        double getWidth(){
            return width;
        }
        
        // Mutators or Transformers
        void setLength(double l){
            length = l;
        }
        void setWidth(double w){
            width = w;
        }
        // Other operations
        double computeArea(){
            return length * width;
        }
        double computePerimeter(){
            return 2 * length + 2 * width;
        }
};
    
int main(){
    Rectangle room(45,20);
    
    double w = room.width;        // error width is private
    w = room.getWidth();          // okay, returns 20   
     
    Rectangle square(5,5);
    cout << square.computeArea();  // prints 25;
    
    Rectangle yard;
    cout << yard.computePerimeter(); // prints 0
    
    yard.setLength(3);
    yard.setWidth(10);
    cout << yard.computePerimeter(); // prints 26

    computeArea();           // error, not a global function;
        
    system("pause");
    return 0;
}

Defining Class Methods

class Rectangle 
{
    private:
        double length;
        double width;
        
    public: 
        // Constructors
        Rectangle();
        Rectangle(double l, double w);
        // Accessors
        double getLength();
        double getWidth();  
             
        // Mutators or Transformers
        void setLength(double l);          
        void setWidth(double w); 
                  
        // Other operations
        double computeArea();          
        double computePerimeter();           
};

Rectangle::Rectangle()
{
    length = 0.0;
    width = 0.0;
}
Rectangle::Rectangle(double l, double w)
{
    length = l;
    width = w;
}

double Rectangle::getLength(){
    return length;
}
void Rectangle::setLength(double l){
    length = l;
}

:
:



An IntegerSet ADT

Design a class called IntegerSet that has an array of type boolean as an instance member. Each IntegerSet object will represent a set of integers in the range 0 - 100 (inclusive). Instead of storing the integers in an integer array, we will use an array of type boolean. An integer i is in the set if elements[i] is true, where elements is the name of your array.

 
      If 50 is in the set then elements[50] would be  true 
      If 50 is not in the set then elements[50] would be false.
 
 

Before we design the IntegerSet class, it is helpful to look at some test code.

 IntegerSet set;
 set.add(3);
 set.add(5);
 set.add(9):
 set.contains(5);  // returns true
 set.toString();   // returns 3 5 9
 

A Complex Number Class (ADT)

Design a class that can be used to create Complex number objects. A complex number is a number of the form.

a + bi, where a,b are real numbers and i^2. = -1.
a is called the real part and b the imaginary part.

Examples

2 + 3i
1.5 + -4.5i
3.0 + 0i
Complex numbers can be added and subtracted.
(2 + 3i) + (5 + 6i) = 7 + 9i
(3 + 5i) - (1 + 3i) = 2 + 2i

Also two complex numbers are equal if they have the same real and imaginary parts.

(2 + 3i) is equal to (2 + 3i)

Notice that 'i' is just a placeholder used to separate the real part (2) from the imaginary part(3).

Before we design the Complex number class, it is helpful to look at some test code.

Complex c1(2,3), c2(1.5, -4.5), c3(3.0), c4, c5;

cout << c1.toString();    // prints   2 + 3i
cout << c3.toString();    // prints   3
cout << c4.toString();    // prints   0

c5 = c1.plus(c2);
cout << c5.toString();    // prints   3.5 + -1.5

c4.setReal(2).setImag(3);
cout << c4.toString();    // prints   2 + 3i

bool isEqual = c1.equals(c4);   // returns true;
     isEqual = c1.equals(c2);   // returns false;