know yourselves. information, computer, 7 wonders, various.

Wednesday, November 8, 2017

C++ programming language part- 60 More about Constructor and Destructor


More about Constructor and Destructor

It is possible for a base class, a derived class, or both to contain constructor or destructor. The first base constructor is invocked followed by derived and next derived destructor is called followed by base. Because object is immediatly destroyed in any program. Virtual and non-virtual base class constructor and then derived class constructor are invocked in order of inheritance. Destructor are invocked in the reverse order.

Program 44-1
            class A              //class defined
            { public:
                 A()
                 { cout<<"constructor A invoked\n";  }
                 ~A()
                 { cout<<"destructor A invoked\n";    }
                 };
            class B              //class defined
            { public:
                 B()
                 { cout<<"constructor B invoked\n"; }
                 ~B()
                 { cout<<"destructor B invoked\n"; }
                 };



            class C:virtual public A               //class defined
            { public:
                 C()
                 { cout<<"constructor C invoked\n"; }
                 ~C()
                 { cout<<"destructor C invoked\n";   }
                 };
            class D:virtual public A               //class defined
            { public:
                 D()
                 { cout<<"constructor D invoked\n"; }
                 ~D()
                 { cout<<"destructor D invoked\n"; }
                 };
            class E              //class defined
            { public:
                 E()
                 { cout<<"constructor E invoked\n"; }
                 ~E()
                 { cout<<"destructor E invoked\n";   }
                 };
            class F:public B,public C,public D
             { private:
             E Evar;            // member objet of class E
                 public:
                 F()
                 { cout<<"constructor F invoked\n"; }
                 ~F()
                 { cout<<"destructor F invoked\n";   }
                 };
                 void main()
                 { F Fvar;
                    cout<<"Program termited\n"; }


Program 44_2
            class A             //class defined
            { private:
               int x,y;
               public:
                 A()
                 { cout<<"default constructor A invoked\n";
                 x=29;
                             y=45; }
                 ~A()
                 { cout<<"destructor A invoked\n"; }
                 void disp()
                 { cout<< "class A \n";
                    cout<<"x= "<                 };





            class B:virtual public A           //class defined
            {   public:
                 B()
                 { cout<<"constructor B invoked\n"; }
                 ~B()
                 { cout<<"destructor B invoked\n"; }
                 void disp()
                 { cout<<"class B \n"; }
                 };
            class C:virtual public A           //class defined
            {   public:
                 C()
                 { cout<<"constructor C invoked\n"; }
                 ~C()
                 { cout<<"destructor C invoked\n"; }
                 void disp()
                 { cout<<"class C \n"; }
                 };
               class D:public B,public C                 //class defined
                 { public:
                 D()
                 { cout<<"constructor D invoked\n"; }
                 ~D()
                 { cout<<"destructor D invoked\n"; }
                 };
                 void main()
                 { D K;
                  int p=234;
                  K.A::disp();
                   K.A::~A();
                   K.B::disp();
                 cout<<"Program termited\n";

                 }


Part- 86 assign the content of twostring


জ্ঞানকোষ প্রকাশনী
৩৮/২-ক, বাংলাবাজার (২য় তলা), ঢাকা।
       ফোনঃ ৭১১৮৪৪৩, ৮১১২৪৪১, ৮৬২৩২৫১.         
                                                     
কলকাতায় পরিবেশক/প্রাপ্তিস্থান
রিতা ইন্টারন্যাশনাল
৩৬, পি.এন. ব্যানার্জি রোড, কলকাতা
ফোনঃ ২৫১৩৮৩৫৯, ৯৮৩০৪৩৯৬৭৯, +৯১৯৮৩০৪৩৯৬৭৯


C++ programming language part- 61 Virtual Function and polymorphism


Virtual Function and polymorphism

Object-oriented programming refers to use of derived classes and virtual function. Suppose you have three televisions with three different VCRs. Those are requiring different and complex operation to use them. Then one day you have got a simple remote control that can operate all kinds of VCRs. That marvelous device represents the essence of object-oriented programming . That is using the same function from base class called virtual function.

Concept of Binding

Consider a class named furniture can be the base class from which the chair class is derived.  So consider the statement: chair X;  X.data_in(); . An object of the class chair named X is created and a member function called data_in(). In this case there is no ambiguity. consider the next statement again furniture *x; X->data_in(). Here is no ambiguity also. This process is called binding. In some situation may the type of object or pointer may not known at the time of compilation that are depended on the request of the user. since the object are dynamically created. This process is called dynamic binding, also known as late binding. consider the following statement.
furniture *f;
f=new chair;
f->data_in();


Virtual function with static polymorphism

Program 45_1
            //virtual class.demo
            class furniture              //class defined
            {private:
              char color[10];
              int h,w;
              public:
              void data_in()
               { cout<<"Type color,width & height \t";
                  cin>>color>>w>>h; }

              void data_out()
             { cout<
              void massage()
             { cout<<"This result from base class"<
              };
            class chair:public furniture
            { private:
               int leg_no;                              
               public:
               void data_in()      //member function
             {furniture::data_in();        // Scope Resolution Operator
               cout<<"Type number of legs\t";
               cin>>leg_no; }
              void data_out() //member function
             {furniture::data_out();       // Scope Resolution Operator
               cout<<" Number of legs are "<
             void massage()
             { cout<<" This result from derived class "<
              };
            void main()
            {furniture X;
             furniture *f;
             chair Y; 
             f=&X;
             f->data_in();
             f->data_out();
              f->massage();
             f=&Y;
             f->data_in();
             f->data_out();
              f->massage();      }

Note: The both function will generate from base class. We can modify the code from base class furniture.
virtual void data_in(); virtual void data_out();virtual void massage();

          Now it will generate from both base and derrived.




Part- 86 assign the content of twostring



জ্ঞানকোষ প্রকাশনী
৩৮/২-ক, বাংলাবাজার (২য় তলা), ঢাকা।
       ফোনঃ ৭১১৮৪৪৩, ৮১১২৪৪১, ৮৬২৩২৫১.         
                                                     
কলকাতায় পরিবেশক/প্রাপ্তিস্থান
রিতা ইন্টারন্যাশনাল
৩৬, পি.এন. ব্যানার্জি রোড, কলকাতা
ফোনঃ ২৫১৩৮৩৫৯, ৯৮৩০৪৩৯৬৭৯, +৯১৯৮৩০৪৩৯৬৭৯