Last modified 3 years ago Last modified on 2015-03-11 13:36:22

Demot » Demo C2, 4.5.2015

You have to do a minimum of 60% of all the tasks in demo C1 and C2. A minimum of 40% of tasks must be done in each demo (C1 and C2). Course is graded pass / fail. The maximum tasks for each demo is 10, so to pass this course, you have to do a minimum of 12 tasks. Return using Nettidemowww.

At least two of these tasks must be tested using automated tests (Comtest for example).

Task 1. Bus

Make class cBus, which has the number of seats and the number of free seats. Make methods print, add and decrease to change the number of passengers. Use the following main:

int main(void)
  cBus pikkubussi(10),isobussi(45);
  pikkubussi.add(4); pikkubussi.print();
  isobussi.add(30);  isobussi.print();
  int over = pikkubussi.add(15);
  pikkubussi.print(); isobussi.print();
  if ( isobussi.hasFreeSeats() ) 
    cout << "Isoon bussiin mahtuu!" << endl;
  return 0;

Task 2. Vehicles

Make class cVehicle, which has at least the speed of the vehicle and the number of passengers. Inherit classes cShip and cAirplane from cVehicle, which both have some special attributes compared to cVehicle. Make a test main program.

Task 3. Currencies

Change valuutts.cpp's class cValuutat so that instead of inheriting class use aggregation. You are not allowed to make changes to any other classes. Tips (in finnish).

Task 4*. Arrays

Make taul_d.cpp method set, which sets an array to another array:

int main(void)
  cTaulukko numbers(7);
  numbers.lisaa(0); numbers.lisaa(2);
  cout << numbers << endl;  // 0 2
  cTaulukko taul;
  taul.set(numbers);   // or even  taul = luvut;
  cout << taul << endl;  // prints same as before
  return 0;

Class can be tested for example using the following test ComTestC++:lla

#include <sstream>
using std::ostringstream;

 * Luokka dynaamiselle int-taulukolle
 * @code
 * <pre name="test">
 *   ostringstream ss;
 *   cTaulukko luvut(10);
 *   ss << luvut; ss.str() === "";
 *   luvut.lisaa(0); luvut.lisaa(2); luvut.lisaa(2);
 *   luvut.lisaa(4); luvut.lisaa(2);
 *   ss << luvut; ss.str() === "0 2 2 4 2 ";       ss.str("");
 *   cTaulukko taul;
 *   taul.set(luvut); 
 *   ss << taul;  ss.str() === "0 2 2 4 2 ";       ss.str("");
 *   taul.lisaa(99);
 *   luvut.lisaa(88);
 *   ss << taul;  ss.str() === "0 2 2 4 2 99 ";    ss.str("");
 *   ss << luvut; ss.str() === "0 2 2 4 2 88 ";    ss.str("");
 * </pre>
 * @endcode

Task 5*. Add operator

Make add operator to the class used in previous task. Add operator should work like this:

  cTaulukko t3 = numbers + taul;
  cout << t3 << endl;    // 0 2 0 2

Evaluate your solution's efficiency. You can have two extra points if you take a look at the C++11 standards Move constructor and Move assignment and use those in your solution.

Task 6-7. Names and sorting

Write a program that keeps asking names until an empty line is written. After asking names the program should print all the names in aplhabetical order. Names are given like firstname surname but they are ordered using surname. Choose a data structure from STL and use it. You can use this as your basis

// Reading a string from user:
 string s;
 cout << "Give string";

Task 8. Solitaire

Change solitaire (pasi.cpp) to a real object oriented program. So change all sctructs to classes and static methods to real methods.

Task 9. Histogram

Change the solitaire program in previous task so that after playing it prints a histogram of the probalities that game ends in certain card:

1 ************************
2 *****************
3 *********
4 ****
5 **
6 *
7 *

In teachers solution the class looked like:

class cHisto {
  int lkm;
  long *esiintymat;
  cHisto(int n);
  int lisaa(int i);
  long etsi_max() const;
  int piirra() const;

Task 10*. Private assignment and copy constructor

Add private assignment operator and copy constructor to cHisto class. (You can do this task even if you don't do task 8 and 9 by doing dummy methods.