🚀 Alguni Start learning

Unit 7: Inheritance

Building a class from another.

Unit 7 of 8 in C++ for kids. Its 3 lessons are Passing Things Down, Changing What You Inherited and Inheritance Master — below is everything each one explains, and a question or two from it to try.

Every sample on this page was compiled and run with g++ before it shipped, and prints exactly what it says it prints.

This unit opens with a fortnight’s trial of everything — no card needed — or with a family plan, bought in the iPhone app. The first two units of every track are free for ever. Try it in the app.

👨‍👩‍👧 Passing Things Down

A class built from another

: public Animal means "start with everything Animal has". Dog gets the members and the methods without writing them again.

C++

#include <iostream>
#include <string>
using namespace std;

class Animal {
public:
    string name;

    void eat() { cout << name << " is eating" << endl; }
};

class Dog : public Animal {
public:
    void fetch() { cout << name << " fetches" << endl; }
};

int main() {
    Dog d;
    d.name = "Rex";
    d.eat();
    d.fetch();
    return 0;
}

It prints

Rex is eating
Rex fetches

The parent is untouched

Adding to a child changes nothing about the class it came from.

C++

#include <iostream>
using namespace std;

class Shape {
public:
    int sides;
};

class Square : public Shape {
public:
    void describe() { cout << "I have " << sides << " sides" << endl; }
};

int main() {
    Square s;
    s.sides = 4;
    s.describe();
    return 0;
}

It prints

I have 4 sides

The parent is built first

When an object is made, the parent's constructor runs before the child's — so a child can rely on whatever its parent set up, and change it afterwards.

C++

#include <iostream>
using namespace std;

class Animal {
public:
    int legs;

    Animal() {
        legs = 4;
        cout << "Animal built" << endl;
    }
};

class Bird : public Animal {
public:
    Bird() {
        legs = 2;
        cout << "Bird built" << endl;
    }
};

int main() {
    Bird b;
    cout << b.legs << endl;
    return 0;
}

It prints

Animal built
Bird built
2

Try it yourself

What does class Dog : public Animal mean?

  • Dog contains an Animal
  • Dog starts with everything Animal has, and may add more
  • Dog replaces Animal
  • Animal is a parameter

Answer it in the app

🔀 Changing What You Inherited

A child can replace a method

Write a method with the same name as the parent's and the child's version is the one that runs. That is called overriding.

C++

#include <iostream>
using namespace std;

class Animal {
public:
    void speak() { cout << "..." << endl; }
};

class Dog : public Animal {
public:
    void speak() { cout << "Woof" << endl; }
};

int main() {
    Animal a;
    Dog d;
    a.speak();
    d.speak();
    return 0;
}

It prints

...
Woof

Each child answers in its own way

One family, three answers. Adding a fourth animal needs no change to anything already written.

C++

#include <iostream>
#include <string>
using namespace std;

class Animal {
public:
    string name;
    void speak() { cout << name << " says nothing" << endl; }
};

class Dog : public Animal {
public:
    void speak() { cout << name << " says Woof" << endl; }
};

class Cow : public Animal {
public:
    void speak() { cout << name << " says Moo" << endl; }
};

int main() {
    Dog d;
    Cow c;
    d.name = "Rex";
    c.name = "Daisy";
    d.speak();
    c.speak();
    return 0;
}

It prints

Rex says Woof
Daisy says Moo

Try it yourself

A child writes a method with the same name as its parent. What runs?

  • The parent version
  • Both, parent first
  • The child's version
  • C++ refuses to compile

What does this print?

C++

#include <iostream>
using namespace std;

class A {
public:
    void go() { cout << "A" << endl; }
};

class B : public A {
};

class C : public B {
public:
    void go() { cout << "C" << endl; }
};

int main() {
    B b;
    C c;
    b.go();
    c.go();
    return 0;
}

Answer them in the app

🏆 Inheritance Master

Try it yourself

What does this print?

C++

#include <iostream>
using namespace std;

class Base {
public:
    int n;
    Base() { n = 1; }
};

class Child : public Base {
public:
    Child() { n = n + 5; }
};

int main() {
    Child c;
    cout << c.n << endl;
    return 0;
}

Answer it in the app