473,497 Members | 2,184 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 1665
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

37
2786
by: Mike Meng | last post by:
hi all, I'm a newbie Python programmer with a C++ brain inside. I have a lightweight framework in which I design a base class and expect user to extend. In other part of the framework, I heavily...
19
3167
by: Mike Tyka | last post by:
Hello community, i'm fairly new to using the STL but i've been experimenting a bit with it. I tried to derive a new class say MyString from string like so: class MyString: public string{...
10
9603
by: makc.the.great | last post by:
now that I am looking at templates, there's the question. why same effect couldn't/shouldn't be achieved with inheritance? provided the difference of the two, when and where do I use templates...
12
7008
by: Steve Jorgensen | last post by:
The classing Visual Basic and VBA support for polymorphism, let's face it, is a bit on the weak side, and built-in support for inheritance is non-existent. This little essay is about some patterns...
13
3209
by: Fao | last post by:
Hello, I am having some problems with inheritance. The compiler does not not return any error messages, but when I execute the program, it only allows me to enter the number, but nothing else...
2
3364
by: sarathy | last post by:
Hi all, I need a small clarification reg. Templates and Polymorphism. I believe templates is really a good feature, which can be used to implement generic functions and classes. But i doubt...
6
3793
by: Bart Simpson | last post by:
I remember reading on parashift recently, that "Composition is for code reuse, inheritance is for flexibility" see (http://www.parashift.com/c++-faq-lite/smalltalk.html#faq-30.4) This confused...
8
20581
by: weird0 | last post by:
Can anyone explain briefly what is the difference between inheritance and polymorphism? i read and seem to forget it again and again... Can anyone along with good examples of c# explain the...
17
3827
by: Bart Friederichs | last post by:
Hello, I created the following inheritance: class Parent { public: void foo(int i); }; class Child : public Parent {
0
7120
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6991
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7160
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7196
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7373
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4897
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4583
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
1
649
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
286
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.