inheritance

0 Royson Mendonca · September 23, 2015
Hello guys i am in a bit of a fix .hope you guys can help me out.I have created an array of objects.
i am writing the code with inheritance but i am not able to pass the array of object to my derived class
eg:
this is an example of my code.when i change the members to public its working fine Shouldn't i be able to access the protected data members.



class A
{
protected:
int a
public :
void hey();

}

class B:public A

{

void display(A obj3[2])


}

int main()
{



A obj1[2];

B obj2;
obj2.display(obj1)


}



thnks in advance





\\
my code for refrence
question:
/*Assume that a bank maintains two kinds of accounts of customers, 
one called as savings account and the other as current account. 
The saving account provides compound interest and withdrawal facilities but no cheque book facility. 
The current account provides cheque book facility but no interest. 
Current account holders should also maintain a minimum balance.
If the same falls below this level, a service charge is imposed.
Create a class account that stores customer name, account number and type of account.
From this derive the classes cur_acct and sav_acct to make them more specific to their requirements.
Include necessary member function in order to achieve the following tasks :
a) Accept deposit from a customer and update the balance.
b) Display the balance.
c) Compute and deposit interest.
d) Permit withdrawal and update the balance
e) Check for the minimum balance, impose penalty, if necessary, and update the balance. */


My code incomplete:
class account
{

protected:
    string name;
    int accno;
    string type;
    int amount;

public:
    void customerdetails();
    void acceptdeposit(account obj1[]);
    void displaydetail();



};


void account::customerdetails()
{
    cout<<"enter your name\n";
    cin>>name;
    cout<<"enter your account no\n";
    cin>>accno;
    cout<<"enter the type of account\n";
    cin>>type;
    cout<<"enter the amount in your account\n";
    cin>>amount;


}

void account::acceptdeposit(account obj1[2])
{
    string name1;
    int money;
    int totalamount;
    int counter;
    label:
    cout<<"enter your name\n";
    cin>>name1;


    for(int i=0;i<2;i++)
    {
         counter=0;

        if(name1==obj1.name)
        {

            cout<<"enter the amount you want to deposit";
            cin>>money;
            totalamount=obj1.amount + money;
            cout<<"the total amount in your account is "<<":"<<totalamount;
            counter=1;
            break;

        }

    }
    if(counter==0)
    {

        cout<<"details not found renter name\n";
        goto label;
    }


}




class curr_acct:protected account
{









};

class sav_acct:protected account
{



public:
    void compoundinterest(account obj1[2]);


};

void sav_acct::compoundinterest(account obj1[2])
{
    string r;
    int compin;
    int water;
    string o;
    cout<<"enter your name\n";
    cin>>r;
    int k;

for(int i=0;i<2;i++)
{
    water=0;
   if(obj1.type=="saving" && obj1.name==r)

    {


        compin=(0.1*obj1.amount)+obj1.amount;

        cout<<"\n your total amount after interest is "<<":"<<compin;
        water=1;
         k=i;
        break;


    }


}
if(water==0)
{
  cout<<"you do not have these priveleges\n";


}


if(obj1[k].type=="saving" && o=="yes")
{

cout<<"account no-:"<<obj1[k].accno;
cout<<"name-:"<<obj1[k].name ;


}
else{

    cout<<"you dont have these priveleges";
}
}











int main()

{
string y;
string b;
  account obj1[2];


  for(int i=0;i<2;i++)
  {

      obj1.customerdetails();

  }

  account obj2;

  cout<<"do you want to deposit cash in your account\n(yes/no)";
  cin>> y;
  if(y=="yes")
  {
obj2.acceptdeposit(obj1);
  }



  cout<<"do you want to check your compound interset\n";
  cin>> b;
  if(b=="yes")
  {
    sav_acct obj3;
   obj3.compoundinterest(obj1);


  }








}



Post a Reply

Replies

Oldest  Newest  Rating
0 Dol Lod · September 23, 2015
Why are you using protected members? That is bad form. Make getter and setter functions instead to manipulate the value in the superclass.

In the industry, people try to avoid using protected as much as possible.

Change your code before it becomes too difficult to do.
  • 1

C++

107,310 followers
About

Used in many types of software including music players, video games, and many large scale applications.

Links
Moderators
Bucky Roberts Administrator