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

Monday, November 6, 2017

C++ programming language part- 70 More abuot Integer Input and Output


More abuot  Integer Input and Output

In the program 47_3, write() function has used. Here, &p is the address of the variable. The (char*) is a type cast. The type cast is required because the first parameter to both read() and write() function is a character pointer. So the address of the object has to be type cast to a character pointer. The process is  int a; char b[5]; b=(char*) &a; the other is int=a,b; b=sizeof(a); 

Think about the following program

Program 47_9
            //inherit.demo
            #include
            #include
                        class customer             //class defined
            {
              char cname[21];
              int no_people;
              char ccat;
              float fcost;
              char cstart_date[9];

          public:
              void data_in()
               {
               cout<<"Type name \t";
               cin>>cname;
               cout<<" Type number of people \t";
               cin>>no_people;
               cout<<"Type catagory(N/S) \t";     // Normal or Special
               cin>>ccat;
               cout<<"Type the cost \t";
               cin>>fcost;
               cout<<"Type start date \t";
           cin>>cstart_date;
               }

              void data_out()
               {
               cout<<"name is= "<
               cout<<"No of people are = "<
   cout<<"catagory is = "<
               cout<<"cost is = "<
               cout<<"start date is = "<
               }
             };

             class custfile
               {
               customer c1;
               public:
               void data_in()
           {
               char creply;
               fstream fil("custfile.dat",ios::out); //ios::app
               do
               {
               c1.data_in();
               fil.write((char*)&c1,sizeof(c1));    //The (char*) is a type cast, The &c1 is address
               cout<<"wish to continue ";
               cin>>creply;
               }
              while (creply=='y');
              }

              void data_out()
          {
              char creply;
              fstream fil("custfile.dat",ios::in); //ios::app
              fil.read((char*)&c1,sizeof(c1));
              while (fil)
              {
              c1.data_out();
              fil.read((char*)&c1,sizeof(c1));
              }
          }
              };

              void main()
              {
               custfile cf1;
               cf1.data_in();
               cf1.data_out();

              }


Part- 86 assign the content of twostring


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


C++ programming language part- 71 Exercise:


Exercise:

Program 47_10

Look here's a programming code with main() function which is display the menu, the options are: 1: Query  2:Accept    3: Display  and  4: Exit. Modify the class and member functions. the member data will be * customer Name, * Number of people, * Package Category, * cost, and tour start date.
 void main()
             {
             char choice;
             customer c1;
             do
             {
             cout<<"\n 1: Query  2:Accept    3: Display   4: Exit ";
             cout<<"\n\n Type choice: ";
             cin>>choice;
             if(choice=='1')
             {
            fstream fill("customer.txt",ios::in); //ios::app
                        int pos,n;
                        cout<
                        cin>>n;
                        pos=(sizeof(customer))*(n-1);
                        fill.seekg(pos,ios::beg);
                        fill.read((char*)&c1,sizeof(c1));  //type cast
                        c1.display ();
                        fill.close();  }
             else if(choice=='2')
             {
                        fstream fill("customer.txt",ios::app);
                        c1.accept();
                        fill.write((char*)&c1, sizeof(c1));  // type cast
                        fill.close();
             }
             else if(choice=='3')
             {
                        fstream fill("customer.txt",ios::in);
                        fill.read((char*)&c1,sizeof(c1));
                        while(fill)
                                    {
                                    c1.display();
                                    fill.read((char*)&c1,sizeof(c1));
                                    }
                        fill.close();
                        }
             }while(choice!='4');

             }



C++ programming language part- 72 Operator overloading


Operator overloading

This session deals with overloading of operators to make Abstract Data Types (ADTs) more natural and closer to fundamental data types. To make a user defined data type as natural as a fundamental data type, the user-defined data type must be associated with the appropriate set of operators. The fundamental data types with the operator +, -, /, and *, make the ADTs.

In the following statement
object3=object1+object2;  two object of the class named fps_distance are added and result is stored in the third object. The + operator used to add integers behaves differently when applied to the ADT fps_distance. Operator overloading refers to giving additional meaning to the normal C++ operators when they are applied to ADTs. An operator can be overloaded by defining a function for it.The function for the operator is declared using the operator  keyword. for example, if the ==operator is to be overloaded in the fps_distance class, the declaration for the operator function is shown as :
int fps_distance::operator==(fps_distance);
The function definition for the ++operator is
int operator==(fps_distance &Fps2)
{
    float fTemp1=iFeet*12+fInch;
    float fTemp2=Fps2.iFeet+Fps2.fInch;
    return ((fTemp1==fTemp2)? 1:0);
}


Program 49_1
            //operatot overloading.demo
//file output

            class fps_distance
            {private:
                        int feet;
                        float inch;
               
               public:
                        fps_distance(int ft,float in)
                        { feet=ft;
                           inch=in;  }

              void operator++(void)
                        {  feet++;  }
              void disp_dist(void)
                        { cout<<"The Distance= "<
              };

              void main()
              {  fps_distance Fps(10,10);
                  ++Fps;
                  Fps.disp_dist();  }  //display 11

The following program overloads the binary operator.

Program 49_2
            //operatot overloading.demo
        //file output
            class fps_distance
            {   private:
                 char str[20];
                  public:
                        fps_distance(char st[20]);
                        fps_distance();
                        void operator+=(fps_distance &);
                        fps_distance operator+(fps_distance &);
                        void display(void);
            };

            fps_distance::fps_distance(char st[20])
                        { strcpy(str,st);  }

            fps_distance::fps_distance()
                        { strcpy(str,"nova");}



            void fps_distance::operator+=(fps_distance &Fps2)
            {    (strcat (str,Fps2.str));                                  }
            fps_distance fps_distance::operator+(fps_distance &Fps2)
                        {  char st[20];
                            strcpy(st,str);
                            strcat(st,Fps2.str);
                            return(fps_distance(st));  }

            void fps_distance::display(void)
                        {  cout<<"\nstring is  "<


            void main()
            { fps_distance W1("NOVA"),W2("nova"),W3("Dhaka");
               W1.display();
               W1=W2+W3;

               W1.display();  }