// This tut contains OOPs Recap & Nesting of Member Functions in C++
//-----------------------------------------------------------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------------------------------------------------------
// OOPs - Classes and objects ----->
// C++ --> initially called --> C with classes by stroustroup
// class --> extension of structures (in C)
// structures had limitations
// - members are public
// - No methods
// classes --> structures + more
// classes --> can have methods and properties
// classes --> can make few members as private & few as public
// structures in C++ are typedefed
// you can declare objects along with the class declarion like this:
/* class Employee{
// Class definition
} harry, rohan, lovish; */
// harry.salary = 8 makes no sense if salary is private
// Nesting of member functions
//-----------------------------------------------------------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------------------------------------------------------
#include<iostream>
#include<string>
using namespace std;
class binary{
private:
string s;
public:
void read();
void chk_binary();
void display();
void ones_compliment();
void display_ones_complement();
};
//-----------------------------------------------------------------------------------------------------------------------------
void binary :: read(void){
cout<<"Enter a binary number :"<< endl;
cin>>s;
}
//-----------------------------------------------------------------------------------------------------------------------------
void binary :: chk_binary(void){
for (int i = 0; i < s.length(); i++)
{
if ((s.at(i)!='1') && (s.at(i)!='0') )
{
cout<<"Invalid Binary Number"<<endl;
exit(0);
}
}
}
//-----------------------------------------------------------------------------------------------------------------------------
void binary :: display(void){
cout<<"Displaying your binary number :"<<endl;
for (int i = 0; i < s.length(); i++)
{
cout<< s.at(i);
}
cout<<endl;
}
//-----------------------------------------------------------------------------------------------------------------------------
void binary :: ones_compliment(void){
for (int i = 0; i < s.length(); i++)
{
if ((s.at(i) == '1'))
{
s.at(i)='0';
}
else
{
s.at(i)='1';
}
}
}
//-----------------------------------------------------------------------------------------------------------------------------
void binary::display_ones_complement(void)
{
cout<<"Displaying your One's complement of binary number : "<<endl;
for (int i = 0; i < s.length(); i++)
{
cout << s.at(i);
}
cout<<endl;
}
//-----------------------------------------------------------------------------------------------------------------------------
int main(){
binary b;
b.read();
b.chk_binary();
b.display();
b.ones_compliment();
b.display_ones_complement();
return 0;
}
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.