Declare a class called logic_gate to represent logic gates. The class has three data
members - input1, input2 and input3 to represent three inputs to the logic gate. The
class also has a virtual function member called get_gate_output. Derive two
classes from the base class logic_gate, namely, and_gate and or_gate to represent
‘logical and gate’ and ‘logical or gate’ respectively. Define function
get_gate_output in both of these classes to get the output of the gate. Show use of
above classes and functions to demonstrate dynamic polymorphism in function
main
#include<iostream>
using namespace std;
class logic_gate{
public:
int input1, input2 , input3;
void get_gate_output();
};
class and_gate: public logic_gate{
public:
void setData(int a, int b, int c){
input1 = a;
input2 = b;
input3 = c;
}
void get_gate_output(){
int result = input1 & input2;
result = result & input3;
cout<<"AND result is: "<<result;
}
};
class or_gate: public logic_gate{
public:
void setData(int a, int b, int c){
input1 = a;
input2 = b;
input3 = c;
}
void get_gate_output(){
int result = input1 | input2;
result = result | input3;
cout<<"\nOr result is: "<<result;
}
};
int main(){
and_gate a;
a.setData(1,3,6);
a.get_gate_output();
or_gate o;
o.setData(5,7,9);
o.get_gate_output();
}
Comments
Leave a comment