F
fabian.lim
Hi,
I am a newbie to C++, however, I programmed quite alot in C before.
This is my first attempt at OOP. In the following segment of my code,
Im trying to implement (as an exercise) a binary variable class. I
know this is extremely redundant
but im learning so please bear
with me
. Anyways, Notice that I have have two "=" operator
overloads. The 1st case takes care of x=y, where both x and y are
binary variables. The 2nd case takes care of x=1 or x=0. As you can
see, both chunks of code are extremely redundant. So my question is,
is there any better way to do this?
//********************** START: BINARY VARIABLES
*************************
class Binary {
char bit;
public:
//********************************************************
// Constructors and Destructor
Binary() { bit =0; };
~Binary(){ };
//********************************************************
// Copiers
//copy constructors
Binary( const Binary& val) { bit = val.bit; };
Binary( const int& val) { bit = val; }; //int specialization
//assignments
Binary& operator=(const Binary& rhs) {
bit = rhs.bit;
return *this;
}
Binary& operator=(const int& rhs) { //int specialization
bit = rhs;
return *this;
}
};
//********************** END: BINARY VARIABLES
*************************
I am a newbie to C++, however, I programmed quite alot in C before.
This is my first attempt at OOP. In the following segment of my code,
Im trying to implement (as an exercise) a binary variable class. I
know this is extremely redundant
with me
overloads. The 1st case takes care of x=y, where both x and y are
binary variables. The 2nd case takes care of x=1 or x=0. As you can
see, both chunks of code are extremely redundant. So my question is,
is there any better way to do this?
//********************** START: BINARY VARIABLES
*************************
class Binary {
char bit;
public:
//********************************************************
// Constructors and Destructor
Binary() { bit =0; };
~Binary(){ };
//********************************************************
// Copiers
//copy constructors
Binary( const Binary& val) { bit = val.bit; };
Binary( const int& val) { bit = val; }; //int specialization
//assignments
Binary& operator=(const Binary& rhs) {
bit = rhs.bit;
return *this;
}
Binary& operator=(const int& rhs) { //int specialization
bit = rhs;
return *this;
}
};
//********************** END: BINARY VARIABLES
*************************