C++

C++ Programming

Slip4

Q.1) Write a C++ program to create a class Part which contains data members as Part_Id,
Part_Name, Part_Price. Create and Initialize all values of Part object by using
parameterized constructor and copy constructor. Display the values of Part object.
(Part_price should be right justified with a precision of two digits) [Marks 15]

Solution

#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
#include<string.h>
class part
{
  private:
   int part_id;
   float part_price;
   char *part_name;
  public:
    void display()
   {

cout.width(20);
cout<<setiosflags(ios::left);
cout<<"Part ID  ";

cout.width(20);
cout<<setiosflags(ios::left);
cout<<"Part Name  ";

cout.width(20);
cout<<setiosflags(ios::left);
cout<<"Part Price  ";
cout<<endl;

cout.width(20);
cout<<setiosflags(ios::left);
cout<<part_id;
cout.width(20);
cout<<setiosflags(ios::left);
cout<<part_name;
cout.width(10);
cout.precision(2);
cout<<setiosflags(ios::right);
cout<<part_price<<endl;
   }
   part(){};
   part(int id, char *name, float price)
   {
part_id=id;
part_name=name;
part_price=price;
   }
   part(part &p)
   {
part_id=p.part_id;
part_name=  p.part_name;
part_price=p.part_price;
   }

  };
  int main()
  {     clrscr();
part p(1,"aniket",12.12);  //Parameterized construct
p.display();
part p1(p);         //Copy constructor
p1.display();
getch();
return 0;
  }


Output

Part ID             Part Name           Part Price
1                   aniket                   12.12                           
Part ID             Part Name           Part Price                           
1                   aniket                   12.12       



Q.2) Create a base class Account (Acc_Holder_Name, Acc_Holder_Contact_No). Derive a
two classes as Saving_Account(S_Acc_No., Balance) and Current_Account(
C_Acc_No., Balance) from Account. Write a C++ menu driven program to perform
following functions :
i. Accept the details for ‘ n’ account holders.
ii. Display the details of ‘ n’ account holders by adding interest amount
where interest rate for Saving account is 5% of balance and interest rate
for Current account is 1.5% of balance.
[Marks 25]      


Solution

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
class Account
{
   char name[20];
   char cno[10];
   public:
Account(char *s,char *c)
{
   strcpy(name,s);
   strcpy(cno,c);
}
void displayA()
{
  cout<<"\n\t Name :"<<name
<<"\t Contact :"<<cno;
}
};

class SavingA : public Account
{
   int accno;
   float bal;
   public:
       SavingA(char *s, char *c,int a,float b) :
  Account(s,c)
       {
     accno = a;
     bal = b;
       }
       void displayS()
       {
  displayA();
  cout<<"\n\t Accno :"<<accno
   <<" \t Bal:"<<bal;
  float amt = 1.05 * bal;
  cout<<"\n\t Amount is :"<<amt;
       }
};

class CurrentA : public Account
{
    int accno;
    float bal;
    public:
      CurrentA(char *s, char *c,int a, float b):
  Account(s,c)
      {
  accno = a;  bal = b;
      }
      void displayC()
      {
  displayA();
  cout<<"\n\t Accno :"<<accno
     <<"\t Bal:"<<bal;
  float amt = 1.015 * bal;
  cout<<"\n\tTotal amount after interest :"<<
       amt;
      }
};

int main()
{
   clrscr();

   SavingA *s[10];
   CurrentA *c[10];
   int cnts=0, cntc=0;

   char n[20];   char cont[10];
   int ano,choice;      float bal;

   char ch,ans;
   do
   {
     cout<<"\n 1: Accept the details \n 2: Display details \n 3: Exit \n";
     cout<<"Enter Your Choice : ";
     cin>>choice;
     switch(choice)
     {
      case 1:cout<<"\n\t Enter name:";
     fflush(stdin); gets(n);
     cout<<"\n\t Enter contact no:"; cin>>cont;
     cout<<"\n\t Enter accno and bal :";
     cin >> ano >> bal;
     cout<<"\n\t Enter account type (S/C):";
     cin>>ch;
     if(ch == 's' || ch == 'S')
  {
     if(cnts == 10)
cout<<"\n\t Capacity full";
     else
   {
s[cnts] = new SavingA(n,cont,ano,bal);
cnts++;
   }
  }
else
  {
    if(cntc == 10)
cout<<"\n\t Capacity full";
    else
      {
c[cntc] = new CurrentA(n,cont,ano,bal);
cntc++;
      }
  }
break;
      case 2:cout<<"\n\t Saving account details:";
    for(int i=0;i<cnts;i++)
     s[i]->displayS();
    cout<<"\n  ______________________________";
    cout<<"\n\t Current account details:";
      for(i=0;i<cntc;i++)
c[i]->displayC();
break;
     }
     }while(choice!=3);

   getch();
   return 0;
}


Output

         Enter name:Rahul 

Enter contact no:9860449276

Enter accno and bal :31140
40000

Enter account type (S/C):s

 1: Accept the details
 2: Display details
 3: Exit
Enter Your Choice : 2

Saving account details:
Name :Rahul    Contact :9860449276¤y
Accno :31140    Bal:40000
Amount is :42000
  ______________________________

Enter name:Rahul 

Enter contact no:9860449276

Enter accno and bal :41140
500000

Enter account type (S/C):c

 1: Accept the details
 2: Display details
 3: Exit
Enter Your Choice : 2

Saving account details:
  ______________________________
Current account details:
Name :Rahul    Contact :9860449276´
Accno :-24396   Bal:500000
Total amount after interest :507500
 1: Accept the details
 2: Display details
 3: Exit
Enter Your Choice : 3


                                                
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