1. Welcome to Tacoma World!

    You are currently viewing as a guest! To get full-access, you need to register for a FREE account.

    As a registered member, you’ll be able to:
    • Participate in all Tacoma discussion topics
    • Communicate privately with other Tacoma owners from around the world
    • Post your own photos in our Members Gallery
    • Access all special features of the site

Assignment help

Discussion in 'Technology' started by tarheelfan_08, Mar 7, 2010.

  1. Mar 7, 2010 at 9:11 PM
    #1
    tarheelfan_08

    tarheelfan_08 [OP] Carolina Alliance

    Joined:
    Jan 4, 2008
    Member:
    #4098
    Messages:
    2,944
    Gender:
    Male
    First Name:
    Justin
    North Carolina
    Vehicle:
    2010 White 4x4 Double Cab Tacoma Short Bed
    Chrome Grill, Chrome accessories(door handles and mirror covers), LEER Super White Cover, Mirrors with Turn Signals, Window Vent Visors, Step Rails, WeatherTech Digital Floor Mats, Mini Maglight Holder, Chrome Bull Bar
    Can someone please advise me as to why my assignment is not working? I am doing the exact same thing I did on another assignment and for some reason it wont work.

    Assignment
    1. You are a programmer that works for a local bank. You are creating classes to be used in the class library for this bank for all of its programs. You will also create a main() to test your new classes.

    Talking to the bank manager you find out that your bank keeps track of Accounts and that there are more than one type of Account. There are CheckingAccounts, SavingsAccounts, and CreditCardAccounts.

    (Credit card accounts are actually called line of credit accounts in the real world. But, let’s call them this here to make it clearer.)

    You learn that all of the accounts are often grouped together and treated the same in many reports. So, they all need to inherit from the account class.

    Account Class:
    Attributes/data members
    number (an integer)
    balance (a double)
    rate (a double)

    Behaviors/member functions:
    1) accessor (get) and mutator (set) functions for all attributes.
    2) ApplyInterest()
    Note: The ApplyInterest() function needs to be a virtual function. It is going to be overridden in the subclasses.

    Constructor(s):
    This class must have a constructor that accepts all of the data to create an Account and initializes its data members.

    Validation in member functions/ Business Rules:
    1) Rates should be between .01 and .18 (it is an interest rate as a decimal)
    2) Balances should never be allowed to be set to a negative value.


    CheckingAccount Class:
    This class inherits from the Account class

    Attributes/data members
    checknumber (an integer) [representing the last check number written/processed.]
    pin (an integer)

    Behaviors/member functions:
    1) accessor (get) and mutator (set) functions for all attributes.
    2) ApplyInterest(), this function should take the balance and multiply it by the rate to find the interest this period…then add it to the existing balance. Print a message to the user with the amount in this format “2.35 has been added to checking”

    Constructor(s):
    This class must have a constructor that accepts all of the data to create a CheckingAccount and initializes its data members. (This includes all of the data it needs to pass to the Account class constructor or set directly for the account class from this constructor.)

    SavingsAccount Class:
    This class inherits from the Account class

    Attributes/data members

    Behaviors/member functions:
    1) accessor (get) and mutator (set) functions for all attributes.
    2) ApplyInterest(), this function should take the balance and multiply it by the rate to find the interest this period…then add it to the existing balance. Print a message to the user with the amount in this format “5.01 has been added to savings”


    Constructor(s):
    This class must have a constructor that accepts all of the data to create a SavingsAccount and initializes its data members. (This includes all of the data it needs to pass to the Account class constructor or set directly for the account class from this constructor.)


    CreditCardAccount Class:
    This class inherits from the Account class

    Attributes/data members
    cardnumber (a string)

    Behaviors/member functions:
    1) accessor (get) and mutator (set) functions for all attributes.
    2) ApplyInterest(), this function should take the balance and multiply it by the rate to find the interest this period…then add it to the existing balance. Print a message to the user with the amount in this format “5.23 interest has been charged to this credit card on its unpaid balance”

    Constructor(s):
    This class must have a constructor that accepts all of the data to create a CreditCardAccount and initializes its data members. (This includes all of the data it needs to pass to the Account class constructor or set directly for the account class from this constructor.)


    Main()

    1) In main for this program create a checking account, savings account, and credit card account for a customer with test data as follows and place them in an array:

    (type) account # balance rate checknumber pin cardnumber
    (checking) 832443 560.10 .02 5854 123
    (savings) 832443 1020.58 .04
    (credit card) 832443 78.00 .16 1238201293731133

    2) Print out all of the data for the accounts for the user.
    3) Use a loop and call the ApplyInterest() method to add interest to the balance and print appropriate messages to the user.
    4) Print out all of the data for the accounts for the user.

    Account Class
    Code:
    #ifndef ACCOUNT_H
    #define ACCOUNT_H
    
    #include <iostream>
    #include <fstream>
    #include <iomanip>
    #include <functional>
    #include <algorithm>
    #include <string>
    #include <cstdlib>
    #include <sstream>
    using namespace std;
    
    //Account Class
    class Account {
    private:
    
            int number;
            double balance;
               double rate;
            double interest;
            double NewBalance;
    
    public:
            Account(); // Constructor
            Account(int, double, double) ;
                   void setBalance(double);
                    void setRate(double);
                    void setNumber(int);
    
                    int getNumber();
                    double getBalance();
                    double getRate();
                    void valid_rate(double);
                    virtual void ApplyInterest();
                    
                    void operator +=( double );
                    void operator -=( double );
                       void operator ++ ();
                    };      
    //-----------------------------------------------------------------------------
    //class definition
            Account::Account() {
                    number = 0;
                    balance = 0;
                    rate = 0;
            }
    
            Account::Account(int number, double balance, double rate) {
                    Account::number = number;
                    Account::balance = balance;
                    valid_rate(rate);
                    }
    
    void Account::setNumber(int number) {
        Account::number = number;
    }
    
    void Account::setBalance(double balance) {
        Account::balance = balance;
    }
    
    void Account::setRate(double rate) {
            valid_rate(rate);
    }
    
    int Account::getNumber() {
        return number;
    }
    double Account::getBalance() {
        return balance;
    }
    double Account::getRate() {
        return rate;
    }
    
    void Account::valid_rate(double rate) {
            if (rate >=.01 && rate <= .18)
                    Account::rate=rate;
            else {
                    Account::rate=0;
                    cout << "WARNING! You have entered an invalid rate!\n";
            }
    }
    
    void Account::operator += ( double dValue ) {
            setBalance( getBalance()+ dValue );
                    //return this;
        }
    void Account::operator -= ( double dValue ) {
            setBalance( getBalance() - dValue );
                    //return this;
        }
    
    
    #endif
    
    Checking Classk
    Code:
    #ifndef CHECKING_H
    #define CHECKING_H
    
    #include "Account.h"
    
    //Checking Class
    class Checking {
    private:
            Checking myChecking[1]; 
            int CheckNumber;
            int CheckingPin;
    
    
    
    public:
            Checking(); // Constructor
            Checking(int, int) ;
                       void setCheckNumber(int);
                    void setCheckingPin(int);
    
                    int getCheckNumber();
                    int getCheckingPin();
    
                    };      
    //-----------------------------------------------------------------------------
    //class definition
            Checking::Checking() {
                    CheckNumber = 0;
                    CheckingPin = 0;
                    
            }
    
            Checking::Checking(int CheckNumber, int CheckingPin) {
                    Checking::CheckNumber = CheckNumber;
                    Checking::CheckingPin = CheckingPin;
                    
                    }
    
    void Checking::setCheckNumber(int CheckNumber) {
        Checking::CheckNumber = CheckNumber;
    }
    
    void Checking::CheckingPin(int CheckingPin) {
        Checking::CheckingPin = CheckingPin;
    }
    
    
    int Checking::getCheckNumber() {
        return CheckNumber;
    }
    int Checking::getCheckingPin() {
        return CheckingPin;
    }
    
    void Account::ApplyInterest() {
                    interest = rate * balance;
                    NewBalance = interest + balance;
                    cout << interest << " has been added to checking.\n\n";
    }
    
    #endif
    
    Credit Class
    Code:
    #ifndef CHECKING_H
    #define CHECKING_H
    
    #include "Account.h"
    //Checking Class
    class Credit {
    private:
    
        Credit myCredit[1];    
        string CardNumber;
    
    public:
            Credit(); // Constructor
            Credit(string) ;
                       void setCardNumber(string);
    
                    string getCardNumber();
    
    
                    };      
    //-----------------------------------------------------------------------------
    //class definition
            Credit::Credit() {
                    CardNumber = " ";
                                    
            }
    
            Credit::Credit(string CardNumber) {
                    Credit::CardNumber = CardNumber;
                                    
                    }
    
    void Credit::setCardNumber(string CardNumber) {
        Credit::CardNumber = CardNumber;
    }
    
    
    string Credit::getCardNumber() {
        return CardNumber;
    }
    
    
    void Account::ApplyInterest() {
                    interest = rate * balance;
                    NewBalance = interest + balance;
                    cout << interest << " 23 interest has been charged to this credit card on its unpaid balance.\n\n";
    }
    
    #endif
    
    Saving Class
    Code:
    #ifndef SAVING_H
    #define SAVING_H
    
    //Customer Class
    class Saving {
    private:
    Saving mySaving[1]; 
    public:
                    };      
    
    void Account::ApplyInterest() {
                    interest = rate * balance;
                    NewBalance = interest + balance;
                    cout << interest << " has been added to checking.\n\n";
    
    }
    #endif
    
    Main
    Code:
    #include "Account.h"
    #include "Checking.h"
    #include "Saving.h"
    #include "Credit.h"
    using namespace std;
    
    int main() {
    
        int current_cust=0;
        Checking *myChecking[1];
        Saving *mySaving[1];
        Credit *myCredit[1];
    
        Checking myRate;
            myChecking[0] = new Checking(832443, 560.10, .02, 5);
            mySaving[0] = new Saving(832243, 1020.58, .04);
            myCredit[0] = new Credit(832443, 78.00, .16, 1238201293731133); 
    
        cout << "__________________________________________________________\n"<< endl;
    
        for (int c=0; c<1; c++)
            myChecking[c]->ApplyInterest();
    
            cout << "__________________________________________________________\n"<< endl;
    
        for (int s=0; s<1; s++)
            mySaving[s]->ApplyInterest();
    
            cout << "__________________________________________________________\n"<< endl;
    
        for (int x=0; x<1; x++)
            myCredit[x]->ApplyInterest();
    
        cout << "__________________________________________________________\n"<< endl;
    
            return 0;
    }
    
     
  2. Mar 7, 2010 at 9:55 PM
    #2
    tarheelfan_08

    tarheelfan_08 [OP] Carolina Alliance

    Joined:
    Jan 4, 2008
    Member:
    #4098
    Messages:
    2,944
    Gender:
    Male
    First Name:
    Justin
    North Carolina
    Vehicle:
    2010 White 4x4 Double Cab Tacoma Short Bed
    Chrome Grill, Chrome accessories(door handles and mirror covers), LEER Super White Cover, Mirrors with Turn Signals, Window Vent Visors, Step Rails, WeatherTech Digital Floor Mats, Mini Maglight Holder, Chrome Bull Bar
    Here are my errors!

    1>------ Build started: Project: TestTwo, Configuration: Debug Win32 ------
    1> Main.cpp
    1>\checking.h(9): error C2148: total size of array must not exceed 0x7fffffff bytes
    1>\checking.h(9): error C2079: 'Checking::myChecking' uses undefined class 'Checking'
    1>\checking.h(27): fatal error C1903: unable to recover from previous error(s); stopping compilation
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
     
  3. Mar 7, 2010 at 9:56 PM
    #3
    THXEY

    THXEY Panda Jerk

    Joined:
    Dec 15, 2008
    Member:
    #11614
    Messages:
    12,669
    Gender:
    Male
    First Name:
    Jacob
    San Diego
    Vehicle:
    2013 Subaru WRX
    STi Short Shifter.OEM floor illumination kit. Rally Armor Mud Flaps. BC BR Coilovers. Invidia N1 Exhaust
    wayyyyy too much reading
     
  4. Mar 8, 2010 at 7:02 AM
    #4
    Evil Monkey

    Evil Monkey There's an evil monkey in my truck

    Joined:
    Aug 8, 2007
    Member:
    #2352
    Messages:
    8,262
    Gender:
    Male
    First Name:
    Robert
    Escondido, CA
    Vehicle:
    07 4x4 DC SR5 TRD Off-road
    Weathertech front & rear mats, rear suspension TSB, Toytec AAL for TSB, Hi-Lift Jack, Bilstein 5100 & Toytec Adjustable coilovers, Built Right UCAs, KMC XD 795 Hoss Wheels, Definity Dakota MTs 285/75R16, Leer XR, Thule Tracker II & Thule MOAB basket
    It's been a while but I believe your virtual function should include an "=0;" at the end.
    virtual void applyInterest()=0;

    That means the function will not be called directly but force the coder to rewrite the function for each class that inherits account.

    Also, watch your coding form. Functions should start with lowercase, so applyInterest instead of ApplyInterest. Names that start with a capital should be reserved for classes for clarity's sake.

    Also, in your class savings, it looks like you've closed the class without including the applyInterest function.

    You also don't try to implement Account:: in your savings class. You just use your class name. Saving::applyInterest() vs. Account::applyInterest()
    Code:
    #ifndef SAVING_H
    #define SAVING_H
    
    //Customer Class
    class Saving {
    private:
    Saving mySaving[1]; 
    public:
                    };      //   [B]<<<<<   This closes the class without the applyInterest function
    [/B]
    void Account::ApplyInterest() {    //  [B]<<<<< don't use Account::, use your class
    [/B]                interest = rate * balance;
                    NewBalance = interest + balance;
                    cout << interest << " has been added to checking.\n\n";
    
    }
    #endif
    You need to take a look at the syntax for inheriting from a class because you're not currently using the base class Account in any of your classes.
     
  5. Mar 8, 2010 at 9:05 AM
    #5
    Evil Monkey

    Evil Monkey There's an evil monkey in my truck

    Joined:
    Aug 8, 2007
    Member:
    #2352
    Messages:
    8,262
    Gender:
    Male
    First Name:
    Robert
    Escondido, CA
    Vehicle:
    07 4x4 DC SR5 TRD Off-road
    Weathertech front & rear mats, rear suspension TSB, Toytec AAL for TSB, Hi-Lift Jack, Bilstein 5100 & Toytec Adjustable coilovers, Built Right UCAs, KMC XD 795 Hoss Wheels, Definity Dakota MTs 285/75R16, Leer XR, Thule Tracker II & Thule MOAB basket
    A clarification:
    Your syntax could be correct if you include an implementation in the Account class as a default.

    My example is a pure virtual function.

    The difference is how the programmer is forced to code.

    In a pure virtual, the programmer must override the function in any base class; whereas, in the basic virtual function, it's optional. Looking at how the example was set up, the instructor is suggesting that all base classes will re-impliment the function.

    Also, any class that has a pure virtual function cannot be instantiated. It has to be derived from. e.g. with a pure virtual function, you can't create an object of type "Account".

    This would flag as an error in a class with a pure virtual function:
    Account myAccount = new Account();
     
  6. Mar 11, 2010 at 5:52 PM
    #6
    tarheelfan_08

    tarheelfan_08 [OP] Carolina Alliance

    Joined:
    Jan 4, 2008
    Member:
    #4098
    Messages:
    2,944
    Gender:
    Male
    First Name:
    Justin
    North Carolina
    Vehicle:
    2010 White 4x4 Double Cab Tacoma Short Bed
    Chrome Grill, Chrome accessories(door handles and mirror covers), LEER Super White Cover, Mirrors with Turn Signals, Window Vent Visors, Step Rails, WeatherTech Digital Floor Mats, Mini Maglight Holder, Chrome Bull Bar
    Ok guys I appreciate your help on my stupid mistakes. But on further review of classes I was making dumb mistakes and I changed a lot! I have gotten my compiler down to 3 errors and I am unsure of what they mean, can someone please assist me!

    Here is my updated code:

    Account
    Code:
    #ifndef ACCOUNT_H
    #define ACCOUNT_H
    
    #include <iostream>
    #include <fstream>
    #include <iomanip>
    #include <functional>
    #include <algorithm>
    #include <string>
    #include <cstdlib>
    #include <sstream>
    using namespace std;
    
    //Account Class
    class Account {
    protected:
            int number;
            double balance;
               double rate;
            double interest;
            double NewBalance;
    
    public:
            Account(); // Constructor
            Account(int, double, double) ;
                   void setBalance(double);
                    void setRate(double);
                    void setNumber(int);
    
                    int getNumber();
                    double getBalance();
                    double getRate();
                    void valid_rate(double);
    
                    //Virtual Function
                    virtual void applyInterest() {
                    }
                    
                    void operator +=( double );
                    void operator -=( double );
                       void operator ++ ();
                    };      
    //-----------------------------------------------------------------------------
    //class definition
            Account::Account() {
                    number = 0;
                    balance = 0;
                    rate = 0;
            }
    
            Account::Account(int number, double balance, double rate) {
                    Account::number = number;
                    Account::balance = balance;
                    valid_rate(rate);
                    }
    
    void Account::setNumber(int number) {
        Account::number = number;
    }
    
    void Account::setBalance(double balance) {
        Account::balance = balance;
    }
    
    void Account::setRate(double rate) {
            valid_rate(rate);
    }
    
    int Account::getNumber() {
        return number;
    }
    double Account::getBalance() {
        return balance;
    }
    double Account::getRate() {
        return rate;
    }
    
    void Account::valid_rate(double rate) {
            if (rate >=.01 && rate <= .18)
                    Account::rate=rate;
            else {
                    Account::rate=0;
                    cout << "WARNING! You have entered an invalid rate!\n";
            }
    }
    
    void Account::operator += ( double dValue ) {
            setBalance( getBalance()+ dValue );
                    //return this;
        }
    void Account::operator -= ( double dValue ) {
            setBalance( getBalance() - dValue );
                    //return this;
        };
    
    
    #endif
    
    Checking
    Code:
    #ifndef CHECKING_H
    #define CHECKING_H
    
    #include "Account.h"
    
    //Checking Class
    class Checking:public Account
    {
    private:
            int CheckNumber;
            int CheckingPin;
    
    public:
            Checking(); // Constructor
            Checking(int, int) ;
            Checking(int, double, double, int);
                       void setCheckNumber(int);
                    void setCheckingPin(int);
                    void applyInterest();
                    int getCheckNumber();
                    int getCheckingPin();
    
    };      
    //-----------------------------------------------------------------------------
    //class definition
            Checking::Checking() {
                    CheckNumber = 0;
                    CheckingPin = 0;
                    
            }
    
            Checking::Checking(int CheckNumber, int CheckingPin) {
                    Checking::CheckNumber = CheckNumber;
                    Checking::CheckingPin = CheckingPin;
                    
                    }
    
    void Checking::setCheckNumber(int CheckNumber) {
        Checking::CheckNumber = CheckNumber;
    }
    
    void Checking::setCheckingPin(int CheckingPin) {
        Checking::CheckingPin = CheckingPin;
    }
    
    
    int Checking::getCheckNumber() {
        return CheckNumber;
    }
    int Checking::getCheckingPin() {
        return CheckingPin;
    }
    
    void Checking::applyInterest() {
                    interest = rate * balance;
                    NewBalance = interest + balance;
                    cout << interest << " has been added to checking.\n\n";
    };
    
    #endif
    
    Credit
    Code:
    #ifndef CREDIT_H
    #define CREDIT_H
    
    #include "Account.h"
    //Checking Class
    class Credit:public Account
    {
    private:   
        string CardNumber;
    
    public:
            Credit(); // Constructor
            Credit(string);
            Credit(int, double, double, string);
                       void setCardNumber(string);
                    string getCardNumber();
                    void applyInterest();
    
    };      
    //-----------------------------------------------------------------------------
    //class definition
            Credit::Credit() {
                    CardNumber = " ";
                                    
            }
    
            Credit::Credit(string CardNumber) {
                    Credit::CardNumber = CardNumber;
                                    
                    }
    
    void Credit::setCardNumber(string CardNumber) {
        Credit::CardNumber = CardNumber;
    }
    
    
    string Credit::getCardNumber() {
        return CardNumber;
    }
    
    
    void Credit::applyInterest() {
                    interest = rate * balance;
                    NewBalance = interest + balance;
                    cout << interest << " interest has been charged to this credit card on its unpaid balance.\n\n";
    };
    
    #endif
    
    Saving
    Code:
    #ifndef SAVING_H
    #define SAVING_H
    
    //Customer Class
    class Saving:public Account
    {
    private:
    
    public:
    Saving(int, double, double);
    void applyInterest(); 
    };
    
    void Saving::applyInterest() {
                    interest = rate * balance;
                    NewBalance = interest + balance;
                    cout << interest << " 23 interest has been charged to this credit card on its unpaid balance.\n\n";
    };
    #endif
    
    Main.cpp
    Code:
    #include "Account.h"
    #include "Checking.h"
    #include "Saving.h"
    #include "Credit.h"
    using namespace std;
    
    int main() {
    
        int current_cust=0;
        Checking *myChecking[1];
        Saving *mySaving[1];
        Credit *myCredit[1];
    
        Checking myRate;
            myChecking[0] = new Checking(832443, 560.10, .02, 5);
            mySaving[0] = new Saving(832243, 1020.58, .04);
            myCredit[0] = new Credit(832443, 78.00, .16, "1238201293731133"); 
    
        cout << "__________________________________________________________\n"<< endl;
    
        for (int c=0; c<1; c++)
            myChecking[c]->applyInterest();
    
            cout << "__________________________________________________________\n"<< endl;
    
        for (int s=0; s<1; s++)
            mySaving[s]->applyInterest();
    
            cout << "__________________________________________________________\n"<< endl;
    
        for (int x=0; x<1; x++)
            myCredit[x]->applyInterest();
    
        cout << "__________________________________________________________\n"<< endl;
    
            return 0;
    }
    
    ERRORS:

    1>------ Build started: Project: TestTwo, Configuration: Debug Win32 ------
    1> Main.cpp
    1>Main.obj : error LNK2019: unresolved external symbol "public: __thiscall Credit::Credit(int,double,double,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (??0Credit@@QAE@HNNV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) referenced in function _main
    1>Main.obj : error LNK2019: unresolved external symbol "public: __thiscall Saving::Saving(int,double,double)" (??0Saving@@QAE@HNN@Z) referenced in function _main
    1>Main.obj : error LNK2019: unresolved external symbol "public: __thiscall Checking::Checking(int,double,double,int)" (??0Checking@@QAE@HNNH@Z) referenced in function _main
    1>\TestTwo.exe : fatal error LNK1120: 3 unresolved externals
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
     
  7. Mar 11, 2010 at 6:24 PM
    #7
    Evil Monkey

    Evil Monkey There's an evil monkey in my truck

    Joined:
    Aug 8, 2007
    Member:
    #2352
    Messages:
    8,262
    Gender:
    Male
    First Name:
    Robert
    Escondido, CA
    Vehicle:
    07 4x4 DC SR5 TRD Off-road
    Weathertech front & rear mats, rear suspension TSB, Toytec AAL for TSB, Hi-Lift Jack, Bilstein 5100 & Toytec Adjustable coilovers, Built Right UCAs, KMC XD 795 Hoss Wheels, Definity Dakota MTs 285/75R16, Leer XR, Thule Tracker II & Thule MOAB basket
    You have to implement those constructors that take variables in each of your classes. You only have a prototype.
     
  8. Mar 11, 2010 at 8:27 PM
    #8
    tarheelfan_08

    tarheelfan_08 [OP] Carolina Alliance

    Joined:
    Jan 4, 2008
    Member:
    #4098
    Messages:
    2,944
    Gender:
    Male
    First Name:
    Justin
    North Carolina
    Vehicle:
    2010 White 4x4 Double Cab Tacoma Short Bed
    Chrome Grill, Chrome accessories(door handles and mirror covers), LEER Super White Cover, Mirrors with Turn Signals, Window Vent Visors, Step Rails, WeatherTech Digital Floor Mats, Mini Maglight Holder, Chrome Bull Bar
    what do you mean?

    Do you mean I need to have investments and rate and such defined in every class?
     
  9. Mar 12, 2010 at 6:53 AM
    #9
    Evil Monkey

    Evil Monkey There's an evil monkey in my truck

    Joined:
    Aug 8, 2007
    Member:
    #2352
    Messages:
    8,262
    Gender:
    Male
    First Name:
    Robert
    Escondido, CA
    Vehicle:
    07 4x4 DC SR5 TRD Off-road
    Weathertech front & rear mats, rear suspension TSB, Toytec AAL for TSB, Hi-Lift Jack, Bilstein 5100 & Toytec Adjustable coilovers, Built Right UCAs, KMC XD 795 Hoss Wheels, Definity Dakota MTs 285/75R16, Leer XR, Thule Tracker II & Thule MOAB basket
    For example, inside your savings class, you have the following constructor:

    public:
    Saving(int, double, double);


    But in your implementation, you don't specify what that constructor should do when it's passed an int, double, double. That's why you're getting an error.

    The class itself will compile because it'll just see the function prototype and assume it's implemented elsewhere, but when you call the constructor in main, it doesn't know what to do. It doesn't just automatically call the base class. That's why the error is in main vs a line in the class. The reason is that in c++ it's possible to inherit from multiple base classes so you have to specifically tell the derived class what you want it to do with the variables you've passed just like you did in your Account class constructor Account(int, double, double).

    For example, if you just want it to initialize the base class, then you have to specifically call the base class constructor.

    public Saving::Saving (int variable1, double variable2, double variable3) : Account (variable1, variable2, variable3) {

    //maybe do something else in here that is
    //specific to a Saving class that an account class doesn't do

    }

    Or you could do something like this. Here my constructor initializes the base class' default constructor and I use the base class' functions to do something further:

    public Saving::Saving (int variable1, double variable2, double variable3):Account() {

    Account::setNumber(variable1);
    Account::setBalance(variable2);
    Account::setRate(variable3);
    }
     
  10. Mar 12, 2010 at 7:42 AM
    #10
    Evil Monkey

    Evil Monkey There's an evil monkey in my truck

    Joined:
    Aug 8, 2007
    Member:
    #2352
    Messages:
    8,262
    Gender:
    Male
    First Name:
    Robert
    Escondido, CA
    Vehicle:
    07 4x4 DC SR5 TRD Off-road
    Weathertech front & rear mats, rear suspension TSB, Toytec AAL for TSB, Hi-Lift Jack, Bilstein 5100 & Toytec Adjustable coilovers, Built Right UCAs, KMC XD 795 Hoss Wheels, Definity Dakota MTs 285/75R16, Leer XR, Thule Tracker II & Thule MOAB basket
    Also, I believe any function in account that you want to be able to call from a derived class, should be rewritten for the derived class. If it's just doing the same thing, then you can just call the base class' function inside the derived class. For example, in your base class you have the function public void setBalance(double);

    If you want the same functionality in your derived class you need to create a function in the derived class that does the same thing or calls the base class function. The way you have it now you couldn't do this:

    Checking myChecking = Checking();
    myChecking.setBalance(variable);

    I believe it would flag as an error because there is no setBalance(double) function defined in Checking.

    You would want a function in Checking that calls the base class function.

    public void Checking::setBalance(double variable) {
    Account::setBalance(variable); //call the base class' function
    }



    So if we're having to rewrite everything, you may ask why bother?

    It allows us to write complex functions once and reuse them in the derived class.

    The setBalance function call is relatively trivial but if the function call were complex, we'd save rewriting and debugging another complex function. Also, if we decided we didn't like the way the function worked, we'd only have to change it in one place vs. three classes. In a simple class, it might not be a big deal but in a complex class, it might, especially if the other classes get out of sync (it's not uncommon to forget everywhere you created a similar function).

    It also comes in handy if you were going to pass the object to a function.

    For example, if I had a function that I wanted to be applicable to all types of accounts, I could create a function with each variable type. Let's say I wanted to create a printBalance function.

    I could create one for each class type:
    void printBalance(Checking); //prototype to pass an object of type Checking
    void printBalance(Saving); //prototype to pass an object of type Saving
    void printBalance(Credit); //prototype to pass an object of type Credit

    I would then have to implement each one of those functions.

    Or I could create one function that takes an Account.

    void printBalance(Account); //prototype to pass an object of type Account

    void printBalance(Account variable1) {
    //This is calling Account.getBalance()
    cout<<"The balance in your account is " << variable1.getBalance();
    }

    With that function I can pass an Account object or any object that's derived from Account.
    So:
    Checking myChecking = Checking();
    Saving mySavings = Saving();

    //using the printBalance(Account) function call, this would be valid
    printBalance (myChecking);
    printBalance (mySavings);

    Inside that function, it would see the object as an account object. Only those functions available in Account would work (unless you recast but that's for another day).
     
  11. Mar 12, 2010 at 7:45 AM
    #11
    Evil Monkey

    Evil Monkey There's an evil monkey in my truck

    Joined:
    Aug 8, 2007
    Member:
    #2352
    Messages:
    8,262
    Gender:
    Male
    First Name:
    Robert
    Escondido, CA
    Vehicle:
    07 4x4 DC SR5 TRD Off-road
    Weathertech front & rear mats, rear suspension TSB, Toytec AAL for TSB, Hi-Lift Jack, Bilstein 5100 & Toytec Adjustable coilovers, Built Right UCAs, KMC XD 795 Hoss Wheels, Definity Dakota MTs 285/75R16, Leer XR, Thule Tracker II & Thule MOAB basket
    Ignore the thumbs down icon on my previous post. I must have hit it by accident. :)
     

Products Discussed in

To Top