#Classes and Function in C++

48 messages · Page 1 of 1 (latest)

hearty haloBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

sand moat
#

What is "a Wallet 3"?

uncut harbor
#

A Wallet 3 by adding wallet 1 and wallet 2

sand moat
#

You already did that.

uncut harbor
#

Wallet w3(w1.getQuarters() + w2.getQuarters(), w1.getDimes() + w2.getDimes(), w1.getNickles() + w2.getNickles(), w1.getCents() + w2.getCents());

cout << endl
     << "W3:";
cout << "\t" << w3.getQuarters() << " quarters" << endl;
cout << "\t" << w3.getDimes() << " dimes" << endl;
cout << "\t" << w3.getNickles() << " nickels" << endl;
cout << "\t" << w3.getCents() << " cents" << endl;

double total3 = ((w3.getQuarters() * 25 + w3.getDimes() * 10 + w3.getNickles() * 5 + w3.getCents()) / 100.0);
cout << "\t--------> amount = $" << total3 << endl;
#

Instead of these lines in the main function to have the Wallet 3

#

we will create it by using a regular function outside the main function

sand moat
#

Make a function, copy/paste the code into it 🤷

uncut harbor
#

What do you mean ?

#

you mind showing me a code ?

sand moat
#
void f(Wallet w1, Wallet w2) {
Wallet w3(w1.getQuarters() + w2.getQuarters(), w1.getDimes() + w2.getDimes(), w1.getNickles() + w2.getNickles(), w1.getCents() + w2.getCents());

    cout << endl
         << "W3:";
    cout << "\t" << w3.getQuarters() << " quarters" << endl;
    cout << "\t" << w3.getDimes() << " dimes" << endl;
    cout << "\t" << w3.getNickles() << " nickels" << endl;
    cout << "\t" << w3.getCents() << " cents" << endl;

    double total3 = ((w3.getQuarters() * 25 + w3.getDimes() * 10 + w3.getNickles() * 5 + w3.getCents()) / 100.0);
    cout << "\t--------> amount = $" << total3 << endl;
}
#

It's just your code with void f(Wallet w1, Wallet w2) {} around it.

uncut harbor
#

Wallet addTwoWallets(Wallet w1, Wallet w2);

#

This is a hint from the homework

sand moat
#

So just use addTwoWallets instead of f.

#

And return w3; at the end.

#

And you probably don't want to print it.

uncut harbor
#

Can I just have these line in the Function void f(Wallet w1, Wallet w2) {
Wallet w3(w1.getQuarters() + w2.getQuarters(), w1.getDimes() + w2.getDimes(), w1.getNickles() + w2.getNickles(), w1.getCents() + w2.getCents());

#

And then I will print the wallet in the main ?

sand moat
#

Almost. w3 only lives inside f and you cannot access it from main. That's why addTwoWallets returns a Wallet.

#

That way you can access the result later.

uncut harbor
#

I got errors

#

What is your code if you do that way

#

like Function to add 2 wallets

#

and then print out the result at the main function

#

?

sand moat
#

Like I said, you take the hint:
Wallet addTwoWallets(Wallet w1, Wallet w2)
The code:
Wallet w3(w1.getQuarters() + w2.getQuarters(), w1.getDimes() + w2.getDimes(), w1.getNickles() + w2.getNickles(), w1.getCents() + w2.getCents());
and the return:
return w3;
and that should be it.

#

Well you also need the { and }.

uncut harbor
#

Let's say If we just have these line in the main function

#

out << endl
<< "W3:";
cout << "\t" << w3.getQuarters() << " quarters" << endl;
cout << "\t" << w3.getDimes() << " dimes" << endl;
cout << "\t" << w3.getNickles() << " nickels" << endl;
cout << "\t" << w3.getCents() << " cents" << endl;

double total3 = ((w3.getQuarters() * 25 + w3.getDimes() * 10 + w3.getNickles() * 5 + w3.getCents()) / 100.0);
cout << "\t--------> amount = $" << total3 << endl;
#

What's the function look like out side the main ?

sand moat
#

If you do it properly it should be std::ostream &operator<<(std::ostream &os, const Wallet &w).

#

But void print(Wallet w) will do too.

uncut harbor
#

Could you show the code you do by the void print(Wallet w)

sand moat
#

I'm confused why you need that.

#
void print(Wallet w) {
cout << endl
         << "W3:";
    cout << "\t" << w.getQuarters() << " quarters" << endl;
    cout << "\t" << w.getDimes() << " dimes" << endl;
    cout << "\t" << w.getNickles() << " nickels" << endl;
    cout << "\t" << w.getCents() << " cents" << endl;

    double total3 = ((w.getQuarters() * 25 + w.getDimes() * 10 + w.getNickles() * 5 + w.getCents()) / 100.0);
    cout << "\t--------> amount = $" << total3 << endl;
}
#

There is no intelligence, it's just copy/paste.

uncut harbor
#

Maybe I misunderstood the requires of the homework

#

I am also confused the questions on the homework from my professor too

#

I know how to do it niw

#

thank you for your helping

sand moat
#

Putting the printing into a function makes sense, because print(w2); print(w3); is a lot shorter than your previous code.

#

And you'll also need print(w3);.

uncut harbor
#

Got it

#

thank you

hearty haloBOT
#

@uncut harbor Has your question been resolved? If so, run !solved :)

#

@uncut harbor

Please Do Not Delete Posts!

Please don't delete forum posts. They can be helpful to refer to later and other members can learn from them. In the future you can use !solved to close a post and mark a post as solved.

uncut harbor
#

!solved