Click here to Skip to main content
15,877,296 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
This is the Workout class I want to use in order to make a "BikeWorkout" class. I am not sure how to use aggregation, but I have to do so.

class Workout { 
Workout ( ); 
Workout( int minutes, int heartRate ); 

int getLength( ) const; 
int getHeartRate( ) const; 
int my_Length; // this workout's length 
int my_HeartRate; // this workout's acheived heart-rate 

I also have the .cpp file.

Workout::Workout () 
my_HeartRate = 165; 
my_Length = 30; 

Workout::Workout( int len, int rate ) 
my_HeartRate = rate; 
my_Length = len; 

int Workout::getLength() const 
return my_Length; 

int Workout::getHeartRate() const 
return my_HeartRate; 

How should I start my "BikeWorkout" class and how do I use aggregation to call my parent class? Thank you for your time.
Updated 16-Aug-15 3:14am
Kornfeld Eliyahu Peter 16-Aug-15 9:20am    
"aggregation" of what?
Didn't you meant "inheritance" by any chance?
Member 11912526 16-Aug-15 9:34am    
The way this problem was explained to me was using the word "Aggregation". I have to learn to use class members that are themselves classes. If that is inheritance, then yes. I apologize, I am fairly new to C++.
Kornfeld Eliyahu Peter 16-Aug-15 9:40am    
I'm not sure the word "aggregate" is good or wrong here...
Aggregation is minimum, maximum, sum, count, average, median and more. It's about to take a group of values and run some math over them...
However, when you create a class from another class ('I want to use in order to make a') it mostly called inheritance in the world of OOP (and has nothing directly to do with C++, except, that C++ implements OOP)...
You may explain in more details your problem, so we can understand it better...
Richard MacCutchan 16-Aug-15 13:19pm    
It is more likely that your Workout class should be an abstract class. The BikeWorkout class should then inherit it, and implement all the abstract methods. If you are not clear then check back with your professor to make sure that is what he/she meant.
chandanadhikari 17-Aug-15 1:44am    
as already pointed out, the question is a bit ambiguous.
To know more about aggregation, check out this link (code example is also provided) ::

Aggregation is a kind of 'has a' relationship between classes where a parent contains a child and the child can exist independent of the parent.

In your example, Workout would be the child class and the 'BikeWorkout' class would contain a pointer to 'Workout'. One condition is that the allocation and de-allocation of the Workout class should not be the responsibility of BikeWorkout class. It may be created somewhere else and passed as an argument to the BikeWorkout constructor.

The BikeWorkout class would look like,

class BikeWorkout
  BikeWorkout(Workout *workout)
    : mWorkout(workout)
  Workout *mWorkout;
Share this answer
CPallini 17-Aug-15 15:40pm    
This is the one answer that makes sense. :-)
I wouldn't maintain a pointer to Workout, though.
Ujesh Mohanan 17-Aug-15 16:44pm    
You mean to use a reference? Or smart pointer?
CPallini 17-Aug-15 16:51pm    
I would have used composition, as per your comment to my solution. If forced to aggregation, then yes, I would have used a smart pointer.
Ujesh Mohanan 17-Aug-15 16:59pm    
Thanks for sharing your thought.
Have also a look at this Wikipedia page: "Composition over inheritance"[^].
Share this answer
Ujesh Mohanan 17-Aug-15 16:39pm    
Basically composition is different from aggregation. Composition is a kind of strong bonding where the child (or the class which is contained) cant exist without parent and vice versa.
CPallini 17-Aug-15 16:51pm    
Ah, OK, my bad.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900