inheritance and virtual methods

This is a discussion on inheritance and virtual methods within the C++ Programming forums, part of the General Programming Boards category; I'm having some problems with these 2 concepts. Any Advice? Thanks My errors: Code: eclared in class ‘expandedKey128’ expandedKey.cpp:10: error: ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    57

    inheritance and virtual methods

    I'm having some problems with these 2 concepts. Any Advice?

    Thanks

    My errors:
    Code:
    eclared in class ‘expandedKey128’
    expandedKey.cpp:10: error: no ‘void expandedKey192::printKey()’ member function declared in class ‘expandedKey192’
    expandedKey.cpp:14: error: no ‘void expandedKey256::printKey()’ member function declared in class ‘expandedKey256’
    Header:
    Code:
    #ifndef _EXPANDED_KEY_H_
    #define _EXPANDED_KEY_H_
    
    class expandedKey {
            public:
                    virtual void printKey();
    };
    
    class expandedKey128: public expandedKey {
            public:
                    char expandedKey[4][4];
    };
    
    class expandedKey192: public expandedKey {
            public:
                    char expandedKey[4][4];
    };
    
    class expandedKey256: public expandedKey {
            public:
                    char expandedKey[4][4];
    };
    
    #endif
    Source:
    Code:
    #include <iostream>
    using namespace std;
    
    #include "expandedKey.h"
    
    void expandedKey128::printKey() {
            cout << "print key" << endl;
    }
    
    void expandedKey192::printKey() {
            cout << "print key" << endl;
    }
    
    void expandedKey256::printKey() {
            cout << "print key" << endl;
    }

  2. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    You have to include this:
    Code:
    class expandedKey128: public expandedKey {
            public:
                    char expandedKey[4][4];
                    void printKey();
    };
    So this printKey will override the virtual printKey().

    The idea is that there will be also a expandedKey:rintKey() declared, otherwise you don't need a virtual function simple a normal printKey() function.

    If you want to make sure that every class will have a version of the function you should declare a pure virtual method like:
    Code:
    class expandedKey {
            public:
                    virtual void printKey() = 0;
    };
    so that every subclass has to have a printKey() class


    If you just want every class to have the printKey() function then virtual is not what you want. Just declare a normal function printKey(). Every inherited class will have it.

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604
    The point is, when you inherit something, you inherit the declarations and the definitions. So printKey is already defined for the base class, so you don't need to define it again.
    If you want the derived classes to do something special for the method, then you must redeclare it in the derived class where you intend to use it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Virtual Inheritance: Building Menu items
    By csonx_p in forum C++ Programming
    Replies: 12
    Last Post: 01-22-2009, 11:28 AM
  2. Virtual inheritance
    By 6tr6tr in forum C++ Programming
    Replies: 13
    Last Post: 05-07-2008, 11:20 AM
  3. Virtual function and multiple inheritance
    By George2 in forum C++ Programming
    Replies: 68
    Last Post: 02-13-2008, 12:15 AM
  4. Exporting Object Hierarchies from a DLL
    By andy668 in forum C++ Programming
    Replies: 0
    Last Post: 10-20-2001, 01:26 PM
  5. Multiple virtual inheritance
    By kitten in forum C++ Programming
    Replies: 3
    Last Post: 08-10-2001, 10:04 PM

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