Pages

Derive two classes triangle and rectangle from the base class shape having data members length and breadth. Create functions to find the area of the triangle and the rectangle by runtime polymorphism

#include<iostream.h>
#include<conio.h>
class shape
{
   protected:
      double x;
      double y;
   public:
      virtual void display(){}
      void getdata(){cout<<"\nEnter x and y\n";cin>>x>>y;}
};
class triangle:public shape
{
   public:
      void display(){cout<<"\nArea  of triangle="<<(x*y*0.5);}
};
class rectangle: public shape
{
   public:
      void display(){cout<<"\nArea of rectangle="<<(x*y);}
};
void main()
{
   clrscr();
   shape *ptr;
   triangle t;
   rectangle r;
   ptr=&t;
   ptr->getdata();
   ptr->display();
   ptr=&r;
   ptr->getdata();
   ptr->display();
   getch();
}

Please make modifications

Write a program in CPP to automate a banking system by facilitating creation of accounts, deposit, withdrawal and searching using files. Use an appropriate Bank class.

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<iomanip.h>
#include<stdlib.h>
#include<stdio.h>
class DEPOSITOR
{
      int acno;
      char name[30];
      int balance;
   public:
      void getdata(void);
      void showdata(void);
      void deposit(int);
      void withdraw(int);
};
void DEPOSITOR::deposit(int d_amt)
{
   balance = balance + d_amt;
}
void DEPOSITOR::withdraw(int w_amt)
{
   if (w_amt > balance)
      cout << "No sifficient Amount in the Account"<<endl;
   else
      balance = balance - w_amt;
}
void DEPOSITOR::getdata()
{
   cout<<"Enter Account No : ";
   cin >>acno;
   cout<<"Enter the Name : ";
   cin >>name;
   cout<<"Enter Amount : ";
   cin >>balance;
}
void DEPOSITOR::showdata()
{
   cout.setf(ios::left,ios::adjustfield);
   cout<<setw(5)<<acno;
   cout<<setw(20)<<name;
   cout<<setw(10)<<balance<<endl;
}
void main()
{
   DEPOSITOR d;
   int opt, ac;
   int a_amt;
   fstream file;
   file.open("DEPOSITOR.DAT", ios::in | ios::out);
   clrscr();
   do
   {
      d.getdata();
      file.write((char*)&d, sizeof(d));
      flushall();
      cout<<"Enter 1 to Continue...";
      cin>>opt;
   }
   while(opt==1);
   file.seekg(0);
   while (1)
   {
       cout<<"Menu"<<endl;
       cout<<"1. Deposit"<<endl;
       cout<<"2. Withdrawal"<<endl;
       cout<<"3. View"<<endl;
       cout<<"4. Exit"<<endl;
       cout<<"Enter Your Option : ";
       cin>>opt;
       if(opt==4)
   break;
       if(opt==1)
       {
   cout<<"Enter The Account Number : ";
   cin>>ac;
   cout<<"Enter The Amount to be Deposited : ";
   cin>>a_amt;
   int loc = (ac - 1)*sizeof(d);
   file.seekg(loc);
   file.read((char*)&d,sizeof(d));
   if(file.eof() != 0) break;
   d.showdata();
   d.deposit(a_amt);
   d.showdata();
      file.seekp(loc);
   file.write((char*)&d, sizeof(d));
   flushall();
       }
       if(opt==2)
       {
   cout<<"Enter The Account Number : ";
   cin>>ac;
   cout<<"Enter The Amount to be Withdrew : ";
   cin>>a_amt;
   int loc = (ac - 1)*sizeof(d);
   file.seekg(loc);
   file.read((char*)&d,sizeof(d));
   if(file.eof() != 0) break;
   d.showdata();
   d.withdraw(a_amt);
   d.showdata();
   file.seekp(loc);
   file.write((char*)&d, sizeof(d));
   flushall();
       }
       if(opt==3)
       {
   cout<<"Enter The Account Number : ";
   cin>>ac;
   int loc = (ac - 1)*sizeof(d);
   file.seekg(loc);
   file.read((char*)&d,sizeof(d));
   if(file.eof() != 0) break;
   d.showdata();
       }
   }
   cout<<"==================================================="<<endl;
   file.close();
}

