C++

C++ Programming

Slip7

Q.1) Write a C++ program to create a class which contains single dimensional integer array of
given size. Write a member function to display even and odd numbers from a given array.
(Use Dynamic Constructor to allocate and Destructor to free memory of an object)
[Marks 15]

Solution

#include<iostream.h>
#include<conio.h>
class array
{
int a[20],*l;
public:
array()
{

}
array(int x[20])
{
a[20]=x[20];

}
void accept()
{
int i;
int n;
cout<<"Enter the how much elements";
cin>>n;
l=new int [n];
for(i=0;i<n;i++)
{
cin>>a[i];
}
}
void odd()
{
int i,n;
for(i=0;i<n;i++)
{

if(a[i]%2==0)
{
cout<<"even no"<<a[i];
}

else
{
cout<<"odd no"<<a[i];
}

}
}
~array()
{
delete(l);
cout<<"\nMemory is free";
}
};


void main()
{
clrscr();
int y[20];
array q1,q(int y[20]);
q1.accept();
q1.odd();
getch();
}


Output

Enter the how much elements
5
3
4
6
9
8
odd no3
even no4
even no6
odd no9
even no8
Memory is free


Q.2) Create a Base class Train containing protected data members as Train_no, Train_Name.
Derive a class Route (Route_id, Source, Destination) from Train class. Also derive a
class Reservation(Number_Of_Seats, Train_Class, Fare, Travel_Date) from Route. Write
a C++ program to perform following necessary functions :
i. Enter details of ‘ n’ reservations
ii. Display details of all reservations
iii. Display reservation details of a specified Train class [Marks 25]

Solution

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

class Train
{
   protected:
     int t_no;
     char t_name[20];
   public:
       void acceptT()
       {
cout<<"\n\t Enter tno and name:";
cin>>t_no;
fflush(stdin);
gets(t_name);
       }
};
class Route : public Train
{
   protected :
       int r_id;
       char source[20],dest[20];
   public:
       void acceptR()
       {
  acceptT();
  cout<<"\n\t Enter r_id,source and dest:";
  fflush(stdin);
  cin>>r_id;
  gets(source);
  gets(dest);
       }
};

class Reservation : public Route
{
   protected :
       int no_of_seats;
       char tclass[20];
       float fare;
       char tdate[20];
  public:
       void acceptRes();
       void display()
       {
  cout<<"\n\t tno :"<<t_no<<" tname:"
  <<t_name;
  cout<<"\n\t route id:"<<r_id;
  cout<<"\n\t Source :"<<source;
  cout<<"  Destination :"<<dest;
  cout<<"\n\n\t No of seats :"<<no_of_seats;
  cout<<"\n\t Class :"<<tclass;
  cout<<"\n\t Fare :"<<fare;
  cout<<"\n\t Date :"<<tdate;
  cout<<"\n  _______________________________";
       }

       int display(char *c)
       {
if(strcmp(tclass,c) == 0)
{
    display();
    return 1;
}
return 0;
       }
};
void Reservation :: acceptRes()
{
    acceptR();
    cout<<"\n\tEnter no of seats :";
    cin>>no_of_seats;
    cout<<"\n\t Enter train class:";
    fflush(stdin);    gets(tclass);
    cout<<"\n\t Enter fare :"; cin>>fare;
    cout<<"\n\t Enter date :";
    gets(tdate);
}

int main()
{
   Reservation r[10];
   int n = 0,flag;
   int ch;
   char cl[20];
   int i;
   clrscr();

   do
   {
      cout<<"\n\t1.Accept";
      cout<<"\n\t2.Display all";
      cout<<"\n\t3.Display sp. Train Class details";
      cout<<"\n\t4.Exit";
      cout<<"\n\t5.Enter your choice:";
      cin>>ch;
      switch(ch)
      {
case 1:
      if(n == 10)
      {
cout<<"\n\t Full";
break;
      }
      r[n].acceptRes();
      n++;
      break;

case 2:
      for(i=0;i<n;i++)
r[i].display();
      break;
case 3:
     cout<<"\n\t Enter train class:";
     fflush(stdin);
     gets(cl);
     flag = 0;
     for(i=0;i<n;i++)
     {
flag = r[i].display(cl);
     }
     if(flag == 0)
       cout<<"\n\t Not found";
     break;
case 4:
     break;
default:
   cout<<"\n\t Invalid choice";
      }
   }while(ch != 4);

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