Solved

!!!

Posted on 2004-10-28
229 Views
Last Modified: 2010-04-01
Hey dudes,

I got this question that i am working on currently. But, having problems.. I just need help on the errors. and how to fix them.

Here is the question: -------------------------------------------------------------------------------------------------------------

Design an object oriented program which computes and returns the output (0 or 1) for the AND, OR, NAND and NOR gate respectively.
Create a Gate class that contains the following members:
-data members to store two inputs and an output of a gate
-four inline member functions that compute and return the output for the AND, OR, NAND and NOR gate respectively  
-a non-inline function that displays menu options (types of gates available) when choosing a type of the gate
-a non-inline function that gets two inputs of the gate from the user
-a constructor function that initializes two inputs of the gate

SAMPLE OUTPUT LOOKS LIKE THIS:

               Types of gates available:
1.      AND
2.      OR
3.      NAND
4.      NOR

 
                 Choose the type of gate for gate 1 => 1
                 Enter two inputs for gate 1 => 0 1
 
                 Choose the type of gate for gate 2 => 3
                 Enter two inputs for gate 2 => 1 0

                 Choose the type of gate for gate 3 => 2

                  Outputs =>
                                      y1 = 0
                                      y2 = 1
                                      y3 = 1

-----------------------------------------------------------------------------------------------------------------------------------------


HERE IS MY CODE:

#include <iostream>
#include <stdlib.h>
#include<conio.h>

using namespace std;


class Gate {

private:

int x1, x2, y;

public:
int op_and(int x1 , int x2)
{
return(x1&x2);
}

int op_or(int x1, int x2)
{
return (x1|x2);
}

int op_nand(int x1, int x2)
{
return !(x1&x2);
}

int op_nor(int x1, int x2)
{
return !(x1|x2);
}

Gate (int in1, int in2);
void display ();
void getinput(int x, int y);
int schoice (int choice, int x, int y);

};

void Gate::display()
{
cout<<"Types of gates available: "<<endl;
      cout<<"1. AND"<<endl;
      cout<<"2. OR"<<endl;
      cout<<"3. NAND"<<endl;
      cout<<"4. NOR"<<endl;
      cout<<endl;

        }
       
void Gate::getinput(int x1, int x2) {

    cout<<"Enter two inputs for gate: "<<x1<<x2<<endl;
   
}

Gate::Gate (int in1, int in2) {
   
      in1 = x1;
      in2 = x2;
  }    

