pondělí 9. března 2009

Cvičení 2

probráno
  • new, delete, new[], delete[]
  • statické atributy
  • konstruktor, destruktor
  • vytváření pole dynamické velikosti
  • ukázka std::vector a iterátorů

soubor priklad2.cpp

#include <iostream>
#include <iomanip>

#include "car.h"

using namespace std;

int main() {
Vehicle v1("Skoda 105", 2000);
Vehicle v2("Fiat Uno", 12000);
cout << Vehicle::getCount() << endl;
return 0;
}

soubor car.h

/** Library which contains class Vehicle
*/

#ifndef CAR_h
#define CAR_h

#include <string>

using namespace std;

class Vehicle
{
public:
Vehicle(string Type, int Price);
~Vehicle();
friend ostream& operator <<(ostream& out, Vehicle& v);
static int getCount();
protected:
private:
int mPrice;
string mType;
static int msCount;
};

ostream& operator <<(ostream& out, Vehicle& v);

#endif // _XX_h_

soubor car.cpp - nová část
#include <iostream>
#include "car.h"

using namespace std;

int Vehicle::msCount = 0;

Vehicle::Vehicle(string Type, int Price): mType(Type), mPrice(Price) {
msCount++;
}

Vehicle::~Vehicle() {
msCount--;
}

int Vehicle::getCount() {
return msCount;
}

Žádné komentáře:

Okomentovat