How to postpone implementation of abstract class in c++
-
can anybody let me how to postpone implementation of abstract class methods
#include<iostream>
using namespace std;
class abstrs
{
public:
virtual int add(int i,int j)=0;
virtual int Substract(int i,int j)=0;};
class derieved : public abstrs
{
public :
virtual int add(int i,int c)
{
return 100;}//i dont want to implement this piece of code as of now. /\*virtual int Substract(int i,int c) { return 100; }\*/
};
int main()
{
derieved dr;
int count=dr.add(12,34);
cout<<g;}
vikas da
-
can anybody let me how to postpone implementation of abstract class methods
#include<iostream>
using namespace std;
class abstrs
{
public:
virtual int add(int i,int j)=0;
virtual int Substract(int i,int j)=0;};
class derieved : public abstrs
{
public :
virtual int add(int i,int c)
{
return 100;}//i dont want to implement this piece of code as of now. /\*virtual int Substract(int i,int c) { return 100; }\*/
};
int main()
{
derieved dr;
int count=dr.add(12,34);
cout<<g;}
vikas da
If you don't, you won't be able to create an instance of derived. Derived will still be abstract. You'll have to derive another layer and define Subtract there.
Once you agree to clans, tribes, governments...you've opted for socialism. The rest is just details.
-
If you don't, you won't be able to create an instance of derived. Derived will still be abstract. You'll have to derive another layer and define Subtract there.
Once you agree to clans, tribes, governments...you've opted for socialism. The rest is just details.
-
can anybody let me how to postpone implementation of abstract class methods
#include<iostream>
using namespace std;
class abstrs
{
public:
virtual int add(int i,int j)=0;
virtual int Substract(int i,int j)=0;};
class derieved : public abstrs
{
public :
virtual int add(int i,int c)
{
return 100;}//i dont want to implement this piece of code as of now. /\*virtual int Substract(int i,int c) { return 100; }\*/
};
int main()
{
derieved dr;
int count=dr.add(12,34);
cout<<g;}
vikas da
The best way of avoiding the need to implement member functions (C++ doesn't have methods) is to avoid deriving a class from an abstract class. So the first thing to ask is... why are you writing code like you have in your main? Derived doesn't actually need to implement any abstract classes so just implement what you need to get your code compiling:
class adder
{
public:
int add( int i, int j ) const { return i + j; }
};int main()
{
std::cout << adder().add( 12, 34 ) << std::endl;
}You only need abstract classes as your code gets bigger and you want to start cutting down on the dependencies between lumps of code. You use abstract classes as a design tool to avoid most of your code needing to know what concrete types they're dealing with. If on the other hand you've got an interface you need to implement to use a particular lump of code then you haven't got a lot of choice but to implement the member functions. You could take the Java route and implement a minimal class and then derive from that:
class adder
{
public:
virtual int add( int i, int j ) = 0;
};class minimal_adder
{
public:
virtual int add( int, int ) { return 0; }
};But that can turn into a maintenance nightmare (you have to look at two implementations to find out where a member function is implemented, not just one. It gets worse when some wit adds another level, then another...). So the points here are: - Don't use an abstract class until you need to, never do it "just in case" [1] - When you create abstract classes try and create them from the existing interface of a concrete class. You'll at least know there's client code (and unit tests) ready to use your new abstract class against - Only use Java style stub implementations as a last resort (some chunk of client code is expecting an interface with 30 member functions, half of which you have no idea what the contracts are) Cheers, Ash [1] Needing to includes using one as a firewall between code you're writing and code your colleagues are writing. If you sit down with a co-worker that needs a service from code you're writing hack out a quick interface together, stub implement it for them and then take your time implementing it properly.
-
Thanks Tim, so we need to have implementation of all methods defined in abstract class to the single derived class to instantiate the class. am i right..?
vikas da
-
can anybody let me how to postpone implementation of abstract class methods
#include<iostream>
using namespace std;
class abstrs
{
public:
virtual int add(int i,int j)=0;
virtual int Substract(int i,int j)=0;};
class derieved : public abstrs
{
public :
virtual int add(int i,int c)
{
return 100;}//i dont want to implement this piece of code as of now. /\*virtual int Substract(int i,int c) { return 100; }\*/
};
int main()
{
derieved dr;
int count=dr.add(12,34);
cout<<g;}
vikas da
-