Suppose a class is derived from a concrete base class.
Can we make the derived class abstract. I haven't tried this out. Is it permitted? Does it have any use or Is it just senseless.
Printable View
Suppose a class is derived from a concrete base class.
Can we make the derived class abstract. I haven't tried this out. Is it permitted? Does it have any use or Is it just senseless.
Yes you can.
There is no problem.
You just need to defines some virtual pure function in its declaration.
C++ doesn't have some specific keyword to mark class as abstract (like "abstract" in C#)
But you can prevent class from direct using by adding to it's declaration so-called "pure" functions. Pure function is a virtual function that doesn't have body and should be implemented in derived classes.
Code:#include <iostream>
#include <string>
using namespace std;
class Man // concrete class
{
public:
string name;
void Introduce()
{
cout<<"My name is "<<name<<endl;
}
};
class Employee: public Man // abstract class derived from Man.
{
public:
virtual void TalkAboutWork()=0; // pure function
virtual ~Employee() {}
};
class Manager: public Employee
{
public:
virtual void TalkAboutWork()
{
cout<<"I'm a manager"<<endl;
}
virtual ~Manager() {}
};
class Programmer: public Employee
{
public:
virtual void TalkAboutWork()
{
cout<<"I'm a programmer"<<endl;
}
virtual ~Programmer() {}
};
int main(int argc, char* argv[])
{
Employee *m = new Manager;
m->name = "Alex";
m->Introduce();
m->TalkAboutWork();
delete m;
Employee *p = new Programmer;
p->name = "Bob";
p->Introduce();
p->TalkAboutWork();
delete p;
}
Well...even pure virtual functions can have a default implementation...a pure virtual function is indicated by the '=0' at the end of its declaration...Quote:
Originally Posted by deesan
Could you please provide little example of such default implementation? Looks like i missed something here :)Quote:
Well...even pure virtual functions can have a default implementation...
You can make a virtual destructor pure, but that has to have a body:
On the original point, though: yes, it can be done, but it might suggest a design problem. The general advice is that only "leaf" nodes in a hierarchy should be concrete, all others should be abstract. By deriving an abstract class from a concrete class, you are making a non-leaf class concrete. This can lead to problems later on. See Effective C++by Scott Meyers for more detail on this subject.Code:class foo
{
public:
virtual ~foo() = 0;
};
foo::~foo()
{
}
Can you pls give some more instances where we would have pure virtual functions with a body?
The simpelst reason for doing it is to avoid a derived class "accidentally" inheriting a default implementation of a virtual function. Suppose you have a base class and there is an implementation of a certain function that most derived classes can use quite happily. A few, however, need to override the function in order for them to be used correctly. It makes sense to provide the "default" implementation in the base class so that you don't have to keep rewriting it in derived classes. If the base implementation is a plain virtual function, then a derived class will inherit the base function if the programmer forgets to override it. In some circumstances, this could be disastrous. One solution, where it is deemed necessary, is to make the default implementation pure. That way, no class can accidentally inherit it. If you want to use, then you have to provide a one-line override that calls the base function. This simply ensures that using the base implementation is a deliberate choice, not an accident.
Code:class base
{
protected:
virtual void func() = 0;
};
void base::func() { /* default implementation */ }
class derived : public base
{
private:
virtual void func()
{
base::func();
}
};
class different : public base
{
private:
virtual void func()
{
// different implementation
}
};
Thanks a lot :)!
Judging by the above code, that looks like a simple way to convert an inherited public or protected function into some other access qualifier. Just offhand, does anyone know of a direct way to do this, such as (in EngliC++):
Convert_from_protected_to_public:
void func1();
or. . .
protected -> public:
void func1();
First thing Regarding Abstract Class
As we know a Class Contain atleast One Pure Virtual Function Know as Abstract class.we can't instantiate a abstract class directly but with the help of some derived class for which the base class is our abstract class we can use it.A Pure virtual function Initialize with 0 also know as pure access specifie.Main thing use of abstract class to hide your Implement part
like
virtual void fx1()=0; here 0 is pure access specifier
and if we put this in a class
like
is know as abstract classs.Code:class a
{
public :
virtual void fx1()=0;
};
we can see use of abstract class in com implementation in com it is used very frequestly.
Remaining thing graham Explain ver Clearly.
in COMCode:class base
{
protected:
virtual void func() = 0;
};
void base::func() { /* default implementation */ }
class derived : public base
{
private:
virtual void func()
{
base::func();
}
};
class different : public base
{
private:
virtual void func()
{
// different implementation
}
};
Code:interface IX:public IUnknown
{
public:
virtual void fx1()=0;
};
class use:public IX
{
public:
virtual void func()
{
//write your code here
}
}