1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Final Class in C++

Discussion in 'C++' started by asadullah.ansari, Jan 16, 2008.

  1. asadullah.ansari

    asadullah.ansari TechCake

    Joined:
    Jan 9, 2008
    Messages:
    356
    Likes Received:
    14
    Trophy Points:
    0
    Occupation:
    Developer
    Location:
    NOIDA
    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:
    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:
    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:
    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:
    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
     
  2. flyingfor

    flyingfor New Member

    Joined:
    Feb 6, 2008
    Messages:
    10
    Likes Received:
    0
    Trophy Points:
    0
    why it's not inbuilt in C++ like java.
     
  3. rahul.mca2001

    rahul.mca2001 New Member

    Joined:
    Feb 13, 2008
    Messages:
    103
    Likes Received:
    0
    Trophy Points:
    0
    what is this final class,please explain i m new
     
  4. talk2mohdsaif

    talk2mohdsaif New Member

    Joined:
    Mar 8, 2009
    Messages:
    21
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    Hamirpur(hp)
    so....
    by defining destructor as private we can get final class...
    but can anyone tell me that.. is private member inherited or not...
     
  5. RRT2010

    RRT2010 New Member

    Joined:
    Apr 7, 2011
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    India
    Thanks for sharing the article :)
     
  6. msdnguide

    msdnguide New Member

    Joined:
    May 14, 2011
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    msdnguide
    Location:
    Delhi
    Home Page:
    It was asked to me in Amazon interview. Its a very good interview question
     
  7. fashionbop

    fashionbop New Member

    Joined:
    Sep 11, 2009
    Messages:
    27
    Likes Received:
    1
    Trophy Points:
    0
    Occupation:
    Shopping from China
    Location:
    Guangzhou, China
    Home Page:
    a good article
     

Share This Page