polymorphism

I need to alter a class, to make it from "<<KG*10" to "<<KG*100", very simple, however KG returns with the max value as if it wasn't given a value (on KG*100)

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <iomanip>
#include <iostream>
#include <ostream>
#include <string>

using namespace std;

class PACKAGE{
protected:
double KG;
public:
string EXP_NAME_1,EXP_ADRE_1;
void SET_EXP_INFO(string,string);
void SET_KG();
virtual void CALC_N();
 };//class PACKAGE

void PACKAGE::SET_EXP_INFO(string EXP_NAME_1,string EXP_ADRE_1){
cout<<"EXP_NAME_1:";getline(cin,EXP_NAME_1);
cout<<"EXP_ADRE_1:";getline(cin,EXP_ADRE_1);
cout<<"EXP_NAME_1="<<EXP_NAME_1<<"\n";
cout<<"EXP_ADRE_1="<<EXP_ADRE_1<<"\n";
 }//SET_EXP_INFO

void PACKAGE::SET_KG(){
cout<<"SET KG:";
LOOP1:
cin>>KG;
if(KG<0){cout<<"YOU ARE AN IDIOT, WE DO NOT ACCEPT ITEMS THAT MOVE AGAINTS THE FORCE OF GRAVITY\n";goto LOOP1;}
cout<<"KG=    "<<KG;
 }//void SET_KG

void PACKAGE::CALC_N (){
cout<<"\nNORMAL PRICE FOR THE DELIVERY WITHIN THE ESTIMATED GOOGOL DAYS IS:"<<KG*10;
 }//CALC_N

//---------------problem here
class PACKAGE_2:public PACKAGE{
public:
void CALC_N();
 };//class ONE
void PACKAGE_2::CALC_N(){
cout<<"\nONE DAY PRICE FOR THE DELIVERY WITHIN THE ESTIMATED GOOGOL DAYS IS:"<<KG*100;
 }//ONE
//---------------problem here

int main(){
int A0=0;
do{cout<<"CHOOSE PROGRAM NUMBER:28\n";int B0=28;switch(B0){
case  1:{cout<<"0=EXIT\n1=COMMAND LIST\n25=2505111-T2-2-2\n";break;}
case  0:{return 0;}
case 28:{
int Z_28_1=0;cout<<"1+1=";cin>>Z_28_1;cout<<endl;cin.ignore();//clear buffer

string name_t,adre_t;

PACKAGE TEMP_1;
TEMP_1.SET_EXP_INFO(name_t,adre_t);
TEMP_1.SET_KG();
TEMP_1.CALC_N();

//---------------problem here
PACKAGE_2 TEMP_2;
TEMP_2.CALC_N();
//---------------problem here

cout<<"\n";
 break;}//switch B0=28
 }//switch B0
 }while(1);//doodoo
 return 0;
 }//main 
KG returns with the max value as if it wasn't given a value

That's because it wasn't. You call SET_EXP_INFO and SET_KG on your instance TEMP_1 but not on your instance TEMP_2. You need to call TEMP_2.SET_KG(); before calling TEMP_2.CALC_N();.
Last edited on
I need the KG on KG*10 to have the same exact value as KG*100 except multiplied by 10 times

if KG=3 -> KG*10=30 and KG*100=300

the KG value needs to be specified only once from TEMP_1.SET_KG();

the KG*100 *must* be from the altered calculation from the derived class CALC_N, from the base class SET_KG

thank you very much
Last edited on
How about PACKAGE_2 TEMP_2 = TEMP_1;? (Note: the assignment will work because PACKAGE and PACKAGE_2 don't use dynamic memory allocation).
Last edited on
doesn't work for me... I get the error conversion from `PACKAGE' to non-scalar type `PACKAGE_2' requested
Last edited on
Oops, that would only work if you did PACKAGE_1 TEMP_3 = TEMP_2;, i.e. up the inheritance chain, my bad. So, it looks like you will need polymorphism to solve this problem. I would recommend you read this first:
http://www.cplusplus.com/doc/tutorial/polymorphism/

You will need to downcast your instance TEMP_1 to be treated as an instance of PACKAGE_2 dynamically.
Last edited on
You will need to downcast your instance TEMP_1 to be treated as an instance of PACKAGE_2 dynamically.
The same problem arise. A Base is not a Derived.

You could use a strategy, code another method or add a parameter. However it is a weird requirement.
Also, please indent your code.
The same problem arise. A Base is not a Derived.

You can use dynamic_cast to cast from a base class to a derived class if the base class is polymorphic:
http://www.cplusplus.com/doc/tutorial/typecasting/
Last edited on
You can use dynamic_cast to cast from a base class to a derived class if the base class is polymorphic:


Downcasting only works if the variable "actually is" the type you're casting to. If you have a Base and you try to downcast it to a Derived when the object wasn't a Derived, the cast will fail (which I think is what ne555 was saying).

That said, downcasting is usually not the solution anyway. The need for downcasting is actually quite rare and using it is usually a sign of poor design.
The need for downcasting is actually quite rare and using it is usually a sign of poor design.

Or a weird requirement:

the KG value needs to be specified only once from TEMP_1.SET_KG();

llVIU, if you remove the requirement that KG must be set in an instance of PACKAGE, then this will work:

1
2
3
4
PACKAGE_2 PACKAGE_2_INSTANCE;
PACKAGE_2_INSTANCE.SET_EXP_INFO(name_t,adre_t); //this technically gets done in the base class
PACKAGE_2_INSTANCE.SET_KG(); //this technically gets done in the base class
PACKAGE_2_INSTANCE.CALC_N();


Perhaps that's what you were looking for?

Topic archived. No new replies allowed.