Click to See Complete Forum and Search --> : Abstract classes
miteshpandey
July 23rd, 2005, 12:27 PM
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.
SuperKoko
July 23rd, 2005, 12:45 PM
Yes you can.
There is no problem.
You just need to defines some virtual pure function in its declaration.
deesan
July 23rd, 2005, 01:03 PM
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.
#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;
}
Andreas Masur
July 23rd, 2005, 02:17 PM
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.
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...
deesan
July 23rd, 2005, 02:30 PM
Well...even pure virtual functions can have a default implementation...
Could you please provide little example of such default implementation? Looks like i missed something here :)
Graham
July 23rd, 2005, 04:31 PM
You can make a virtual destructor pure, but that has to have a body:
class foo
{
public:
virtual ~foo() = 0;
};
foo::~foo()
{
}
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.
logan
July 24th, 2005, 12:22 PM
Can you pls give some more instances where we would have pure virtual functions with a body?
Graham
July 24th, 2005, 04:41 PM
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.
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
}
};
logan
July 26th, 2005, 01:02 AM
Thanks a lot :)!
paradoxresolved
September 20th, 2005, 12:42 AM
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();
humptydumpty
September 20th, 2005, 01:22 AM
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
class a
{
public :
virtual void fx1()=0;
};
is know as abstract classs.
we can see use of abstract class in com implementation in com it is used very frequestly.
Remaining thing graham Explain ver Clearly.
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
}
};
in COM
interface IX:public IUnknown
{
public:
virtual void fx1()=0;
};
class use:public IX
{
public:
virtual void func()
{
//write your code here
}
}
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.