Implementing Inheritence into your design

This is a discussion on Implementing Inheritence into your design within the C++ Programming forums, part of the General Programming Boards category; Right! I am designing a very simple (text based) digital circuit simulator (it is a university coursework) ...I have created ...

  1. #1
    Matt Conway bobthebullet990's Avatar
    Join Date
    Nov 2005
    Location
    Cambridge
    Posts
    121

    Implementing Inheritence into your design

    Right! I am designing a very simple (text based) digital circuit simulator (it is a university coursework) ...I have created a design, but am not sure whether it will work the way that I want; here is my idea...

    I have classes for each of the following; AndGate, OrGate, XorGate and NotGate, I also have a class Circuit (which will store the details for each circuit a user creates). Then I have the application class for the text based GUI.

    My idea is to have a vector inside the Circuit class to store logic gate objects. (I know how to create vectors of objects; this is not the problem!!!) ...The problem starts when trying to store objects of different types into a single vector, so I have come up with the idea that by using inheritance, If i create another class; LogicGate, which has functions and variables used by all types of logic gate, I can create inheritted classes from this class, but adding extra functionality and being able to store them into the same vector!

    Now, From my ideas and reading about C++ programming (im a novice to OO design!!! ...I have been using C, which i am confident in using; but this program has to be done in C++), I can do the above design, maybe I am wrong, but I have created a smaller scaled down version of the above problem, and when i compile, i get errors saying class redefined, so am having second thoughts about the design!

    Is this design one that is valid? is it possible, or do i have to go about the problem in a different way???

    class LogicGate definition...
    Code:
    class LogicGate{
    private:
      int theID;
      int parent1;
      int parent2;
      int input1;
      int input2;
      int output;
    public:
      LogicGate(int); //constructor Requires just the ID number
      setInputs(int, int);
      setInput1(int);
      setInput2(int);
      int getID();
      int getPID1();
      int getPID2();
      int setPID1();
      int setPID2();
    };
    Class AndGate Definition:
    Code:
    #include "LogicGate.cpp" // for inheritance use
    class AndGate : LogicGate{
    private:
    public:
      int getOutput(int int); // for when two inputs are supplied
      int getOutput(); // for when the input variables have already been set
    };
    ...and so on for each of the classes NotGate, OrGate, XorGate are exactly as the AndGate class. When i compile, it tells me that I have already declared the class LogicGate at line 1 inside AndGate class (but to me I am only including it!!!)

    Can you please let me know how I can implement the above design? ...Maybe i am thinking stupidly, but Im not sure!!! I've spent ages looking at my book, and it looks ok to me and can't see how im defining LogicGate twice!!!!!!

    Thankyou for your time!!!!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,925
    It should be:
    Code:
    class AndGate : public LogicGate{
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,074
    Also, off of a hunch, in your LogicGate class, you probably either want either the private variables to be protected or the public variables to be protected... depending on what and how you plan to implement. Also, consider the C++ advantage of Operator Overloading.
    Sent from my iPadŽ

  4. #4
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    i get errors saying class redefined
    You need include-guards in the headers.
    Kurt

  5. #5
    System.out.println("");
    Join Date
    Jan 2005
    Posts
    84
    I don't really know if this is the type of input you are looking for, but I will give it anyway and you can feel free todisregard it you don't need/like it.

    I have done some structural VHDL programming that reminds me of this. Basically in VHDL to create a digital system you create these "black boxes" so-to-speak where you give it a set of inputs and it will spit out a given output based on those (assuming purely combinational logic). Once you have these black boxes that make the system up, you use structural VHDL to wire them up so-to-speak. This is exactly what your AND/OR/NOT, etc gates are.

    Now you could make classes for each of these with that contain the inputs (all 2 input, 3 input or however many you want) with pointers to what they are connected to. Also each output could contain a pointer to what it's connected to. Basically your data structures would be the gate objects (parent level) and the particular gate function could inherit from there to become a child AND, OR, etc. Inputs and outputs would simply be bools (0 or 1). Mind you, I am pretty new to C++ myself so I don't know how feasible this is to implement, but intuitively it seems to make sense to me.

    Code:
    Gate    Input1    Input2   Output1    Name
    AND       1         0       U2        U1
    OR       0          U1        OUT       U2
    You could even read the schematics in from a text file like the one shown above (PSPICE does something similar). It would basically read that your schematic has two gates: an AND and an OR. The AND gate has two inputs and its output connects to U2 the OR gate. The other input from the OR gate is a logic 0. The output from this gate is called OUT. Now this might be getting too complicated for what you need, but I figured it would help make the schematic entry easier.

    Anyways this seems like it could be a really fun project to work on. Have fun

  6. #6
    Matt Conway bobthebullet990's Avatar
    Join Date
    Nov 2005
    Location
    Cambridge
    Posts
    121
    Cheers Loctan!!! ...I guess that is another way to approach it!!! ...If i get nowhere with my current ideas, i may well have a blast at that idea!!! ...seeing as i have started with my ideas now and have got a bit of code to get going on!!!

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    NEVER include .cpp files! You need better division into headers and sources.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. which design is better to wrap another class instance
    By George2 in forum C++ Programming
    Replies: 7
    Last Post: 04-13-2008, 12:27 AM
  2. any comments about a cache design?
    By George2 in forum C Programming
    Replies: 6
    Last Post: 09-14-2006, 12:53 PM
  3. Opinions on new site design
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 01-21-2005, 12:34 PM
  4. Cprog tutorial: Design Patterns
    By maes in forum C++ Programming
    Replies: 7
    Last Post: 10-11-2004, 01:41 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21