Link to home
Start Free TrialLog in
Avatar of eboen
eboen

asked on

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();
   };
ASKER CERTIFIED SOLUTION
Avatar of RodneyYeo
RodneyYeo

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of KangaRoo
KangaRoo

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.
Avatar of eboen

ASKER

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??????;
}
Avatar of eboen

ASKER

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)

Avatar of eboen

ASKER

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)

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 :)
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 :)
Avatar of eboen

ASKER

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;
}
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 :)
Avatar of eboen

ASKER

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'
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++ :)
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.
You mean like this?
change

pricing.TotalPrice() to

pricing.TotalPrice(orderinfo);

hmm..that might be a better solution :)
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?).
His errors looks like syntax errors though :)
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.
Avatar of eboen

ASKER

Got it working. Thank you both for all your input.  Greatly appretiated.

Sincerely,

Eric Boen