Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
Comments
CPallini 29-Jan-13 6:24am    
Nope.

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
 
Share this answer
 
Comments
Faisalabadians 29-Jan-13 5:40am    
can you please give me a little example?
No.
All abstract properties and methods must be implemented in the concrete class in order to fulfil the contract.
 
Share this answer
 
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++
C++
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;
   }
};
 
Share this answer
 
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)
Nope, see, for instance: What is a "pure virtual" member function?[^] at C++ FAQ.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900