Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Pure Virtual functions

Hello,

I've got :-

#include <iostream>
#include <string>
#include <new>

using namespace std;

class Message
{
public:
virtual ~Message() {}
virtual Message* clone() const = 0;
virtual string get_type() const = 0;
virtual void set(const string s1, const string s2) = 0;
virtual void print() = 0;
};

class Mail: public Message
{

private:
string m_address;
string m_text;

public:
Mail() {}
Mail(const string addr, const string text) : m_address(addr), m_text(text) {}
virtual Message* clone() const { return new Mail(*this);}
virtual string get_type() const { return "Mail"; }
virtual void set(const string addr, const string text) { m_address = addr; m_text = text; }
virtual void print() const { cout << " * Mail: address = " << m_address << " text = " << m_text << "\n"; }
};

int main()
{
return 0;
}
I'm getting compilation errors :-

-------Configuration: Message - Win32 Debug-------
Compiling...
Message.cpp
C:\C++\Message.cpp(28) : error C2259: 'Mail' : cannot instantiate abstract class due to following members:
C:\C++\Message.cpp(18) : see declaration of 'Mail'
C:\C++\Message.cpp(28) : warning C4259: 'void __thiscall Message::print(void)' : pure virtual function was not defined
C:\C++\Message.cpp(14) : see declaration of 'print'
C:\C++\Message.cpp(28) : error C2259: 'Mail' : cannot instantiate abstract class due to following members:
C:\C++\Message.cpp(18) : see declaration of 'Mail'
C:\C++\Message.cpp(28) : warning C4259: 'void __thiscall Message::print(void)' : pure virtual function was not defined
C:\C++\Message.cpp(14) : see declaration of 'print'
Error executing cl.exe.

Message.obj - 2 error(s), 2 warning(s)

Can someone please help ?

Thanks,

Imanuel.
[2135 byte] By [ami] at [2007-11-11 9:57:16]
# 1 Re: Pure Virtual functions
It is my recollection that when you implement a "virtual" method in a child class, that class must be rooted to the class - it cannot be "virtual", too. The whole virtual thing is permitting an object which is one of a line of inheritance to be able to execute a particular hierarchial level's method. The compiler is telling you that there is no "nonvirtual" method which has ever been defined so that it can execute it.
nspils at 2007-11-11 20:59:48 >
# 2 Re: Pure Virtual functions
You have to override *all* the virtual functions of Message in class Mail, if you want to create an isntance of Mail. And since your Mail::clone does exactly that, Mail must implement print and set too.
Other notes: don't pass strings by value. Always pass them by reference, or reference to const if you don't want to callee to change them:
virtual void set(const string& s1, const& string s2) = 0;

Secondly, avoid the use of the virtual keyword when you override a base class function. It's not a mistake to do so, but it's redundant and besides, use virtual only when you declare a new function, not overriding an existing function declared in the base class.
Finally, your get_type return const char *, not string. It's a minor issue since there's an implicit conversion of "Mail" to string. However, there's a much better way to automate the retrieval of class names: use RTTI.

string get_type() const {return string (typeid(*this).name());}

This way, you ensure that for every descendant class, get_type returns the correct class name.
Danny at 2007-11-11 21:00:44 >