Please do modifications and send to mes.murugan@gmail.com

Create a virtual base class student that stores rollno with member functions getno( ) and putno( ). From this derive a class test with data members mark1 and mark2 and member functions getmarks( ) and putmarks( ) and derive another class sports with data member score and member functions getscore( ) and putscore( ). From test and sports classes derive the class result that stores total mark. Write a CPP program to test the classes.

#include<iostream.h>
#include<conio.h>
class student
{
   protected:
      int rno;
   public:
     void getno(){cout<<"\n\n            Roll no.: ";cin>>rno;}
     void putno(){cout<<"\n            Roll no.:"<<rno;}
};
class test : virtual public student
{
   protected :
      int m1,m2;
   public:
      void getmark(){cout<<"Marks of 2 subjects : ";cin>>m1>>m2;}
      void putmark(){cout<<"\nMarks of 2 subjects : "<<m1<<","<<m2;}
};
class sports : virtual  public student
{
   protected:
      int score;
   public:
      void getscore(){cout<<"        Sports mark : ";cin>>score;}
      void putscore(){cout<<"\n        Sports mark : "<<score;}
};
class result:public test,public sports
{
      int total;
   public:
      void display(){total=m1+m2+score;putno();putmark();putscore();
       cout<<"\n        Total Marks : "<<total;}
};
void main()
{
   result r[10];
   int n,i;
   clrscr();
   cout<<"\n Enter no: of students : ";
   cin>>n;
   clrscr();
   cout<<"\n\n  Enter details of student\n";
   for(i=1;i<=n;i++)
   {
      cout<<"\n Enter details of student : "<<i;
      r[i].getno();
      r[i].getmark();
      r[i].getscore();
   }
   clrscr();
   cout<<"\n Details of student\n\n";
   for(i=1;i<=n;i++)
   {
      cout<<"\n\n---------------------------------------------";
      cout<<"\n Details of student : "<<i<<endl;
      r[i].display();
      cout<<"\n\n---------------------------------------------";
   }
   getch();
}

A publication company that markets both book and audio-cassettes. Create a class “publication” that stores the title and price. From this class derive two classes “book” which adds a page count and “tape” which adds a playing time in minutes. Each of the three classes have a display function to display the details of the book and tape. Implement this with the use of runtime polymorphism.

#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
class publication
{
   protected:
      char title[30];
      float price;
   public:
      publication(char *s, float a){strcpy(title,s);price=a;}
      virtual void display(){}
};
class book: public publication
{
      int pages;
   public:
      book(char *s, float a, int p):publication(s,a){pages=p;}
      void display();
};
class tape: public publication
{
      float time;
   public:
      tape(char *s, float a, float t):publication(s,a){time=t;}
      void display();
};
void book::display(){cout<<endl<<"    Title : "<<title
    <<endl<<"    Pages : "<<pages
    <<endl<<"    Price : "<<price;}
void tape::display(){cout<<endl<<"    Title : "<<title
    <<endl<<"Play Time : "<<time<<" Minutes"
    <<endl<<"    Price : "<<price;}
void main()
{
   char * title = new char[30];
   float price, time;
   int pages;
   clrscr();

   // Book Details
   cout<<endl<<"Enter Book Details"<<endl;
   cout<<"Title : ";gets(title);
   cout<<"Price : ";cin>>price;
   cout<<"Pages : ";cin>>pages;

   book book1(title,price,pages);

   // Tape Details
   cout<<endl<<"Enter Tape Details"<<endl;
   cout<<"Title : ";gets(title);
   cout<<"Price : ";cin>>price;
   cout<<"Play Time(in Mins) : ";cin>>time;

   tape tape1(title,price,time);

   publication* list[2];
   list[0]=&book1;
   list[1]=&tape1;

   cout<<endl<<"------------------------------";
   cout<<endl<<"Media Details";
   cout<<endl<<"-------------";
   cout<<endl<<"Book Details";
   cout<<endl<<"------------------------------";
   list[0]->display();
   cout<<endl<<"==============================";
   cout<<endl<<"Tape Details";
   cout<<endl<<"------------------------------";
   list[1]->display();
   cout<<endl<<"==============================";
   getch();
}

