Solved

problem with accessing info from class

Posted on 2004-04-26
10
166 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

623 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