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