int schoice (int choice, int in1, int in2){
   
    int x1, x2, x3, x4;
    Gate (in1, in2);
    switch(choice)
    {
        case 1: x1 = op_and(in1,in2);
                return x1;
        break;
        case 2: x2 = op_or(in1,in2);
                return x2;
        break;
        case 3: x3 = op_nand(in1,in2);
                return x3;
        break;
        case 4: x4 = op_nor(in1,in2);
                return x4;
        break;
        default:
            cout<<"wrong entry";
           
}    
           


int main()
{
 int choice,x1, x2, x3,x4, gate_out1, gate_out2, gate_out3 ,x,y,y1, y2, y3;
    Gate gate;
       
    cout<<"Choose the type of gate for gate 1: "<<endl;
      cin>>choice;
      gate.getinput();
      gate.y = gate.schoice(choice, in1, in2);
      cout<<endl;
      cout<<"Enter the type of gate for gate 2: "<<endl;
      cin>>choice;
      cout<<"Enter two inputs for gate 2: "<<endl;
/*      cin>> x>>y;
      y2 = schoice(choice, x, y);
      cout<<endl;
      cout<<"Enter the type of gate for gate 3: "<<endl;
      cin>>choice;
      cout<<"Enter two inputs for gate 3: "<<endl;
      cin>> x>>y;
      cout<<endl;
      y3 = schoice(choice, x, y);
      cout<< "Outputs:"<<endl;
      cout<< "y1= "<<y1<<endl;
      cout<<"y2= "<<y2<<endl;
      cout<<"y3= "<<y3<<endl;
      cout<<endl<<endl;
      //code for the 3-AND gate network--------------------
      cout<<" -----3-AND gate Network------"<<endl<<endl;
      cout<<"Enter 2 inputs for gate 1:" <<endl;
      cin>> x1>>x2;
      cout<<"Enter 2 inputs for gate 2:" <<endl;
      cin>> x3>>x4;
      gate_out1 = (x1&x2);
      gate_out2 = (x3&x4);
      gate_out3 = (gate_out1)&(gate_out2);
      cout<<endl;
      cout<<"Gate 1 output is:"<<gate_out1<<endl;
      cout<<"Gate 2 output is:"<<gate_out2<<endl;
      cout<<"Gate 3 output is:"<< gate_out3<<endl; */
      getch();
      return 0;
}

-------------------------------------------------------------------------------------------------------------------------------

I got whole bunch of INITIAL ERROS on the op_and function, and the rest..that they haven't been declared etc..

Thanks
Dino.
0
Question by:dinorama
    6 Comments
     
    LVL 10

    Expert Comment

    by:Sys_Prog
    OK, the schoice function is a class function so it should be defined as a member of class
    U have declared it as a member, but while defining, u are defininfg it as a standalone function


    So i changed this line in your program
    int schoice (int choice, int in1, int in2){
    to
    int Gate::schoice (int choice, int in1, int in2){


    2.

    A closing brace is missing for the switch statement in schoice


    3.

    In main, u have declared a Gate variable passing no parameters to constructir
    Since u have declared one constructor taking parameter, u should define a no argument [or construcor with all arguments having default values] constructor to define a variable without requiring to pass parameters.

    Amit
    0
     
    LVL 10

    Assisted Solution

    by:Sys_Prog
    One more logical error that I see in your program is
    Gate::Gate (int in1, int in2) {
       
          in1 = x1;
          in2 = x2;
      }  

    Here, u are assigning the values in x and y to the arguments, but actually u should be doing the other way round
    Hence it should be

    Gate::Gate (int in1, int in2) {
       
          x1 = in1 ;
          x2 = in2 ;
      }
    0
     
    LVL 10

    Expert Comment

    by:Sys_Prog
    also, to be able to create an instence of Gate class without passing any parameters,
    U could write the above constructor as

    Gate::Gate (int in1 = 0, int in2 = 0) {
       
          x1 = in1 ;
          x2 = in2 ;
      }

    so that by default the x and y inputs get initialized to 0 if the user does not pass aything.

    Amit
    0
     
    LVL 86

    Expert Comment

    by:jkr
    Your code seems to have a lot of misconceptions. I tried to fix some (it compiles at least :o), but it is still up to you to finish it:

    #include <iostream>
    #include <stdlib.h>
    #include<conio.h>

    using namespace std;


    class Gate {

    private:

    int x1, x2, y;

    public:
    int op_and(int x1 , int x2)
    {
    return(x1&x2);
    }

    int op_or(int x1, int x2)
    {
    return (x1|x2);
    }

    int op_nand(int x1, int x2)
    {
    return !(x1&x2);
    }

    int op_nor(int x1, int x2)
    {
    return !(x1|x2);
    }
    Gate() {};
    Gate (int in1, int in2);
    void display ();
    void getinput();
    int schoice (int);

    };

    void Gate::display()
    {
    cout<<"Types of gates available: "<<endl;
        cout<<"1. AND"<<endl;
        cout<<"2. OR"<<endl;
        cout<<"3. NAND"<<endl;
        cout<<"4. NOR"<<endl;
        cout<<endl;

           }
           
    void Gate::getinput() {

       cout<<"Enter two inputs for gate: ",
       cin>>x1>>x2;
       
    }

    Gate::Gate (int in1, int in2) {
       
         x1 = in1;
         x2 = in2;
     }    

    int Gate::schoice (int choice){
       
       int x;
       
       switch(choice)
       {
           case 1: x = op_and(x1,x2);
                   return x;
           break;
           case 2: x = op_or(x1,x2);
                   return x;
           break;
           case 3: x = op_nand(x1,x2);
                   return x;
           break;
           case 4: x = op_nor(x1,x2);
                   return x;
           break;
           default:
               cout<<"wrong entry";
       }
    }    
               


    int main()
    {
    int choice,x1, x2, x3,x4, gate_out1, gate_out2, gate_out3 ,x,y,y1, y2, y3;
       Gate gate;
         
       cout<<"Choose the type of gate for gate 1: "<<endl;
        cin>>choice;
        gate.getinput();
        y = gate.schoice(choice);
        cout<<endl;
        cout<<"Enter the type of gate for gate 2: "<<endl;
        cin>>choice;
        cout<<"Enter two inputs for gate 2: "<<endl;
    /*     cin>> x>>y;
        y2 = schoice(choice, x, y);
        cout<<endl;
        cout<<"Enter the type of gate for gate 3: "<<endl;
        cin>>choice;
        cout<<"Enter two inputs for gate 3: "<<endl;
        cin>> x>>y;
        cout<<endl;
        y3 = schoice(choice, x, y);
        cout<< "Outputs:"<<endl;
        cout<< "y1= "<<y1<<endl;
        cout<<"y2= "<<y2<<endl;
        cout<<"y3= "<<y3<<endl;
        cout<<endl<<endl;
        //code for the 3-AND gate network--------------------
        cout<<" -----3-AND gate Network------"<<endl<<endl;
        cout<<"Enter 2 inputs for gate 1:" <<endl;
        cin>> x1>>x2;
        cout<<"Enter 2 inputs for gate 2:" <<endl;
        cin>> x3>>x4;
        gate_out1 = (x1&x2);
        gate_out2 = (x3&x4);
        gate_out3 = (gate_out1)&(gate_out2);
        cout<<endl;
        cout<<"Gate 1 output is:"<<gate_out1<<endl;
        cout<<"Gate 2 output is:"<<gate_out2<<endl;
        cout<<"Gate 3 output is:"<< gate_out3<<endl; */
        getch();
        return 0;
    }
    0
     
    LVL 86

    Accepted Solution

    by:
    BTW, some more improvements - you are passing member variables of your 'Gate' when you don't have to, especially with the 'op_*()' functions:

    class Gate {

    private:

    int x1, x2, y;

    public:
    int op_and()
    {
    return(x1&x2);
    }

    int op_or()
    {
    return (x1|x2);
    }

    int op_nand()
    {
    return !(x1&x2);
    }

    int op_nor()
    {
    return !(x1|x2);
    }
    Gate() {};
    Gate (int in1, int in2);
    void display ();
    void getinput();
    int schoice (int);

    };

    void Gate::display()
    {
    cout<<"Types of gates available: "<<endl;
        cout<<"1. AND"<<endl;
        cout<<"2. OR"<<endl;
        cout<<"3. NAND"<<endl;
        cout<<"4. NOR"<<endl;
        cout<<endl;

           }
           
    void Gate::getinput() {

       cout<<"Enter two inputs for gate: ",
       cin>>x1>>x2;
       
    }

    Gate::Gate (int in1, int in2) {
       
         x1 = in1;
         x2 = in2;
     }    

    int Gate::schoice (int choice){
       
       int x;
       
       switch(choice)
       {
           case 1: x = op_and();
                   return x;
           break;
           case 2: x = op_or();
                   return x;
           break;
           case 3: x = op_nand();
                   return x;
           break;
           case 4: x = op_nor();
                   return x;
           break;
           default:
               cout<<"wrong entry";
       }
    }    
    0
     

    Author Comment

    by:dinorama
    thanks to everybody who helped me out. I finished my assignment successfully!
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Suggested Solutions

    Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
    In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
    The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
    The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

    934 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now