Inheritence Problem.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Danger1234
    New Member
    • Apr 2008
    • 2

    Inheritence Problem.

    Sorry I was an idiot for posting. I'd delete it but don't know how.
    Last edited by Danger1234; Apr 29 '08, 10:33 PM. Reason: Realized what I was doing wrong.
  • Savage
    Recognized Expert Top Contributor
    • Feb 2007
    • 1759

    #2
    Originally posted by Danger1234
    Sorry I was an idiot for posting. I'd delete it but don't know how.
    You may have realised what did you do wrong,but don't you want to save others from the same problem you have had?

    Comment

    • Danger1234
      New Member
      • Apr 2008
      • 2

      #3
      Originally posted by Savage
      You may have realised what did you do wrong,but don't you want to save others from the same problem you have had?
      Sorry about that I just left out a hug chunk of code that made everything not work, however I did run into another problem that I need help on.

      I've made three classes (buried, bond, mutualfund) that all inherit from the account class. However when I attempt to compile it states that there is already an instance for the Account class when it tries to compile more than one of the three classes. However if I comment out two of the three classes they have no problem compiling and running. Any thoughts?

      Account.h
      Code:
      #include <iostream>
      #include <string>
      #include <ctime>
      #include <cstdlib>
      using namespace std;
      
      
      class Account
      {
      public:
      	Account(string, double, double);
      	~Account();
      	string accountOwner(string name);
      	int accountNum();
      	double inflation(double, double);	
      
      private:
      	string owner;
      	int acctNum;
      	double value;
      	double penalty;
      	double inflationRate;
      	double inflateRate;
      };
      
      Account::Account(string name, double value, double inflationRate)
      {
      	srand((unsigned int)time(0));
      
      	value=value;
      	inflateRate=inflationRate;
      
      	owner=accountOwner(name);
      	acctNum=accountNum();
      	value=inflation(value, inflateRate);
      }
      
      Account::~Account()
      {
      }
      
      string Account::accountOwner(string name)
      {
      	owner=name;
      	return owner;
      }
      
      int Account::accountNum()
      {
      	acctNum =(rand()%10)+0;
      	return acctNum;
      }
      
      double Account::inflation(double value, double inflationRate)
      {
      	penalty=value*inflationRate;
      	value=value-penalty;
      	return value;
      }
      Buried.cpp
      Code:
      #include <iostream>
      #include <string>
      #include <ctime>
      #include <cstdlib>
      #include <sstream>
      using namespace std;
      
      #include "Account.h"
      
      class Bury: public Account
      {
      public:
      	Bury(string, double, double);
      	~Bury();
      	void updateValue(double);
      	string toString();
      	bool moneyDestruction(bool);
      private:
      	int acctNumber;
      	string acctOwner;
      	string currentValue;
      	string acctNum;
      	double value;
      	double inflatRate;
      };
      
      Bury::Bury(string owner, double intValue, double inflation): Account(owner, intValue, inflation)
      {
      	srand((unsigned)time(0));
      	value=intValue;
      	acctOwner=owner;
      	inflatRate=inflation;
      	acctNumber=Account::accountNum();
      }
      
      Bury::~Bury()
      {
      }
      
      bool Bury::moneyDestruction(bool destroyed)
      {
      	double gone=((double)rand());
      	if(gone<=2.0)
      		destroyed=true;
      	return destroyed;
      }
      
      string Bury::toString()
      {
      	
      	ostringstream ss;
      	ss << value;
      	currentValue = ss.str();
      
      	ostringstream ff;
      	ff << acctNumber;
      	acctNum = ff.str();
      
      	string statement="Owner: "+ acctOwner +" \n Account Number: "+ acctNum+" \n Current Value of the money: "+ currentValue+"\n";
      	return statement;
      }
      
      void Bury::updateValue(double years)
      {
      	bool destroyed=false;
      	while(years>=0 && destroyed!=true)
      	{
      		destroyed=moneyDestruction(destroyed);
      		if(destroyed!=false)
      		{
      			cout<<endl;
      			cout<<"You money has been eaten by worms."<<endl;
      			cout<<endl;
      			value=0.0;
      		}
      		else
      			value=Account::inflation(value, inflatRate);
      		--years;
      	}
      }
      
      
      
      /*
      int main()
      {
      	Bury b1("Tamay", 3000.0, 0.02);
      	cout<<b1.toString()<<endl;
      	b1.updateValue(100.0);
      	cout<<b1.toString()<<endl;
      	return 0;
      }*/
      Bond.cpp
      Code:
      #include <iostream>
      #include <string>
      #include <ctime>
      #include <cstdlib>
      #include <sstream>
      using namespace std;
      
      #include "Account.h"
      
      class Bond : public Account
      {
      public:
      	Bond(string, double, double, double);
      	~Bond();
      	void updateValue(double);
      	string toString();
      	double interestRate(double, double);
      private:
      	int acctNumber;
      	string acctOwner;
      	string currentValue;
      	string acctNum;
      	double value;
      	double inflatRate;
      	double interest;
      };
      
      Bond::Bond(string owner, double intValue, double interestRate, double inflation): Account(owner, intValue, inflation)
      {
      	acctOwner=owner;
      	value=intValue;
      	inflatRate=inflation;
      	acctNumber=Account::accountNum();
      	interest=interestRate;
      }
      
      Bond::~Bond()
      {
      }
      
      double Bond::interestRate(double value, double interest)
      {
      	double gainedInterest=value*interest;
      	value=value+gainedInterest;
      	return value;
      }
      
      string Bond::toString()
      {
      	ostringstream ss;
      	ss << value;
      	currentValue = ss.str();
      
      	ostringstream ff;
      	ff << acctNumber;
      	acctNum = ff.str();
      
      	string statement="Owner: "+ acctOwner +" \n Account Number: "+ acctNum+" \n Current Value of the money: "+ currentValue+"\n";
      	return statement;
      }
      
      void Bond::updateValue(double years)
      {
      	while(years>=0)
      	{
      		value=interestRate(value, interest);
      		value=Account::inflation(value, inflatRate);
      		--years;
      	}
      }
      
      /*
      int main()
      {
      	Bond b1("Suzie", 3000.0, 0.02, 0.04);
      	cout<<b1.toString()<<endl;
      	b1.updateValue(100.0);
      	cout<<b1.toString()<<endl;
      	return 0;
      }*/
      MutualFund
      Code:
      #include <iostream>
      #include <string>
      #include <ctime>
      #include <cstdlib>
      #include <sstream>
      using namespace std;
      
      #include "Account.h"
      
      class MutualFund : public Account
      {
      public:
      	MutualFund(string, double, double, double);
      	~MutualFund();
      	void updateValue(double);
      	string toString();
      	double interestRate(double);
      	double taxRate(double, double);
      private:
      	int acctNumber;
      	string acctOwner;
      	string currentValue;
      	string acctNum;
      	double value;
      	double inflatRate;
      	double tax;
      	double gainedInterest;
      };
      
      MutualFund::MutualFund(string owner, double intValue, double tax_Rate, double inflation): Account(owner, intValue, inflation)
      {
      	acctOwner=owner;
      	value=intValue;
      	inflatRate=inflation;
      	acctNumber=Account::accountNum();
      	tax=tax_Rate;
      	gainedInterest=0.0;
      }
      
      MutualFund::~MutualFund()
      {
      }
      
      double MutualFund::interestRate(double value)
      {
      	double rate;
      	rate=(-0.15)+((double)rand()/((double)(RAND_MAX)+(double)(0.25)));
      	gainedInterest=value*rate;
      	return gainedInterest;
      }
      
      double MutualFund::taxRate(double tax, double gainedInterest)
      {
      	tax=gainedInterest*tax;
      	value=value+tax;
      	return value;
      }
      
      string MutualFund::toString()
      {
      	ostringstream ss;
      	ss << value;
      	currentValue = ss.str();
      
      	ostringstream ff;
      	ff << acctNumber;
      	acctNum = ff.str();
      
      	string statement="Owner: "+ acctOwner +" \n Account Number: "+ acctNum+" \n Current Value of the money: "+ currentValue+"\n";
      	return statement;
      }
      
      void MutualFund::updateValue(double years)
      {
      	while(years>=0)
      	{
      		gainedInterest=interestRate(value);
      		value=taxRate(tax, gainedInterest);
      		value=Account::inflation(value, inflatRate);
      		--years;
      	}
      }
      
      
      
      /*int main()
      {
      	MutualFund b1("Bob",3000.0,0.15,0.02);
      	cout<<b1.toString()<<endl;
      	b1.updateValue(100.0);
      	cout<<b1.toString()<<endl;
      	return 0;
      }*/

      Comment

      • Savage
        Recognized Expert Top Contributor
        • Feb 2007
        • 1759

        #4
        Do you use all three classes in main unit?
        If so make sure you have included Account.h only once.

        You can put your account header code inside:

        [CODE=cpp]#ifndef _ACCOUNT_H
        #define _ACCOUNT_H

        //Your code......

        #endif[/CODE]

        Comment

        Working...