Creating Objects within a Class

  • Context: MHB 
  • Thread starter Thread starter WarGhSt
  • Start date Start date
  • Tags Tags
    Class
Click For Summary
SUMMARY

The discussion focuses on the implementation of a Billing class in C++ that tracks interactions between Athlete and Trainer objects. The initial implementation incorrectly attempts to create Athlete and Trainer objects within the Billing class instead of passing them as parameters. The correct approach involves defining the Billing class to accept existing Athlete and Trainer objects, allowing it to access their properties directly. This ensures better encapsulation and adheres to object-oriented programming principles.

PREREQUISITES
  • C++ programming language proficiency
  • Understanding of object-oriented programming concepts
  • Familiarity with class constructors and member functions
  • Knowledge of inheritance in C++
NEXT STEPS
  • Implement the Billing class constructor to accept references to Athlete and Trainer objects: Billing(const Athlete& athlete, const Trainer& trainer).
  • Research how to overload the output stream operator in C++ to allow cout << aBill.
  • Explore how to calculate total income from multiple Billing records.
  • Investigate best practices for encapsulating logic within classes in object-oriented design.
USEFUL FOR

Software developers, particularly those working with C++ and object-oriented programming, will benefit from this discussion. It is also relevant for educators teaching programming concepts and students learning about class design and object interactions.

WarGhSt
Messages
15
Reaction score
0
Working on a project here - I've got the classes [MINUS Billing] defined and, as far as I know, meet the requirements for the constructors, accessors, and mutator functions.

My problem lies with the Billing class. It asks me to create an athlete and trainer object inside the billing class but either it was a topic that was supposed to be so easy that it was assumed I'd know it immediately or it wasn't covered. I've spent the last hour or two looking around google and through my textbook but so far I've come up with very little. On top of that, I'm not sure I understand the need for Billing to be its own class to begin with.


View attachment 5573My code:
Code:
#include <iostream>
#include <string>
using namespace std;

class FitnessMember {
    private:
        string name;
    public:
        FitnessMember(){
            name = "";
        }
        FitnessMember(string fitName){
            name = fitName;
        }
        string getName() const {
            return name;
        }
        void setName(string fitName){
            name = fitName;
        }
};

class Athlete: public FitnessMember {
private:
    int idNum;
public:
    Athlete(string name, int idNumIs) : FitnessMember(name)
        {
            idNum = idNumIs;
        }
        int getidNum() {
            return idNum;
        }

};

class Trainer: public FitnessMember {
private:
    string specialty;
    double cost;
public:
    Trainer(string name, string specialtyIs, double costIs) : FitnessMember(name)
    {
        specialty = specialtyIs;
        cost = costIs;
    }
    string getSpecialty(){
    return specialty;
    }
    double getCost(){
    return cost;
    }
};
int main()
    {
        Athlete aAthlete("Jerry", 1348);
        Athlete bAthlete("Crabbe", 10041);
        Trainer aTrainer("Sally", "Squats", 75.44);
        Trainer bTrainer("Malfoy", "Cardio", 90.12);
        cout << "Athlete name: " << aAthlete.getName() << endl;
        cout << "Athlete idnum: " << aAthlete.getidNum() << endl;
        cout << "Athlete name: " << bAthlete.getName() << endl;
        cout << "Athlete idnum: " << bAthlete.getidNum() << endl;

        cout << "Trainer name: " << aTrainer.getName() << endl;
        cout << "Trainer specialty: " << aTrainer.getSpecialty() << endl;
        cout << "Trainer cost: $" << aTrainer.getCost() << endl;
        cout << "Trainer name: " << bTrainer.getName() << endl;
        cout << "Trainer specialty: " << bTrainer.getSpecialty() << endl;
        cout << "Trainer cost: $" << bTrainer.getCost() << endl;
    return 0;
    }
 

Attachments

  • C++.png
    C++.png
    7.1 KB · Views: 116
Technology news on Phys.org
WarGhSt said:
Working on a project here - I've got the classes [MINUS Billing] defined and, as far as I know, meet the requirements for the constructors, accessors, and mutator functions.

My problem lies with the Billing class. It asks me to create an athlete and trainer object inside the billing class but either it was a topic that was supposed to be so easy that it was assumed I'd know it immediately or it wasn't covered. I've spent the last hour or two looking around google and through my textbook but so far I've come up with very little. On top of that, I'm not sure I understand the need for Billing to be its own class to begin with.


My code:
Code:
#include <iostream>
#include <string>
using namespace std;

class FitnessMember {
    private:
        string name;
    public:
        FitnessMember(){
            name = "";
        }
        FitnessMember(string fitName){
            name = fitName;
        }
        string getName() const {
            return name;
        }
        void setName(string fitName){
            name = fitName;
        }
};

class Athlete: public FitnessMember {
private:
    int idNum;
public:
    Athlete(string name, int idNumIs) : FitnessMember(name)
        {
            idNum = idNumIs;
        }
        int getidNum() {
            return idNum;
        }

};

