C++

C++ Programming

Slip5

Q.1) Write a C++ program to create a class Item with data members Item_Code, Item_Name,
Item_Price. Write member functions to accept and display Item information also display
number of objects created for a class. (Use Static data member and Static member
function) [Marks 15]

Solution

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class item
{
 private:
int icode;
char iname[20];
char iprice[20];
static int cnt;

 public:
item()
  {
   cnt++;
  }
void accept()
{
cout<<"Enter the item code:";
cin>>icode;
cout<<"Enter the item name:";
gets(iname);
  cout<<"Enter the price:";
cin>>iprice;
       }
void display()
{
  cout<<"\nITEM  CODE IS: "<<icode;
  cout<<"\nItem nmae IS: "<<iname;
  cout<<"\nItem price IS: "<<iprice;
}
static void count()
{
cout<<"\nCount ="<<cnt;
}
};
int item:: cnt=0;
void main()
{
 item i1,i2,i3,i4;
 clrscr();
 i1.accept();
 i2.accept();
 i1.display();
 i2.display();
 cout<<"\ndisplay number of object crated by class";
 item::count();
 getch();
}


Output

Enter the item code:123
Enter the item name:aaa                                                       
Enter the price:1000                                                         
Enter the item code:223                                                       
Enter the item name:bbb                                                       
Enter the price:500                                                           
                                                                             
ITEM  CODE IS: 123                                                           
Item nmae IS: aaa                                                             
Item price IS: 1000                                                           
ITEM  CODE IS: 223                                                           
Item nmae IS: bbb                                                             
Item price IS: 500                                                           
display number of object crated by class                                     
Count =4                                                                     
             


Q.2) Write C++ program to Create two classes Array1 and Array2 which contains data
members as Integer array of a specified size. Write necessary member functions to accept
and display array elements of both the classes. Find and display smallest number from
both the array. (Use Friend function and Memory Management operators)
[Marks 25]

Solution

#include<iostream.h>
#include<conio.h>

class Array2;       //forward declaration

class Array1
{
  int *a;
  int n;
  public:
      Array1(int);
      void display();
      friend int findmin(Array1,Array2);
};

Array1 :: Array1(int s)
{
   n = s;
   a = new int[n];
   cout<<"\n\t First Array ";
   for(int i=0;i<n;i++)
   {
     cout<<"\n\t Enter a["<<i<<"]=";
     cin>>a[i];
   }
}

void Array1 :: display()
{
   cout<<"\n\t Array1 =";
   for(int i=0;i<n;i++)
     cout<<"  "<<a[i];
}

class Array2
{
  int *a;
  int n;
  public:
       Array2(int);
       void display();
       friend int findmin(Array1,Array2);
       ~Array2() { delete a;}
};
Array2 :: Array2(int s)
{
    n = s;
    a = new int[n];
    cout<<"\n\t For Array2 ";
    for(int i=0;i<n;i++)
    {
      cout<<"\n\tEnter a["<<i<<"]=";
      cin>>a[i];
    }
}

void Array2 :: display()
{
   cout<<"\n\t Array2 =";
   for(int i=0;i<n;i++)
    cout<<" "<<a[i];
}

int findmin(Array1 obj1,Array2 obj2)
{
   int min = obj1.a[0];
   for(int i=1;i<obj1.n;i++)
   {
      if(obj1.a[i] < min)
min = obj1.a[i];
   }
   for(i=0;i<obj2.n;i++)
   {
     if(obj2.a[i] < min)
       min = obj2.a[i];
   }
   return min;
}

int main()
{
   int n;
   cout<<"\n\tEnter size of first array = ";
   cin>>n;
   Array1 obj1(n);

   cout<<"\n\t Enter size of second array = ";
   cin>>n;
   Array2 obj2(n);

   int min = findmin(obj1,obj2);

   obj1.display();
   obj2.display();
   cout<<"\n\t Minimum is "<<min;

   getch();
   return 0;
}

Output

Enter size of first array = 3

First Array
Enter a[0]=3

Enter a[1]=5

Enter a[2]=7

Enter size of second array = 3

For Array2
Enter a[0]=2

Enter a[1]=4

Enter a[2]=6

Array1 =  3  5  7
Array2 = 2 4 6
Minimum is 2
BCA Pratical Solution

My name is Vivek And I from Mumbai and Complete my Graduation Bca.my Age is 23 Years.

Post a Comment

Previous Post Next Post