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


Please Help Members By Posting Answers For Below Questions

How to call a non virtual function in the derived class by using base class pointer

5272


What is purpose of inheritance?

645


Why we use classes in oop?

583


What is polymorphism used for?

574


What is object and example?

604






just right the logic of it 1--> If few people are electing then every time ur candidate should win 2--> arrange books in box, if box carry weight == books weight then take another box..... find the no of box required.

6486


They started with the brief introduction followed by few basic C++ questions on polumorphism, inheritance and then virtual functions. What is polymorphims? How you will access polymorphic functions in C? How virtual function mechanism works?

1408


What is oops in programming?

568


What is object in oop with example?

704


What is encapsulation in oops?

538


Why is polymorphism needed?

602


What are benefits of oop?

639


How oops is better than procedural?

589


Which is not an object oriented programming language?

544


What is the purpose of polymorphism?

681