Sign in to follow this  
shadowfire36

polymorhism

Recommended Posts

shadowfire36    100
Ok I sat up about 6 hrs yesterday trying to do an example of polymorphism ….this is how I have it im gonna first typing each class and header im trying to call a function and change it to do something else don’t quaote my exact programming this is not the actually program declarations (close) this is only psudeo at best . I have getting two errors one for indirect uses of pointer and the other says invalid call of virtual function , function already contains body in .obj Text the goal of this example is to continue reuse my open text function and then resue it somewhere else using polymorhism any help would be greatly apprecaited
Text.h :

//base class 

 

#Ifndef TEXT_H

#define TEXT_H

 

#include <iostream>

 

using namespace std;

 

 

class Text

 

 Text():

//default constructor 

 

void openOneText();

 //function to open 1 text file 

// trying to use this function for polymorphism 

 

void setNameOneText();

//sets name for one text file 

 

setTwoTextFiles();

setTwoTextName()

 

private :

 

char *solo

//variable for setNameOneText

 

char *fileName1

 

char *fileName2

 

text.cpp:

 

#include “Text.h"

 

Text::Text()

{

public :

     ///default constructor

}

void Text:: void setNameOneText();

 

{

    ///sets variable solo for text file name

 

}

void Text:: openOneText();

{

            ifstream fin;

            fin.open(solo);

            fin.close();

        

          ///working function

}

 

 

            Message.h

         //derived class

 

 

#Ifndef Message_H

#define Message_H

 

#include <iostream>

 

using namespace std;

 

class Message : public Text 

{

public:

            message ();

            // default constructor

                        

            void firstUserChoice();

            void secondUsersChoice();

            void openOneText();   //trying to set up for polymorphism

            virtual void openOne();//I think this is what is need to use for function use 

 

 

}

 Message .cpp

 

#include “Message.h"

 

Message::message();

{

}

 

void firstUserschoice ()

{

            Text temp;

            

            cout<< “welcome"<<endl;

            temp.openOneText();

 

}

 

void Text::openOneText();

{

            //restating original function im not sure if this is needed    

            fstream fin;

            fin.open(solo);

            fin.close();

}

 

virtual void openOne();

{           

            char  answer

            char *carName;

            

            openOneText();

do

 

{  

                        cout << " Please Enter Your VehicleName: " << endl;

 

                        cin >> carName;

 

                        cout << "You have entered " << carName << ". Is this correct? (y/n)" << flush;

                        cin >> answer;

 

 

                        if ( static_cast<int>( playerName.length() ) > 10) 

                        {

                        

 

                        cout << " You have exceed 10 chracters.  Try Again" << endl;

                        answer = 'n';

                        }

                        else if ((answer == 'Y') || (answer == 'y'))

                        {

                        cout << playerName << " Let's Begin "<< flush;

                        cin.clear();

 

                        cin.ignore(500,'\n');

 

 

                        }

                        else if ((answer == 'N') && (answer == 'n'))

                        {

                        cout <<  " Please Re-enter. " << endl;

                        }

 

 

} while ((answer == 'N') || (answer == 'n'));

 

}

 

 

main .cpp

{

            Message  showUserText;

            Message *waitForUserInput new (I cant remember  what I had here since my file is at home)

            Message Input;

 

            showUser.Text.set setNameOneText(“splash.txt);

            *waitUserInput->input ;/// this is where I am trying to call function I set up in message 

             

}


Share this post


