Arvutiteaduse instituut
Courses.cs.ut.ee Arvutiteaduse instituut Tartu Ülikool
  1. Kursused
  2. 2025/26 kevad
  3. Programmeerimine keeles C++ (MTAT.03.158)
EN
Logi sisse

Programmeerimine keeles C++ 2025/26 kevad

  • 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
  • Arvutiteaduse instituut
  • Loodus- ja täppisteaduste valdkond
  • Tartu Ülikool
Tehniliste probleemide või küsimuste korral kirjuta:

Kursuse sisu ja korralduslike küsimustega pöörduge kursuse korraldajate poole.
Õppematerjalide varalised autoriõigused kuuluvad Tartu Ülikoolile. Õppematerjalide kasutamine on lubatud autoriõiguse seaduses ettenähtud teose vaba kasutamise eesmärkidel ja tingimustel. Õppematerjalide kasutamisel on kasutaja kohustatud viitama õppematerjalide autorile.
Õppematerjalide kasutamine muudel eesmärkidel on lubatud ainult Tartu Ülikooli eelneval kirjalikul nõusolekul.
Courses’i keskkonna kasutustingimused