fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4.  
  5. class String
  6. {
  7. int len ;
  8. string ptr;
  9. friend void compare(String,String);
  10. public:
  11. String(string A)
  12. {
  13. ptr=A;
  14. len=A.size();
  15. cout<<"Constructor for " <<ptr<<endl;
  16. }
  17. String(const String &s1)
  18. {
  19. ptr=s1.ptr;
  20. len=s1.len;
  21. cout<<"Copy for "<<ptr<<endl;
  22.  
  23. }
  24. ~String(){
  25. cout<<"Distructor for " <<ptr<<endl;
  26. }
  27. void print()
  28. {
  29.  
  30. cout<<ptr<<endl;
  31. }
  32.  
  33. };
  34. void compare(String A,String B)
  35. {
  36. int sum1=0;
  37. int sum2=0;
  38. for(int i=0;i<A.len;i++){
  39. sum1+=A.ptr[i];
  40. }
  41. for(int i=0;i<B.len;i++){
  42. sum2+=B .ptr[i];
  43. }
  44. if(sum2>sum1) cout<<"Result :"<<"-1"<<endl;
  45. if(sum2<sum1) cout<<"Result :"<<"1"<<endl;
  46.  
  47. if(sum2==sum1) cout<<"Result :"<<"0"<<endl;
  48.  
  49. }
  50. int main()
  51. {
  52.  
  53. string X,Y;
  54.  
  55. cout<<"Enter String 1: ";
  56. cin>>X;
  57. String S1(X);
  58.  
  59. cout<<"Enter String 2: ";
  60. cin>>Y;
  61. String S2(Y);
  62. compare(S1,S2);
  63. }
  64.  
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
Enter  String 1: Constructor for 
Enter  String 2: Constructor for 
Copy for 
Copy for 
Result :0
Distructor for 
Distructor for 
Distructor for 
Distructor for