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



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



C++ programming language part: 52 Inheritance


  Inheritance


The way to reuse existing software to create new software is by means of inheritance also called specialization or derivation. It is an important feature of object-oriented programming. The most important of them being the reusability of code. Once a class is defined and debugged, it can be used to create new subclass. The class from which another class has been derived is called the base class The class that inherits the properties of the base class is called the derived class. The derived class override some or all the properties of the base class. An object defined outside the class can access only the public member of the class. Therefore, private members of a class cannot be directly accessed from outside the class. The protected members of a class can be accessed by its member function, or within any class derived from it. Protected members behave like public members with respect to the derived class, and like private members with respect to the rest of the program.
The common syntax for deriving a class Y from a class X is: class Y:public X

Program 42_1
//inherit.demo
            class furniture              //class defined
            { protected:
              char color[10];
              int width,height;
            };
            class bookshelf:public furniture
            {  private:
               int self_no;               // member data
               public:
               void data_in()
             {
               cout<<"Type color of bookshelf (red or blue?)\t";
               cin>>color;
               cout<<"Type width\t";
               cin>>width;
               cout<<"Type height\t";
               cin>>height;
               cout<<"Type number of selves\t";
               cin>>self_no;  }
             
               void data_out() //member function
            { cout<<" The color is "<
              cout<<" The width is "<
              cout<<" The height is "<
              cout<<" Number of selves is "<
            };
            class chair:public furniture
            { private:
               int leg_no;                
               public:
             



  void data_in()      //member function
              {cout<<"Type color of chair (yellow or black?)\t";
               cin>>color;
               cout<<"Type width\t";
               cin>>width;
               cout<<"Type height\t";
               cin>>height;
               cout<<"Type number of legs\t";
               cin>>leg_no;  }
             
               void data_out() //member function
            {cout<<" The color is "<
             cout<<" The width is "<
             cout<<" The height is "<
             cout<<" Number of legs is "<
            };

void main()
            {bookshelf A;    // k is called object
             chair B;
             A.data_in();                //would be k->data_in() if pointer
             A.data_out();
             B.data_in();                 //would be k->data_in() if pointer
             B.data_out(); }


Keyword Size Of

Program 42_extra
          { char a1[10],a2[10];//array of size 10 defined
            char *a3;          //[20];  
            int i,j;
            a3=new char[20];
            cout<<"type the  string "<<"\n";
            cin>>a1;
            cout<<"type another string "<<"\n";
             cin>>a2;
            for(i=0;a1[i];i++)
            a3[i]=a1[i];
            for(j=0;a2[j];j++,i++)
            a3[i]=a2[j];
            a3[i]='\0';
            cout<<"The marged string is "<

            cout<<" The size of strings= "<




Part- 86 assign the content of twostring



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