Tuesday, 8 January 2019

CPP-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)     



#include<conio.h>
#include<iostream.h>
#include<string.h>
class part
{
                int pid;
                float price;
                char name[10];
                public:
                part(int prtid,float prce,char nam[])
                {
                                pid=prtid;
                                price=prce;
                                strcpy(name,nam);
                }
                part(part &ob1)
                {
                                pid=ob1.pid;
                                price=ob1.price;
                                strcpy(name,ob1.name);
                }

                void display()
                {
                                cout.precision(2);
                                cout<<"part id"<<"\t"<<pid<<endl;
                                cout<<"name"<<"\t"<<name<<endl;
                              cout.setf(ios::right,ios::adjustfield);
               cout << setfill('*') << setw(10) <<price;
                                cout<<"price"<<"\t"<<price<<endl;
                }
};

void main()
{
                int pid;
                float price;
                char name[10];
                clrscr();
                cout<<"\n enter pid :";
                cin>>pid;
                cout<<"\n Enter price : ";
                cin>>price;
                cout<<"\n Entr name : ";
                cin>>name;
                part ob(pid,price,name),ob1(ob);
                ob.display();
                cout<<"\n Ater copy constructor : \n ";
                ob1.display();

                getch();
}







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. 

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

class Account
{
                public:
                                char name[10];
                                long cno;
                public:
                                void getdata()
                                {
                                                cout<<"\n Account holder name : ";
                                                cin>>name;
                                                cout<<"\n contact no : ";
                                                cin>>cno;
                                }
                                void display()
                                {
                                                cout<<"\n Name :"<<name;
                                                cout<<"\n contact No :"<<cno;
                                }
};
class saving_Account:public Account
{
                int acc_no;
                float bal,T_bal;
                public:
                                void getdata()
                                {   Account::getdata();
                                                cout<<"\n Account no : ";
                                                cin>>acc_no;
                                                cout<<"\n Enter balance: ";
                                                cin>>bal;
                                }
                                void cal()
                                {
                                                T_bal=bal+(bal*0.05);
                                }
                                void display()
                                {  Account::display();

                                                cout<<"\nAccount no : "<<acc_no;
                                                cout<<"\nbalance :"<<bal<<endl;
                                                cout<<"\nTotal balance :"<<T_bal<<endl;
                                }
};

class current_Account:public Account
{              int acc_no;
                float bal;
                public:
                                void getdata()
                                {    Account::getdata();
                                                cout<<"\n Enter current account number";
                                                cin>>acc_no;
                                                cout<<"\n Enter available balance : ";
                                                cin>>bal;

                                }

                                void cal()
                                {
                                                bal=bal+(bal*(1.5/100));
                                }
                                void display()
                                {  Account::display();
                                                cout<<"\n Account no : "<<acc_no;
                                                cout<<"\n Total balance :"<<bal<<endl;
                                }


};
void main()
{
                int i,n,n1,ch;
                clrscr();
                current_Account c[10];saving_Account s[10];

                do
                {cout<<"\n 1.Enter Current Account datails \n 2.Enter Saving Account Details \n 3.Display Current Account Holder \n 4.Display Saving Account Holders \n 0.Exit";
                                cout<<"\n Enter your choice : ";
                                cin>>ch;
                                switch(ch)
                                {
                                                case 1 : cout<<"\n Enter how many current Account holders ? ";
                                                                cin>>n;
                                                                for(i=0;i<n;i++)
                                                                {
                                                                                c[i].getdata();
                                                                                c[i].getdata();
                                                                }
                                                                break;
                                                case 2 : cout<<"\n Enter how many saving Account holders ? ";
                                                                cin>>n1;
                                                                for(i=0;i<n1;i++)
                                                                {
                                                                                s[i].getdata();
                                                                                s[i].cal();
                                                                }
                                                                break;
                                                case 3 : cout<<"\n Details of curent account holders : ";
                                                                for(i=0;i<n;i++)
                                                                {              c[i].cal();
                                                                                c[i].display();
                                                                }
                                                                break;
                                                case 4 : cout<<"\n Details of saving account holders : ";
                                                                for(i=0;i<n1;i++)
                                                                {            
                                                                                s[i].display();
                                                                }
                                                                break;
                                                case 0 : break;
                                }
                }while(ch!=0);
                getch();
}