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)
//Note: If you are not using Borland or Turbo C then add the following line after header files :
//using namespace std;
#include<conio.h>
#include<iostream.h>
class arr
{
public:
int *a,i,n; //declare pointer a
arr()
{
cout<<"Enter how many no in an array: ";
cin>>n;
a=new int [n]; //allocating memory dynamically
for(i=0;i<n;i++)
{
cout<<"Enter an elements: ";
cin>>a[i];
}
}
void dis()
{
cout<<"1 D matrix is: "<<endl;
for(i=0;i<n;i++)
{
cout<<a[i]<<" ";
}
}
void check()
{
cout<<"\nEven numbers are: "<<endl;
for(i=0;i<n;i++)
{
if(a[i]%2==0)
{
cout<<a[i]<<" ";
}
}
cout<<"\nOdd numbers are: "<<endl;
for(i=0;i<n;i++)
{
if(a[i]%2!=0)
{
cout<<a[i]<<" ";
}
}
}
~arr()
{
delete a; //memory destroyed dynamically
cout<<"\n1 D array is destroyed.";
getch();
}
};
void main()
{
arr t; //Instance of class arr is created
t.dis(); //calling member functions
t.check();
getch();
}
-
Next 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
-
Previous Write a C++ program to create a class which contains two dimensional integer array of size mXn. Write a member function to display sum of all elements of entered matrix. (Use Dynamic Constructor for allocating memory and Destructor to free memory of an object)
0 Comments