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
Why is polymorphism needed?
#include
What does and I oop and sksksk mean?
What is polymorphism programming?
what type of question are asked in thoughtworks pair programming round ?
What is overriding in oops?
How is polymorphism achieved?
What is solid in oops?
Why can't we have instance(stack) of a class as a member of the same class like eg.Class A{A obj;} as we can have self refential pointer
What is object in oops?
What is ambiguity in inheritance?
What is difference between multiple inheritance and multilevel inheritance?
Why is oop better than procedural?
What are different oops concepts?
How oops is better than procedural?