oops recall, basic operations and nesting of member functions
/*OOPS---> Classes and Objects
C++--> initially called as C with classes by stroustroup
Class--> extension of structures in C
Structures had limitations
- members are public
- a function cannot be created inside structure
- no methods
Classes --> Structures + more
Classes --> can have methods and properties
Classes --> can make few members as private and few as public
Structues in c++ are typedef (mtlb typedef na lgakr bi structures ke varibales
define kre to vo hojaenge)
you can declare objects along with class declaration like this:
class Employee {
class definition
} krishna, shalini;
harry.salary = 9 makes no sense if salary is private
*/
/************NESTING OF MEMBER FUNCTIONS******** */
#include<iostream>
using namespace std;
// int main(){
// binary b;
// b.read();
// b.check_binary();
// b.display();
// b.ones_compliment();
// b.display();
// return 0;
// }
class binary
{
private:
string s;
//void check_binary(void); //ye chal jaega agar hum nesting krenge but agar bina nesting
//ke main function m call krenge to nhi chlega
public:
void read(void);
void check_binary(void);
void ones_compliment(void);
void display(void);
};
#include<iostream>
using namespace std;
int main(){
binary b;
b.read();
b.check_binary();
b.display();
b.ones_compliment();
b.display();
return 0;
}
void binary :: read(void)
{
cout<<"Enter a binary number";
cin >> s;
}
void binary :: check_binary(void){
for (int i=0; i<s.length(); i++)
{
if ((s.at(i)) != '0' && s.at(i) != '1')
{
cout<<"The binary format is incorrect"<<endl;
exit(0);
}
}
}
void binary :: ones_compliment(void)
{
//check_binary();//this is nesting of member function
for (int i=0; i<s.length(); i++)
{
if (s.at(i) == '0')
{
s.at(i) = '1';
}
else{
s.at(i) = '0';
}
}
}
void binary :: display(void)
{
cout<<"Displaying your binary number "<<endl;
for (int i=0; i< s.length(); i++)
{
cout<<s.at(i);
}
}
#include<iostream>
using namespace std;
// int main(){
// binary b;
// b.read();
// b.check_binary();
// b.display();
// b.ones_compliment();
// b.display();
// return 0;
// }
Comments
Post a Comment