Click here to Skip to main content
11,477,625 members (75,499 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++
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 29-Jan-13 0:15am
Comments
CPallini at 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 at 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 at 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 at 29-Jan-13 6:00am
   
Implement the function! :laugh:
Faisalabadians at 29-Jan-13 6:22am
   
:( you laughing :(
OriginalGriff at 29-Jan-13 7:00am
   
I'm laughing because it is kinda obvious!
Faisalabadians at 29-Jan-13 7:11am
   
I am new to C++
OriginalGriff at 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 at 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 at 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
0 Sergey Alexandrovich Kryukov 239
1 Richard Deeming 225
2 Tomas Takac 150
3 Sascha Lefèvre 145
4 _duDE_ 140
0 Sergey Alexandrovich Kryukov 7,675
1 OriginalGriff 7,256
2 Sascha Lefèvre 3,034
3 Maciej Los 2,491
4 Richard Deeming 2,305


Advertise | Privacy | Mobile
Web01 | 2.8.150520.1 | Last Updated 29 Jan 2013
Copyright © CodeProject, 1999-2015
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