Link to home
Start Free TrialLog in
Avatar of mappenzellar
mappenzellar

asked on

problem with accessing info from class

ok I have 2 classes. customerData and loanData. the problem is that I cannot access info from loanData in customerData because customerData is defined first. EX:

class customerData
{
public:
      string name;
      string streetAddress;
      string city;
      string state;
      string zip;       
      loanData loanInfo; // THIS DOES NOT WORK BECUASE IT DOESN'T SEE THE CLASS BELOW YET
private:            
};

class loanData: public customerData
{
public:
      loanData();
      ~loanData();
      void displayHeader();
      void performCalcs();
      void enterFinancials();      
private:
   double       p;
   double       i;
   double       j;    
   customerData custInfo; // THIS WORKS CAUSE customerData class already exists
};
Avatar of jkr
jkr
Flag of Germany image

>>class loanData: public customerData

If loanData inherits from customerData, it does not make sense that customerData has a member that is a derived class. Re-check your data model.
Avatar of mappenzellar
mappenzellar

ASKER

ok..so maybe that part is wrong..was an attempt to get it to work..is there anyway to access info from loanData in customerData?
You can not have bothe classes contain each other.
If you are refering to the case where the class has not been yet declared then You could have a forward declaration

//forward declaration
class loanData;

class customerData
{
public:
     string name;
     string streetAddress;
     string city;
     string state;
     string zip;      
     loanData loanInfo;
private:          
};

class loanData
{
public:
     loanData();
     ~loanData();
     void displayHeader();
     void performCalcs();
     void enterFinancials();    
private:
   double       p;
   double       i;
   double       j;    
};
ASKER CERTIFIED SOLUTION
Avatar of guntherothk
guntherothk

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>> If loanData inherits from customerData, it does not make sense that customerData has a member that is a derived class. Re-check your data model.

That's absoutely true. You shouldn't have a derived class member in the base class, even it would work when using pointers or references.

>> ..is there anyway to access info from loanData in customerData

Yes, you may have virtual functions that give infos that can be evaluated in the base class.

class Base
{
     virtual string getClassName() = 0;        // makes Base abstract

     bool isA(const string className)  {  return getClassName() == className; }
};

class Derived : public Base
{
      string getClassName()  { return "Derived"; }
};

Regards, Alex
you need to declare two classes before define.

class A;
class B;
calss C;


class A
{
public:
  B b;
 ...
};

class B
{
public:
  C c;
 ...
};

class C
{
public:
  A a;
 ...
};
So customerData has a loanData, which has a customerData, which as we already know has a loanData, which has a customerData, which as we already know has a loanData, which has a customerData, which as we already know has a loanData, which has a customerData, which as we already know has a loanData, which has a customerData, which as we already know has a loanData, which has a customerData, which as we already know has a loanData, which has a customerData, which as we already know has a loanData, which has a customerData, which as we already know has a loanData, which has a customerData, which as we already know has a loanData, which has a customerData, which as we already know has a loanData, which has a customerData, which as we already know has a loanData, which has a customerData, which as we already know has a loanData, which has a customerData, which as we already know has a loanData....

This class is of infinite size!

> is there anyway to access info from loanData in customerData?

Try to model your problem.

You might adopt the approach that Customers have Loans and Loans have pointers to their customers.

e.g.
--------8<--------
#include <vector>

class Customer; // We'll define this class later. This simply says that Customer is a class.

class Loan {
    // ...price etc.
    Customer* pcustomer; // We can put a pointer to the customer here
public:
    // Your actual ctor will need to initialise the price too
    Loan(Customer* pcustomer) : pcustomer(pcustomer) {} // Initialise the pointer in the ctor        
};

class Customer {
    // ...
    std::vector<Loan> loanList;
public:
    // Silly AddLoad implementation - you'll want price parameters to pass to your Loan ctor
    void AddLoan()
    {
        loanList.push_back(Loan(this));
    }
};

int main()
{
        Customer fred;
        fred.AddLoan();
}
--------8<--------

You may be tempted to make the Loan take a reference to a Customer, but you'll find that STL requires that you define an assignment operator for Loan, which is something you cannot do, because a reference can't be made to refer to something different.