define a string class. overload the operator == to compare
two strings
Answer Posted / moin khan
#include <iostream>
using namespace std;
#include <string.h>
class String{
private:
enum { SZ = 80 };
char str[SZ];
public:
String(){ strcpy(str, ""); }
String( char s[] ){ strcpy(str, s); }
void display() const{ cout << str; }
void getstr(){ cin.get(str, SZ); }
bool operator == (String ss) const{
return ( strcmp(str, ss.str)==0 ) ? true : false;
}
};
int main(){
String s1 = "yes";
String s2 = "no";
String s3;
cout << "\nEnter 'yes' or 'no': ";
s3.getstr();
if(s3==s1)
cout << "You typed yes\n";
else if(s3==s2)
cout << "You typed no\n";
else
cout << "You didn't follow instructions\n";
return 0;
}
| Is This Answer Correct ? | 24 Yes | 3 No |
Post New Answer View All Answers
What is data binding in oops?
Write a program to implement OOPS concepts such as inheritance, polymorphism, friend function, operator overloading?
How do you achieve polymorphism?
What is meant by oops concept?
What is oops and why we use oops?
officer say me - i am offered to a smoking , then what can you say
What is the advantage of oop over procedural language?
What exactly is polymorphism?
How to handle exception in c++, For example in a functions i am assigning memory to some variables and also in next instructions in am dividing one variable also. If this functions generates a error while allocating memory to those variable and also while dividing the variable if i divide by zero then catch block how it will identify that this error has cone from perticular instruction
What is encapsulation with real life example?
Which type does string inherit from?
What is destructor give example?
What causes polymorphism?
Prepare me a program for the animation of train
What is object and example?