{"id":137,"date":"2009-07-24T12:16:59","date_gmt":"2009-07-24T11:16:59","guid":{"rendered":"http:\/\/www.codingfriends.com\/?p=137"},"modified":"2009-07-24T12:16:59","modified_gmt":"2009-07-24T11:16:59","slug":"operators","status":"publish","type":"post","link":"https:\/\/www.codingfriends.com\/index.php\/2009\/07\/24\/operators\/","title":{"rendered":"Operators"},"content":{"rendered":"<p>An operator syntax is part of the standard C\/C++ syntax that allows you to use the +\/-\/* etc aspects of the objects that are assoicated with them, for example, for two values of an integer you can add these two values together with value1 + value2, the adding part is calculated within the operator and the value is returned.<\/p>\n<p>To code a operator you just need to use the <return type> operator <insert type of operator>(<passed parameter(s)>) function.  Within the code below there are 3 types of operators for the operate test class, to add another integer to the internal value, to increament the internal value and also to logically add the internal value with another integer value.<\/p>\n<p>The code<\/p>\n<pre lang=\"cpp\">\r\n#include <iostream>\r\n\r\nusing namespace std;\r\n\r\nclass optest\r\n{\r\npublic :\r\n       int i ;\r\n       optest() { i = 0; }\r\n       optest(int i2) { i = i2;}\r\n       \r\n       virtual int out() { return i;}\r\n\r\n       int operator +(int addi)\r\n       {\r\n              return i + addi;\r\n       }\r\n\r\n       int operator++(int)\r\n       {\r\n              return ++i;\r\n       }\r\n\r\n       int operator &(int addi)\r\n       {\r\n              return i&addi;\r\n       }\r\n};\r\n\r\nclass optest2 : public optest       \/\/ inhert the base class for showing how to use operators within base class and inherited classes\r\n{\r\npublic:\r\n       int out() { return i + 10;}\r\n       optest2() : optest() {;}\r\n       optest2(int i) : optest(i) {;}\r\n};\r\n\r\nint main(int argc, char* argv[])\r\n{\r\n       \/\/ setup and demo the results of the operators\r\n       optest op(3);\r\n       optest2 op2(2);\r\n       cout << op.out() << endl; \r\n       cout << op + 3 << endl;\r\n       cout << op.out() << endl;\r\n       cout << op++ << endl;\r\n       cout << op.out() << endl;\r\n       cout << op2.out() << endl;\r\n       return 0;\r\n}\r\n<\/pre>\n<p>I have included Microsoft Visual Studio 2005 downloadable edition (<a title=\"here\"  href=\"http:\/\/msdn.microsoft.com\/vstudio\/express\/default.aspx\" target=\"_blank\">here<\/a>) and also Linux code for this tutorial as a download.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>An operator syntax is part of the standard C\/C++ syntax that allows you to use the +\/-\/* etc aspects of the objects that are assoicated with them, for example, for two values of an integer you can add these two values together with value1 + value2, the adding part is calculated within the operator and &hellip; <a href=\"https:\/\/www.codingfriends.com\/index.php\/2009\/07\/24\/operators\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Operators<\/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":[10,9,8,398,400],"class_list":["post-137","post","type-post","status-publish","format-standard","hentry","category-c_and_cpp","tag-c","tag-gcc","tag-gnu","tag-linux","tag-windows"],"_links":{"self":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/137","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=137"}],"version-history":[{"count":1,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/137\/revisions"}],"predecessor-version":[{"id":138,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/137\/revisions\/138"}],"wp:attachment":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/media?parent=137"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/categories?post=137"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/tags?post=137"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}