Final Class in C++

asadullah.ansari's Avatar author of Final Class in C++
This is an article on Final Class in C++ in C++.
Rated 4.00 By 1 users
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
talk2mohdsaif like this
0
flyingfor's Avatar
Go4Expert Member
why it's not inbuilt in C++ like java.
0
rahul.mca2001's Avatar, Join Date: Feb 2008
Ambitious contributor
what is this final class,please explain i m new
0
talk2mohdsaif's Avatar, Join Date: Mar 2009
Go4Expert Member
so....
by defining destructor as private we can get final class...
but can anyone tell me that.. is private member inherited or not...
0
RRT2010's Avatar, Join Date: Apr 2011
Newbie Member
Thanks for sharing the article
0
msdnguide's Avatar, Join Date: May 2011
Go4Expert Member
It was asked to me in Amazon interview. Its a very good interview question
0
shzlw's Avatar, Join Date: Jul 2011
Light Poster
a good article