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

Saturday, November 11, 2017

C++ programming language part: 51Destructor


Destructor

Destructor are functions that are complimentary to constructor. They de-initialize objects when they are destroyed. A destructoris invocked when an object of the class goes out of scope, or when the memory occupied by it is deallocated using the delete operator. It is also the same name that of the class but is prefixed with a ~(tilde) sign.

Program 40_3
//class & object.demo
      #include
            #include

            class nova                    //class defined
            {
            private:
               int p1,p2,p3;   // we can't type p1=5 likk, need a 'constructor'

            public:
           nova();               //default constructor
               nova(int,int);              // defined constructor
               void data_in(int,int);       // member function
           void sum();
               void data_out();
           ~nova();
               };       
               nova::nova()
               {  cout<<" Look! It's invocked "<
             // p1=p2=p3=0;
               }
               nova::nova(int x,int y)   // constructor & class would be the same name
               {
                cout<<" two constructor are called "<
                p1=x;
                p2=y;
              
               }
            void nova::data_in(int a,int b)              //member function
               {
                p1=a;
                p2=b;
               }
               void nova::sum()
           {
           p3=p1+p2;
           }
               void nova::data_out()           //scope resolution operator
               {
                cout<<" The sum is= "<
               }

               nova::~nova()
               {
               p1=p2=p3=0;
               cout<<" destructor invocked"<
                                            //argument are called so two defferent
                                                            // constructor and destructor are called.
               }


               void main()
               {
               nova k1,k2(4,3),*k3;                        // k is called object
               k3=new nova(5,10);  // new value are assign
               k2.sum();                  //this is from initialize member data
               k3->sum();
               k3->data_out();
           k2.data_out();
          }

 

Exercise: write a class called word, which will store a string. Include following member function.

@  void store_word(char *cstr) which store the incoming string passed as its parameter.
@  void add_word(char *cstr)., which adds the incoming to the existing content of word
@  void display_word(void), which displays the content of word on standard output[strlen,cat,cpy etc]
@  use default constructor and destructor.




Part- 86 assign the content of twostring



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