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
#Help with classes
60 messages · Page 1 of 1 (latest)
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.
#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;
}```
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
whoops
didnt know that
the only thing that the ai touched were just the constuctors so should i just completely scrap them
Yea, the constructor is a mess lol
whats wrong with it?
like in general
is it bad to initialize all the member variables with the constuctor
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
It’s actually good to initialize everything
hmm ok one sec lemme try something
If you forgot to initialize one of your member variables and tried to use it, it would lead to Undefined behavior or UB for short
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
Hold on, I’ll get on my pc soon
@empty mortar Has your question been resolved? If so, type !solved :)
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
They take time and constant use to get used to
my thought process was that the drink cost had to reach the change class somehow
so i just made change inherit drinktype
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
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
Yea
I noticed you made double cost public, so you could have accessed it from non member functions and main function too
oh what
It's better if it's private
np
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
np
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
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.
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