A publication company that markets both book and audio-cassettes. Create a class “publication” that stores the title and price. From this class derive two classes “book” which adds a page count and “tape” which adds a playing time in minutes. Each of the three classes should have a getdata( ) and putdata( ) function. Write a main function to test the book and tape classes by creating instances of them, and fill their data with getdata( ), and then displaying the data with putdata( )

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class publication
{
      char title[30];
      float price;
   public:
      void getdata(){cout<<endl<<"Enter Title : ";gets(title);
       cout<<"Enter Price : ";cin>>price;}
      void putdata(){cout<<endl<<"      Title : "<<title
    <<endl<<"      Price : "<<price;}
};
class book:public publication
{
      int pagecount;
      publication p;
   public:
      void getdata(){p.getdata();cout<<"Enter Pages : ";cin>>pagecount;}
      void putdata(){p.putdata();cout<<endl<<"No of Pages : "<<pagecount;}
};
class tape:public publication
{
      float p_time;
      publication p;
   public:
      void getdata(){p.getdata();cout<<" Enter Time : ";cin>>p_time;}
      void putdata(){p.putdata();cout<<endl<<"  Play Time : "<<p_time;}
};
void main()
{
   book B;
   tape T;
   clrscr();
   cout<<endl<<"Enter Details of Book";
   cout<<endl<<"---------------------";
   B.getdata();
   cout<<endl<<"Enter Details of Tape";
   cout<<endl<<"---------------------";
   T.getdata();
   cout<<endl<<"Book Details";
   cout<<endl<<"------------";
   B.putdata();
   cout<<endl<<"==================================";
   cout<<endl<<"Tape Details";
   cout<<endl<<"------------";
   T.putdata();
   cout<<endl<<"==================================";
   getch();
}

Create a class employee with employee no, name and salary. Write a menu driven CPP program (i) to create an employee database (ii) to display the content of the database and (iii) to sort the database.

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<process.h>
#include<iomanip.h>
struct Employee
{
   int eno;
   char name[30];
   float salary;
};
void input(Employee E[10], int n)
{
   for(int i=0;i<n;i++)
   {
     cout<<"\nEnter Employee Number : ";
     cin>>E[i].eno;
     cout<<"\n           Enter Name : ";
     gets(E[i].name);
     cout<<"\n         Enter Salary : ";
     cin>>E[i].salary;
   }
}
void show(Employee E[10], int n)
{
     cout<<endl<<"               The Employee Details"<<endl;
     cout<<endl<<"--------------------------------------------------";
     cout<<endl<<"Emp.No    Name of Employee              Salary";
     cout<<endl<<"--------------------------------------------------";
     for(int i=0;i<n;i++)
     {
 cout.setf(ios::left, ios::adjustfield);
 cout<<endl<<setw(10)<<E[i].eno;
 cout<<setw(30)<<E[i].name;
 cout<<setw(10)<<E[i].salary;
     }
     cout<<endl<<"==================================================";
}
void main()
{
     int n, ch, i, j; Employee E[30];
     char choice;
     do
     {
 clrscr();
 cout<<"\n-----MENU-----\n";
 cout<<"\n1. Enter details of employee";
 cout<<"\n2. Sort in descending order of salary";
 cout<<"\n3. Display the details";
 cout<<"\n4. Exit from program";
 cout<<"\n\nEnter your choice : ";
 cin>>ch;
 switch(ch)
 {
   case 1: cout<<"\nEnter the number of Eemployees : ";
    cin>>n;
    input(E,n);
    cout<<"\nThe database is created now";
    cout<<"\n==========================="<<endl;
    break;
   case 2: Employee temp;
    for(i=0;i<n-1;i++)
    {
       for(j=0;j<n-1-i;j++)
       {
   if(E[j].salary<E[j+1].salary)
   {
     temp = E[j];
     E[j] = E[j+1];
     E[j+1] = temp;
   }
       }
    }
    cout<<"\nThe database is sorted now";
    cout<<"\n=========================="<<endl;
    break;
   case 3: show(E,n);
    break;
   case 4: exit(0);
   default:
    cout<<endl<<"Wrong Choice"<<endl;
 }
 cout<<"\n\nDo you want to continue (Y/y) : ";
 cin>>choice;
     }while( choice=='Y'|| choice=='y');
}

