fork download
  1. // Online C++ compiler to run C++ program online
  2. #include <iostream>
  3. #include <string>
  4. using namespace std;
  5. class string_pair {
  6. public:
  7. string_pair(const string & one = "", const string & two = "")
  8. : first(new string(one)), second(new string(two)) { }
  9.  
  10. string_pair(const string_pair & rhs) {
  11. first = new string(*rhs.first);
  12. second = new string(*rhs.second);
  13. }
  14.  
  15. ~string_pair() {
  16. delete first;
  17. delete second;
  18. }
  19.  
  20. string_pair & operator=(const string_pair & rhs) {
  21. if (this != &rhs) {
  22. delete first;
  23. delete second;
  24. first = new string(*rhs.first);
  25. second = new string(*(rhs.second));
  26. }
  27. return *this;
  28. }
  29.  
  30. const string & get_first() const {
  31. return *first;
  32. }
  33.  
  34. const string & get_second() const {
  35. return *second;
  36. }
  37.  
  38. string to_string() {
  39. return "(" + *first + ", " + *second + ")";
  40. }
  41. private:
  42. string * first;
  43. string * second;
  44. };
  45.  
  46. int main() {
  47. // Write C++ code here
  48. std::cout << "Try programiz.pro";
  49. string_pair s1("first", "second");
  50.  
  51. string_pair s2("a", "b");
  52.  
  53. s1=s2;
  54.  
  55. cout<< s1.to_string() <<endl;
  56.  
  57. string_pair s3(s2);
  58.  
  59. cout<< s3.to_string() <<endl;
  60.  
  61. string_pair s4("c","d");
  62. s2=s4;
  63. cout<< s2.to_string() <<endl;
  64. cout<< s3.to_string() <<endl;
  65.  
  66. return 0;
  67. }
Success #stdin #stdout 0.01s 5284KB
stdin
Standard input is empty
stdout
Try programiz.pro(a, b)
(a, b)
(c, d)
(a, b)