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)
#include <iomanip>
#include <iostream>
#include <ostream>
#include <string>
usingnamespace std;
class PACKAGE{
protected:
double KG;
public:
string EXP_NAME_1,EXP_ADRE_1;
void SET_EXP_INFO(string,string);
void SET_KG();
virtualvoid 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();.
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.
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();