Member Avatar for Jbvo

I am having a hard time figuring out polymorphism with vectors. I keep getting random numbers which I'm guessing is the memory location. If that is the case what am I doing wrong with pointers. And here is part of my .cpp file. Thanks in advance for your help.

// part of .cpp file

    int ac;
    string name;
    double balance;

    ac = 0;
    name = 'Jon';
    balance = 300;

    int ac1;
    string name1;
    double balance1;

    ac1 = 1;
    name1 = 'Bill';
    balance1 = 200;

    Info* b[2];  //Info is the name of my base class
    b[0]= new sInfo(ac, name, balance); //sInfo class Inherits from Info
    b[1]= new CInfo(ac1, name1, balance1); //CInfo class Inherits from Info

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

       cout << b[i]->getac() <<b[i]->getName() <<b[i]->getBalance()<<endl;
    }

Recommended Answers

All 6 Replies

The name strings should be "Jon" and "Bill" so I'm surprised it compiles.
Without knowing anything about your classes, it's difficult to comment further.

I don't think you quite understand Polymorphism. Can you show the class definitions please? Also, explain what it is you're actually trying to do..

Member Avatar for Jbvo

Thanks for all the feedback so far.

I'm trying to simultate tuition payments at a school that offers several plans (different payback lengths and interest). Info is the base class and sInfo and CINfo are different payment plans that derives from Info. When it is done a user will enter the student payment info (account number, name, balance, etc) and will store the information and allow the user to access the account information with the account number and simulate payments to the different accounts.

Right now I am trying to make sure I can take in some test examples (jon and bill) then store them and access them/print the info. I will later have it calulate interest, etc depending on the account type (CInfo/sinfo) Here is part of my header.

//part of .h
class Info
{
private:
    int AccountNumber;
    string StudentsName;
    double Balance;

public:

    Info(int A, string N, double M) 
    {AccountNumber = A;
    StudentsName = N;
    Balance = M;
    double getBalance() { return Balance; }
    string getName() { return StudentsName; }
    int getAccount() { return AccountNumber; }
    }
    ~Info(void);
    //...
    };

    class CInfo: public Info
    {
public:
    CInfo(int A, string N, double M) : Info(A,N,M) {}
    ~CInfo(void);

//...

};

class SInfo: public Info
    {
public:
    SInfo(int A, string N, double M) : Info(A,N,M) {}
    ~SInfo(void);

//..


};

Polymorphism changes what the output is depending on which object is called.

For example:

An animal can speak, you can have many animals.. "Cat", "Dog" are both animals so they can be inherited from animal HOWEVER, they both speak different so you can use polymorphism to output different speaks() depending on what object is being called.

Let's take the example of Students, you can have "Home" and "International" student's and they can both inherit from Students HOWEVER, their fee's change. Look at this example I worte:

#include <iostream>

using namespace std;

class Fees {

    public:

        Fees() { }

        virtual float feeAmount() { return 0; }

};

class Home : public Fees {

    public:

        float feeAmount() { 
            return 12000;   
        }

};

class International : public Fees {

    public:

        float feeAmount() {
            return 15000;

        }

};
int main(int argc, char *argv[]) {

    Home h;
    International i;

    cout << h.feeAmount() << endl;

    cout << i.feeAmount();

}

Why are lines 15-17 inside your constructor? Your close bracket on line 18 should be on line 15.

commented: Just noticed that! +6
Member Avatar for Jbvo

Sorry my bad. I was copying and pasting from my header file the lines that I have questions on. In my actuall code they are not inside my constructor. I pasted them in the wrong spot. My problem seems to be that I am not accessing the info correctly after storing it. I am printing the memory location instead of the acutal data. But I can't seem to figure out where I went wrong.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.