Graph after reading a file text

Pages: 12
Help me if I make mistake writing the classes
OK, so now, no playing around, show us exactly what the assignment question is, otherwise just green tick and we can all move on.

By the sound of it you have to model AND, OR gates and stuff like that. If that’s the case then nothing so far looks anything like it. 300 lines - no way.

So where’s the official question/assignment please.
Thanks for posting the class.

Why is Component a template? If it must be a template then shouldn't it be setState(T&) rather than setState(bool)?

Component has two abstract functions: logicFunction() and setState() In class Not, you have defined logicFunction() but not setState(). That's why you got the "invalid new-expression of abstract class type 'Not' " error.

What are members _lh and _hl? Why do you store the previous state? Is it because you need to model propagation delay, setup and hold times?

You assume that a component has just one input and one output. It may be better to make these more general:

1
2
vector<component*> inputs;
vector<bool> outputs;
Well, this is going down a rabbit hole fast.
Seems to me Verilog does something like this, and given the vagueness of the problem, two AND gates work like so:

OR, NOT etc gates are just an extension of this all separately inheriting from Gate.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <iostream>

class Gate
{
protected:
    bool input_Left;
    bool input_Right;
    
    std::string type = "???";
    Gate(){}
    ~Gate(){}
    
public:
    void setInput(bool input)
    {
        input_Left = input;
    }
    
    void setInput(bool inputL, bool inputR)
    {
        input_Left = inputL;
        input_Right = inputR;
    }
};

class AND: public Gate
{
public:
    AND(bool inputL = true, bool inputR = true)
    {
        input_Left = inputL;
        input_Right = inputR;
        type = "AND";
    }
    
    bool output() const
    {
        return input_Left and input_Right;
    }
    
    friend std::ostream & operator<<(std::ostream & out, const AND & aa)
    {
        out << aa.input_Left << '\t' << aa.type << '\t' << aa.input_Right << "\t>\t" << aa.output();
        return out;
    }
};

        
int main()
{
    bool input_1 = true;
    bool input_0 = false;
        
    bool input_2 = true;
    bool input_3 = true;
    
    AND first(input_0, input_1);
    AND second(input_2, input_3);
    
    
        std::cout << " First: " << input_0 << ' ' << input_1 << '\n';
        std::cout << "Second: " << input_2 << ' ' << input_3 << '\n';
        
        std::cout << " First: " << first.output() << '\n';
        std::cout << "Second: " << second.output() << '\n';
        
        std::cout << " First: " << first << '\n';
        std::cout << "Second: " << second << '\n';
        
        AND result(first.output(), second.output());
        std::cout << "Result: " << result << '\n';
        
        return 0;
}
Topic archived. No new replies allowed.
Pages: 12