Solved

problem with accessing info from class

Posted on 2004-04-26
10
160 Views
Last Modified: 2010-04-01
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
Comment
Question by:mappenzellar
10 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 10918972
>>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
 

Author Comment

by:mappenzellar
ID: 10918983
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
 
LVL 6

Expert Comment

by:Mafalda
ID: 10919188
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 2

Accepted Solution

by:
guntherothk earned 50 total points
ID: 10919408
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
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 10922563
>> 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
 
LVL 5

Expert Comment

by:rendaduiyan
ID: 10924187
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
 
LVL 17

Expert Comment

by:rstaveley
ID: 10926773
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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
IdTelnet1DataAvailable handler freezes the Application 4 68
computer science syllabus 3 70
Least Squares Curve Fitting 4 61
Path to  STL Map header file 1 43
Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now