Solved

problem with accessing info from class

Posted on 2004-04-26
10
159 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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++.

707 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

18 Experts available now in Live!

Get 1:1 Help Now