0

for education I have to write a programme involving different objects and inheritance. The project is about different Locks with Transponders to open them. Let me explain with a simplification of my programm:

class Lock
{
public : 
    Lock(int ID, int Transponder1, int Transponder2);
    void OpenLock(int Transponder);
protected:
    int _ID = 0, _Transponder = 0;
};
class SafetyLock : public Lock
{
public:
    SafetyLock(int ID, int Transponder1, int Transponder2);
    void OpenLock(int Transponder1, int Transponder2);
protected:
};

There are Normal Locks and safetylocks. The Safetylocks are inherited from the normal Locks. Normal Locks need only one Transponder to open, thus OpenLockhas only 1 parameter. The SafetyLock needs two Transponders to open, its OpenLockhas two Parameters.

class Managment
{
public:
    void StartSimulation();
protected:
    Lock allLocks[2] =
    {
        Lock(1,10,20),     //"A normal Lock can be opened by either Transponder 10 or 20"
        SafetyLock(4,40,10)//"A safety Lock with needs BOTH 40 and 10 to open"
    };
};

Managment handles everything, this is why I listed all Locks there are(in my simplification only 2) in a List of type Lock.

void Managment::StartSimulation()
{
    allLocks[0].OpenLock(10);                                                                     
    allLocks[1].OpenLock(40, 10); //Here the problem occurs since OpenLock only has one parameter in
                                  //Lock  
}

When I know try to open both Locks with their respective transponders all goes well for the Normal one but for the Second one, it tells me that I have too many arguments even though I declared a different OpenLock for the type SafetyLock.

I can only guess that my problem is that I have an Array of type Lock but I tried to google my problem and don't know how to proceed now. Send help.

Whole Test Code I wrote:

#include <iostream>
using std::cout;
using std::cin;
class Lock
{
public : 
    Lock(int ID, int Transponder1, int Transponder2);
    virtual void OpenLock(int Transponder);
protected:
    int _ID = 0, _Transponder = 0;

};
    
class SafetyLock : public Lock
{
public:
    SafetyLock(int ID, int Transponder1, int Transponder2);
    virtual void OpenLock(int Transponder1, int Transponder2);

protected:



};

class Managment
{
public:
    void StartSimulation();
protected:
      Lock allLocks[2] =
    {
        Lock(1,10,20),     //"A normal Lock can be opened by either Transponder 10 or 20"
        SafetyLock(4,40,10)//"A safety Lock with needs BOTH 40 and 10 to open"
    };
};
void Lock::OpenLock(int Transponder)
{
    if (Transponder == 10 || Transponder == 20)
    {
        cout << "succes!";
    }
    
}
void SafetyLock::OpenLock(int Transponder1, int Transponder2)
{
    if (Transponder1 == 40 && Transponder2 == 10)
    {
        cout << "succes!";
    }
}
void Managment::StartSimulation()
{
    allLocks[0].OpenLock(10);
    allLocks[1].OpenLock(40, 10); //Here the problem occurs since OpenLock only has one parameter in                               
                                  //Lock
}  

int main()
{
    Managment A;
    A.StartSimulation();
}
  • C++ does not work this way. You have an array of `Lock`s. Even though you attempt to initialize it with a `SafetyLock` it is still a `Lock`, so the subclass gets sliced away and all you're left with is the `Lock`. See the linked question for more information. – Sam Varshavchik Dec 30 '20 at 00:33
  • An array of `Lock` [ain't gonna work](https://stackoverflow.com/questions/274626/what-is-object-slicing). To use polymorphism, you need an array of (possibly smart) pointers to `Lock` – Igor Tandetnik Dec 30 '20 at 00:33

0 Answers0