trying to understand Inheritance, Polymorphism and templates

Discussion in 'C++' started by sapropel, May 14, 2004.

  1. sapropel

    sapropel Guest

    ive been trying to understand Inheritance, Polymorphism and templates,
    and so ive tried to make a test using all of those. it is about a base
    class taht receives two params on the template and two derivies
    class's that print those into console or into a file.

    template <class T, class S> class Base{

    protected:

    T t;
    S s;

    public:

    virtual void Print() = 0;
    };
    //prints to console
    template <class T, class S> class Console : public Base{

    public:

    explicit Console( const T &t, const S &s ){

    this->t = t;
    this->s = s;
    }
    void Print(){

    std::cout << this->t << " " << this->s << std::endl;
    }
    };
    //prints to file
    template <class T, class S> class File : public Base{

    public:

    explicit File( const T &t, const S &s ){

    this->t = t;
    this->s = s;
    }

    void Print(){

    std::eek:fstream file( "teste.txt", std::ios::eek:ut );
    file << this->t << " " << this->s << std::endl;
    }
    };

    int main(){

    Base <int, int> *b = new Console <int, int>( 2, 4 );
    b->Print();
    delete b;
    return 0;
    }

    that doesnt work, case he says that he cannot convert Console<T,S>* to
    Base<T,S>* so i must be doing something wrong.

    another thing, is there a way to do this but only declaring the
    template in the vase class? instead of declaring in all of them?

    thanks.
    sapropel, May 14, 2004
    #1
    1. Advertising

  2. sapropel wrote:
    > ive been trying to understand Inheritance, Polymorphism and templates,
    > and so ive tried to make a test using all of those. it is about a base
    > class taht receives two params on the template and two derivies
    > class's that print those into console or into a file.
    >
    > template <class T, class S> class Base{
    >
    > protected:
    >
    > T t;
    > S s;
    >
    > public:
    >
    > virtual void Print() = 0;
    > };
    > //prints to console
    > template <class T, class S> class Console : public Base{


    template<class T, class S> class Console : public Base<T,S> {

    >
    > public:
    >
    > explicit Console( const T &t, const S &s ){
    >
    > this->t = t;
    > this->s = s;
    > }
    > void Print(){
    >
    > std::cout << this->t << " " << this->s << std::endl;
    > }
    > };
    > //prints to file
    > template <class T, class S> class File : public Base{
    >
    > public:
    >
    > explicit File( const T &t, const S &s ){
    >
    > this->t = t;
    > this->s = s;
    > }
    >
    > void Print(){
    >
    > std::eek:fstream file( "teste.txt", std::ios::eek:ut );
    > file << this->t << " " << this->s << std::endl;
    > }
    > };
    >
    > int main(){
    >
    > Base <int, int> *b = new Console <int, int>( 2, 4 );
    > b->Print();
    > delete b;
    > return 0;
    > }
    >
    > that doesnt work, case he says that he cannot convert Console<T,S>* to
    > Base<T,S>* so i must be doing something wrong.


    Perhaps it has something to do with the way you inherited Console...

    >
    > another thing, is there a way to do this but only declaring the
    > template in the vase class? instead of declaring in all of them?


    Not sure what you mean.

    Victor
    --
    Please remove capital As from my address when replying by mail
    Victor Bazarov, May 14, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JKop
    Replies:
    3
    Views:
    467
  2. glen stark

    templates and polymorphism

    glen stark, Mar 28, 2006, in forum: C++
    Replies:
    2
    Views:
    268
    Stuart Redmann
    Mar 29, 2006
  3. Krivenok Dmitry
    Replies:
    13
    Views:
    1,422
    Axter
    Jun 1, 2006
  4. recover
    Replies:
    2
    Views:
    799
    recover
    Jul 25, 2006
  5. K. Frank
    Replies:
    5
    Views:
    194
    James Kanze
    Aug 19, 2013
Loading...

Share This Page