{"id":753,"date":"2010-02-24T12:29:21","date_gmt":"2010-02-24T12:29:21","guid":{"rendered":"http:\/\/www.codingfriends.com\/?p=753"},"modified":"2010-02-24T12:29:21","modified_gmt":"2010-02-24T12:29:21","slug":"operator-comparisons","status":"publish","type":"post","link":"https:\/\/www.codingfriends.com\/index.php\/2010\/02\/24\/operator-comparisons\/","title":{"rendered":"operator &#8211; comparisons"},"content":{"rendered":"<p>Comparisons between classes are different compared to local variables e.g. int(eger), float, doubles.. because the comparisons aspects of the standard variables have already been done e.g. <\/p>\n<pre lang=\"cpp\">\r\nint a=2;\r\nint b =3;\r\nif (a==b)....\r\n<\/pre>\n<p>the comparison being the &#8220;==&#8221;, within your own classes if you want to test against a another object that is of the same class you will have to write a comparison function that will either return true or false, of course there are other comparisons <,>, <=, >= etc and also +,- which can be implemented if you wanted to.<\/p>\n<p>The basics of the comparison operator is  <\/p>\n<pre lang=\"cpp\">\r\nbool operator == (const yourclassname &tester)\r\n<\/pre>\n<p>where yourclassname is what you have called your class, of course you can run comparisons with other classes and also standard variables like floats, but you will need to write a separate one for each.  The operator is the keyword, and it returns a bool(ean) result which is either true\/false, so once you have done a comparison within this function you just return either true or false and the code below will still compile<\/p>\n<pre lang=\"cpp\">\r\nclassA a;\r\nclassA b;\r\nif (a==b) .. \r\n<\/pre>\n<p>below is code that you can compile to demonstrate operator keyword abit more.<\/p>\n<pre lang=\"cpp\">\r\n#include <iostream>\r\n\r\nusing namespace std;\r\n\r\nclass classA\r\n{\r\n  public : \r\n    int x;\r\n    \r\n  \/\/ of course you cannot alter the testing class so const \r\n  \/\/ the tester is the right hand side of the boolean test e.g.\r\n  \/\/ if (A == B ) . A = this class and B = tester\r\n  bool operator == (const classA &tester)\r\n  {\r\n      if (x == tester.x) \r\n\treturn true; \r\n      else \r\n\treturn false;\r\n  };\r\n};\r\n\r\nint main()\r\n{\r\n    classA a;\r\n    classA b;\r\n    a.x = 0;\r\n    b.x = 0;\r\n    \r\n    if (a==b)\r\n      cout << \"the same\" << endl;\r\n    else\r\n      cout << \"not the same\" << endl;\r\n    \r\n    b.x = 1;\r\n\r\n    if (a==b)\r\n      cout << \"the same\" << endl;\r\n    else\r\n      cout << \"not the same\" << endl;\r\n\r\n    return 0;\r\n}\r\n<\/pre>\n<p>and the output would be<\/p>\n<pre lang=\"bash\">\r\nthe same\r\nnot the same\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Comparisons between classes are different compared to local variables e.g. int(eger), float, doubles.. because the comparisons aspects of the standard variables have already been done e.g. int a=2; int b =3; if (a==b)&#8230;. the comparison being the &#8220;==&#8221;, within your own classes if you want to test against a another object that is of the &hellip; <a href=\"https:\/\/www.codingfriends.com\/index.php\/2010\/02\/24\/operator-comparisons\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">operator &#8211; comparisons<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[122,109],"class_list":["post-753","post","type-post","status-publish","format-standard","hentry","category-c_and_cpp","tag-122","tag-operator"],"_links":{"self":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/753","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/comments?post=753"}],"version-history":[{"count":2,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/753\/revisions"}],"predecessor-version":[{"id":758,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/753\/revisions\/758"}],"wp:attachment":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/media?parent=753"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/categories?post=753"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/tags?post=753"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}