Saturday, 16 May 2015

U4P8

Create a class ITEM with item_code, item_rate and quantity as a data members. Create an array of pointers to objects of class ITEM. Write a member function which will calculate the amount of item. Print item_code and amount of item.

Code :
 #include<iostream.h>  
 #include<conio.h>  
 class item  
 {  
      public:  
      float item_code, item_rate, quantity,amount;  
      void get()  
      {  
           cout<<"\nItem code: ";  
           cin>>item_code;  
           cout<<"Item Rate: ";  
           cin>>item_rate;  
           cout<<"Quantity : ";  
           cin>>quantity;  
      }  
      void calculate()  
      {  
           cout<<"\nCode : "<<item_code<<" :: Amount : "<<item_rate*quantity;  
      }  
 };  
 void main()  
 {  
      clrscr();  
      item o;  
      item *po[3];  
      po[3] = &o;  
      for(int i=0;i<3;i++)  
      {  
           po[i]->get();  
      }  
      for(i=0;i<3;i++)  
      {  
           po[i]->calculate();  
      }  
      getch();  
 }  


Output : 

U4P7

Write a program which include class to represent a vector(a serious of float values). Include member functions to perform the following tasks:
a> To create the vector
b> To modify the value of given elements
c> To Display the given vector in the form (10,20,30)

Code : 
 #include<iostream.h>  
 #include<conio.h>  
 #include<process.h>  
 class vector  
 {  
      public:  
      int size;  
      float v[10];  
      void create()  
      {  
           agn:  
           cout<<"\nEnter size of vector (MAX 10) : ";  
           cin>>size;  
           if(size>10){goto agn;}  
           cout<<"Enter Values for Vector : ";  
           for(int i=0;i<size;i++)  
           {  
                cin>>v[i];  
           }  
           cout<<"Vector Created.\n\n";  
      }  
      void modify()  
      {  
           int old1,new1,flag=0;  
           cout<<"\nOld Element Value : ";  
           cin>>old1;  
           cout<<"New Element Value : ";  
           cin>>new1;  
           for(int i=0;i<size;i++)  
           {  
                if(v[i]==old1)  
                {  
                     v[i] = new1;  
                     flag = 1;  
                }  
           }  
           if(flag==0)  
           {  
                cout<<"\nElement Not Found..";  
           }  
           cout<<"Vector Modified.\n\n";  
      }  
      void display()  
      {  
           cout<<"\nVector V() = (";  
           for(int i=0;i<size;i++)  
           {  
                cout<<v[i];  
                if(i<size-1)  
                {  
                cout<<",";  
                }  
           }  
           cout<<")\n\n";  
      }  
 };  
 void main()  
 {  
      clrscr();  
      vector vec;  
      int task;  
      again:  
      cout<<"Select Vector Task: (1)Create (2)Modify (3)Display (0)Exit || choice: ";  
      cin>>task;  
      switch(task)  
      {  
      case 0:  
           exit(0);  
      case 1:  
           vec.create();  
           goto again;  
           //break;  
      case 2:  
           vec.modify();  
           goto again;  
           //break;  
      case 3:  
           vec.display();  
           goto again;  
           //break;  
      default:  
           cout<<"\n==BAD CHOICE==";  
           goto again;  
           //break;  
      }  
      /*  
      char ch;  
      cout<<"\nCONTINUE? (y/n) : ";  
      cin>>ch;  
      if(ch == 'y' || ch == 'Y')  
      {  
           goto again;  
      }  
      else  
      {  
           exit(0);  
      } */  
      getch();  
 }  


Output :

U4P5

Create a class Account. It has three data member account id, name and balance. Define function to assign value and display value. Define function that search account number given by the user. If account number exists, print detail of that account. Write a program using array of object. Declare at least 5 account and print details.

