fork download
  1. #include <iostream>
  2. #include <iomanip>
  3. using namespace std;
  4. class S {
  5. private:
  6. string strName;
  7. char point;
  8. public:
  9. S() : strName(""), point('\0') {}
  10. S(const string& cstrName, char p) : strName(cstrName), point(p) {}
  11. // operator==
  12. friend bool operator==(char *sz, const S& k) {return k.strName == string(sz);};
  13. friend bool operator==(char a, const S& k) {return k.point == a;}
  14. friend bool operator==(const S& a, const S& b) {return a.strName == b.strName && a.point == b.point;}
  15. bool operator==(const S& a) const {return strName == a.strName && point == a.point;}
  16. bool operator==(const char *k) const {return strName == string(k);}
  17. bool operator==(char k) const {return point == k;}
  18. // operator=
  19. //13.5.3 Assignment [over.ass]
  20. //1 An assignment operator shall be implemented by a non-static member function with exactly one parameter.
  21. // S& operator=(char k){point = k; cout << "inline";return *this;};
  22. S& operator=(char c){point = c; return *this;};
  23. S& operator=(char *k){strName = string(k); return *this;};
  24. S& operator=(S& a) {strName = a.strName;point = a.point; return *this;}
  25. friend ostream& operator<<(ostream& os, const S& s)
  26. {
  27. os << "Name: " << s.strName << "\nPoint: " << s.point;
  28. return os;
  29. }
  30. };
  31. int main()
  32. {
  33. S a("Hello World!", '.');
  34. cout << "\nConstruction " << endl;
  35. cout << a << endl;
  36. cout << "\nAssigning char value: >>>,<<<" << endl;
  37. a = ',';
  38. cout << a << endl;
  39. cout << "\nAssigning char* value: >>>New World<<<" << endl;
  40. char c[] = "New World";
  41. a = c;
  42. cout << a << endl;
  43.  
  44. return 0;
  45. }
Success #stdin #stdout 0.02s 2816KB
stdin
Standard input is empty
stdout
Construction 
Name: Hello World!
Point: .

Assigning char value: >>>,<<<
Name: Hello World!
Point: ,

Assigning char* value: >>>New World<<<
Name: New World
Point: ,