26. Exercises – Modern C++ for Absolute Beginners: A Friendly Introduction to C++ Programming Language and C++11 to C++20 Standards

© Slobodan Dmitrović 2020
S. DmitrovićModern C++ for Absolute Beginnershttps://doi.org/10.1007/978-1-4842-6047-0_26

26. Exercises

Slobodan Dmitrović1 
(1)
Belgrade, Serbia
 

26.1 Inheritance

Write a program that defines a base class called Person. The class has the following members:
  • A data member of type std::string called name

  • A single parameter, user-defined constructor which initializes the name

  • A getter function of type std::string called getname(), which returns the name’s value

Then, write a class called Student, which inherits from the class Person. The class Student has the following members:
  • An integer data member called semester

  • A user-provided constructor that initializes the name and semester fields

  • A getter function of type int called getsemester(), which returns the semester’s value

In a nutshell, we will have a base class Person and extend its functionality in the derived Student class:
#include <iostream>
#include <string>
class Person
{
private:
    std::string name;
public:
    explicit Person(const std::string& aname)
        : name{ aname }
    {}
    std::string getname() const { return name; }
};
class Student : public Person
{
private:
    int semester;
public:
    Student(const std::string& aname, int asemester)
        : Person::Person{ aname }, semester{ asemester }
    {}
    int getsemester() const { return semester; }
};
int main()
{
    Person person{ "John Doe." };
    std::cout << person.getname() << '\n';
    Student student{ "Jane Doe", 2 };
    std::cout << student.getname() << '\n';
    std::cout << "Semester is: " << student.getsemester() << '\n';
}
Explanation: We have two classes, one is a base class (Person), and the other (Student) is a derived class. Single parameter constructors should be marked with explicit to prevent the compiler from making implicit conversions. This is the case with Person’s user-provided single parameter constructor:
 explicit Person(const std::string& aname)
        : name{ aname }
    {}
Member functions that do not modify the member fields should be marked as const. The const modifier in member functions promises the functions will not modify the data members and are easier for compiler to optimize the code. This is the case with both getname():
std::string getname() const { return name; }
and getsemester() member functions:
int getsemester() const { return semester; }
The Student class inherits from the Person class and ads additional data field semester and member function getsemester() . The Student class has everything a base class has, plus it extends the functionality of a base class by adding new fields. The Student’s user provided constructor uses the base class constructor in its initializer list to initialize a name field:
Student(const std::string& aname, int asemester)
        : Person::Person{ aname }, semester{ asemester }
    {}
In the main() program, we instantiate both classes:
Person person{ "John Doe." };
and:
Student student{ "Jane Doe", 2 };
And call their member functions:
person.getname();
and:
student.getname();
student.getsemester();
Important

We will make a polymorphism exercise later in the book, when we cover the smart pointers. This is because we want to depart from the use of new and delete and raw pointers.