Home


Lesson 8

OOP++

Extending extending

Task 0: Self-resizing array

std::vector is like most usable component of Java: ArrayList. It's an "array", which changes its size dynamically.

Use it, when it comes to containing a collection of elements.


#include <vector>

std::vector<int> v;
v.push_back(1);
v.push_back(3);
v.push_back(3);
v.push_back(7);

// like an array
for(size_t i = 0; i < v.size(); ++i)
    std::cout << v[i] << std::endl;

// using iterator
for (std::vector<int>::iterator it = v.begin(); it != v.end(); ++it)
    std::cout << *it << std::endl;

Task 1: Vehicle and parts

Create classes VehiclePart and Vehicle. Vehicle contains many parts, which can we modify any time we want.

Task 2: Car

Car is a vehicle, which moves across lands.

It should have a method: "drive".

If you want to drive a car, the car should contain a driver.

Task 3: Boat

Boat is a vehicle, which natural environment is water.

Boat has a method "sail".

Each boat has a harpoon.

Task 4: Amphibian

Amphibian is a boat and a car.

It can move, way depends on calling a method from base calss according to a type of terrain.