如何使用c sharp在派生类中使用基类的构造函数

时间:2017-06-24 03:44:33

标签: c# c++

我有一个c ++代码并尝试将其转换为急剧代码

c ++如下

class gate {
    friend class netlist;
    std::string type_, name_;
    virtual bool validate_structural_semantics();
protected:
    gate(std::string type, std::string name);
    ~gate();

}; // class gate
class netlist {
public:
    netlist();
    ~netlist();//destructor
}; // class netlist

class flip_flop: public gate
{
    bool state_, next_state_;
    bool validate_structural_semantics();
public:
    flip_flop(std::string name)
    : gate("dff", name), state_(false), next_state_(false) {}
}; // class flip_flop
bool flip_flop::validate_structural_semantics() 
{
    if (pins_.size() != 2) return false;
    pins_[0]->set_as_output();
    pins_[1]->set_as_input();
    return true;
}
我试图写的尖锐代码看起来像这样

    class Gate
    {
        public Gate(string name) { }
        public string type_, name_;
        public virtual bool Validate_structural_semantics()
        {
            // should be provided by derived classes                
        }
        public Gate(string type,string name) { }
    }; // class gate
    public class Netlist
    {
        public Netlist() { }
    }; // class netlist
    class Flip_flop : Gate
    {
        Flip_flop():base ("type","name") { }//issue is here cant use state_
        bool state_, next_state_;
        public Flip_flop(string name)//i see a red line under Flip_flop
        {
           this.name_ = name;
        }
        public override bool Validate_structural_semantics()
        {
             if (pins_.Count != 2) return false;
            pins_[0].Set_as_output();
            pins_[1].Set_as_input();
            return true;
        }
    }; // class flip_flop

根据我的代码,如何定义带有不同参数的c sharp的构造函数?

2 个答案:

答案 0 :(得分:0)

问题是Gate没有默认构造函数,所以当你调用

public Flip_flop(string name)

它不知道如何构建基地。

尝试

public Flip_flop(string name) : base(name)

答案 1 :(得分:0)

您的基类public Gate(string name)public Gate(string type,string name)中有两个构造,并且您希望在Flip_Flop类中使用它们,因此您的类将如下所示:

   class Flip_flop : Gate
    {
        bool state_, next_state_;

        public Flip_flop(string name) : base(name)
        {
            this.name_ = name;
        }

        public Flip_flop(string type, string name) : base(type, name)
        {
            this.name_ = name;
            state_ = false;
            next_state_ = false;
        }


        public override bool Validate_structural_semantics()
        {
            if (pins_.Count != 2) return false;
            pins_[0].Set_as_output();
            pins_[1].Set_as_input();
            return true;
        }
    }; // class flip_flop