I have a problem and I want to resolve it using Inheritance and Polymorphism with C :
An Equipment may be a device or a cable
Every Equipment has an identifier N
the device has a position : OPEN or CLOSED
the cable has a length L
Every Equipment has a resistance:
NULL for a device
coefficient 0.5 of the length for a cable
I had made this implementation with C classes:
#include <iostream>
#include <list>
using namespace std;
class Phy{
protected :
float resistance;
public:
virtual void updateResistance() = 0;
` float getResis(){return resistance;}
};
class Device: public Phy{
protected :
int position;
public:
Device(int pos):position(pos){};
virtual void updateResistance(){resistance = 0;};
int GetPos()const{return position;}
};
class Cable: public Phy{
protected :
float longeur;
public:
Cable(float longeu):longeur(longeu){};
virtual void updateResistance(){resistance = 0.5 * longeur;};
float Getlong()const{return longeur;}
};
class Equipement: public Device, public Cable{
protected :
int id;
public:
Equipement(int idd, float longe = 0, int pos =0):id(idd),Cable(longe), Device(pos){};
int GetId()const {return id;}
float GetResistance(){ return resistance;}
};
int main(){
Equipement Equip(25, 20, 0);
Equip.GetResistance();
return 0;
}
My question : how can I made an equipement as Cable Only or as a device only? I have doubts about my architecture, my Equipement Class is both Cable and Device
Thank you for your help
CodePudding user response:
You should not derive Equipment
from Device
or Cable
. Instead, derive them from Equipment
.
Equipment
should implement things that are common to both, and Device
and Cable
should implement things that are specific to each class.
Resistance should be implemented as virtual
function in Equipment
class, which is overriden in each derived class.
class Equipment {
// N
// virtual resistance
};
class Device : public Equipment {
// position
// resistance: NULL
};
class Cable : public Equipment {
// L
// resistance: 0.5 * L
};
To use polymorphism, you should create the objects as pointers, for example:
Equipment* eq = new Cable;
CodePudding user response:
Now this is my corrected implementation:
#include <iostream>
#include <list>
using namespace std;
class Equipment {
protected :
int id;
float resistance;
public:
Equipment(int idd):id(idd){ };
int GetId()const {return id;}
virtual float GetResistance()= 0;
};
class Device : public Equipment {
protected :
int position;
public:
Device(int id, int pos):position(pos),Equipment(id){};
virtual float GetResistance(){resistance = 0;
return resistance;};
int GetPos()const{return position;}
};
class Cable : public Equipment {
protected :
float length;
public:
Cable(int id, float lon):length(lon),Equipment(id){};
virtual float GetResistance(){resistance = 0.5 * length;return resistance;};
float Getlong()const{return length;}
};
int main()
{
list<Equipment*> lstEqui;
lstEqui.push_back(new Cable(334, 20));
lstEqui.push_back(new Device(678, 5));
list<Equipment*>::iterator it;
for(it = lstEqui.begin(); it != lstEqui.end();it )
{
cout <<"id="<< (*it)->GetId()<< " "<<"Position ="<<(*it)->GetPos() << " resistance= "<<(*it)->GetResistance()<<endl;
}
return 0;
}
My question is how can I display Position for Device instance and Lenght for Cable instance?
I got an error that GetPos() is not a member of Equipment Class