We are working on overloading of operators in C++ programs. In the following program both unary and binary operators are overloaded . I think this C++ program would help students.
#include< iostream.h >
#include< conio.h >
#include< stdio.h >
#include< string.h >
class Count
{
private:
char str[40];
char address[40];
int age;
public:
Count()
{
strcpy(str,"***");
strcpy(address,"!!!!");
age=0;
}
void operator ++ ()
{
cout<< "\nEnter Your name:-";
gets(str);
}
void operator--()
{
cout<< "\nEnter your address( Town/City):-";
cin>>address;
}
void operator +()
{
cout<< "\nEnter your age:-";
cin>>age;
}
void operator -()
{
cout<< "\nName="<< str;
cout<< "\nAddress="<< address;
cout<< "\nAge="<< age;
}
};
void main ()
{
clrscr ();
Count c1;
char ch;
cout<< "\nWant to enter name (y/n):-";
cin>>ch;
if(ch=='y')
++c1;
cout<<"\nWant to enter address (y/n):-";
cin>>ch;
if(ch=='y')
--c1;
cout<< "\nWant to enter age (y/n):-";
cin>>ch;
if(ch=='y')
+c1;
-c1;
getch ();
}
No comments:
Post a Comment