Skip to main content

C++ Program to implement Operator Overloading (>>, <<)

Write a class which contains a string as private data member. Add a member function IsPalindrome that returns TRUE/FALSE depending upon whether the str is palindrome or not. Overload the <> operator to insert the string and print the string respectively along with appropriate message.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include
#include
#include
 
enum bool {false, true};
 
class Cpal{
    private:
        char *str;
    public:
        Cpal(){}
 
        bool IsPalindrome();
 
        friend ostream& operator<<(ostream &, const Cpal &);
 
        friend istream& operator>>(istream &, const Cpal &);
    };
 
bool Cpal :: IsPalindrome(){
    int len, i, j;
    bool valid;
 
    len = strlen(str);
 
    for(i=0, j=len-1; i
        if(str[i] == str[j])
            valid = true;
        else{
            valid = false;
            break;
            }
        }
    return valid;
    }
 
istream& operator>>(istream &istr, const Cpal &Opal){
    return istr.get(Opal.str, 80);
    }
 
ostream& operator<<(ostream &ostr, const Cpal &Opal){
    return ostr<
    }
 
void main(){
    Cpal Opal;
    clrscr();
 
    cout<<"\n Enter a String: ";
    cin>>Opal;
 
    cout<<"\n The String: ";
    cout<
 
    if(Opal.IsPalindrome())
        cout<<"\n\t is a Palindrome.";
    else
        cout<<"\n\t is not a Palindrome.";
 
    getch();
}

Comments