Code : 
 #include<iostream.h>  
 #include<conio.h>  
 int setflag = 0;  
 class account  
 {  
      public:  
      int ac_id, bal;  
      char name[5];  
      void get()  
      {  
           cout<<"Enter Account id : ";  
           cin>>ac_id;  
           cout<<"Enter Name of the account holder : ";  
           cin>>name;  
           cout<<"Enter available balance : ";  
           cin>>bal;  
      }  
      void show()  
      {  
           cout<<"\n\nID : "<<ac_id;  
           cout<<"\nName : "<<name;  
           cout<<"\nBalance : "<<bal;  
      }  
      void search(account ob,int s_id)  
      {  
           if(ob.ac_id == s_id)  
           {  
                setflag = ob.ac_id;  
           }  
      }  
 };  
 void main()  
 {  
      int sr_id;  
      clrscr();  
      account ac[5];  
      for(int d=0;d<5;d++)  
      {  
           ac[d].get();  
      }  
      for(d=0;d<5;d++)  
      {  
           ac[d].show();  
      }  
      cout<<"\nEnter the ID to search : ";  
      cin>>sr_id;  
      for(d=0;d<5;d++)  
      {  
           ac[d].search(ac[d],sr_id);  
      }  
      if(setflag == 0)  
      {  
           cout<<"\nRecord Not Found..";  
      }  
      else  
      {  
           cout<<"\nRecord Found : ";  
           ac[setflag-1].show();  
      }  
      getch();  
 }  


Output : 

U4P4

Define Class named point which represents 2-D Point, i.e P(x, y). Define Default Constructor to initialize both data member value 5, parameterized constructor to initialize member according to value supplied by user and Copy constructor. Define Necessary Function and Write a program to test class Point.

Code : 
 #include<iostream.h>  
 #include<conio.h>  
 class point  
 {  
      public:  
      int x,y;  
      point()  
      {  
           x=5;  
           y=5;  
      }  
      point(int a,int b)  
      {  
           x=a;  
           y=b;  
      }  
      point(point &p)  
      {  
           x=p.x;  
           y=p.y;  
      }  
      void show()  
      {  
           cout<<"(X,Y) : ("<<x<<","<<y<<")"<<endl<<endl;  
      }  
 };  
 void main()  
 {  
      clrscr();  
      cout<<"Default Constructor Value :\n";  
      point p1;  
      p1.show();  
      cout<<"\nParameterized Constructor Value :\n";  
      point p2(8,3);  
      p2.show();  
      cout<<"\nCopy Constructor Value :\n";  
      point p3(p2);  
      p3.show();  
      getch();  
 }  


Output :

U4P3

Write a class called “arithmetic” having two integer and one character data members. It performs the operation on its integer members indicated by character member (+, -, *, /). For example * indicates multiplication on data members as d1 * d2. Write a class with all necessary constructors and methods to perform the operation and print the operation performed in format Ans = d1 op d2. Test your class using main().

Code :
 #include<iostream.h>  
 #include<conio.h>  
 class arithmetic  
 {  
      int a,b;  
      char op;  
      public:  
      arithmetic()  
      {  
           cout<<"Give the value of A and B : ";  
           cin>>a>>b;  
           cout<<"\nSelect Operation (+,-,*,/) : ";  
           cin>>op;  
      }  
      void show()  
      {  
           cout<<"\n A "<<op<<" B :== "<<a<<" "<<op<<" "<<b<<" :== ";  
           if(op == '+')  
           {  
                cout<<a+b;  
           }  
           else if(op == '-')  
           {  
                cout<<a-b;  
           }  
           else if(op == '*')  
           {  
                cout<<a*b;  
           }  
           else if(op == '/')  
           {  
                cout<<a/b;  
           }  
           else  
           {  
                cout<<"\nERROR : INVALID CHARACTER";  
           }  
      }  
 };  
 int main()  
 {  
      clrscr();  
      arithmetic obj;  
      obj.show();  
      getch();  
      return 0;  
 }  


Output :

U4P2

Write a program that demonstrates the Static Data Member And static member function.

Code :
 #include<iostream.h>  
 #include<conio.h>  
 class test  
 {  
      int code;  
      static int count;  
      public:  
      void setcode()  
      {  
           code=++count;  
      }  
      void showcode()  
      {  
           cout<<"\nObject Num : "<<code;  
      }  
      static void showcount()  
      {  
           cout<<"\nCount : "<<count;  
      }  
 };  
 int test::count;  
 void main()  
 {  
      clrscr();  
      test t1,t2;  
      t1.setcode();  
      t2.setcode();  
      test::showcount();  
      test t3;  
      t3.setcode();  
      test::showcount();  
      t1.showcode();  
      t2.showcode();  
      t3.showcode();  
      getch();  
 }  

Output :