{"id":10801,"date":"2024-03-14T23:37:14","date_gmt":"2024-03-14T23:37:14","guid":{"rendered":"https:\/\/adelnasim.com\/docs\/courses\/oop\/operator-overloading\/"},"modified":"2024-04-07T01:19:46","modified_gmt":"2024-04-07T01:19:46","slug":"operator-overloading","status":"publish","type":"docs","link":"https:\/\/adelnasim.com\/ar\/docs\/courses\/oop\/operator-overloading\/","title":{"rendered":"\u0627\u0644\u062a\u062d\u0645\u064a\u0644 \u0627\u0644\u0632\u0627\u0626\u062f"},"content":{"rendered":"<p>\u0625\u0646 \u0627\u0644\u062a\u062d\u0645\u064a\u0644 \u0627\u0644\u0632\u0627\u0626\u062f \u0644\u0644\u0645\u0634\u063a\u0644 \u0641\u064a \u0644\u063a\u0629 C++ \u0647\u0648 \u0645\u064a\u0632\u0629 \u062a\u0633\u0645\u062d \u0644\u0643 \u0628\u0625\u0639\u0627\u062f\u0629 \u062a\u0639\u0631\u064a\u0641 \u0633\u0644\u0648\u0643 \u0627\u0644\u0645\u0634\u063a\u0644\u064a\u0646 (\u0645\u062b\u0644 +\u060c -\u060c *\u060c \/\u060c \u0648\u0645\u0627 \u0625\u0644\u0649 \u0630\u0644\u0643) \u0644\u0644\u0623\u0646\u0648\u0627\u0639 \u0627\u0644\u0645\u062d\u062f\u062f\u0629 \u0645\u0646 \u0642\u0628\u0644 \u0627\u0644\u0645\u0633\u062a\u062e\u062f\u0645. \u0641\u0647\u0648 \u064a\u0645\u0643\u0651\u0646\u0643 \u0645\u0646 \u062a\u0648\u0633\u064a\u0639 \u0648\u0638\u0627\u0626\u0641 \u0627\u0644\u0645\u0634\u063a\u0644\u064a\u0646 \u0644\u0644\u0639\u0645\u0644 \u0645\u0639 \u0643\u0627\u0626\u0646\u0627\u062a \u0645\u0646 \u0641\u0626\u0627\u062a\u0643 \u0627\u0644\u062e\u0627\u0635\u0629.<\/p>\n<p><strong>\u0645\u062b\u0627\u0644 1:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;iostream&gt;\r\nusing namespace std;\r\nclass triangle\r\n{\r\n    private:\r\n        float width, height;\r\n    public:\r\n        triangle(float a = 0, float b = 0)\r\n        {\r\n            width = a;\r\n            height = b;\r\n        }\r\n        void getdata()\r\n        {\r\n            cout &lt;&lt; \"Enter width \\n\";\r\n            cin &gt;&gt; width;\r\n            cout &lt;&lt; \"Enter height \\n\";\r\n            cin &gt;&gt; height;\r\n        }\r\n        void showdata()\r\n        {\r\n            cout &lt;&lt; \"width and height = (\" &lt;&lt; width &lt;&lt; \",\" &lt;&lt; height &lt;&lt; \")\" &lt;&lt; endl;\r\n        }\r\n        void add(triangle c1, triangle c2)\r\n        {\r\n            width = c1.width + c2.width;\r\n            height = c1.height + c2.height;\r\n        }\r\n};\r\nint main()\r\n{\r\n    triangle c1, c2(3.5, 1.5), c3;\r\n    c1.getdata();\r\n    c3.add(c1, c2);\r\n    c3.showdata();\r\n    return 0;\r\n}<\/pre>\n<p>\u064a\u062d\u062f\u062f \u0628\u0631\u0646\u0627\u0645\u062c C++ \u0647\u0630\u0627 \u0627\u0644\u0641\u0626\u0629 triangle \u0627\u0644\u062a\u064a \u062a\u0645\u062b\u0644 \u0645\u062b\u0644\u062b\u064b\u0627. \u062a\u062d\u062a\u0648\u064a \u0639\u0644\u0649 \u0639\u0631\u0636 \u0648\u0627\u0631\u062a\u0641\u0627\u0639 \u0623\u0639\u0636\u0627\u0621 \u0627\u0644\u0628\u064a\u0627\u0646\u0627\u062a \u0627\u0644\u062e\u0627\u0635\u0629\u060c \u0648\u0645\u0646\u0634\u0626 \u0644\u062a\u0647\u064a\u0626\u062a\u0647\u0645\u060c \u0648\u062f\u0648\u0627\u0644\u0627\u0644\u0623\u0639\u0636\u0627\u0621 getdata \u0644\u0625\u062f\u062e\u0627\u0644 \u0642\u064a\u0645 \u0627\u0644\u0639\u0631\u0636 \u0648\u0627\u0644\u0627\u0631\u062a\u0641\u0627\u0639\u060c \u0648\u0625\u0638\u0647\u0627\u0631 \u0627\u0644\u0628\u064a\u0627\u0646\u0627\u062a \u0644\u0639\u0631\u0636 \u0627\u0644\u0639\u0631\u0636 \u0648\u0627\u0644\u0627\u0631\u062a\u0641\u0627\u0639\u060c \u0648\u0627\u0644\u0625\u0636\u0627\u0641\u0629 \u0644\u0625\u0636\u0627\u0641\u0629 \u0645\u062b\u0644\u062b\u064a\u0646 \u0645\u0639\u064b\u0627.<\/p>\n<p>\u062a\u0639\u0631\u064a\u0641 \u0627\u0644\u0640class:<\/p>\n<ul>\n<li>\u064a\u062a\u0645 \u062a\u0639\u0631\u064a\u0641  triangle class \u0628\u0639\u0631\u0636 \u0648\u0627\u0631\u062a\u0641\u0627\u0639 \u0623\u0639\u0636\u0627\u0621 \u0627\u0644\u0628\u064a\u0627\u0646\u0627\u062a \u0627\u0644\u062e\u0627\u0635\u0629.<\/li>\n<li>\u064a\u062d\u062a\u0648\u064a \u0639\u0644\u0649 constructor \u064a\u0642\u0648\u0645 \u0628\u062a\u0647\u064a\u0626\u0629 \u0627\u0644\u0639\u0631\u0636 \u0648\u0627\u0644\u0627\u0631\u062a\u0641\u0627\u0639 \u0628\u0627\u0644\u0642\u064a\u0645 \u0627\u0644\u0627\u0641\u062a\u0631\u0627\u0636\u064a\u0629 0.<\/li>\n<li>\u064a\u062d\u062a\u0648\u064a \u0639\u0644\u0649 \u062f\u0648\u0627\u0644 \u0627\u0644\u0623\u0639\u0636\u0627\u0621 getdata\u060c showdata\u060c \u0648\u0627\u0644\u0625\u0636\u0627\u0641\u0629.<\/li>\n<\/ul>\n<p>\u0627\u0644\u062f\u0627\u0644\u0629 \u0627\u0644\u0631\u0626\u064a\u0633\u0629 main:<\/p>\n<ul>\n<li>\u0641\u064a \u0627\u0644\u062f\u0627\u0644\u0629 \u0627\u0644\u0631\u0626\u064a\u0633\u064a\u0629\u060c \u064a\u062a\u0645 \u0625\u0646\u0634\u0627\u0621 \u062b\u0644\u0627\u062b\u0629 (triangle objects): c1 \u0648c2 \u0648c3.<\/li>\n<li>\u062a\u062a\u0645 \u062a\u0647\u064a\u0626\u0629 c1 \u0628\u0627\u0644\u0642\u064a\u0645 \u0627\u0644\u0627\u0641\u062a\u0631\u0627\u0636\u064a\u0629 \u0628\u0627\u0633\u062a\u062e\u062f\u0627\u0645 default constructor.<\/li>\n<li>\u062a\u062a\u0645 \u062a\u0647\u064a\u0626\u0629 c2 \u0628\u0627\u0644\u0642\u064a\u0645\u062a\u064a\u0646 3.5 \u06481.5 \u0628\u0627\u0633\u062a\u062e\u062f\u0627\u0645 parameterized constructor.<\/li>\n<li>\u064a\u062a\u0645 \u0625\u0646\u0634\u0627\u0621 c3 \u062f\u0648\u0646 \u0623\u064a \u062a\u0639\u0631\u064a\u0641 \u0645\u0633\u0628\u0642.<\/li>\n<li>\u064a\u062a\u0645 \u0627\u0633\u062a\u062f\u0639\u0627\u0621 getdata \u0644\u0640 c1 \u0644\u0625\u062f\u062e\u0627\u0644 \u0642\u064a\u0645 \u0627\u0644\u0639\u0631\u0636 \u0648\u0627\u0644\u0627\u0631\u062a\u0641\u0627\u0639.<\/li>\n<li>\u064a\u062a\u0645 \u0627\u0633\u062a\u062f\u0639\u0627\u0621 \u0627\u0644\u0625\u0636\u0627\u0641\u0629 \u0644\u0640 c3 \u0645\u0639 c1 \u0648c2 \u0643\u0640arguments\u060c \u0645\u0645\u0627 \u064a\u0636\u064a\u0641 \u0627\u0644\u0639\u0631\u0636 \u0648\u0627\u0644\u0627\u0631\u062a\u0641\u0627\u0639 \u0627\u0644\u0645\u0642\u0627\u0628\u0644\u064a\u0646 \u0644\u0640 c1 \u0648c2 \u0648\u064a\u0639\u064a\u0646 \u0627\u0644\u0646\u062a\u064a\u062c\u0629 \u0625\u0644\u0649 c3.<\/li>\n<li>\u064a\u062a\u0645 \u0627\u0633\u062a\u062f\u0639\u0627\u0621 showdata \u0644\u0640 c3 \u0644\u0639\u0631\u0636 \u0627\u0644\u0639\u0631\u0636 \u0648\u0627\u0644\u0627\u0631\u062a\u0641\u0627\u0639 \u0627\u0644\u0646\u0627\u062a\u062c \u0628\u0639\u062f \u0627\u0644\u0625\u0636\u0627\u0641\u0629.<\/li>\n<\/ul>\n<p><strong>\u0645\u062b\u0627\u0644 2:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;iostream&gt;\r\nusing namespace std;\r\nclass triangle\r\n{\r\n    private:\r\n        float width, height;\r\n    public:\r\n        triangle(float a = 0, float b = 0)\r\n        {\r\n            width = a;\r\n            height = b;\r\n        }\r\n        void getdata()\r\n        {\r\n            cout &lt;&lt; \"Enter width \\n\";\r\n            cin &gt;&gt; width;\r\n            cout &lt;&lt; \"Enter height \\n\";\r\n            cin &gt;&gt; height;\r\n        }\r\n        void showdata()\r\n        {\r\n            cout &lt;&lt; \"width and height = (\" &lt;&lt; width &lt;&lt; \",\" &lt;&lt; height &lt;&lt; \")\" &lt;&lt; endl;\r\n        }\r\n        triangle add(triangle c2)\r\n        {\r\n            triangle c3;\r\n            c3.width = width + c2.width;\r\n            c3.height = height + c2.height;\r\n            return c3;\r\n        }\r\n};\r\nint main()\r\n{\r\n    triangle c1, c2(3.5, 1.5), c3;\r\n    c1.getdata();\r\n    c3 = c1.add(c2);\r\n    c3.showdata();\r\n    return 0;\r\n}\r\n<\/pre>\n<p>\u0641\u064a \u0647\u0630\u0627 \u0627\u0644\u0645\u062b\u0627\u0644\u060c \u064a\u062a\u0645 \u0623\u064a\u0636\u064b\u0627 \u0625\u062c\u0631\u0627\u0621 \u0639\u0645\u0644\u064a\u0629 \u0625\u0636\u0627\u0641\u0629 \u0645\u062b\u0644\u062b\u064a\u0646 \u0628\u0627\u0633\u062a\u062e\u062f\u0627\u0645 \u062f\u0627\u0644\u0629 \u0639\u0636\u0648 \u062a\u0633\u0645\u0649 add. \u0648\u0645\u0639 \u0630\u0644\u0643\u060c \u0628\u062f\u0644\u0627\u064b \u0645\u0646 \u062a\u0639\u062f\u064a\u0644 \u0627\u0644\u0643\u0627\u0626\u0646 \u0627\u0644\u0630\u064a \u064a\u062a\u0645 \u0627\u0633\u062a\u062f\u0639\u0627\u0621 \u0627\u0644\u062f\u0627\u0644\u0629 \u0639\u0644\u064a\u0647\u060c \u064a\u062a\u0645 \u0625\u0646\u0634\u0627\u0621 \u0643\u0627\u0626\u0646 \u0645\u062b\u0644\u062b \u062c\u062f\u064a\u062f \u062f\u0627\u062e\u0644 \u062f\u0627\u0644\u0629 \u0627\u0644\u0625\u0636\u0627\u0641\u0629 \u0644\u062a\u062e\u0632\u064a\u0646 \u0646\u062a\u064a\u062c\u0629 \u0627\u0644\u0625\u0636\u0627\u0641\u0629. \u064a\u062a\u0645 \u0628\u0639\u062f \u0630\u0644\u0643 \u0625\u0631\u062c\u0627\u0639 \u0647\u0630\u0627 \u0627\u0644\u0643\u0627\u0626\u0646 \u0627\u0644\u062c\u062f\u064a\u062f \u0645\u0646 \u0627\u0644\u062f\u0627\u0644\u0629\u060c \u0645\u0639 \u062a\u0631\u0643 \u0627\u0644\u0643\u0627\u0626\u0646\u0627\u062a \u0627\u0644\u0623\u0635\u0644\u064a\u0629 \u062f\u0648\u0646 \u062a\u063a\u064a\u064a\u0631.<\/p>\n<p><strong>\u0645\u062b\u0627\u0644 3:<br \/>\n<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;iostream&gt;\r\nusing namespace std;\r\nclass triangle\r\n{\r\n    private:\r\n        float width, height;\r\n    public:\r\n        triangle(float a = 0, float b = 0)\r\n        {\r\n            width = a;\r\n            height = b;\r\n        }\r\n        void getdata()\r\n        {\r\n            cout &lt;&lt; \"Enter width \\n\";\r\n            cin &gt;&gt; width;\r\n            cout &lt;&lt; \"Enter height \\n\";\r\n            cin &gt;&gt; height;\r\n        }\r\n        void showdata()\r\n        {\r\n            cout &lt;&lt; \"width and height = (\" &lt;&lt; width &lt;&lt; \",\" &lt;&lt; height &lt;&lt; \")\" &lt;&lt; endl;\r\n        }\r\n        triangle operator+(triangle c2) \/\/ Overloading the + operator\r\n        {\r\n            triangle c3;\r\n            c3.width = width + c2.width;\r\n            c3.height = height + c2.height;\r\n            return c3;\r\n        }\r\n};\r\nint main()\r\n{\r\n    triangle c1, c2(3.5, 1.5), c3;\r\n    c1.getdata();\r\n    c3 = c1 + c2;  \/\/ Using the overloaded + operator\r\n    c3.showdata();\r\n    return 0;\r\n}\r\n<\/pre>\n<p>\u064a\u0648\u0636\u062d \u0628\u0631\u0646\u0627\u0645\u062c C++ \u0647\u0630\u0627 \u0627\u0633\u062a\u062e\u062f\u0627\u0645 \u0627\u0644\u062a\u062d\u0645\u064a\u0644 \u0627\u0644\u0632\u0627\u0626\u062f \u0644\u0644\u0645\u0634\u063a\u0644 \u0644\u0625\u062c\u0631\u0627\u0621 \u0639\u0645\u0644\u064a\u0629 \u0627\u0644\u062c\u0645\u0639 \u0628\u064a\u0646 \u0643\u0627\u0626\u0646\u064a\u0646 \u0645\u062b\u0644\u062b\u064a\u0646. \u062f\u0639\u0646\u0627 \u0646\u062d\u0644\u0644 \u0627\u0644\u0643\u0648\u062f \u0648\u0646\u0633\u0644\u0637 \u0627\u0644\u0636\u0648\u0621 \u0639\u0644\u0649 \u0627\u0644\u0627\u062e\u062a\u0644\u0627\u0641\u0627\u062a \u0645\u0642\u0627\u0631\u0646\u0629 \u0628\u0627\u0644\u0623\u0645\u062b\u0644\u0629 \u0627\u0644\u0633\u0627\u0628\u0642\u0629:<\/p>\n<p>\u0627\u0644\u0627\u062e\u062a\u0644\u0627\u0641\u0627\u062a \u0645\u0642\u0627\u0631\u0646\u0629 \u0628\u0627\u0644\u0623\u0645\u062b\u0644\u0629 \u0627\u0644\u0633\u0627\u0628\u0642\u0629:<\/p>\n<ol>\n<li>\u0627\u0644\u062a\u062d\u0645\u064a\u0644 \u0627\u0644\u0632\u0627\u0626\u062f:<br \/>\n\u0641\u064a \u0647\u0630\u0627 \u0627\u0644\u0645\u062b\u0627\u0644\u060c \u064a\u062a\u0645 \u062a\u062d\u0645\u064a\u0644 \u0639\u0627\u0645\u0644 \u0627\u0644\u062a\u0634\u063a\u064a\u0644 + \u0628\u0634\u0643\u0644 \u0632\u0627\u0626\u062f \u0628\u0627\u0633\u062a\u062e\u062f\u0627\u0645 \u062f\u0627\u0644\u0629 \u0639\u0636\u0648 \u062a\u0633\u0645\u0649  operator+. \u064a\u062a\u064a\u062d \u0644\u0646\u0627 \u0630\u0644\u0643 \u0625\u062c\u0631\u0627\u0621 \u0639\u0645\u0644\u064a\u0629 \u0627\u0644\u062c\u0645\u0639 \u0628\u064a\u0646 \u0643\u0627\u0626\u0646\u064a\u0646 \u0645\u062b\u0644\u062b\u064a\u0646 \u0628\u0627\u0633\u062a\u062e\u062f\u0627\u0645 \u0627\u0644\u0639\u0627\u0645\u0644 + \u0628\u0637\u0631\u064a\u0642\u0629 \u0645\u0634\u0627\u0628\u0647\u0629 \u0644\u0644\u0640built-in types.<br \/>\n\u0641\u064a \u0627\u0644\u0645\u0642\u0627\u0628\u0644\u060c \u0627\u0633\u062a\u062e\u062f\u0645\u062a \u0627\u0644\u0623\u0645\u062b\u0644\u0629 \u0627\u0644\u0633\u0627\u0628\u0642\u0629 \u062f\u0648\u0627\u0644 \u0627\u0644\u0623\u0639\u0636\u0627\u0621 (add) \u0644\u0625\u062c\u0631\u0627\u0621 \u0639\u0645\u0644\u064a\u0629 \u0627\u0644\u062c\u0645\u0639 \u0628\u064a\u0646 \u0627\u0644\u0643\u0627\u0626\u0646\u0627\u062a.<\/li>\n<li>\u0627\u0644\u0625\u0633\u062a\u062e\u062f\u0627\u0645 \u0641\u064a \u0627\u0644\u062f\u0627\u0644\u0629 \u0627\u0644\u0631\u0626\u064a\u0633\u0629 main:<br \/>\n\u0641\u064a \u0627\u0644\u062f\u0627\u0644\u0629 \u0627\u0644\u0631\u0626\u064a\u0633\u064a\u0629\u060c \u064a\u062a\u0645 \u062a\u0646\u0641\u064a\u0630 \u0625\u0636\u0627\u0641\u0629 c1 \u0648c2 \u0628\u0627\u0633\u062a\u062e\u062f\u0627\u0645  + operator: c3 = c1 + c2;.<br \/>\n\u064a\u0639\u062f \u0628\u0646\u0627\u0621 \u0627\u0644\u062c\u0645\u0644\u0629 \u0647\u0630\u0627 \u0623\u0643\u062b\u0631 \u0633\u0647\u0648\u0644\u0629 \u0648\u0625\u064a\u062c\u0627\u0632\u064b\u0627 \u0645\u0642\u0627\u0631\u0646\u0629 \u0628\u0627\u0633\u062a\u062f\u0639\u0627\u0621 \u062f\u0627\u0644\u0629 \u0639\u0636\u0648 \u0645\u0646\u0641\u0635\u0644\u0629 (add).<\/li>\n<li>\u0646\u0648\u0639 \u0627\u0644\u0625\u0633\u062a\u0631\u062c\u0627\u0639 return:<br \/>\n\u062a\u0642\u0648\u0645 \u0627\u0644\u062f\u0627\u0644\u0629 operator+ \u0628\u0625\u0631\u062c\u0627\u0639  triangle object \u064a\u0645\u062b\u0644 \u0646\u062a\u064a\u062c\u0629 \u0639\u0645\u0644\u064a\u0629 \u0627\u0644\u0625\u0636\u0627\u0641\u0629.<br \/>\n\u0641\u064a \u0627\u0644\u0623\u0645\u062b\u0644\u0629 \u0627\u0644\u0633\u0627\u0628\u0642\u0629\u060c \u0644\u0645 \u062a\u064f\u0631\u062c\u0639 \u062f\u0648\u0627\u0644 \u0627\u0644\u0625\u0636\u0627\u0641\u0629 \u0623\u064a \u0642\u064a\u0645\u0629\u060c \u0648\u0628\u062f\u0644\u0627\u064b \u0645\u0646 \u0630\u0644\u0643 \u0642\u0627\u0645\u062a \u0628\u062a\u0639\u062f\u064a\u0644 \u0623\u062d\u062f \u0627\u0644\u0640objects \u0645\u0628\u0627\u0634\u0631\u0629\u064b.<\/li>\n<\/ol>","protected":false},"excerpt":{"rendered":"<p>Operator overloading in C++ is a feature that allows you to redefine the behavior of operators (such as +, -, *, \/, etc.) for user-defined types. It enables you to extend the functionality of operators to work with objects of your own classes. Example 1: #include &lt;iostream&gt; using namespace std; class triangle { private: float [&hellip;]<\/p>","protected":false},"author":3,"featured_media":0,"parent":4291,"menu_order":51,"comment_status":"closed","ping_status":"closed","template":"","doc_tag":[],"class_list":["post-10801","docs","type-docs","status-publish","hentry","no-post-thumbnail"],"acf":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/adelnasim.com\/ar\/wp-json\/wp\/v2\/docs\/10801","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/adelnasim.com\/ar\/wp-json\/wp\/v2\/docs"}],"about":[{"href":"https:\/\/adelnasim.com\/ar\/wp-json\/wp\/v2\/types\/docs"}],"author":[{"embeddable":true,"href":"https:\/\/adelnasim.com\/ar\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/adelnasim.com\/ar\/wp-json\/wp\/v2\/comments?post=10801"}],"version-history":[{"count":5,"href":"https:\/\/adelnasim.com\/ar\/wp-json\/wp\/v2\/docs\/10801\/revisions"}],"predecessor-version":[{"id":10809,"href":"https:\/\/adelnasim.com\/ar\/wp-json\/wp\/v2\/docs\/10801\/revisions\/10809"}],"up":[{"embeddable":true,"href":"https:\/\/adelnasim.com\/ar\/wp-json\/wp\/v2\/docs\/4291"}],"wp:attachment":[{"href":"https:\/\/adelnasim.com\/ar\/wp-json\/wp\/v2\/media?parent=10801"}],"wp:term":[{"taxonomy":"doc_tag","embeddable":true,"href":"https:\/\/adelnasim.com\/ar\/wp-json\/wp\/v2\/doc_tag?post=10801"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}