Create a class "string" as user defined string type. Include constructors to create and initialize a string. Overload the operators + and <= to add and compare two strings.

#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
class string
{
      char str[100];
   public:
      string(){strcpy(str,"");}
      string(char a[100]){strcpy(str,a);}
      void showstr(){cout<<str;}
      string operator +(string);
      int operator <=(string);
};
string string::operator +(string str2)
{
   string str3;
   strcpy(str3.str, str);
   strcat(str3.str,str2.str);
   return(str3);
}
int string::operator <=(string str2)
{
   if (strcmp(str,str2.str) > 0)
      return(0);
   else
      return(1);
}
void main()
{
   char s1[100], s2[100];
   clrscr();
   cout<<"Enter the First String : ";
   gets(s1);
   cout<<"Enter the Second String : ";
   gets(s2);
   string so1(s1), so2(s2), so3;
   cout<<endl<<"First String = ";
   so1.showstr();
   cout<<endl<<"------------------------------------------";
   cout<<endl<<"Second String = ";
   so2.showstr();
   cout<<endl<<"------------------------------------------";
   so3=so1+so2;
   cout<<endl<<"The Concatenated String = ";
   so3.showstr();
   cout<<endl<<"------------------------------------------";
   if (so1<=so2)
      cout<<endl<<"String-1 <= String-2";
   else
      cout<<endl<<"String-1 > String-2";
   cout<<endl<<"==========================================";
   getch();
}

CPP Program for implementing various Matrix Operations

