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

Lab 2 -classes and objects

Inheritance as a concept

In Object Oriented Programming, inheritance is typically a fundamental concept, and so it is in C++. Remember that object orientation is based on the idea of forming "natural" units of data and code, resembling objects in the "real world" (which can be an imagined real world). In reality, concept are often related - we define things by comparing them to others, we create things by taking existing concepts and adopting them, we classify things as being "similar to X, except aspect Y is different". This is the idea of inheritance in object oriented languages. As usual with computers, it is all tightly defined and works in a specific fashion. In most OO languages (c++ is slightly more powerful, we will come back to that later) the inheritance relationships form a tree, where the child objects inherit from the parent, basically meaning they are the same concept with additional features:

Here, we illustrate the idea that "vehicle" is a very general concept, and "Wheeled" and "Gliding" vehicles are more specific types of it. For wheeled vehicles, we can distinguish the specific concepts of "Car" and "Bicycle". Of course, as you may have noticed, this is not the only possible modelling. If you wonder where actual cars etc. are, remember we still need to create instances.

In terms of prgramming, inheritance means that attributes and methods of parent objects are found in child objects as well. For illustration, some attributes are shown below:

Notice that there may be more and there are also methods. Typically, in class diagrams (that's the proper name of these diagrams) you would not show inherited attributes in the children.

Of course, you may disagree about which attributes are useful, and it certainly depends on the use case.

Inheritance in C++

In C++, inheritance is expressed as follows:

// Base class
class Vehicle {
  public:
    int length = 5
    void honk() {
      cout << "Tuut, tuut! \n" ;
    }
};

// Derived class
class WheeledVehicle : public Vehicle {
  public:
    float wheelSize = 28;
};

int main() {
  WheeledVehicle myBike;
  myBike.honk();
  cout << myBike.length + " " + myCar.wheelSize;
  return 0;
}

The colon indicates inheritance. Our Bikehere combines the properties of Vehicle and WheeledVehicle. Notice that attributes are public here, so they can be accessed from everywhere. private attributes (and methods) can only be accessed from the class itself. protected can be accessed from child classes, but not from the outside (so a protected attribute of Vehile could be used in WheeledVehicle, a private one could not).

Visibility

Every instance of a class will contain the values for all attributes of the class itself and its parents. private attributes and methods can only be used from the class itself, not from child classes (so private attributes cannot be used in child classes directly, but methods using them can still be used if public). The modifier protected is making attributes/methods available in child classes as well, but not from the outside.

The friend declaration has two meanings: First, in a class another class can be declared as friend. This means the friend class can access private and protected attributes and methods. Notice the declartion must be "started" by the class who wants to expose something: If class A declares B a friend, B can look into A, not the other way round. Second, friend can be used with unimplemented methods. These friend methods can be implemented as global methods and still access private and protected attributes (example was the ostream overload in the last lab).

Inheritance and constructors

  • 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