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();
};
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
{ the Ordering class object****
Ordering order;
int Total;
Price = order.ShowPrice(Price);
NumberOf = order.ShowNum(NumberOf);
Total = Price*NumberOf;
}
void Purchaser::DisplayElements
{
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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??????;
}
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??????;
}
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: :TotalPric e(Ordering &)' is not a member of 'Purchaser'
Error: #1 void TotalPrice(Ordering& orderinfo);
Error: #2 void Purchaser::TotalPrice(Orde ring& orderinfo)
{fuction as above
I am including the #include "order.h" in my purch.cpp not purch.h(it complains about multiple declarations there)
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:
Error: #1 void TotalPrice(Ordering& orderinfo);
Error: #2 void Purchaser::TotalPrice(Orde
{fuction as above
I am including the #include "order.h" in my purch.cpp not purch.h(it complains about multiple declarations there)
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: :TotalPric e(Ordering &)' is not a member of 'Purchaser'
Error: #1 void TotalPrice(Ordering& orderinfo);
Error: #2 void Purchaser::TotalPrice(Orde ring& orderinfo)
{fuction as above
I am including the #include "order.h" in my purch.cpp not purch.h(it complains about multiple declarations there)
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:
Error: #1 void TotalPrice(Ordering& orderinfo);
Error: #2 void Purchaser::TotalPrice(Orde
{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 :)
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 :)
Feel free to supply me with more questions with some codes attached :)
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(orderin fo.GetData ());//hope fully 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(cons t 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;
}
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
pricing.TotalPrice(orderin
}
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(cons
{
Ordering order;
int Total;
Price = order.ShowPrice(Price);
NumberOf = order.ShowNum(NumberOf);
Total = Price*NumberOf;
}
void Purchaser::DisplayElements
{
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 :)
--> 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 :)
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(Orde ring& 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: :TotalPric e(Ordering &)' is not a member of 'Purchaser'
OR void Purchaser::TotalPrice(Orde
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:
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++ :)
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(Order ing& order)
and remove the
Ordering order;
from its body.
The only thing you need to change is
Puchaser::TotalPrice(Order
and remove the
Ordering order;
from its body.
You mean like this?
change
pricing.TotalPrice() to
pricing.TotalPrice(orderin fo);
hmm..that might be a better solution :)
change
pricing.TotalPrice() to
pricing.TotalPrice(orderin
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.
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.
ASKER
Got it working. Thank you both for all your input. Greatly appretiated.
Sincerely,
Eric Boen
Sincerely,
Eric Boen
g1: void Purchaser::TotalPrice(Orde
g2: void Purchaser::TotalPrice(Orde
b: void Purchaser::TotalPrice(Orde
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(cons
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.