passing an object to another class

This is for a class project.  I need to pass an object(orderinfo) to another class(Purchaser) for further manipulations.  I don't know how to pass the data I think I need to pass orderinfo as an argument of pricing.TotalPrice(the instance of Purchaser).

Thanks, Eric

Ex: void main(void)
{
Ordering orderinfo;  //defining the objects
Purchaser pricing;

orderinfo.GetData(); //fuction calls
pricing.TotalPrice(); <--need to pass the data to here
}
/---------------------------------------------------
here's the .cpp file for Ordering Class
#include "order.h"
void Ordering::GetData ()
{
 cout << "Please enter the vendor name:  " ;
   cin >> VendName;

   cout << "Please enter the Product Number:  ";
   cin >> ProdNo;

   cout <<"Please enter the quantity ordered:  ";
   cin >> NumOrd;

   cout <<"Please enter the total amount for the order:  " <<"$" ;
   cin >> Cost;

   cout <<"How is the order to be delivered, [S]tandard or [O]overnight:  ";
   cin >> Delivery;

} //end of function Getdata
/**************************************************/
float Ordering::ShowPrice(float Cost)
{
return Cost;
}
/**************************************************/
int Ordering::ShowNum(int NumOrd)
{
return NumOrd;
}
/**************************************************/
here's the .h file for Ordering Class
#include <iostream>
#include <stdio.h>
#include <cstring.h>
#include <conio.h>

class Ordering
      {
         protected:                  
         string      VendName;            
         string      ProdNo;                  
         int            NumOrd;                  
         float      Cost;                        
         char      Delivery;            

        public:                  
            Ordering()
                    {}
         //functions are called from main.cpp via class Ordering.cpp,
         void GetData();                   
         float ShowPrice(float Cost);
         int ShowNum(int NumOrd);
      };

/***************************************************
here's the .cpp from Purchaser class
#include "purch.h"
#include "order.h"
here's the .cpp for Purchaser class
void Purchaser::TotalPrice(void)<----This is where I need the data from
{                                                 the Ordering class object****
Ordering order;
   int Total;

   Price = order.ShowPrice(Price);
   NumberOf = order.ShowNum(NumberOf);
   Total = Price*NumberOf;
}
void Purchaser::DisplayElements(void)
{
cout << Price << endl;
cout << NumberOf << endl;
}
**********************************************
here's the .h from the Purchaser class
#include <iostream>
#include <stdio.h>
#include <cstring>

class Purchaser
      {
   protected:
   float Price;
   int NumberOf;
 
  public:

   Purchaser() : Price(0.0), NumberOf(0)
    {      }
   void TotalPrice();
   void DisplayElements();
   };
eboenAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RodneyYeoCommented:
Try this...using passing by reference(I suppose that's the term used)

Passing class into the function
  pricing.TotalPrice(orderinfo.GetData()); <--need to pass the data to here

change the return to returning by reference like this...
   Ordering& Ordering::GetData ()

don't forget to change the corresponding class member function in the .h header file.

change for Purchaser class also

   void Purchaser::TotalPrice (Ordering&   order)
                                         Ordering order; <--- delete away

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
KangaRooCommented:
Pass the Orderinfo object to the purchaser object. There are two 'good' ways and one 'bad' way.

g1: void Purchaser::TotalPrice(Ordering* order);
g2: void Purchaser::TotalPrice(Ordering& order);

b:  void Purchaser::TotalPrice(Ordering order);

The 'bad' method passes a copy of the object to the function. This is something one usually avoids. The good methods pass a reference. In g1 you pass a pointer and the methods of the object are called as in
   order->ShowPrice()
In g2 you call methods with:
   order.ShowPrice();

Since it os possible to pass NULL pointers to the first method, this should be test. The second method is safer in this respect. By far the best is
  void Purchaser::TotalPrice(const Ordering& order);
The const will forbid the function TotalPrice to make any changes to the Ordering object, or call any of its methods that would change it. I don't think it is expected of you n this stage.
If you have any questions, please ask.
0
eboenAuthor Commented:
Hello,

Question:
Since a fuction can return only a single value, should I break the Ordering& Ordering::GetData () into five seperate fuctions so I can return ptr's to each data elemnt, ex: VendName, ProdNo etc..  I can't figure out how to return the entire set of data from the one function. I will need each element of data at some pt.  One other thing, how do I declare the Ordering object in my purchasing class for the subsequent header void TotalPrice(Order& order).  When I tried this:

class Purchaser
{
private:
Ordering order;
public:
void TotalPrice(Order&); /**also with Order& order**/
}
Compiler complained about earlier declaration in order.h.  I included the order.h header in purchaser.h.?????

