Creating Objects within a Class

  • MHB
  • Thread starter WarGhSt
  • Start date
  • Tags
    Class
In summary: Name() const {...} void setName(string fitName){...}};class Athlete: public FitnessMember {private: int idNum;public: Athlete(string name, int idNumIs) : FitnessMember(name) {...} int getidNum() {...}};class Trainer: public FitnessMember {private: string specialty; double cost;public: Trainer(string name,
  • #1
WarGhSt
15
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: 75
Technology news on Phys.org
  • #2
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.
 
  • #3
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.
 
  • #4
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...
 
  • #5
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.
 
  • #6
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.
 
  • #7
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).
 

FAQ: Creating Objects within a Class

What is the purpose of creating objects within a class?

The purpose of creating objects within a class is to create instances of that class that can hold unique data and perform specific functions. This allows for code reusability and organization.

How do you create an object within a class?

To create an object within a class, you first need to define the class using the class keyword. Then, you can use the new keyword followed by the class name and parentheses to create an instance of the class.

What is the difference between a class and an object?

A class is a blueprint or template that defines the properties and methods of an object. An object, on the other hand, is an instance of that class with its own unique data and behavior.

Can you create multiple objects from the same class?

Yes, you can create multiple objects from the same class. Each object will have its own set of properties and methods, but they will all share the same structure defined by the class.

How do you access the properties and methods of an object within a class?

To access the properties and methods of an object within a class, you use dot notation. This means using the object name followed by a dot and then the property or method name. For example, myObject.myProperty or myObject.myMethod().

Similar threads

Replies
5
Views
2K
Replies
89
Views
5K
Replies
23
Views
2K
Replies
8
Views
2K
Replies
35
Views
3K
Replies
6
Views
1K
Replies
5
Views
2K
Replies
4
Views
3K
Replies
23
Views
2K
Back
Top