Click here to Skip to main content
12,956,234 members (68,336 online)
Rate this:
 
Please Sign up or sign in to vote.
See more:
hi, I want to make my class B concrete which is derived from class A (Abstract class).

say class A have a pure virtual function "void SayHello()=0;" and class B is deriving from class A. is it possible to make class B concrete without providing pure virtual function "SayHello" definition?
Posted 28-Jan-13 23:15pm
Comments
CPallini 29-Jan-13 6:24am
   
Nope.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Yes, in fact you cannot have any pure virtual functions in a concrete class so the overridden version of SayHello in B must be concrete for B to be concrete.

/Fredrik
  Permalink  
Comments
Faisalabadians 29-Jan-13 5:40am
   
can you please give me a little example?
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

No.
All abstract properties and methods must be implemented in the concrete class in order to fulfil the contract.
  Permalink  
Comments
Faisalabadians 29-Jan-13 5:51am
   
then please help my, I have mentioned problem where class A is abstract and class B is deriving from class A then how to make class B concrete in this scenario?
OriginalGriff 29-Jan-13 6:00am
   
Implement the function! :laugh:
Faisalabadians 29-Jan-13 6:22am
   
:( you laughing :(
OriginalGriff 29-Jan-13 7:00am
   
I'm laughing because it is kinda obvious!
Faisalabadians 29-Jan-13 7:11am
   
I am new to C++
OriginalGriff 29-Jan-13 7:18am
   
That's fine - we all have to start somewhere! :)
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

class A { // abstract class
public:
   virtual void SayHello() = 0; // abstract member function
};
 
class B { // concrete class
public:
   void SayHello() { // concrete function
      std::cout << "Hello, I am B." << std::endl;
   }
};
  Permalink  
Comments
Faisalabadians 29-Jan-13 6:21am
   
bundle of thanks, please tell me one thing, if sayhello method accepts some arguments, then? to make class b concrete, it is necessary to implement the say hello function?
Stefan_Lang 29-Jan-13 7:25am
   
The prototype of the function must be the same, i. e. same number of arguments and same argument type. The function in class B may be declared virtual or not, that is optional. It is also possible, albeit not recommended, to use a different return type.

And yes, class B is only considered concrete if all of its or its base classes' virtual functions are 'implemented' (i. e. defined as not abstract). Otherwise, B is considered abstract. (which is also possible - you can always derive another class C from B and make that class concrete by providing the missing functions)
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

Nope, see, for instance: What is a "pure virtual" member function?[^] at C++ FAQ.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Print Answers RSS
Top Experts
Last 24hrsThis month
OriginalGriff 6,869
CHill60 3,550
Maciej Los 3,243
ppolymorphe 2,070
Jochen Arndt 1,973


Advertise | Privacy | Mobile
Web02 | 2.8.170525.1 | Last Updated 29 Jan 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100