471,075 Members | 1,342 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,075 software developers and data experts.

trying to understand Inheritance, Polymorphism and templates

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::ofstream file( "teste.txt", std::ios::out );
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.
Jul 22 '05 #1
1 1567
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::ofstream file( "teste.txt", std::ios::out );
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
Jul 22 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

19 posts views Thread by Mike Tyka | last post: by
10 posts views Thread by makc.the.great | last post: by
12 posts views Thread by Steve Jorgensen | last post: by
2 posts views Thread by sarathy | last post: by
6 posts views Thread by Bart Simpson | last post: by
17 posts views Thread by Bart Friederichs | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.