Thursday 21 July 2016

  • OBJECT ORIENTED PROGRAMMING EXAMPLE

    Basic logic gates operations in C++

    // there is a simple object oriented program that will read the input values of different logic gates and show their output values.

    #include <iostream>

    using namespace std;

    class basic_gate
    {
        //member variables
        bool input1;
        bool input2;
        bool output;

    public :


        basic_gate()//base class constructor for initializing member variables to false
        {
            input1 = false;
            input2 = false;
            output = false;
        }

        void set_input(bool a,bool b)//for setting the input
        {
            input1 = a;
            input2 = b;
        }

        void set_input(bool a)//overloading set_input()
        {
            input1 = a;
        }

        void set_output(bool x)//for setting the output
        {
            output = x;
        }

        bool get_input1()//for returning 1st input
        {
            return input1;
        }

        bool get_input2()//for returning 2nd input
        {
            return input2;
        }

        bool get_output()//for returning the output
        {
            return output;
        }

        virtual void operation() = 0;//declaring a pure virtual member function

    };


    class AND_gate : virtual public basic_gate//here base class is inherited as virtual
    {

    public :

        void operation()//a new definition of operation function
        {
            bool o;

            o = this -> get_input1() * this -> get_input2();//processing output

            this -> set_output(o);//setting the output
        }
    };


    class OR_gate : virtual public basic_gate//here the base class is inherited as virtual
    {

    public :

        void operation()//another definition of operation function
        {
            bool o;

            o = this -> get_input1() + this -> get_input2();//processing output

            this -> set_output(o);//setting the output
        }
    };

    class NOT_gate : virtual public basic_gate//base class is inherited as virtual
    {

    public :

        void operation()//another definition of operation function
        {
            if(this -> get_input1() == false)

                this -> set_output(true);//setting the output

            else

                this -> set_output(false);
        }
    };

    class NAND_gate : public AND_gate , public NOT_gate//this derived class derived from two base classes
    {

        void operation()//another definition of operation function
        {
            AND_gate :: operation();
            this -> set_input( this -> get_output() );
            NOT_gate :: operation();
        }
    };

    class NOR_gate : public OR_gate , public NOT_gate//this derived class also derived from two base classes
    {

        void operation()
        {
            OR_gate :: operation();
            this -> set_input( this -> get_output() );
            NOT_gate::operation();
        }
    };


    int main()
    {
        bool X,Y;

        basic_gate *ptr;//declare a base type pointer

        AND_gate A;

        ptr = &A;//point the derived class

        cout << "Enter the inputs of AND gate:";
        cin >> X >> Y;

        ptr -> set_input(X,Y);
        ptr -> operation();
        cout << ptr -> get_output() << endl;

        OR_gate B;
        ptr = &B;//point the derived class

        cout << "Enter the inputs of OR gate:";
        cin >> X >> Y;

        ptr -> set_input(X,Y);
        ptr -> operation();
        cout << ptr -> get_output() << endl;

        NOT_gate c;
        ptr = &c;//point the derived class

        cout << "Enter the inputs of NOT gate:";
        cin >> X;

        ptr -> set_input(X);
        ptr -> operation();
        cout << ptr -> get_output() << endl;

        NAND_gate r;
        ptr = &r;//point the derived class

        cout << "Enter the inputs of NAND gate:";
        cin >> X >> Y;

        ptr -> set_input(X,Y);
        ptr -> operation();
        cout << ptr->get_output() << endl;

        NOR_gate z;
        ptr = &z;//point the derived class

        cout << "Enter the inputs of NOR gate:";
        cin >> X >> Y;

        ptr -> set_input(X,Y);
        ptr -> operation();
        cout << ptr -> get_output() << endl;

        return 0;
    }
     



No comments:

Post a Comment

Kruskal Algorithm

Minimum Cost Spanning Tree By Kruskal Algorithm using priority_queue in c++ #include<bits/stdc++.h> using namespace std; typedef p...