Thanks again

eric

I tried to return this (without the question marks of course)
Ordering& Ordering::GetData()
{
cout << "Please enter the vendor name:  " ;
cin >> VendName;
cout << "Please enter the Product Number:  ";
cin >> ProdNo;
cout <<"Please enter the quantity ordered:  ";
 cin >> NumOrd;
cout <<"Please enter the total amount for the order:  " <<"$" ;
cin >> Cost;
cout <<"How is the order to be delivered, [S]tandard or[O]overnight:  ";
cin >> Delivery;

return Ordering??????;
}
0
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.

eboenAuthor Commented:
Error adendum:
Compiling errors Borland C++ 5.02

Info :Compiling D:\eric\c\purch.cpp
Error:  PURCH.H(18,29):) expected
Error:  purch.cpp(6,2):'Purchaser::TotalPrice(Ordering &)' is not a member of 'Purchaser'


Error: #1 void TotalPrice(Ordering& orderinfo);
Error: #2 void Purchaser::TotalPrice(Ordering& orderinfo)
                  {fuction as above

I am including the #include "order.h" in my purch.cpp not purch.h(it complains about multiple declarations there)

0
eboenAuthor Commented:
Error adendum:
Compiling errors Borland C++ 5.02

Info :Compiling D:\eric\c\purch.cpp
Error:  PURCH.H(18,29):) expected
Error:  purch.cpp(6,2):'Purchaser::TotalPrice(Ordering &)' is not a member of 'Purchaser'


Error: #1 void TotalPrice(Ordering& orderinfo);
Error: #2 void Purchaser::TotalPrice(Ordering& orderinfo)
                  {fuction as above

I am including the #include "order.h" in my purch.cpp not purch.h(it complains about multiple declarations there)

0
RodneyYeoCommented:
oopps...I forgot to tell you to return :)

it should be
    return *this;
whereby the instance will be returned.

Useful practice...try to use forward declaration instead of including the header file...examples

//in a.h
class b;
class a{
   int something(b& variable);
};

do the implementation in a.cpp
// in a.cpp
#include "a.h"
#include "b.h"
int a::something(b& variable)
{
....
}

// in b.h
class a;
class b{
    int something_b(a* another_variable);
};

// in b.cpp
#include "a.h"
#include "b.h"
int something_b(a* another_variable)
{....
}

Hope this helps :)
0
RodneyYeoCommented:
You don't have to break the GetData into 5 different functions...just return *this, you will "return the whole class", something like return the whole structure.

Feel free to supply me with more questions with some codes attached :)
0
eboenAuthor Commented:
This can't be that hard!  I've increased the points thanks again

eric

This project requires us to keep the classes in their own .h and .cpp files

here's the error message
Error:  order.cpp(61,18):Improper use of typedef 'Ordering'


Here is the main function call;

Ordering orderinfo; //creates objects
Purchaser pricing;

void main(void)
{
orderinfo.GetData();//this call queries for data
pricing.TotalPrice(orderinfo.GetData());//hopefully passing object to                                                        pricing
}

