Click here to Skip to main content
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 28-Jan-13 23:15pm
Comments
CPallini at 29-Jan-13 6:24am
   
Nope.
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  
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?

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Shai Vashdi 458
1 Tadit Dash 300
2 Sergey Alexandrovich Kryukov 250
3 OriginalGriff 173
4 Peter Leow 130
0 Sergey Alexandrovich Kryukov 9,435
1 OriginalGriff 5,498
2 Peter Leow 4,230
3 Maciej Los 3,540
4 Abhinav S 3,353


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