Thread: Can you put the virtual functions into a seperate .cpp file?

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    18

    Can you put the virtual functions into a seperate .cpp file?

    Hi. I don't know if my subject really made sense, so I'll try to explain here. I have a file that contains all my classes, and in those classes are some derived classes with virtual functions in them. Is it possible to put all the code in those virtual functions into a separate file? Such as:
    Code:
    virtual void displayInfo() {
            cout << title << endl;
            cout << author << endl;
            cout << description << endl;
            cout << sku << endl;
            cout << cost << endl;
            cout << quantity << endl;
    }
    This function is in the actual derived class right now.

    Can I clean it up a bit and make it look like this somehow?
    Code:
    virtual void displayInfo() {   }
    And in a separate .cpp file:
    Code:
    virtual void dClass::vfunction() {
            cout << title << endl;
            cout << author << endl;
            cout << description << endl;
            cout << sku << endl;
            cout << cost << endl;
            cout << quantity << endl;
    }
    dClass was my derived class by the way.


    Can I take that code out of the class, and put it in a separate .cpp file? I hope this makes sense. The code above didn't work when I tried it that way, but I'm hoping there's someway to do this.

    Thanks.

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    [code]
    class something {
    public:
    virtual void displayInfo() { }
    // other stuff omitted
    };

    That right there defines the displayInfo function inside of the class... the function does nothing. What you want is

    Code:
    class something {
        virtual void displayInfo();
    }
    Which says, this class has a function displayInfo which takes no argumenets and returns nothing, but it is defined elsewhere.

    You were defining the function when you only wanted to declare it.

    The function would look like this, when it's defined in the .cpp file

    Code:
    virtual void something::displayInfo() {
       // output all the stuff
    }
    Last edited by SilentStrike; 05-28-2002 at 03:43 AM.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    18
    Sorry, I don't think I was clear enough earlier. That was my derived class, which I usually put virtual keyword in front of functions in there just in case I ever derive a class off of that class. In my base class, I have that function = 0, as the base class has no data in it.

    So, in my base class:
    Code:
    class InvItem {
    public:
         virtual void displayInfo() = 0; 
    };
    and in my derived class it looks like my previous post, like this:

    Code:
    class Book : public InvItem {
    private:
          data;
    public:
          virtual void displayInfo() {
              cout << title << endl;
              cout << author << endl;
              cout << description << endl;
              cout << sku << endl;
              cout << cost << endl;
              cout << quantity << endl;
          }
    };
    I had tried it the way you mentioned, but got these errors:

    Code:
    'displayInfo' : 'virtual' storage-class specifier 
    illegal on function definition
    Well, I can get it to work if I keep the code of the virtual function inside the derived class, and not try to put it in a separate .cpp file. Hmmm...I've tried different variations, using the name of the base class and a mixture of the names of the base class and derived class, but I'm stumped.

    Doh!!! I had compiled it under the wrong name, and had the wrong error messages listed. The correct error message is now listed.
    Last edited by EDL; 05-28-2002 at 04:09 AM.

  4. #4
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    You are right.. I as wrong.

    virtual void something::displayInfo() {
    // output all the stuff
    }

    The virtual shouldn't be there. The virutal only belongs in the function declaration, not the definition.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  5. #5
    Registered User
    Join Date
    May 2002
    Posts
    18
    Hey, thanks a ton!!! That worked.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  3. Replies: 2
    Last Post: 10-02-2005, 05:04 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM