C++: Members of a subclass

This entry is part 48 of 61 in the series C++

Here we work out a simple example of a base class and a subclass shown in the example below.

#include <cstdlib>
#include <iostream>
 
using namespace std;
 
// Base class
 
class Music {
 
public:
    string singer;
    string title;
};
 
// Derived class
 
class Rock : public Music {

public:    
    string album;

};
 
int main(void) {
     
    Rock rock;
    
    rock.singer = "Katy Perry";
    cout << rock.singer;
 
    return 0;
}

Rock is a subclass (or derived class) of Music and therefore it inherits all Music members (base class) other than constructors.

An instance of Rock therefore has 3 data members: singer, title and album.

subclass

 

 

 

Series Navigation<< C++: An experiment on a static variable
C++: Assessing private members of the base class >>