Institute of Computer Science
Courses.cs.ut.ee Institute of Computer Science University of Tartu
  1. Courses
  2. 2025/26 spring
  3. Programming in C++ (MTAT.03.158)
ET
Log in

Programming in C++ 2025/26 spring

  • Home
  • Labs
    • Submission of solutions
  • Examination times
  • Guides
  • References

You are asked to implement a small system for managing vehicles.

Part 1 — Base Class Vehicle (3 points)

Create a class Vehicle with the following properties:

Attributes

  • brand (string)
  • year (int)
  • speed (double)

Access

brand and year should be protected

speed should be private protected as well (private will cause trouble, sorry if you already tried this, that was a mistake)

Methods

  • A constructor that initializes brand and year and sets speed to 0.
  • accelerate(double amount)

Increases the speed.

  • brake(double amount)

Decreases the speed, but speed cannot become negative.

  • printInfo()

Prints brand, year, and speed.

Example output:

Brand: Toyota
Year: 2019
Speed: 50 km/h

Part 2 — Derived Class Car (2 points)

Create a class Car that inherits from Vehicle.

Additional attribute

  • doors (int)

Methods

  • A constructor that initializes brand, year, and doors.
  • Override printInfo() so it also prints the number of doors.

Add an overloaded method

  • accelerate()

without parameters that increases speed by 10 km/h.

Example output:

Car
Brand: Toyota
Year: 2019
Doors: 4
Speed: 60 km/h

Part 3 — Derived Class ElectricCar (3 points)

Create another class ElectricCar that inherits from Car.

Additional attribute

  • batteryLevel (int, percentage 0–100)

Methods

  • Constructor initializing brand, year, doors, batteryLevel
  • charge(int amount)

increases the battery level but cannot exceed 100%

  • Override printInfo() so it prints battery level as well.

Example:

Electric Car
Brand: Tesla
Year: 2022
Doors: 4
Battery: 85%
Speed: 70 km/h

Part 4 — Test Program (2 points)

Write a main() that:

  • Creates one Vehicle
  • Creates one Car
  • Creates one ElectricCar

Perform operations like:

  • accelerate
  • brake
  • charge
  • printInfo

Example sequence:

Car c("Toyota", 2019, 4);
c.accelerate();
c.accelerate(30);
c.printInfo();

What this exercise tests

In this exercise, you should have used:

  • inheritance
  • multi-level inheritance
  • constructors
  • constructor chaining
  • method overriding
  • method overloading
  • access modifiers
  • of course, classes with attributes and behavior
  • Institute of Computer Science
  • Faculty of Science and Technology
  • University of Tartu
In case of technical problems or questions write to:

Contact the course organizers with the organizational and course content questions.
The proprietary copyrights of educational materials belong to the University of Tartu. The use of educational materials is permitted for the purposes and under the conditions provided for in the copyright law for the free use of a work. When using educational materials, the user is obligated to give credit to the author of the educational materials.
The use of educational materials for other purposes is allowed only with the prior written consent of the University of Tartu.
Terms of use for the Courses environment