473,779 Members | 1,873 Online
Bytes | Software Development & Data Engineering Community
+ 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 1689
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
2848
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 use the instance of this base class (or its children class). How can I ensure the instance IS-A base class instance, since Python is a fully dynamic typing language? I searched and found several different ways to do this:
19
3196
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{ public: MyString(){} };
10
9635
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 instead of inheritance, and other way around? thoughts, please.
12
7057
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 I've ended up using successfully for certain kinds of inheritance and polymorphism, and some that have not worked out so well. Let's start with obvious things that turn out not to work well: 1. Use interface classes and "Implements" for...
13
3261
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 happend. I think the problem may be in my input function or in the main function. If anyone out there can help me it woul be greatly appreciated. Here is the code: #include <iostream>
2
3389
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 whether it should not be used in certain cases. Consider the case when all the params to a template function/class are similar. My questions is that whatever can be acheived by a template in such a case, can be acheived by runtime polymorphism....
6
3828
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 me somewhat as I have always thought you get code reuse "for free" with inheritance. Am I missing something?. Will someone care to explain ??
8
20614
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 fundanmental concept so i can remember it forever as it is a common interview question.... Thanks in advance
17
3882
by: Bart Friederichs | last post by:
Hello, I created the following inheritance: class Parent { public: void foo(int i); }; class Child : public Parent {
0
9632
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10136
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10071
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7478
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6723
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5501
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4036
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3631
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2867
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.