trying to understand Inheritance, Polymorphism and templates

S

sapropel

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.
 
V

Victor Bazarov

sapropel said:
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 said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top