Tuesday, 8 January 2019

CPP-Slip7

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)

#include<stdio.h>
#include<conio.h>
#include<iostream.h>
class dynamic
{
                int size,*ptr,*p;
                public:
                dynamic(int no)
                {
                                size=no;
                                ptr=new int[size];
                                for(int i=0;i<size;i++)
                                {
                                                cout<<"enter element";
                                                cin>>ptr[i];
                                }
                }
                void display()
                {
                                cout<<"elements are";
                                for(int i=0;i<size;i++)
                                {
                                                cout<<ptr[i]<<"\t";
                                }
                }

                void evnodd()
                {
                                int i;
                                cout<<"even nos are : \t";
                                for(i=0;i<size;i++)
                                {
                                                if(ptr[i]%2==0)
                                                {
                                                                cout<<ptr[i];
                                                                cout<<"\t";
                                                }
                                }
                                cout<<"\n Odd nos are : \t ";
                                for(i=0;i<size;i++)
                                {
                                                if(ptr[i]%2!=0)
                                                {
                                                                cout<<ptr[i];
                                                                cout<<"\t";
                                                }
                                }
                }

                ~dynamic()
                {delete ptr;
                }

};

void main()
{
                int n;
                clrscr();
                cout<<"enter size";
                cin>>n;
                dynamic d(n);
                d.display();
                d.evnodd();
                getch();
}

                                                                                                                                   

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  

 

#include<iostream.h>

#include<conio.h>

#include<string.h>

 

class Train

{

protected :

int t_no;

char t_name[20];

public:

void getdata()

{

cout<<"\n Enter train no";

cin>>t_no;

cout<<"Enter tain name ";

cin>>t_name;

}

void display()

{

cout<<"\nEnter train no "<<t_no;

cout<<"\nEnter tain name "<<t_name;

 

}

};

 

class Route : public Train

{

 

int r_id;

char src[20];

char dest[20];

public:

void getdata()

{              Train::getdata();

cout<<"Enter route id \t";

cin>>r_id;

cout<<"Enter source \t";

cin>>src;

cout<<"Enter destination \t";

cin>>dest;

}

void display()

{              Train::display();

cout<<"\n route id "<<r_id;

cout<<"\nsource is  "<<src;

cout<<"\ndestination is "<<dest;

 

}

};

 

class Reservation : public Route

{              int seat; float f;

char cls[20];

char date[20];

public:

void getdata()

{              Route::getdata();

cout<<"Enter no of seats \t";

cin>>seat;

cout<<"Enter classs \t";

cin>>cls;

cout<<"Enter fare \t";

cin>>f;

cout<<"Enter date for reservation";

cin>>date;

}

void display()

{

Route::display();

cout<<"\nNO of seats is "<<seat;

cout<<"\nClass is  "<<cls;

cout<<"\nFare is "<<f<<"\nDate is "<<date;

cout<<"\n===============================" ;

 

}

int search(char s[])

{

if(strcmp(s,cls)==0)

{

display();

return 1;

}

return 0;

}

};

 

void main()

{

int no,ans,i;

Reservation R[10];

cout<<"\n Enter how many resevation";

cin>>no;

for(int i=0;i<no;i++)

{

R[i].getdata();

}

cout<<"Information about resrcation is : ";

for(i=0;i<no;i++)

{

R[i].display();

 

}

cout<<"\n Enter name of class ";

char scls[10];

cin>>scls  ;

int cnt=0;

for(i=0;i<no;i++)

{    ans=R[i].search(scls);

if(ans==1)

cnt++;

}

if(cnt==0)

{

cout<<"\n Record not found";

}

getch();

}