Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Implementing String class
- */
- #include <iostream>
- #include <cstdlib>
- #include <cstring>
- #include <cstdio>
- using namespace std;
- class String{
- char *str;
- int size;
- public:
- String(const char* = "");
- ~String();
- int length();
- bool operator >(String&);
- bool operator <(String&);
- bool operator ==(String&);
- friend istream& operator >>(istream&, String&);
- friend ostream& operator <<(ostream&, String&);
- };
- inline String::String(const char *str){
- String::str = new char[strlen(str) + 1];
- strcpy(String::str, str);
- size = strlen(str);
- }
- inline String::~String(){
- delete[] str;
- str = NULL;
- }
- inline int String::length(){
- return size;
- }
- inline bool String::operator >(String &obj){
- return strcmp(str, obj.str) > 0;
- }
- inline bool String::operator <(String &obj){
- return strcmp(str, obj.str) < 0;
- }
- inline bool String::operator ==(String &obj){
- return strcmp(str, obj.str) == 0;
- }
- istream& operator >>(istream &in, String &obj){
- /* Try to do this in the constructor */
- char temp[1024];
- gets(temp);
- delete[] obj.str;
- obj.str = new char[strlen(temp) + 1];
- strcpy(obj.str, temp);
- return in;
- }
- ostream& operator <<(ostream &out, String &obj){
- out << obj.str;
- return out;
- }
- int main() {
- system("cls");
- cout << "Enter the first string: ";
- String str1;
- cin >> str1;
- cout << "Enter the second string: ";
- String str2;
- cin >> str2;
- if(str1 > str2){
- cout << str1 << " is greater than " << str2;
- }
- else if(str1 < str2){
- cout << str1 << " is lesser than " << str2;
- }
- else {
- cout << str1 << " is equal to " << str2;
- }
- cin.get();
- return 0;
- }
Add Comment
Please, Sign In to add comment