Thread: Inteface extending another interface

  1. #1
    Registered User
    Join Date
    Dec 2015
    Posts
    92

    Inteface extending another interface

    Hello every0x01,
    I am trying to do 3 interfaces where,

    A Controller interface extends Writeable and Readable interfaces and class I2CCommController implements Controller interface.

    But I can't get my code compiled

    Code:
    class Writeable
    {
        public:
            virtual ~Writeable() {}
            virtual bool writeByte(uint8_t byte) = 0;
            virtual uint8_t writeBytes(uint8_t* bytes, uint8_t offset, uint8_t length) = 0;
            virtual uint8_t writeBytes(uint8_t* bytes, uint8_t length) = 0;
            virtual uint8_t write(const char* string) = 0;
            virtual uint8_t write(int value) = 0;
            virtual uint8_t getAvailableForWrite() = 0;
    };
    Code:
    class Readable
    {
        public:
            virtual ~Readable() {}
            virtual bool readByte(uint8_t* byte) = 0;
            virtual uint8_t readBytes(uint8_t* buffer, uint8_t length) = 0;
            virtual uint8_t getAvailable() = 0;
    };

    Code:
    class Controller : public Readable, public Writeable
    {
        public:
            virtual ~Controller(){}
    
    
            virtual uint8_t open() = 0;    // Open I2C communication I/O Stream
            virtual void close() = 0;            // Close I2C communication I/O Stream
            virtual bool isOpened() = 0;
            //virtual Readable getReceiver() = 0;
            //virtual Writeable getWritter() = 0;
    };

    Problem is caused in a class I2CCommController which implements Controller interface

    Code:
    class I2CCommController : public Controller 
    {
        private:
            uint16_t clockFrequency;
            uint8_t address;
            bool isOpen;
            volatile uint8_t status;
            volatile uint8_t slaveRW;
            volatile uint8_t sendStopBit;            // should the transaction end with a stop
            volatile uint8_t inRepStart;            // in the middle of a repeated start
    
            char controllerState;
            CircularQueue rxQueue;
            CircularQueue txQueue;
            static I2CCommController* i2cControllerPtr;
        
        public:
            I2CCommController(int _clockFrequency, uint8_t _address);
            I2CCommController();
            void setFrequency(uint16_t _clockFrequency);
            uint16_t getFrequency();
            void setAddress(uint8_t _address);
            uint8_t getAddress();
            
            void start();
            void stop();
            // Methods for Output Stream
            bool writeByte(uint8_t byte);
            uint8_t writeBytes(uint8_t* bytes, uint8_t length);
            uint8_t writeBytes(uint8_t* bytes, uint8_t length, const bool sendStop);
            uint8_t writeBytes(uint8_t* bytes, uint8_t offset, uint8_t length);
            uint8_t write(int value);
            uint8_t write(const char* string);
            uint8_t getAvailableForWrite();
            
            // Methods for Input Stream
            bool readByte(uint8_t* byte);
            uint8_t readBytes(uint8_t* buffer, uint8_t length);
            uint8_t readBytes(uint8_t* buffer, uint8_t length, const bool sendStop);
            uint8_t getAvailable();
            
            // Methods for I/O Stream Control
            uint8_t open();    // Open I2C communication I/O Stream
            void close();            // Close I2C communication I/O Stream
            bool isOpened();
            //Readable getReceiver();
            //Writeable getWritter();
            
            friend void TWI_vect(void);
            inline void interruptTWIHandler(voi
    Writing without inheriting from Controller, and it compiles.
    Code:
    class I2CCommController
    How is proper interface inheritance implemented in C++?

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    At the point when a variable is declared/defined, all pure virtual methods must've been implemented. Some method somewhere in there is still pure virtual, I presume.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Dec 2015
    Posts
    92
    Quote Originally Posted by GReaper View Post
    At the point when a variable is declared/defined, all pure virtual methods must've been implemented. Some method somewhere in there is still pure virtual, I presume.

    In interface Controller do I have to rewrite virtual methods from Readable and Writeable interface since Controller inherits from both Readable and Writeable or I can leave implementation to a class which implements Controller interface without copying it?

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Unless you want to use an object of type "Controller", no you don't have to implement all of the pure virtuals.

    What errors are you getting? Or if they are too many, what are they essentially saying?

    EDIT: Remember that if you accidentally use different types for the parameters or fewer/more parameters, it's considered "overloading". In the compiler's perspective, the pure virtual method is still undefined.
    Last edited by GReaper; 03-11-2017 at 02:11 PM.
    Devoted my life to programming...

  5. #5
    Registered User
    Join Date
    Dec 2015
    Posts
    92
    Quote Originally Posted by GReaper View Post
    Unless you want to use an object of type "Controller", no you don't have to implement all of the pure virtuals.

    What errors are you getting? Or if they are too many, what are they essentially saying?

    EDIT: Remember that if you accidentally use different types for the parameters or fewer/more parameters, it's considered "overloading". In the compiler's perspective, the pure virtual method is still undefined.
    I had that in mind, it seems like I forgot to implement isOpened() method in my .cpp file.

    However I have another question if you could suggest me what could I change if any in my design and what would be overhead in my design. I attached UML diagram below.
    If you look at I2CCommController class it has many behaviours, it's both opening/closing port, reading and receiving from the port and handling interrupt .
    Attached Images Attached Images Inteface extending another interface-uml-diagram-i2c-jpg 
    Last edited by High Voltage; 03-12-2017 at 07:34 AM.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Your image is far too fuzzy to bother with the eye strain it causes.
    Save it as a PNG, which is at least pixel accurate.
    JPG quantisation just makes fine detail fuzzy.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Dec 2015
    Posts
    92
    Quote Originally Posted by Salem View Post
    Your image is far too fuzzy to bother with the eye strain it causes.
    Save it as a PNG, which is at least pixel accurate.
    JPG quantisation just makes fine detail fuzzy.
    Originally it was saved as PNG
    https://s11.postimg.org/d1039cy2b/UML_Diagram_I2_C.pngupload pics

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extending wireless range?
    By bertazoid in forum Tech Board
    Replies: 1
    Last Post: 05-18-2009, 08:49 AM
  2. Malloc for extending the size....!
    By patil.vishwa in forum C Programming
    Replies: 5
    Last Post: 09-11-2008, 03:34 AM
  3. AddRef and Release function of COM inteface
    By George2 in forum C++ Programming
    Replies: 0
    Last Post: 10-18-2007, 06:35 AM
  4. extending ostream
    By ilear_01 in forum C++ Programming
    Replies: 3
    Last Post: 06-10-2003, 11:27 PM
  5. Automatic <cr> in User Inteface
    By chubaka in forum C Programming
    Replies: 3
    Last Post: 01-14-2002, 01:06 PM

Tags for this Thread