• Status: Solved
• Priority: Medium
• Security: Public
• Views: 244

# !!!

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
dinorama
• 3
• 2
2 Solutions

Commented:
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

Commented:
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

Commented:
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

Commented:
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

Commented:
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 Commented:
thanks to everybody who helped me out. I finished my assignment successfully!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.