Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/articles/cpp-tutorials/)
-   -   Final Class in C++ (http://www.go4expert.com/articles/final-class-cpp-t8278/)

asadullah.ansari 16Jan2008 09:57

Final Class in C++
 
Definition : Class which Can't be inherited by other class, that class is called final class.

You all knows that final class is inbuilt in java. But in C++ you have to create final class.Two types of Final class, you can create . One who want to create object of final class on Heap and other who wants to create object of Final class on stack.

1. Final class: Object of it will be on heap.
Code: CPP

class final2
{
public:
    static final2* Create()
    {
        return (new final2()) ;
    }
private:
    ~final2()
    {
       ;
    }
};

int main()
{
    final2 *f ;
    f = final2::Create() ; // Object only on Heap
}

No one can Inherit this final2 class. Suppose if one is going to inherit it then
Example:
Code: CPP

class child : public final2
{
public:
    child(){ ;}
} ;

Now Error will come.Child class can'nt inherit final class.. Because destructor of final class is private.

2. Final class: Object of it will be on Stack

Code: CPP

class temp
{
private:
    ~temp() {; }
    friend class Final; // Due to friend , Final class can use private member functions of temp class
};
class Final: virtual public temp
{
// Define all data members and functions as you want
public:
    Final()
    {
    ;
    }
    ~Final();
};

Now this final class can't be inherited by other class.Suppose one is going to inherit it to his class then,
Example:
Code: CPP

class Child : public Final
{
public:
    Child(){ ;}
    ~Child() { ;}
};

Due to temp class is inherited virtually to Final class. So when child class's constructor called, then first temp class constructor will be called. By Child class's constructor, we are calling temp class constructor. Child class can't call temp class's private destructor because child is not a friend of temp class. This is the rule of C++.
Due to this rule in C++, Complier sees that it is violating the rules, then it flashes error.

Extra Dose: In Both Example,
Example 1. In place of declaring Destructor of final2 class as private and
Example 2. In place of declaring Destructor of temp class as private
You can declare private as constructor of them. But you know that constructor can be overloaded but Destructor can'nt be Overloaded. So to restrict outside to not use with 100% safe , define Destructor as private than defining it's constructor

flyingfor 6Mar2008 10:40

Re: Final Class in C++
 
why it's not inbuilt in C++ like java.

rahul.mca2001 6Mar2008 13:10

Re: Final Class in C++
 
what is this final class,please explain i m new

talk2mohdsaif 15Nov2009 17:56

Re: Final Class in C++
 
so....
by defining destructor as private we can get final class...
but can anyone tell me that.. is private member inherited or not...

RRT2010 13Apr2011 11:05

Re: Final Class in C++
 
Thanks for sharing the article :)

msdnguide 27May2011 19:47

Re: Final Class in C++
 
It was asked to me in Amazon interview. Its a very good interview question

fashionbop 1Jul2011 17:52

Re: Final Class in C++
 
a good article


All times are GMT +5.5. The time now is 23:40.