class Trainer: public FitnessMember {
private:
    string specialty;
    double cost;
public:
    Trainer(string name, string specialtyIs, double costIs) : FitnessMember(name)
    {
        specialty = specialtyIs;
        cost = costIs;
    }
    string getSpecialty(){
    return specialty;
    }
    double getCost(){
    return cost;
    }
};
int main()
    {
        Athlete aAthlete("Jerry", 1348);
        Athlete bAthlete("Crabbe", 10041);
        Trainer aTrainer("Sally", "Squats", 75.44);
        Trainer bTrainer("Malfoy", "Cardio", 90.12);
        cout << "Athlete name: " << aAthlete.getName() << endl;
        cout << "Athlete idnum: " << aAthlete.getidNum() << endl;
        cout << "Athlete name: " << bAthlete.getName() << endl;
        cout << "Athlete idnum: " << bAthlete.getidNum() << endl;

        cout << "Trainer name: " << aTrainer.getName() << endl;
        cout << "Trainer specialty: " << aTrainer.getSpecialty() << endl;
        cout << "Trainer cost: $" << aTrainer.getCost() << endl;
        cout << "Trainer name: " << bTrainer.getName() << endl;
        cout << "Trainer specialty: " << bTrainer.getSpecialty() << endl;
        cout << "Trainer cost: $" << bTrainer.getCost() << endl;
    return 0;
    }

Hi WarGhSt!

So let's create a Billing class and a couple of Billing records shall we?
It is supposed to track the generated income of a meeting of a Trainer with an Athlete.
Suppose Sally has a training session with Jerry doing Squats.
Then Jerry has to pay the fee of 75.44, which is the billing amount.

So the Billing class should have an Athlete and a Trainer as input and keep track of them.
That means we need accessors and mutators for those.
Additionally we should have an accessor to retrieve the generated income.
 
Think I've done it! Could you take a look?

Code:
class Billing {
private:
    double bill;
    string trainer;
    string athlete;
public:
    Billing(string athleteIs, string trainerIs, double bil){
        athlete = athleteIs;
        trainer = trainerIs;
        if (bil > 0) {
            bill = bil;
        }
        else {
            bill = 0.0;
        }
        }
        string getAthlete(){
        return athlete;
        }
        string getTrainer(){
        return trainer;
        }
        double getBill(){
        return bill;
        }
    };

Inside Int Main

Code:
        Billing aBill("Justin", "Stan", 134.44);
        Billing bBill("Tommy", "Will", 61.12);
        cout << "Athlete " << aBill.getAthlete() << ", used trainer " << aBill.getTrainer() <<
        ", and now has a bill of: $" << aBill.getBill() << endl;
        cout << "Athlete " << bBill.getAthlete() << ", used trainer " << bBill.getTrainer() <<
        ", and now has a bill of: $" << bBill.getBill() << endl;

- - - Updated - - -

I could also re-post the full program if that helps.
 
WarGhSt said:
Think I've done it! Could you take a look?

Code:
class Billing {
private:
    double bill;
    string trainer;
    string athlete;
public:
    Billing(string athleteIs, string trainerIs, double bil){
        athlete = athleteIs;
        trainer = trainerIs;
        if (bil > 0) {
            bill = bil;
        }
        else {
            bill = 0.0;
        }
        }
        string getAthlete(){
        return athlete;
        }
        string getTrainer(){
        return trainer;
        }
        double getBill(){
        return bill;
        }
    };

Inside Int Main

Code:
        Billing aBill("Justin", "Stan", 134.44);
        Billing bBill("Tommy", "Will", 61.12);
        cout << "Athlete " << aBill.getAthlete() << ", used trainer " << aBill.getTrainer() <<
        ", and now has a bill of: $" << aBill.getBill() << endl;
        cout << "Athlete " << bBill.getAthlete() << ", used trainer " << bBill.getTrainer() <<
        ", and now has a bill of: $" << bBill.getBill() << endl;

- - - Updated - - -

I could also re-post the full program if that helps.

Well... that satisfies the immediate requirements... but in object oriented programming we want to keep the logic that belongs to an object inside that object. (Thinking)

We don't want to have to tell a Billing object how much a Trainer charges.
Instead we want the Billing object itself to ask the Trainer how much (s)he charges, which it needs to figure out what the generated income is.
Furthermore, the bills should be for trainers and athletes that are already defined.

The usage in main() might for instance be:
Code:
    Billing aBill(aAthlete, aTrainer);
    Billing bBill(bAthlete, aTrainer);
    cout << "Athlete " << aBill.getAthlete().getName() << ", used trainer " << aBill.getTrainer().getName() <<
        ", and now has a bill of: $" << aBill.getBill() << endl;
    cout << "Athlete " << bBill.getAthlete().getName() << ", used trainer " << bBill.getTrainer().getName() <<
        ", and now has a bill of: $" << bBill.getBill() << endl;

Can you come up with a class definition that allows this usage? (Wondering)

Btw, later on we might want to improve the Billing class such that we can write [m]cout << aBill << endl;[/m].
That is, we want the Billing class to stream to cout (or any other file stream) whatever it thinks should be there, rather than us trying to do that repeatedly. But that's probably for a later exercise.

Oh, and the problem statement also asks for the total income generated from at least 2 billing records...
 
I'm afraid I'm not seeing what I can do here.
I've tried creating objects inside the class but I'm doing it wrong, I suppose.
It's probably very simple. I just am not getting it.
 
WarGhSt said:
I'm afraid I'm not seeing what I can do here.
I've tried creating objects inside the class but I'm doing it wrong, I suppose.
It's probably very simple. I just am not getting it.

You don't want to create objects in the class you want the class to take objects as parameters, specifically one trainer and one athlete objects.
 
Exactly!
We're looking for a constructor like:
Billing( Athlete athlete, Trainer trainer).
Or from a C++ perspective, better would be:
Billing(const Athlete& athlete, const Trainer& trainer).
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 3 ·
Replies
3
Views
10K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
9K