• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 175
  • Last Modified:

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
};
0
mappenzellar
Asked:
mappenzellar
1 Solution
 
jkrCommented:
>>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.
0
 
mappenzellarAuthor Commented:
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?
0
 
MafaldaCommented:
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;    
};
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
guntherothkCommented:
To access an instance of loanData within a method of customerData, you need a pointer or reference to the instance of loanData.

What you have done is try to put an instance of loanData into each instance of customerData, but also have an instance of customerData within each instance of loanData, which as you've understood doesn't work.

One way to solve your problem is to have customerData instance contain a reference to a loanData. It turns out that C++ doesn't need the full class definition of loanData to declare an reference to it.

class loanData; // forward reference, just says there's going to be a class called loanData
class customerData
{
    customerData(loanData& l) // constructor must initialize the reference
      : loan_(l)
   { /*empty ctor body */ }

// your member declarations here
    loanData& loan_; // reference is like a pointer, kinda
// ...
};

class loanData // actual class definition for loanData
{
// loanData members...
};

// now in your main code

loanData l;
customerData c(l); // when you construct the customerData, you hand in a reference to a loanData.

Now in your example it looks like you want to access customerData from loanData methods as well as loanData from customerData methods. If each customer has one loan, then what you could do is have a member of type loanData inside customerData, and initialize the loanData member with a reference to customerData. You use the method above only turned around for the other class.

class customerData; // forward reference
class loanData
{
    customerData& cust_;
//  your other members
public:
    loanData(customerData& cd)
      : cust_(cd)
    { /*empty */ }
//  other loanData methods
};

class customerData
    loanData loan_;
//  other members
public:
    customerData()
      : loan_(*this)
    { /* empty */ }
//  other methods
};

Now when you construct an instance of customerData, it passes a reference to itself into the member instance of loanData. loanData methods use this reference to access customerData members. customerData methods use the contained loanData instance to get access to loanData members.

References can only be initialized in the constructor-initializer-list. You could do the same thing with pointers and initialize the pointer either in the ctor-initializer or in the ctor body. I happen to like references better. Your data model might assume one customer can have several loans, in which case you have an array or container of loanData instances in the customerData, each of which is initialized to point to the customer.

Some compilers like VC6 will give you a warning message if you pass 'this' into a base class initializer. But it's legal as long as you don't dereference 'this' inside the base class initializer.
0
 
itsmeandnobodyelseCommented:
>> 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
0
 
rendaduiyanCommented:
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;
 ...
};
0
 
rstaveleyCommented:
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now