C++ Code Example: single inheritance

In single inheritance, a class may inherit only from another class. Conversely, a subclass can inherit only from a base class.

#include <iostream>
using namespace std;

class Item {
    private: 
        string name;
        int itemNr;
        int quantity;

    public:
        Item() {}
        Item(string name, int quantity, int itemNr) {
            this->name = name;
            this->quantity = quantity;
            this->itemNr = itemNr;
        }
        void setName(string name) {
            this->name = name;
        }
        void setItemNr(int itemNr) {
            this->itemNr = itemNr;
        }
        void setQuantity(int quantity) {
            this->quantity = quantity;
        }
        string getName() {
            return this->name;
        }
        int getItemNr() {
            return this->itemNr;
        }
        int getQuantity() {
            return this->quantity;
        }
};

class Book: public Item {
    private:
        string publisher;
        string language;
        string type; // Book type (bound, unbound)
        int isbn;

    public:
        Book() {}

        Book(string publisher, string language, int isbn, string type) {
            this->publisher = publisher;
            this->language = language;
            this->isbn = isbn;
            this->type = type;
        }
        void setPublisher(string publisher) {
            this->publisher = publisher;
        }
        void setLanguage(string language) {
            this->language = language;
        }
        void setIsbn(int isbn) {
            this->isbn = isbn;
        }
        void setType(string type) {
            this->type = type;
        }
        string getPublisher() {
            return this->publisher;
        }
        string getLanguage() {
            return this->language;
        }
        int getIsbn() {
            return this->isbn;
        }
        string getType() {
            return this->type;
        }
};


int main() {
    Item item("TestItem", 10, 99);
    Book book1("Test Publisher", "en", 645743, "bound");
    Book book2;
    string name;

    cout << "Please insert name for second book object (book2): " << endl;
    cin >> name;

    book2.setName(name);
    book2.setItemNr(100);
    book2.setLanguage("en");

    book1.setName("Book1");
    book1.setQuantity(5);

    // outputs item object
    cout << "\nItem Object (item):" << endl;
    cout << "Name:\t\t" << item.getName() 
         << "\nQuantity:\t" << item.getQuantity() 
         << "\nItem Nr:\t" << item.getItemNr() 
         << endl;

    // outputs first book (book1) object
    cout << "\nFirst Book Object (book1):" << endl;
    cout << "Name:\t\t" << book1.getName() 
         << "\nQuantity:\t" << book1.getQuantity() 
         << "\nPublisher:\t" << book1.getPublisher() 
         << "\nLanguage:\t" << book1.getLanguage() 
         << "\nisbn:\t\t" << book1.getIsbn() 
         << "\nType:\t\t" << book1.getType() 
         << endl;

    // outputs second book (book2) object
    cout << "\nSecond Book Object (book2):" << endl;
    cout << "Item Name:\t" << book2.getName() << endl;
    cout << "Item Nr:\t" << book2.getItemNr() << endl;
    cout << "Language:\t" << book2.getLanguage() << endl;
}
Output
Please insert name for second book object (book2): 
TestBuch2

Item Object (item):
Name:		TestItem
Quantity:	10
Item Nr:	99

First Book Object (book1):
Name:		Book1
Quantity:	5
Publisher:	Test Publisher
Language:	en
isbn:		645743
Type:		bound

Second Book Object (book2):
Item Name:	TestBuch2
Item Nr:	100
Language:	en