Link to post
Share on other sites
Gage64    1235
I suggest that you look at the free book in my sig (2nd link) to learn more about C++, as you have some very fundamental mistakes in your code (inheriting from a function doesn't make sense, for example).

This probably isn't what you wanted to hear, but it will make your life easier in the long run.

Share this post


Link to post
Share on other sites
Zahlman    1682
Quote:
Original post by shadowfire36
Ok I sat up about 6 hrs yesterday trying to do an example of polymorphism ….this is how I have it im gonna first typing each class and header im trying to call a function and change it to do something else don’t quaote my exact programming this is not the actually program declarations (close) this is only psudeo at best .


If you've actually written code, you should show the actually-written code. We're not psychic.

Share this post


Link to post
Share on other sites
Chrono1081    108
From my understanding, a simply polymorphism example would be:



class Monster
{
public:
Monster();
Monster(int, int);
void printMonsterStats();

private:
int _health;
int _strength;
};

Monster Monster()
{
_health = 0;
_strength = 0;
}

Monster Monster(int health, int strength)
{
_health = health;
_strength = strength;
}

void Monster::printMonsterStats()
{
cout<< _health << endl;
cout<< _strength << endl;
}





Now, to me that looks more like function overloading but I am no expert I am just reading this example out of a book and that is what the book shows as a small example of polymorphism.

(If its not someone please let me know so I can delete my post)

Share this post


Link to post
Share on other sites
return0    508
C# code using trite car analogy, becase the analogy is easy and abstract/virtual is clearer in c# than c++ style virtual foo() =0. Look how the inheriting classes override the functionality of their parents to provide different behaviour if required, but can still be referred to as their parents and operated on. They use their parents interface but define their own implementation.


public class MovingObject
{
private float speed;
public float Speed { get { return speed; } protected set { speed = value; } }
public void PrintSpeed() { Console.WriteLine(speed); }
}

abstract public class Vehicle : MovingObject
{
abstract public void PrintName();
virtual public void Accelerate() { }
}


public class Car : Vehicle
{
public override void PrintName() { Console.WriteLine("Car"); }
public override void Accelerate() { Speed = Speed + 10; }
}


public class Bike : Vehicle
{
public override void PrintName() { Console.WriteLine("Bike"); }
public override void Accelerate() { Speed = Speed + 5; }
}

public class ChargedBike : Bike
{
private bool charged = false;
public bool Charged { get { return charged; } set { charged = value; } }
public override void PrintName() { Console.WriteLine("ChargedBike"); }

public override void Accelerate()
{
if (charged)
{
Speed = Speed + 50;
}
else
{
base.Accelerate();
}
}
}



class Program
{
static void Main(string[] args)
{
//Vehicle v = new Vehicle(); // Compile error as abstract

Bike bike = new Bike();
Car car = new Car();
ChargedBike chargedbike = new ChargedBike();

bike.PrintName();
car.PrintName();
chargedbike.PrintName();

//add all vehicles to list
List<Vehicle> vehicleList = new List<Vehicle>();
vehicleList.Add(bike);
vehicleList.Add(car);
vehicleList.Add(chargedbike);

foreach (Vehicle v in vehicleList) { v.PrintSpeed(); }
foreach (Vehicle v in vehicleList) { v.Accelerate(); }
foreach (Vehicle v in vehicleList) { v.PrintSpeed(); }

chargedbike.Charged = true;
chargedbike.Accelerate();
foreach (Vehicle v in vehicleList) { v.PrintSpeed(); }

Console.Read(); //wait
}
}





For what it's worth this prints:


Bike
Car
ChargedBike
0
0
0
5
10
5
5
10
55





If you are confused, compile this and step through, watching for which code path is executed when the child class objects call the parent interface methods. Hope this helps, if you have any questions or corrections please post back.

Share this post


Link to post
Share on other sites
shadowfire36    100
Quote:
Original post by Gage64
I suggest that you look at the free book in my sig (2nd link) to learn more about C++, as you have some very fundamental mistakes in your code (inheriting from a function doesn't make sense, for example).

This probably isn't what you wanted to hear, but it will make your life easier in the long run.


im scared your a programmer if u didn't read this is a pseudo code

Share this post


Link to post
Share on other sites
jpetrie    13159
Why? There's still plenty wrong with it, even as pseudocode. There's no reason to get snippy.

If you want help with your real code, show us your real code, as Zahlman says. Otherwise, take the advice you've been given with respect to your pseudocode.

Share this post


Link to post
Share on other sites
shadowfire36    100
Quote:
Why? There's still plenty wrong with it, even as pseudocode. There's no reason to get snippy.

If you want help with your real code, show us your real code, as Zahlman says. Otherwise, take the advice you've been given with respect to your pseudocode.


im not getting snippy why would i need to take advice if im asking for help TO build something with polymorism , i would think if im starting a topic on it, someone may think i already know some fundamentals of programming already ..im just surprise how some many people get attitudes over something that does not even exist ...i just feel some people are in a way are to literal and a bit on the extreme side but im not downing anyone .. im here to learn not to get fussed at like im a child .(subject dropped)

now that im home i can upload my code i dont think its really that hard or anyone to give me an idea i dont have net access or visual studio at work so i made my post while i was thinking of the code it couldnt get to my files,but i wanted more insight before i got home...

to my issue now i have a function in my text.cpp named test 1 and a virtual function called test 2 the idea is to set a simple cout in these two functions and allocate memory a variable and then i setup the function so it can open a file somewhere else,get some simple input cout a message from a message class
now what i was trying to do in choices.cpp is to call each of the in and display each message in order

sample run would be ...

1st variable(function) = this is an example of
2nd variable(function) = polymorphism
3rd variable(function) = if this isnt
4th variable(function) = polymorphism
5th variable(function) = i dont no what
cout << is << endl;


Text.h

//base class
#ifndef TEXT_H
#define TEXT_H

#include <fstream>
#include <iostream>
#include <string>

using namespace std;

class Text// fucntion class to open and close text files
{
public:
void setFileName(char* name, char* name2);
//sets file name when opening 2 text display

void setTextName(char* name3);
//set file name for 1 text display


void twoTextFile();
//opens 2 text files at 1 time

void oneTextFile();
//open 1 text file at a time

void test();///setup for trying to use polymorphism
virtual void test1();///setup for trying to use polymorphism

Text();
//default constructor
protected:
char *solo;//protected variable

char *file1;//protected variables


char *file2;//protected variables
};

#endif



Text.cpp

#include <iostream>
#include "Text.h"

void Text::setTextName(char *name3)
{
solo = name3 ;// sets variable name for one text file

}

void Text::oneTextFile()// function for opening one text file
{
ifstream fin;
fin.open(solo);
string temp;

while ( !fin.eof() )
{
getline(fin, temp);
cout << temp << endl;
}

fin.close();
cin.clear();
}
void Text::setFileName(char *name,char *name2)
{

file1 = name;
file2 = name2;

}
/// right here ///i have to fucntions 1 opens one file at time the other opens


void Text::twoTextFile()
{
ifstream fin;
fin.open(file1);
string temp;

while ( !fin.eof() )
{
getline(fin, temp);
cout << temp << endl;
}

fin.close();

ifstream fin2;
fin2.open(file2);


while ( !fin2.eof() )
{
getline(fin2, temp);
cout << temp << endl;
}

fin2.close();
cin.clear();

}

Text::Text()
{

}

void Text::test()//function for polymorhism
{
cout << "This better be : "<< endl;
}
void Text::test1()//function for polymorhism
{
cout << "Or i dont know what: " << endl;
}




Message.h

// derived class
#ifndef MESSAGE_H
#define MESSAGE_H


#include <iostream>
#include "Text.h"

using namespace std;

class Message : public Text
{
public:
void firstUserSelection();
// 1st user option message

void secondUsersSelction();


void test();//not sure of this is right

Message();
//default constructor


};
#endif



Message.cpp

#include "Message.h"


void Message::firstUserSelection()
{

cout << "*** Welcome please make a selection . ***" << endl;
cout << "To select an item, enter " << endl;
cout << " [1] To start Car Buyer " << endl;
cout << " [2] To View Nissan Cars " << endl;
cout << " [3] To View Toyota Cars " << endl;
cout << " [4] To Quit " << endl;
cout << " [5] to view polymorphism " << endl;
}

void Message::secondUsersSelction()
{

system("cls");
cout << "*** What type of vehcile are you intrested in ? ***" << endl;
cout << "To select an item, enter " << endl;
cout << " [1] For Hatchbacks " << endl;
cout << " [2] For Sedans " << endl;
cout << " [3] For Coupes " << endl;
cout << " [4] To Quit " << endl;

}
Message::Message()
{
}


void Message::test()//function to change previous fuction
{

int example;
ifstream fin;
fin.open("poly.txt");
string temp;

while ( !fin.eof() )
{
getline(fin, temp);
cout << temp << endl;
}





cout << "how many cars are in list ?" << endl;
cin >> example ;

if(example = 8)
{
cout << "Sweet !!! YOU CAN COUNT !! " << endl;
fin.close();
cin.clear();

}
else
{
cout <<" try again " << endl;
}

}



choices.h
[source="cpp"]
#ifndef CHOICES_H
#define CHOICES_H

#include <fstream>
#include <iostream>
#include <string>
#include "Text.h"
#include "Message.h"

using namespace std;

class Choices: public Text
{
public:

void firstUsersChoice();
void secondUsersChoice(Message msg, Text txt);
void enterUsersInfo();
void carType();


};



#endif




choices.cpp


#include "Choices.h"
#include "Message.h"
#include "Text.h"






void Choices::firstUsersChoice()
{
int menu = true;
int choice;
Text toyota;
Text nissan;
Message back;
Text goodbye;
Choices purchase;
Message showUserOptions;
Message *data = new Message();
Message message;
Text text;
Text *store = &message;
Message *store1 = &message;






do // beginning of loop for user 1st selection
{
back.firstUserSelection();

cin >> choice; // choice selection for users

switch (choice)// begin switch for user selection
{

case 1:
secondUsersChoice(back, goodbye);
break;

case 2:
nissan.setFileName("nissan.txt","nline.txt");
nissan.twoTextFile();
break;

case 3:
toyota.setFileName("toyota.txt","tline.txt");
toyota.twoTextFile();

break;
case 4:
goodbye.setTextName("goodbye.txt");
goodbye.oneTextFile();
break;
case 5:
text.test();
store1->test();
text.test1();
data->test();
cout << " is " << endl;
//store->test();// currently not working
//store1->test1();//currently not working





default : //handles invaild choice


cout << " "<< endl;
cout << " "<< endl;
cout << " "<< endl;
cout << " "<< endl;
cout << "***** 'Please read options again & Then re-enter a vaild choice.'*****" << endl;
cout << " "<< endl;
cout << " "<< endl;
cout << " "<< endl;
cout << " "<< endl;
break;



}
system("PAUSE");
system("cls");

}while (choice != 4);
}

void Choices::enterUsersInfo()
{
Message back;
char answer;
string name;
string vehicleName;
Choices purchase;

do
{

ofstream receipt("car.txt");
cout << " Please Enter The Type of You are Intrested in Vehicle: " << endl;
cin >> name;
cout << "You have entered " << name<< ". Is this correct? (y/n)" << flush;
cin >> answer;

if ( static_cast<int>( vehicleName.length() ) > 10)
{

cout << " You have exceed 10 chracters. Try Again" << endl;
answer = 'n';
}

else if ((answer == 'N') || (answer == 'n'))
{
cout << " Please Re-enter. " << endl;
}
} while ((answer == 'N') || (answer == 'n'));





}
void Choices::carType()
{
int menu = true;
int choice;
Text carType;
Message back;
Text goodbye;
Choices purchase;








do // begining of loop well
{

cin >> choice; // choice selection for users

switch (choice)// begin switch for user selection
{

case 1:
purchase.enterUsersInfo();
carType.setTextName("hatchback.txt");

cout << " These Are Current Models: " << endl;//need clear value
carType.oneTextFile();



break;

case 2:
carType.setTextName("sedan.txt");
cout << " These Are Current Models: " << endl;
carType.oneTextFile();
purchase.enterUsersInfo();
break;

case 3:
carType.setTextName("coupe.txt");
cout << " These Are Current Models: " << endl;
carType.oneTextFile();
purchase.enterUsersInfo();
case 4:
goodbye.setTextName("goodbye.txt");
goodbye.oneTextFile();
return ;

break;


default : //handles invaild choice


cout << " "<< endl;
cout << " "<< endl;
cout << " "<< endl;
cout << " "<< endl;
cout << "***** 'Please read options again & Then re-enter a vaild choice.'*****" << endl;
cout << " "<< endl;
cout << " "<< endl;
cout << " "<< endl;
cout << " "<< endl;
break;
}


}
while (menu != 4);

}




void Choices::secondUsersChoice(Message msg, Text txt)
{
int userChoice;
do
{
msg.secondUsersSelction();
cin >> userChoice;

switch (userChoice)
{
case 1:
txt.setTextName("hatchback.txt");
txt.oneTextFile();
system("PAUSE");
break;
case 4:
cout << "Back the the main menu..." << endl;
break;
default:
cout << "That is not a valid choice" << endl;
}
}while (userChoice != 4);
}





























Share this post


Link to post
Share on other sites
Mantear    251
In order to use polymorphism, you must make use of inheritance and virtual functions. Here is a simple example.
class Athlete
{
public:
Athlete();
virtual ~Athlete(); // Must have a virtual destructor to avoid pitfalls.

void Compete() {cout << "I compete in anything!" << endl;}
};

class Runner : public Athlete
{
public:
Runner();
~Runner(); // Base class destructor was already declared virtual, all
// derived class destructors will automatically also be virtual
// (I believe). No need to re-state 'virtual'

void Compete() {cout << "I compete in marathons!" << endl;}
};

And here's how you could use it.
// Create a Runner object.
Runner* p_runner = new Runner;

// Create an Athlete pointer that points to the Runner object.
Athlete* p_athlete = dynamic_cast<Athlete*>(p_runner);

p_athlete->Compete();

In the above example, I created a Runner object. Next, because an Athlete is a base to a Runner, I was able to assign the Runner object to the Athlete pointer. However, doing this doesn't strip the Runner object of still being a Runner. Therefore, when I call Compete() on the Athlete pointer, it prints out "I compete in marathons!".

This is the power of polymorphism. You might have a base class pointer to an object (p_athlete), not know or care what the actual instantiated class is (as long as it is an Athlete class or another class derived from Athlete), and the appropriate method will be called. If p_athlete points to an Athlete object,
it would print "I compete in anything!". If it points to a Runner, it would print "I compete in marathons!". If it points to a Jumper class that derived from Athlete, it might print "I like to jump high!".

This allows to you do something like have all your athletes compete by looping through a list of base class pointers and calling the same function for all types of athletes.
for (list<Athlete*>::iterator iter = athlete_list.begin(); iter != athlete_list.end(); ++iter)
{
(*iter)->Compete();
}

Share this post


Link to post
Share on other sites
Gage64    1235
Quote:
Athlete* p_athlete = dynamic_cast<Athlete*>(p_runner);


A pointer to a derived class can be implicitly converted to a pointer to a base class, so the cast is not needed. You can just write:

Athlete* p_athlete = p_runner;


dynamic_cast is used to cast a base class pointer/reference to a derived class pointer/reference. This cannot be done implicitly because if you have a pointer to an Athlete, you can't know at compile-time whether it's really pointing to an Athlete or to something derived from it. You have to perform a runtime check to find out, which is what dynamic_cast allows you to do.

Share this post


Link to post
Share on other sites
jpetrie    13159
Quote:

In order to use polymorphism, you must make use of inheritance and virtual functions. Here is a simple example.

The first point is correct, but your example does not demonstrate dynamic dispatch as you perhaps intended -- the function you call, Compete(), is not virtual.

Quote:

im not getting snippy why would i need to take advice if im asking for help TO build something with polymorism

Because the example code you posted has serious flaws. You are clearly inexperienced and lacking in understanding of some fundamental concepts. You were advised as to how you might improve that, and you chose instead to respond with a baseless but subtle insult to the user that offered you advice.

Don't do that again.

Quote:

im here to learn not to get fussed at like im a child

Then stop acting like one. If you are here to learn, you'll appreciate having your errors pointed out so you can learn to fix them, even if they are not directly related to your original question. If you choose, instead, to retaliate against members who don't give the answer you want (instead of a correct answer), then I -- at least -- question your actual desire to better yourself.

Quote:

to my issue now i have a function in my text.cpp named test 1 and a virtual function called test 2 the idea is to set a simple cout in these two functions and allocate memory a variable and then i setup the function so it can open a file somewhere else,get some simple input cout a message from a message class

Slow down, take a deep breath. You need to explain your problem better -- perhaps with some punctuation. It is unclear what you are trying to achieve beyond some simple demonstration of polymorphic behavior.

So let's look at your code. I don't see main() anywhere, so it's impossible to debug your output. I can tell you, however, that you're probably not involving polymorphic behavior anywhere, and you certainly aren't getting the expected virtual function calls.

You're trying to achieve 'dynamic dispatch' -- calling a function based on the dynamic (actual at runtime) type of the object, rather than the static (compile time) type of the object. This is done via virtual methods, as has been pointed out. You have a virtual method test1() in class Text, but you never overload it in any of the base classes. Also not that for dynamic dispatch to work it must occur via a reference or pointer; without seeing your main() and how you create these objects, it's hard to tell if you're doing that part right.

A simple example:

#include <iostream>

struct Base {
virtual void DoStuff() {
std::cout << "Base::DoStuff()\n";
}
};

struct Derived : Base {
void DoStuff() {
std::cout << "Derived::DoStuff()\n";
}
};

int main() {
Base* b = new Base();
Base* d = new Derived();

b->DoStuff(); // Base::DoStuff()
d->DoStuff(); // Derived::DoStuff()

delete b;
delete d;
}

Since DoStuff() is virtual, and the calls are through pointers, the call through d will call the derived method since the dynamic type of the object is Derived, even though the static type is Base. The C++ FAQ discusses this in detail, as do many good C++ books.

Other things wrong with your code you should consider (in brief, major glaring issues only): prefer std::string in C++, not char*, for string data. Do not employ "using namespace std;" in headers files as it pollutes the global namespace. Your comments are mostly superfluous and noisy, and you could really stand to spend some time organizing the code better and coming up with more descriptive names.

Share this post


Link to post
Share on other sites
Zahlman    1682
Quote:
Original post by jpetrie
Quote:

im not getting snippy why would i need to take advice if im asking for help TO build something with polymorism

Because the example code you posted has serious flaws. You are clearly inexperienced and lacking in understanding of some fundamental concepts. You were advised as to how you might improve that, and you chose instead to respond with a baseless but subtle insult to the user that offered you advice.

Don't do that again.

Quote:

im here to learn not to get fussed at like im a child

Then stop acting like one. If you are here to learn, you'll appreciate having your errors pointed out so you can learn to fix them, even if they are not directly related to your original question. If you choose, instead, to retaliate against members who don't give the answer you want (instead of a correct answer), then I -- at least -- question your actual desire to better yourself.


Just what I was thinking.

Quote:

The C++ FAQ discusses this in detail, as do many good C++ books.


Yep; that's the best starting point I could think of. In particular, pay attention to section 6.

Share this post


Link to post
Share on other sites
shadowfire36    100
Quote:


Slow down, take a deep breath. You need to explain your problem better -- perhaps with some punctuation. It is unclear what you are trying to achieve beyond some simple demonstration of polymorphic behavior.




Ok what im actually trying to do with test is show a basic example of polymorphic behavior my by using the two functions I created in The class called Text and the function called test() .

From the example I read I believe I misunderstood the coding example I was reading , I thought u had to state the test() and name a virtual function with a similar name .. so now that I have seen your code I.



I didn’t display my main function because I do not have any thing in there but



main();

{

Choices showUserDisplay;



showUserDisplay.firstUsersChoices();

}



Everything else is controlled in choices ..so what my flow is :

Text = base class , I created a child class which is Message , now it suppose to

inherit the function called test and give test polymorphic behavior that I set in the Message class, from here I go to my choices class it will call the original function ,display its message and then I will call the next function with polymorphic behavior and show its contents for display ,which are declared in case 5 of the “firstUsersChoices();” function in choices.cpp





Quote:


Other things wrong with your code you should consider (in brief, major glaring issues only): prefer std::string in C++, not char*, for string data. Do not employ "using namespace std;" in headers files as it pollutes the global namespace.




Can you show me an example I have only been doing this for 2 months ?


Share this post


Link to post
Share on other sites
chairthrower    440
There are two sides to this polymorphic thing. first there's knowing how technically, to structure and invoke the behavour in a specific programming language. along with this, there is an independent issue in knowing when and where it might make sense to apply it as a way to solve structure problems in any given program design. You are getting lots of responses from members - posting short snippets of code demonstrating the principle as an alternative to commenting in relation to your own code. i think in part, this is because its not really clear that your example problem best presents a clear use case or need for polymorphic behaviour, or else its is too complicated when your question really is - how do I go about doing method overriding in c++?

the example code given by jpetrie is really very good. why? because it is very simple and very clear, allowing anyone to take it and very quickly experiment. copy/paste it into a file and compile it. remove the virtual keyword in the base class definition and see what happens?. change the name of the method so that it is *different* to the name of the method in the parent class and see what happens? change the typing on the signature. In ten minutes you will probably have a lot better a grasp on the subject than trying to compose your own example that runs to several pages. And what is not clear from this you can probably piece together from the other links that have been given in the thread or else could be asked about here with a clearer focus.

Once you grok the detail of the technical implementation then polymorphism becomes much more of a design question - eg would it be appropriate to write a file reader and then subclass the stream reading behvavor to do automatic conversion of utf-8 to ascii or is there a better way? btw the jury is still out about whether/why/when to use polymorphism - i am sure i have seen a video of stroustrop the inventer of the c++ language stating that he think's it is used 'much too often'. also there are lots of threads on the forums here that try to consider other approaches to dynamic object inheritance (static polymorphism, components,aggregation etc).This probably doesnt mean very much at the moment however i say it to provide an explanation as to why you may not be getting the clear cut answers to your question and code that you may have expected.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this