order.h file
class Ordering
      {
private:                  
    static string      VendName;            
    static string      ProdNo;                   
    static int          NumOrd;                  
    static float      Cost;                        
    static char      Delivery;            
    static Ordering order;//do i need to created an object to pass in
public:                  
    Ordering()
        {}
    Ordering& GetData();<------object Ordering order in here?             
    void ShowData();
    float ShowPrice(float Cost);
    int ShowNum(int NumOrd);
     };

order.cpp
Ordering& Ordering::GetData ()
{cout << "Please enter the vendor name:  " ;
   cin >> VendName;
   cout << "Please enter the Product Number:  ";
   cin >> ProdNo;
   cout <<"Please enter the quantity ordered:  ";
   cin >> NumOrd;
   cout <<"Please enter the total amount for the order:  " <<"$" ;
   cin >> Cost;
   cout <<"How is the order to be delivered, [S]tandard or [O]overnight:  ";
   cin >> Delivery;
return *Ordering;//trying to pass Ordering Class out also tried to create
//this is the error  an object in the fuction, didn't work well
}

purch.h
class Purchaser
      {
private:
   float Price;
   int NumberOf;
 public:

   Purchaser() : Price(0.0), NumberOf(0)
    {      }
   void TotalPrice(const Ordering& order);
   void DisplayElements();
   };

purch.cpp
void Purchaser::TotalPrice(const Ordering& order)
{
Ordering order;
   int Total;

   Price = order.ShowPrice(Price);
   NumberOf = order.ShowNum(NumberOf);
   Total = Price*NumberOf;
}

void Purchaser::DisplayElements(void)
{
cout << Price << endl;
cout << NumberOf << endl;
}
0
RodneyYeoCommented:
return *Ordering;
--> I don't know the different but I normally use
return *this;

Under Purchaser class
{
Ordering order <--- delete away because it will take the in from the parameter of the member function.

Try out first..inform me if there is any errors.

Have fun programming :)
0
eboenAuthor Commented:
These are the errors that are still causing me problems. ??? the compiler doesn't like the header void TotalPrice(Order&) in putch.h
OR void Purchaser::TotalPrice(Ordering& order) in purch.cpp.
i'm using *this(pretty cool keyword, new to me) and i deleted the instance or order in purch.cpp.

???????? tried creating ptrs to classes, combining classes in the same .h's.  I'm already starting to like c better.
thanks
eric

Info :Compiling D:\school\c++\order.cpp
Info :Compiling D:\school\c++\purch.cpp
Error:  PURCH.H(17,29):) expected
Error:  purch.cpp(5,2):'Purchaser::TotalPrice(Ordering &)' is not a member of 'Purchaser'
0
RodneyYeoCommented:
I think you missed a ')' on one of the lines...try finding it..the second error might be caused by the first error..if you really cannot find it, you might want to post your solution here..I can try finding for you :)
Welcome to C++ :)
0
KangaRooCommented:
eboen, leave your Ordering class as it was initially. There is no need to return anything.
The only thing you need to change is
   Puchaser::TotalPrice(Ordering& order)

and remove the
   Ordering order;
from its body.
0
RodneyYeoCommented:
You mean like this?
change

pricing.TotalPrice() to

pricing.TotalPrice(orderinfo);

hmm..that might be a better solution :)
0
KangaRooCommented:
Yeah. And it seems that eboen was heading to that solution in the original question, but just didn't know exactly how to do it (syntax?).
0
RodneyYeoCommented:
His errors looks like syntax errors though :)
0
KangaRooCommented:
purch.h and purchr.cpp need to know class Ordering. You must include the header Order.h
How do you mean, you like C better? f you know C then the pointer stuff shouln't be a problem

Your are passing the Ordering object as a 'const Ordering&' Although using const wherever possible is a good habit, it can also be extremily difficult to get it right. I suggest you let the 'const' rest for a while.
0
eboenAuthor Commented:
Got it working. Thank you both for all your input.  Greatly appretiated.

Sincerely,

Eric Boen
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.