CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Apr 2003
    Location
    kathmandu, nepal
    Posts
    1,570

    Abstract classes

    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.
    If there is no love sun won't shine

  2. #2
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    Re: Abstract classes

    Yes you can.
    There is no problem.
    You just need to defines some virtual pure function in its declaration.

  3. #3
    Join Date
    Nov 2002
    Location
    Ufa, Russia
    Posts
    36

    Re: Abstract classes

    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;
    
    
    }

  4. #4
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Re: Abstract classes

    Quote Originally Posted by deesan
    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...

  5. #5
    Join Date
    Nov 2002
    Location
    Ufa, Russia
    Posts
    36

    Re: Abstract classes

    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

  6. #6
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    Re: Abstract classes

    You can make a virtual destructor pure, but that has to have a body:
    Code:
     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.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  7. #7
    Join Date
    Sep 2004
    Location
    New Delhi, India
    Posts
    640

    Re: Abstract classes

    Can you pls give some more instances where we would have pure virtual functions with a body?
    "I rather not play football than wear Nerrazzuri shirt" - Paolo Maldini
    FORZA MILAN!!!

  8. #8
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    Re: Abstract classes

    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
        }
    };
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  9. #9
    Join Date
    Sep 2004
    Location
    New Delhi, India
    Posts
    640

    Re: Abstract classes

    Thanks a lot !
    "I rather not play football than wear Nerrazzuri shirt" - Paolo Maldini
    FORZA MILAN!!!

  10. #10
    Join Date
    Jan 2005
    Location
    Akron, Ohio
    Posts
    670

    Re: Abstract classes

    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();

  11. #11
    Join Date
    May 2005
    Location
    Oregon
    Posts
    3,725

    Thumbs up Re: Abstract classes

    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
    Code:
    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.

    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
        }
    };
    in COM
    Code:
    interface IX:public IUnknown
    {
    public:
    virtual void fx1()=0;
    };
    
    class use:public IX
    {
    public:
        virtual void func()
        {
            //write your code here       
        }
    }
    Last edited by humptydumpty; September 20th, 2005 at 01:25 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured