#Help with classes

60 messages · Page 1 of 1 (latest)

empty mortar
#

Hi, im pretty new to coding as a whole. Right now im having a pretty tough time passing an object of a class to another object. whats supposed to be happening is that DrinkType soda is supposed to be passing its cost, $0.75, to the change class where the change is calculated. However, it does not seem like it is being passed correctly. Ive already tried asking AIs for help and ive been at this for like 2 hours. Any help is appreciated

stiff kernelBOT
#

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 use !howto ask.

empty mortar
#
#include <iostream>
#include <string>
using namespace std;

class DrinkType
{
private:
  string location;
  string name;
  int quantity;
  bool is_drink_stuck();
public:
  DrinkType(double c, int q, string n, string loc){}
  double cost;
};

class change : public DrinkType
{
private:
  int num_coin_dollars;
  int num_quarters;
  int num_dimes;
  int num_nickels;
  int num_pennies;
public:
  change(double amount_of_money, DrinkType& drink) : DrinkType(drink.cost, 0, "", ""), num_coin_dollars(0), num_quarters(0), num_dimes(0), num_nickels(0), num_pennies(0) 
    {
      calculateChange(amount_of_money, drink.cost);
      cout << drink.cost << endl;
    }

  void calculateChange(double amount_of_money, double cost) {
    cout << cost << endl;
    double change_due = amount_of_money - cost;
    cout << change_due<<endl;
    int change_in_cents = static_cast<int>(change_due * 100); 
    num_coin_dollars = change_in_cents / 100;
    change_in_cents %= 100;
   
    num_quarters = change_in_cents / 25;
    change_in_cents %= 25;
 

    num_dimes = change_in_cents / 10;
    change_in_cents%= 10;
  

    num_nickels = change_in_cents / 5;
    change_in_cents%= 5;

    num_pennies = change_in_cents;
    
  }
  void dispenseChange() 
    {
      cout << "Your change: " << endl;
      if (num_coin_dollars > 0) {
        cout << num_coin_dollars << " dollars" << endl;
      }
      if (num_quarters > 0) {
        cout << num_quarters << " quarters" << endl;
      }
      if (num_dimes > 0) {
        cout << num_dimes << " dimes" << endl;
      }
      if (num_nickels > 0) {
        cout << num_nickels << " nickels" << endl;
      }
      if (num_pennies > 0) {
        cout << num_pennies << " pennies" << endl;
      }
    }
};

int main() 
{
  DrinkType soda(0.75, 20, "Soda", "A1");

  change changeDue(2.00, soda);
  changeDue.dispenseChange();

  return 0;
}```
stiff kernelBOT
#
Large Language Models (LLMs)

We highly recommend against the use of LLMs and AI assistants because:

  • LLMs are bad at C and C++
  • LLMs are wrong more often than not
  • LLMs answer with complete confidence even when wrong
  • If you're new to C or C++ you likely don't know enough to know when answers are wrong
empty mortar
#

whoops

#

didnt know that

#

the only thing that the ai touched were just the constuctors so should i just completely scrap them

blazing estuary
#

Yea, the constructor is a mess lol

empty mortar
#

whats wrong with it?

#

like in general

#

is it bad to initialize all the member variables with the constuctor

blazing estuary
#

There’s a few things:

  • the constructor should follow the order of when the members get declared in the class.
  • the parameter names are one letter long which are not that useful or descriptive.
  • it’s taking in those things through the parameter but they’re not getting assigned to the member variables
blazing estuary
empty mortar
#

hmm ok one sec lemme try something

blazing estuary
empty mortar
#

im confused

#

im trying to pass the cost, a member of DrinkType to change

#

so if i defined the object of DrinkType already shouldnt the thing im trying to pass already be defined

blazing estuary
#

Hold on, I’ll get on my pc soon

empty mortar
#

oh ok take your time

#

thank you for helping me

stiff kernelBOT
#

@empty mortar Has your question been resolved? If so, type !solved :)

blazing estuary
#

I don't think the inheritance makes sense in this case

#

Inheritance models "is-a" relationship between two objects

#

Like an apple is a fruit, so fruit can be a child class of fruit. But change is not a DrinkType

empty mortar
#

oh i see

#

i really suck at thinking with classes atm

blazing estuary
#

They take time and constant use to get used to

empty mortar
#

my thought process was that the drink cost had to reach the change class somehow

#

so i just made change inherit drinktype

blazing estuary
#

I'm sure there's better solutions but you can use an accessor function to get value of double cost from DrinkType class

#

You can also pass in the object as an argument which you're doing right now

empty mortar
#

ah i didnt know that

#

so the change class doesnt need to be a parent of drinktype

#

i can just pass it as an argument

blazing estuary
#

Yea

empty mortar
#

i see

#

would cost still have to be public?

blazing estuary
#

I noticed you made double cost public, so you could have accessed it from non member functions and main function too

empty mortar
#

oh what

blazing estuary
#

It's better if it's private

empty mortar
#

this is so confusing

#

ok let me modify my code real quick

#

thank you again

blazing estuary
#

np

empty mortar
#

ok it actually worked now that change is not a parent of drinktype

#

thank you so much again

#

didnt know that ai was so bad

#

also if you dont mind

#

do you happen to have any resources you used when learning about using classes?

#

this is just like a small part of a project i have and im having trouble knowing when to use or not use a class

blazing estuary
#

np

stiff kernelBOT
#
How to Learn C++ Programming

We generally recommend a good book to learn the necessary fundamentals:

To actually write and run C++ code, you will need a compiler, editor, and debugger. We strongly recommend to start out using an IDE, which will provide all these tools for you:

<:microsoft:1165512917047853127> Windows
  • [Visual Studio](#1165492293810257920 message)
  • CLion
<:tux:1165505626894520361> Linux
<:apple:1165508607798943754> Mac
Words of Advice

The wise programmer is told about the debugger and uses it.
The average programmer is told about the debugger and avoids it.
The foolish programmer is told about the debugger and laughs at it.

empty mortar
#

thank you!

#

ur a lifesaver

#

!solved

stiff kernelBOT
#

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity

blazing estuary
#

You welcome!

#

If you need more help you can open another thread or keep posting here btw