dynamic allocation and polymorphism

Hi there!

I have an abstract base class Exercise
with virtual void presentSpecific()const=0;
and two sub-classes Weight and cardio.

and now I would like to crate a main where i declare two pointers of the base class type.
Create an object of each exercise type and make em present them selves on the screen.

this is what I've written.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include "Exercise.h"
#include "Cardio.h"
#include "Weight.h"
int main()
{
	_CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);

	exr * e1=new cardio;
	cardio c1("Cross trainer",45,4000);//(exercise name,duration,distance)
	exr *e2=new WT;
	WT wt1("Low Pulley",20,40,40,90);//(exercise name, duration,reps,lightest weight,highest)
	
	c1.presentSpecific();
	system("pause");

	wt1.presentSpecific();
	system("pause");

	delete e1;
	delete e2;

	return 0;

}


Distance: 4000
Reps:40 Lightest: 40 Heaviest: 90


As you may have noticed the exercise name and duration isn't displayed.
What am I doing wrong?
you write this
1
2
exr * e1=new cardio;
	cardio c1("Cross trainer",45,4000);


but want to do this

 
exr* e1 = new cardio("Cross trainer", 45, 4000);
ok but now when I write the following.
1
2
3
exr* e1 = new cardio("Cross trainer", 45, 4000);
cardio c1;
c1.presentSpecific();

I get.
Distance: 0
That's because you want to work with the pointers you just created, not with the local objects.
ok. How would you do if you want to send that info to the constructor and then show it on the screen?
Last edited on
Question: Do you know what pointers are, and what this:
 
exr* e1 = new cardio("Cross trainer", 45, 4000);


does? It occurs to me you don't, in that case you want to read a tutorial on pointers:
http://cplusplus.com/doc/tutorial/pointers/
I allocate new memory? and point to it?
thanks for the help, I fixed it.
Correct. Now, what comes next?
this is how it looks now and it does what it supposed to.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include "Exercise.h"
#include "Cardio.h"
#include "Weight.h"
int main()
{
	_CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);

	exr * e1=new cardio("Cross trainer",45,4000);
	cardio c1;
	exr *e2=new WT("Low Pulley",20,40,40,90);
	WT wt1;

	e1->present();
	system("pause");

	e2->present();
	system("pause");


	if(typeid(*e2)==typeid(WT))
	{
		cout<<((WT*)e2)->getLight();
	}
	if(typeid(*e1)==typeid(cardio))
	{
		cout<<((cardio*)e1)->getLength();
	}

	delete e1;
	delete e2;

	return 0;

}
Bjarne Stroustrup wrote:
Code that creates an object using new and then deletes it at the end of the same scope is ugly, error-prone, and inefficient.
Just use objects, [edit] not pointers [/edit]

if(typeid(*e2)==typeid(WT)) That doesn't work.

As you may have noticed the exercise name and duration isn't displayed.
What am I doing wrong?
Post the present methods
Last edited on
Just that your C-style casts don't belong there (use dynamic_cast for this), and you don't really use polymorphism like that - polymorphism is used to provide an interface for interacting with an object, without having to know the concrete type of that object.
Its an old assignment from an examination, and its stated that I should have pointers.

why doesn't it work?

I get the correct couts.

Our professor showed us three was we can handle this.
1. typeid (as I did)
2. clone()
3. dynamic_cast
I use typeid bcs he uses only that one, and it is in all examples he has written.
ne555 wrote:
if(typeid(*e2)==typeid(WT)) That doesn't work.


Why? This works just fine for me:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>

using namespace std;



int main(int argc, char** argv)
{
	cout<<"typeid int "<<typeid(int).name()<<endl; //int
	cout<<"typeid 5L "<<typeid(5L).name()<<endl; //long
	cout<<"typeid 5==typeid int? "<< (typeid(int)==typeid(5) ? "true" : "false") <<endl; //true
	cin.ignore(cin.rdbuf()->in_avail()+1);
	return 0;
}


And when he has to do it for an assignment, he should do just that. Of course it doesn't make very much sense, but it's just to introduce the concept of polymorphism here. My only complaint with his code are the C-Style casts he uses.
Last edited on
Sorry, my mistake. When I tested it, I forgot to put a virtual method.
Topic archived. No new replies allowed.