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 used?
Write a program to implement OOPS concepts such as inheritance, polymorphism, friend function, operator overloading?
What does and I oop mean?
Is oop better than procedural?
Why do pointers exist?
What is property in oops?
write string class as your own class in java without using any built-in function
Why do we use inheritance?
What is the difference between a constructor and a destructor?
What is a class in oop?
What are oops functions?
Is data hiding and abstraction same?
What is the types of inheritance?
write a program to enter a string like"sunil is a good boy and seeking for a job" not more than 10 characters including space in one line,rest characters should b in other line.if the next line starts from in between the previous word,then print whole word to next line.
Will I be able to get a picture in D drive to the c++ program? If so, help me out?