Exercise 2:

Submit your solutions (source code) to the questions below by email to your instructor and TA(s) by Monday, October 24th (16:30).

Question 1: An array-based stack (50 points).

In this question, we are going to implement a class Stack. We will use an array as a container for the elements of the stack. Elements of the stack will be of type 'int'.

Create the files: "Stack.h", "Stack.cpp" and "test_stack.cpp". "Stack.h" will contain the definition of the class Stack. "Stack.cpp" will contain the implementation of the methods of the class Stack. "test_stack.cpp" will contain the main function that will test our created class.

Step 1: Type the following code in the file "Stack.h":

// Stack.h
#ifndef STACK_H
#define STACK_H

class Stack {
public:
  Stack(int N) : max_size(N), size(0), data(new int[N]), top(-1) {}
  ~Stack() { delete[] data; }

  // push the element el in the stack
  // print an err mesg if trying to push an element in a full stack
  void push(int el); 

  // pop the element on top of the stack and return it
  // print an err mesg if trying to pop from an empty stack and returns a dummy int
  int pop(); 
  bool is_full(); // return true if stack is full
  bool is_empty(); // return true if stack is empty
  int num_elements(); // return the num of elements in the stack

private:
  int size;
  int max_size;
  int top; // top is the index to the top el of the stack
  int* data;
};

#endif // STACK_H

Step 2: Implement the methods push, pop, is_full, is_empty and num_elements in the file "Stack.cpp"

Step 3: Type the following code in the file "test_stack.cpp". We will use this code to test our stack implementation.

// test_stack.cpp
#include <iostream>
#include "Stack.h"

int main() {
  Stack s(5);

  if (s.is_empty()) std::cout << "Empty stack" << std::endl;

  s.push(2);
  s.push(5);
  s.push(7);
  s.push(9);
  s.push(9);
  
  if (s.is_full) std::cout << "full stack" << std::endl;

  std::cout << "Num of elements: " << s.num_elements() << std::endl;

  int t;
  while (!s.is_empty()) {
   t = s.pop();		
   std::cout << t << std::endl;
   std::cout << "Num of elements: " << s.num_elements() << std::endl;
  }
}

Question 2: Static members (50 points).

In this question, we are going to review how to use static variables (or static member data or class variable) and static methods (or class methods). We are going to create a class Car that produces a car. Our class Car will keep track of how many cars are produced by using a static variable: "num_car_produced". Users of the class Car can access the number of cars produced so far by the static method: "get_num_car_produced()".

Create the files: "Car.h", "Car.cpp" and "test_car.cpp". Type the following code in the file "Car.h":

// Car.h

#include <string>

#ifndef CAR_H
#define CAR_H
class Car {
public:
  // construct a car using the brand and name
  // Ex: Car c("Ferrari", "F40");
  Car(const std::string& brand, const std::string& name) 
                    : _brand(brand), _name(name) { num_car_produced++; }
  ~Car() {}

  std::string get_brand(); // return the brand 
  std::string get_name();  // return the name

  static int get_num_car_produced(); // return the num of produced cars so far

private:
  std::string _brand;
  std::string _name;
  
  static int num_car_produced; // class variable: total num of cars produced so far
};
#endif // CAR_H

Write the implementation of the methods: get_brand(), get_name() and get_num_car_produced() in the file "Car.cpp". You also need to initialize the class variable "num_car_produced".

Write a main function with some code to test your class Car in the class "test_car.cpp". You can complete the following code (or you can also write your own test class if you prefer):

// test_car.cpp

// Include the necessary headers here

int main ()
{
  std::cout << "Num of cars produced: " 
            << /* call the class method get_num_car_produced() here */ 
            << std::endl;
  
  Car f40("Ferrari", "F40");
  Car flamb("Lamborgini", "Countach");

  std::cout << f40.get_brand() << " " << f40.get_name() 
            << std::endl;

  std::cout << "Num of cars produced so far: "
            << /* call the class method get_num_car_produced() here */ 
            << std::endl;
}