#include<iostream.h>
#include<conio.h>
#include<ctype.h>
void matread(int m[10][10],int r,int c)
{
   int i,j;
   cout<<endl<<"Enter the Elements of the Matrix : ";
   for(i=0;i<r;i++)
      for(j=0;j<c;j++)
  cin>>m[i][j];
}
void matdisplay(int m[10][10],int r,int c)
{
   int i,j;
   cout<<"The Resultant Matrix : ";
   for(i=0;i<r;i++)
   {
      cout<<endl;
      for(j=0;j<c;j++)
  cout<<m[i][j]<<" ";
   }
}
void matadd(int m1[10][10], int m2[10][10], int m3[10][10], int r, int c)
{
   int i,j;
   for(i=0;i<r;i++)
      for(j=0;j<c;j++)
  m3[i][j]=m1[i][j]+m2[i][j];
}
void matsub(int m1[10][10], int m2[10][10], int m3[10][10], int r, int c)
{
   int i,j;
   for(i=0;i<r;i++)
      for(j=0;j<c;j++)
  m3[i][j]=m1[i][j]-m2[i][j];
}
void matprod(int m1[10][10], int m2[10][10], int m3[10][10], int r, int c, int rc)
{
   int i,j,k;
   for(i=0;i<r;i++)
      for(j=0;j<c;j++)
      {
  m3[i][j]=0;
  for(k=0;k<rc;k++)
     m3[i][j]+=m1[i][k]*m2[k][j];
      }
}
void transpose(int m1[10][10],int m2[10][10], int r, int c)
{
   int i,j;
   for(i=0;i<r;i++)
      for(j=0;j<c;j++)
  m2[i][j]=m1[j][i];
}
void rowsum(int m[10][10], int r, int c)
{
   int i,j,rs;
   cout<<endl<<"Sum of each Row elements";
   cout<<endl<<"------------------------";
   for(i=0;i<r;i++)
   {
      rs=0;
      for(j=0;j<c;j++)
  rs+=m[i][j];
      cout<<endl<<rs;
   }
   cout<<endl<<"========================";
}
void colsum(int m[10][10], int r, int c)
{
   int i,j,cs;
   cout<<endl<<"Sum of each Column elements";
   cout<<endl<<"---------------------------";
   for(j=0;j<c;j++)
   {
      cs=0;
      for(i=0;i<r;i++)
  cs+=m[i][j];
      cout<<endl<<cs;
   }
   cout<<endl<<"===========================";
}
void maindiag(int m[10][10], int r, int c)
{
   int i,mds=0;
   if(r==c)
   {
      for(i=0;i<r;i++)
  mds+=m[i][i];
      cout<<endl<<"The Sum of Mian Diagonal Elements = "<<mds;
   }
   else
      cout<<endl<<"Invalid Matrix : This is not Square Matrix";
}
void offdiag(int m[10][10], int r, int c)
{
   int i,j,ods=0;
   if(r==c)
   {
      for(i=0;i<r;i++)
      {
  for(j=0;j<r;j++)
     if(i+j == r-1)
        ods+=m[i][j];
      }
      cout<<endl<<"The Sum of Off Diagonal Elements = "<<ods;
   }
   else
      cout<<endl<<"Invalid Matrix : This is not Square Matrix";
}
void uptrisum(int m[10][10], int r, int c)
{
   int i,j,uts=0;
   if(r==c)
   {
      for(i=0;i<r;i++)
      {
  for(j=0;j<r;j++)
     if(i<j)
        uts+=m[i][j];
      }
      cout<<endl<<"The Sum of Upper Triangular Elements = "<<uts;
   }
   else
      cout<<endl<<"Invalid Matrix : This is not Square Matrix";
}
void lowtrisum(int m[10][10], int r, int c)
{
   int i,j,lts=0;
   if(r==c)
   {
      for(i=0;i<r;i++)
      {
  for(j=0;j<r;j++)
     if(i>j)
        lts+=m[i][j];
      }
      cout<<endl<<"The Sum of Lower Triangular Elements = "<<lts;
   }
   else
      cout<<endl<<"Invalid Matrix : This is not Square Matrix";
}
void main()
{
   clrscr();
   int r1,c1,r2,c2,ch;
   int A[10][10],B[10][10],C[10][10];
   char yn;
   cout<<endl<<"Enter the order of the First Matrix : ";
   cin>>r1>>c1;
   matread(A,r1,c1);
   cout<<endl<<"Enter the order of the Second Matrix : ";
   cin>>r2>>c2;
   matread(B,r2,c2);
   do
   {
      clrscr();
      cout<<endl<<" Menu";
      cout<<endl<<"------";
      cout<<endl<<" 1. Matrix Addition"
   <<endl<<" 2. Matrix Subtraction"
   <<endl<<" 3. Matrix Multiplication"
   <<endl<<" 4. Transpose of Matrix"
   <<endl<<" 5. Sum of Elements in Each Row"
   <<endl<<" 6. Sum of Elements in Each Column"
   <<endl<<" 7. Sum of Mian Diagonal Elements"
   <<endl<<" 8. Sum of Off Diagonal Elements"
   <<endl<<" 9. Sum of Upper Triangular Elements"
   <<endl<<"10. Sum of Lower Triangular Elements"
   <<endl<<endl<<"Enter Your Choice : ";
      cin>>ch;
      switch (ch)
      {
  case 1: if((r1==r2)&&(c1==c2))
   {
     matadd(A,B,C,r1,c1);
     matdisplay(C,r1,c1);
   }
   else
     cout<<"Matrices are not confirmable for Addition";
   break;
  case 2: if((r1==r2)&&(c1==c2))
   {
     matsub(A,B,C,r1,c1);
     matdisplay(C,r1,c1);
   }
   else
   cout<<"Matrices are not confirmable for subtraction";
   break;
  case 3:if(c1==r2)
   {
     matprod(A,B,C,r1,c2,c1);
     matdisplay(C,r1,c2);
   }
   else
     cout<<"Matrices are not confirmable for multiplication";
   break;
  case 4: transpose(A,C,r1,c1);
   matdisplay(C,r1,c1);
   break;
  case 5: rowsum(A,r1,c1);
   break;
  case 6: colsum(A,r1,c1);
   break;
  case 7: maindiag(A,r1,c1);
   break;
  case 8: offdiag(A,r1,c1);
   break;
  case 9: uptrisum(A,r1,c1);
   break;
  case 10:lowtrisum(A,r1,c1);
   break;
      }
   cout<<endl<<endl<<"To Continue...(Y/y) : ";
   cin>>yn;
   }
   while(toupper(yn)=='Y');
}