Inheritance, overriding, problems

This is a discussion on Inheritance, overriding, problems within the C++ Programming forums, part of the General Programming Boards category; Hello. I'm playing around with vitual members and inheritance for the first real time, and I've come across some problems. ...

  1. #1
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    902

    Inheritance, overriding, problems

    Hello. I'm playing around with vitual members and inheritance for the first real time, and I've come across some problems.
    What I have is a base class consisting mostly of purely virtual functions, some "regular" ones, and data members. Data memebers are protected. I then have two classes that are derived from this base class, overriding the virtual members.
    Trying to compile my code is not working. The derived classes cannot use the base's protected members, and I cannot create instances of the derived classes because "they contain pure virtual functions", even though it is the base class that does. What am I doing wrong?
    Here is some relevent code (well, ok -- it's most of the program):
    Please excuse the code dumpage. It's rather barebones -- promise.
    Code:
    //BASE CLASS
    class xor_file
    {
     public:
            xor_file(file_manager & manager);
            xor_file(string & in_path, file_manager & manager);
            xor_file(file_manager & manager, string & in_psw, string & in_key, string & in_name, string & in_path);
            ~xor_file() throw() {}
            virtual bool init() = 0;
            virtual bool code() = 0;
            bool crypt(fs_ptr & in, fs_ptr & out);
            int const key_size() { return key.size(); }
            //xor_file & operator = (xor_file & other);
     protected:
            std::string psw;
            std::string name;
            std::string key;
            std::string path;
            file_manager & f_man;
    };
    Code:
    //one of two derived classes
     class encrypted_file : public xor_file
    {
          public:
                 //constructors call base constructor too 
                 encrypted_file(file_manager & manager);
                 encrypted_file(string & in_path, file_manager & manager);
                 encrypted_file(file_manager & manager, string & in_psw, string & in_key, string & in_name, string & in_path);
                 bool code();
                 bool init(string & _path);
    };
    Code:
    //THE OTHER CLASS (PRETTY MUCH THE SAME -- THEY DIFFER IN HOW THEY OVERRIDE THE FUNCTIONS)
    class unencrypted_file : public xor_file
    {
          public:
                 unencrypted_file(file_manager & manager);
                 unencrypted_file(string & in_path, file_manager & manager);
                 unencrypted_file(file_manager & manager, string & in_psw, string & in_key, string & in_name, string & in_path);
                 bool code();
                 bool init(string & in_psw, string & in_key = "GENERATE RANDOM");
                 //The function above uses the std::string members inherited from xor_file
                 //error: 'some_string_object' not declared in unencrypted_file  
    };
    Code:
    //Main, where the design flaws manifest themselves
    int main(int argc, char *argv[])
    {
        file_manager f_man;
        encrypted_file test_file(f_man);
        //error: cannot declare type encrypted_file because it contains pure virtual functions
        encrypted_file another(test_file);
        //error: can't do that, either  
        std::system("PAUSE");
        return EXIT_SUCCESS;
    }
    All classes that should include headers from others do so. Any help is appreciated.
    PS: using std::string; where applicable
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    When you have pure virtual functions in the base class, you have to override all of them in the derived classes in order to instantiate the derived classes. In this case, your base class has an init() function that takes no parameters, but the derived classes don't implement an init function that takes no parameters. Their init functions are different because the parameter list is different.

    You need to make init take the same parameters in all classes, or not make it pure (or virtual) at all.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    You have to overload or implement the pure virtual functions to create an instantiable class. (Is that a word?)

    This doesn't compile.
    Code:
    class base {
    public:
        virtual void function() = 0;
    };
    
    class inherit : public base {
    
    };
    
    inherit i;
    But this does:
    Code:
    class base {
    public:
        virtual void function() = 0;
    };
    
    class inherit : public base {
    public:
        virtual void function() {}  // virtual keyword optional
    };
    
    inherit i;
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    902
    I see. Thank you. That cleared up. What about the member access problem? the derived classes still can't access the base's protected members.
    Last edited by CodeMonkey; 01-03-2007 at 03:40 PM. Reason: typo
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Can you show the code that gives the error, and the exact error message?

  6. #6
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    902
    Ah -- in defining the function, I had forgotten to scope it into its class. Works now.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    BTW, when using dynamic polymorphism (public inheritance), you are creating an interface that you can call through a base class pointer or reference. For example, you may have a function that takes an xor_file reference and then calls init() and code() from that reference. The derived class implement those functions in a way that is specific to them. If you think of it in this way, you will not be tempted to create virtual functions that require different parameter lists for the different derived classes. You will also hopefully gain the full power of public inheritance and virtual functions.

    If you enjoy reading Stroustrup, I strongly suggest that you check out C++ Coding Standards by Sutter and Alexandrescu before or alongside TC++PL. It is a lot easier to read and digest in small doses, and the good practices are the focus rather than the language details. It will really help you understand when and why to use many language features.

  8. #8
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    >>I strongly suggest that you check out C++ Coding Standards

    Great book. One of the many that I own. Reading the words of true proffesionals makes you think about just "how well" youi know C++ in general. One for keeps thats for sure. When I become a proffesional programmer ( not if ) I will be more than happy to pass it on to a person I know who is interested in C++ programming. Although not a teaching book like most books, the practices it forces on programmers of all levels can only be most beneficial to a beginner.
    I'm just trying to be a better person - My Name Is Earl

  9. #9
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    902
    Cool. Thanks a lot.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Rendering problems (DirectX?)
    By OnionKnight in forum Tech Board
    Replies: 0
    Last Post: 08-17-2006, 12:17 PM
  2. Inheritance and program structure planning please help a newbie
    By ninjacookies in forum C++ Programming
    Replies: 1
    Last Post: 10-23-2005, 12:18 PM
  3. inheritance problems
    By generic83 in forum C++ Programming
    Replies: 5
    Last Post: 04-10-2005, 05:04 PM
  4. Multiple Inheritance - Size of Classes?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 11-26-2004, 08:04 AM
  5. contest problems on my site
    By DavidP in forum Contests Board
    Replies: 4
    Last Post: 01-10-2004, 08:19 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21