Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with Classes

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
I'm not sure how to properly use classes and header files and then implementing the class in another file. So let'S say I have two files. Formulas.h and then Formulas.cc. Formulas.h is my header file and .cc is where I implement the information. This is just an example so I can reference when I start writing my program. Thanks

Following Criteria:
User enters two integers. The two integers are then added together to get let's say the "Pie Value" (these are not the formulas but just for purposes of learning) the two integers are also subtracted to get the "Quad Value." My problem I am having is I"m not sure if I"m writin the class and implementing it correctly. This was what I was thinking but I"m not sure.

***************************************************


//Formula.h
Class Formula{
private:
int pie, quad;
int AddSub();
public:
Formula(int const a, int const b); // constructor
};

//Formula.cc
Formula::Formula(int a, int b) : pie(a), quad(b) {AddSub()};

int Formula::AddSub(){
double pieval, addval;
Formula A;

cin >> A.pie >> A.quad;

pieval = A.pie + A.quad;
quadval = A.pie - A.quad;

cout << pieval << '\n' << quadval;
};

***************************************************

This is the only way to do this (with a .h and a .cc file because the program I am going to create needs these two files and I am not sure how either should look)
 
Well, you are mixing up doubles and ints. You wont see any warnings because the values are being promoted but you will never have any decimal values. Next... why do you need to declare a Formula in the AddSub function? With it's implementation, you could just read into the two values within the class. Maybe it is necessary in future implementation.

Matt
 
So could you please show me how I would go about writing it correctly? Thanks
 
Look what you want to mean with your pie if it is a value or a function.It is declared as an int value and it is used as function.And there is no need to instanciate the formula(Formula A) cause you are in this class and the variables you used and the function are all members of Formula class I think a simple way is to do in Formula.cc
Formula::Formula(int a, int b)
{
pie=a;
qad=b;
AddSub() // if you realy need to call it in this case i would put the 2 lines right here you can also send the parameters into the AddSub function.
}
int Formula::AddSub(){
int pieval=pie+qad;
int quadval=pie-qad;
cout << pieval << '\n' << quadval;
};


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top