C++ Class template Deriving a Class

Description

C++ Class template Deriving a Class

#include <iostream>

using namespace std;

class Book/*from  w ww  .  j a  v  a2 s  . c o m*/
{
public:
    string Name;
    string Author;
    string Publisher;
    Book(string aname, string anauthor, string apublisher) :
        Name(aname), Author(anauthor), Publisher(apublisher)
        {}
};

class Magazine
{
public:
    string Name;
    string Issue;
    string Publisher;
    Magazine(string aname, string anissue,
        string apublisher) :
        Name(aname), Issue(anissue), Publisher(apublisher)
        {}
};

template <typename T>
class MediaHolder
{
public:
    T *array[100];
    int Count;

    void Add(T *item)
    {
        array[Count] = item;
        Count++;
    }

    MediaHolder() : Count(0) {}
};

class BookHolder : public MediaHolder<Book> {
public:
    enum GenreEnum
        {childrens, scifi, romance,
         horror, mainstream, hownotto};

    GenreEnum GenreOfAllBooks;
};

class Website : public MediaHolder<Magazine>
{
public:
    bool CompleteSet;
};

int main()
{
    Website dl;
    dl.Add(new Magazine("java2s.com", "Vol 1 No 1", "C++"));
    dl.Add(new Magazine("java2s.com", "Vol 1 No 2", "C++"));
    dl.Add(new Magazine("java2s.com", "Vol 1 No 3", "C++"));
    dl.CompleteSet = false;
    cout << dl.Count << endl;

    BookHolder bh;
    
    bh.Add(new Book("C", "G", "C++"));
    
    bh.Add(new Book("D", "W","C++"));
    
    bh.Add(new Book("T","W","C++"));
    
    bh.GenreOfAllBooks = BookHolder::hownotto;
    
    cout << bh.Count << endl;

    return 0;
}



PreviousNext

Related