Regarding Pure virtual functions

I have just finished reading about pure virtual functions and I have a question

If we have a class that has just one pure virtual function and the other member functions are NOT pure virtual then does that make any sense. Because if a class has just one pure virtual function then it cannot be instantiated.
Yes, it can make sense.

The language allows you to do a great variety of things. It's up to you to use the available features to implement your design effectively.
Sorry dint get you.. is there a way to call base class non virtual member functions if any one of the other member functions is virtual...
A pure virtual function is the same as a virtual function, it just has to be overridden for the class to be instantiable. Nothing else is affected, everything works the same way as before.
Consider the following example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#ifndef _SPEAKINGPERSON_H_
#define _SPEAKINGPERSON_H_
#include <iostream>

class SpeakingPerson {
public :
	SpeakingPerson() {}

	void sayHello() {std::cout << "And I say hello!" << std::endl;}

	virtual void whatDoISpeak() = 0;

};

class EnglishPerson : public SpeakingPerson {
public :
	EnglishPerson() {}

	void whatDoISpeak() {std::cout << "I speak English" << std::end;}

};

class GermanPerson : public SpeakingPerson {
public :
	GermanPerson() {}

	void whatDoISpeak() {std::cout << "I speak German" << std::endl;}

};

#endif 
Last edited on
In this example is there a way to invoke sayHello method of class SpeakingPerson.
Since we are not allowed to make an object of class SpeakingPerson

void main()
{

EnglishPerson EP; // I can do this
SpeakingPerson SP; // but I cant do this since SpeakingPerson is abstract class . // So there is no way to print "And I say hello!"

}

So is it correct that since there is no way to print "And I say hello!" or
in other words since there is no way to invoke non virtual methods of abstract base class,
these non virtual member functions of base classes are redundant?????

I hope this time i am clearer...
What you are forgetting is that child classes inherit all of their parents' members. Inheritance forms an "is a" relationship.

- SpeakingPerson has a 'sayHello' function.

- EnglishPerson is a child of SpeakingPerson
-- this means that an EnglishPerson "is a" SpeakingPerson

Therefore... EnglishPerson also has a 'sayHello' function (it inherits it from SpeakingPerson)

1
2
3
4
5
6
7
int main() // <- always int with main... never void
{
    EnglishPerson ep;

    ep.sayHello();  // will call SpeakingPerson::sayHello
        // and will print "And I say hello!"
}
Last edited on
got it now finally.. cant thank u all enough..
Topic archived. No new replies allowed.