Sunday, 28 April 2013

Constructors in inheritance c++


/*This program illustrates use of constructor in inheritance.That is it is mandatory to use constructor in derived class if base class have constructor.So how many arguments should be passed to it.The ans is no of arguments required for base class + that required for derived class. YOU can see this in the following program.
design a class called Account having customer information.
derive a class called Savings from Account class, which 
have additional data member called years of deposit.
interest is calculated at 8.75% rate and given on total
years of deposit.
Derive another class called Current from Account
class,which does not provide any interest on deposit.
Use appropriate member functions and constructors.
*/
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<math.h>
class Account
{
 protected:
 char name[30];
 char acno[15];
 char type[12];
 float balance;
 public:
 Account(char *n,char *a,char *t,float b)
   {
strcpy(name,n);
strcpy(acno,a);
strcpy (type,t);
balance=b;
   }

   void display()

    {
    cout<<name<<" "<<acno<<" "<<balance;
    }

 };

 class Savings:public Account
 {
int years;
    public :
    Savings(char* n,char* a,char* t,float b,int y):
Account(n,a,t,b)
{
   years=y;
}

    void deposit(float d,int y)

     {
      years=years+y;
      balance= balance+d;
      balance=balance*(pow(1.0875,years));

      }



    };

   class Current:public Account
   {
public:
       Current(char* n,char* a,char* t,float b):
Account(n,a,t,b)
 {
   }

void deposit(float d)

{
balance=balance+d;
}
      };
int main()
{
clrscr();
  Savings s("Mr S.K.NAYAK","10137386991","SAVINGS",1000.0,2);
cout<<"savings ac before deposit"<<endl;
s.display();
s.deposit(2000,3);
cout<<endl<<"savings ac after deposit"<<endl;
s.display();
 Current c("Mr S.K.Nayak","30344739822","CURRENT",5000);
cout<<endl<<"current ac before deposit"<<endl;
c.display();
c.deposit(2000);
cout<<endl<<"current ac after deposit"<<endl;
c.display();
getch();
 return 0;
}
/************************OUTPUT****************************
savings ac before deposit
Mr S.K.NAYAK 10137386991 1000
savings ac after deposit
Mr S.K.NAYAK 10137386991 4563.179688
current ac before deposit
Mr S.K.Nayak 30344739822 5000
current ac after deposit
Mr S.K.Nayak 30344739822 7000

**********************************************/

C Programming

  Table of Contents ​ About the Author 3 ​ Introduction 4 ​ Introduction to Computers 4 1. What is a Computer? 4 2. History...