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

Monday, November 6, 2017

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();  }




C++ programming language part- 73 Look here is a program with a class and overload the ==,>,< and += operators


Look here is a program with a class and overload the ==,>,< and += operators.

Program 49_3
            class fps_distance
            {private:
              char str[20];
              public:
                        fps_distance(char st[20]);
                        fps_distance();
                        int operator==(fps_distance &);
                        int operator>(fps_distance &);
                        int operator<(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");  }

            int fps_distance::operator == (fps_distance & Fps2)
            {          return ((strcmp(str,Fps2.str)==0)?1:0);  }

            int fps_distance::operator >(fps_distance & Fps2)
            {          return ((strcmp(str,Fps2.str)>0)?1:0);    }


            int fps_distance::operator <(fps_distance &Fps2)
            {          return ((strcmp(str,Fps2.str)<0 nbsp="" o:p="">

            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");
            clrscr();
            if(W1==W2)
                        cout<< "equal ";
            else
                        if(W1>W2)
                        cout<<" first is grater";
                        if(W1
                        cout<<"first is less";
                        W1+=W2;
            W1.display();

            W1=W2+W3;

            W1.display();  }    




C++ programming language part- 74 Linked List


Linked List

A linked list is a chain of structures in which each structure contains of data as well as pointer, which stores the address of the next logical structure in the list. We will discuss about some programs allow the user to create a linked list in reverse. An element in a linked list is called a node. Each node has two parts: the first part contains the information and second part is a pointer, which contains the address of next node. A linked list is identified by a collection of nodes, and, a pointer called start, which has the address of the first node of the list. The operation that can be performed on linked lists are: Insertion of Node, Deletion of Node, Traversal of list.
The class definition and operation for list is as follows:
class list
{ private:
node *start;      // data type is node
  public:
list()                 //  The constructor
void add_node();
void delete_node();
void traversal();  };

Inserting a Node

This involves adding a new node to the linked list. The following sequence performs the insertion of a node in the beginning of the list:
Step 1: Allocate memory for a new node
Step 2: Accept data for the Information part of the node
Step 3: Modify the content of the Start pointer so that it points to the new node
Step 4: modify the content of the next node.
The add_node() function is defined as follows:

Void list::add_node()
{
node *Fresh;
Fresh = new node;
Fresh->add_word();      // member function of class node
Fresh->next=start;
start=Fresh


Traversing a list
In order to display the contents of a list, you have to traverse the list. The following sequence performs the traversal of a list to display the Information part of each node.
Step 1: Set a temporary pointer (Temp) to start
Step 2: Repeat step 2 and 4 until Temp is not equal to NULL
Step 3: Print the Information part of the node pointed to by Temp
Step 4: Advance the pointer Temp so that points to the next node
The traversal() function is defined as follows:
void list::traversal()
{
node *Temp;
for(Temp=Start;Temp!=NULL;Temp=Temp->Next)
       {
Temp->display_node() // member function of node class
       }


}