Thread: Polymophism

  1. #16
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by bikr692002
    I almost have it, just one line messing me up
    Code:
    		char hello[25];
    		hello="Hello world, my friend.\n";
    It's saying "ISO C++ Forbids assignments of arrays"
    Should I do hello="Hello"+" "+"world,"+" "+"my"+" "+friend.\n"; ???? I know... it's a stupid idea
    You need to use strcpy(), you can't assign to arrays with =. If I had to guess, I'd say you're not quite ready for polymorphism. Keep working at it and you'll be there soon.
    Sent from my iPadŽ

  2. #17
    Registered User
    Join Date
    Sep 2005
    Posts
    241
    Ok, Damn oh well have to learn in order.

    EDIT: YEY *Does the happy dance*
    I got it to work
    Code:
    #include <fstream>
    #include <iostream>
    #include <string.h>
    
    
    class First 
    {
    	public:
    	virtual void MyFunc() = 0;
    	~First() { };
    };
     
     
    class Second:
    public First
    {
    	public:
    	void MyFunc() 
    	{
    		std::cout<<"Hello, world\n";
    	};
    };
    /* End of class */
    
    class Third:
    public First
    {
    	public:
    	void MyFunc()
    	{
    		std::string Hello;
    		Hello="Hello world!\n";
    		std::cout<<Hello;
    	};	
    };
    /* End of class */
    
    
    class Fourth:
    public First
    {
    	public:
    	void MyFunc()
    	{
    		char hello[25];
    		strcpy(hello,"Hello world, my friend.\n");
    		printf(hello);
    	};	
    };
    /* End of class */
    
    
    int main()
    {
        std::ofstream a_file("Classes.txt");
        a_file<<"Second\nThird\nFourth";
        a_file.close();
        First* MyClass = new Second;
        MyClass->MyFunc();
        delete MyClass;
        std::cin.get();
        return 0;
    }
    Now I just want it to read each line in "Classes.txt" Instead of me saying "new Second"

    EDIT:ermmm I was talking to a friend and he told me this isn't the type of polymorphism I'm looking for, oh well, it was a learning experience right?
    Last edited by bikr692002; 03-21-2006 at 04:37 PM.

Popular pages Recent additions subscribe to a feed