Hello,
This is the problem: I get the right function call only from the 3rd derivated class, when the other twos are called are not.
Source :
#include<iostream.h>
#include<conio.h>
#include<string.h>
class IV{
char *loc;
public:
IV(){loc=NULL;}
IV(char *_loc){
loc=new char [strlen(_loc)+1];
strcpy(loc,_loc);
}
~IV(){
if(loc) delete [] loc;
loc=NULL;
}
void setloc(char *_loc){
loc=new char [strlen(_loc)+1];
strcpy(loc,_loc);
}
char *getloc(){
return loc;
}
virtual void print(){
cout <<"Descrierea IV"<< endl;
}
};
class IVP:public IV{
char *name;
public:
IVP(){name=NULL;}
IVP(char *_name,char *_loc):IV(_loc){
name=new char [strlen(_name)+1];
strcpy(name,_name);
}
~IVP(){
if(name) delete [] name;
name=NULL;
}
void setname(char *_name){
name=new char [strlen(_name)+1];
strcpy(name,_name);
}
char *getname(){
return name;
}
void print(IVP *iv){
cout << "Descrierea IVP" << endl;
}
};
class IVM: public IV{
char *name;
public:
IVM(){}
IVM(char *_name,char *_loc):IV(_loc){
name=new char [strlen(_name)+1];
strcpy(name,_name);
}
~IVM(){
if(name) delete [] name;
name=NULL;
}
void setname(char *_name){
name=new char [strlen(_name)+1];
strcpy(name,_name);
}
char *getname(){
return name;
}
void print(IVP *iv){
cout << "Descrierea IVM" << endl;
}
};
class IVS: public IV{
char *name;
public:
IVS(){}
IVS(char *_name,char *_loc):IV(_loc){
name=new char [strlen(_name)+1];
strcpy(name,_name);
}
~IVS(){
if(name) delete [] name;
name=NULL;
}
void setname(char *_name){
name=new char [strlen(_name)+1];
strcpy(name,_name);
}
char *getname(){
return name;
}
void print(){
cout << "Descrierea IVS" << endl;
}
};
void main(){
clrscr();
IV *iv[3];
IVP ivp("Guguta","Balti");
IVM ivm("Columna","Chisinau");
IVS ivs("UTM","Chisinau");
iv[0]=&ivp;
iv[1]=&ivm;
iv[2]=&ivs;
for(int i=0;i<3;i++){
iv[i]->print();
}
getch();
}
THIS IS THE OUTPUT:
Descrierea IV
Descrierea IV
Descrierea IVS
EXPECTED OUTPUT:
Descrierea IVP
Descrierea IVM
Descrierea IVS