{"id":6010,"date":"2022-08-31T22:31:44","date_gmt":"2022-08-31T17:01:44","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-c-class-implementation-closed\/"},"modified":"2022-08-31T22:31:44","modified_gmt":"2022-08-31T17:01:44","slug":"solved-c-class-implementation-closed","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-c-class-implementation-closed\/","title":{"rendered":"[Solved] C++ Class implementation [closed]"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-49089887\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"49089887\" data-parentid=\"49089192\" data-score=\"1\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>Although this is not a <code>Matrix<\/code> class but rather a simple <code>Vector2<\/code> class that accepts <code>floats<\/code> this class demonstrates how you can overload the <code>operators<\/code>.<\/p>\n<pre><code>class Vector2 {     \npublic:\n    union {\n        float _f2[2];\n        struct {\n            float _x;\n            float _y;\n        };\n    };\n\n    inline Vector2();\n    inline Vector2( float x, float y);\n    inline Vector2( float *pfv );\n\n    \/\/ Operators\n    inline Vector2  operator+( const Vector2 &amp;v2 ) const;\n    inline Vector2  operator+() const;\n    inline Vector2&amp; operator+=( const Vector2 &amp;v2 );\n    inline Vector2  operator-( const Vector2 &amp;v2 ) const;\n    inline Vector2  operator-() const;\n    inline Vector2&amp; operator-=( const Vector2 &amp;v2 );\n    inline Vector2  operator*( const float &amp;value ) const;\n    inline Vector2&amp; operator*=( const float &amp;value );\n    inline Vector2  operator\/( const float &amp;value ) const;\n    inline Vector2&amp; operator\/=( const float &amp;value );   \n\n    inline friend Vector2 Vector2::operator*( const float &amp;fValue, const Vector2 v2 ) {\n        \/\/ Pre Multiple Vector By A Scalar\n        return Vector2( value*v2._x, value*v2._y );\n    } \n\n    inline friend Vector2 Vector2::operator\/( const float &amp;value, const Vector2 v2 ) {\n        \/\/ Pre Divide Vector By A Scalar\n        Vector2 vec2;\n        \/\/ Math:: external class not shown here isZero() to check if value is minimally close to 0\n        if ( Math::isZero( v2._x) )\n            vec2._x= 0.0f;\n        else\n            vec2._x = value \/ v2._x;\n\n        if ( Math::isZero( v2._y ) )\n            vec2._y = 0.0f;\n        else\n            vec2._y = value \/ v2._y;\n\n         return vec2;\n    } \n\n};\n\ninline Vector2::Vector2() :\n  _x(0.0f), _y(0.0f) {  \n} \n\ninline Vector2::Vector2( float x, float y ) :\n  _x(x), _y(y) {\n}\n\ninline Vector2::Vector2( float *pv ) :\n  _x( pv[0] ), _y( pv[1] ) {\n}\n\ninline Vector2 Vector2::operator+() const {\n    return *this; \/\/ Unary + Operator\n} \n\ninline Vector2 Vector2::operator+( const Vector2 &amp;v2 ) const {\n    \/\/ Binary + Take This Vector And Add Another Vector To It\n    return Vector2( _x + v2._x, _y + v2._y );\n}\n\ninline Vector2 &amp;Vector2::operator+=( const Vector2 &amp;v2 ) {\n    _x += v2._x;\n    _y += v2._y;\n    return *this;\n}\n\ninline Vector2 Vector2::operator-() const {\n    return Vector2( -_x, -_y );  \/\/ Unary - Operator: Negate Each Value\n}\n\ninline Vector2 Vector2::operator-( const Vector2 &amp;v2 ) const {\n    \/\/ Unary - Take This Vector And Subtract Another Vector From It\n    return Vector2( _x - v2._x, _y - v2._x );\n}\n\ninline Vector2 &amp;Vector2::operator-=( const Vector2 &amp;v2 ) {\n    _x -= v2._x;\n    _y -= v2._y;\n    return *this;\n}\n\ninline Vector2 Vector2::operator*( const float &amp;value ) const {\n    \/\/ Post Multiply Vector By A Scalar\n    return Vector2( _x*value, _y*value );\n} \n\ninline Vector2&amp; Vector2::operator*=( const float &amp;value ) {\n    \/\/ Multiply This Vector By A Scalar\n    _x *= value;\n    _x *= value;    \n    return *this; \n}\n\ninline Vector2 Vector2::operator\/( const float &amp;value ) const {\n    \/\/ Post Divide Vector By A Scalar    \n    Vector2 vec2;\n    \/\/ Again Math:: not shown here...\n    if ( Math::isZero( value ) ) {\n        vec2._x = 0.0f;\n        vec2._y = 0.0f;\n    } else {\n        float inv = 1\/value;\n        vec2._x *= inv;\n        vec2._y *= inv;\n    }    \n    return vec2;  \n}\n\ninline Vector2&amp; Vector2::operator\/=( const float &amp;value ) {\n    \/\/ Divide This Vector By A Scalar Value\n    \/\/ Same as above; Math:: not shown here.\n    if ( Math::isZero( value ) ) {\n        _x = 0.0f;\n        _y = 0.0f;\n    } else {\n        float inv = 1\/value;\n        _x *= inv;\n        _y *= inv;\n    }    \n    return *this; \n}\n<\/code><\/pre>\n<p>This is rather an older class of mine and this is not the complete class as it has other member functions that are not operators such as <code>normalize<\/code>, <code>dot<\/code>, <code>length<\/code> etc. I just shown the class with its constructors and operators to demonstrate how you might do something similar with your matrix class(s). <\/p>\n<hr>\n<p>As a note of reference if you want to see how a versatile matrix class is implemented there are two major libraries that are worth checking out and read through both their documentations and source files: <code>GLM<\/code> is a header only library and very easy to setup and use. <code>Eigen<\/code> on the other hand is a little more involved to setup up and does have a little bit of a learning curve to get use to.<\/p>\n<ul>\n<li><a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/glm.g-truc.net\/0.9.8\/index.html\">GLM<\/a>   <\/li>\n<li><a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/eigen.tuxfamily.org\/index.php?title=Main_Page\">Eigen<\/a><\/li>\n<\/ul>\n<\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\"><\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved C++ Class implementation [closed] <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] Although this is not a Matrix class but rather a simple Vector2 class that accepts floats this class demonstrates how you can overload the operators. class Vector2 { public: union { float _f2[2]; struct { float _x; float _y; }; }; inline Vector2(); inline Vector2( float x, float y); inline Vector2( float *pfv ); &#8230; <a title=\"[Solved] C++ Class implementation [closed]\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-c-class-implementation-closed\/\" aria-label=\"More on [Solved] C++ Class implementation [closed]\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[324,519],"class_list":["post-6010","post","type-post","status-publish","format-standard","hentry","category-solved","tag-c","tag-class"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>[Solved] C++ Class implementation [closed] - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-c-class-implementation-closed\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] C++ Class implementation [closed] - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] Although this is not a Matrix class but rather a simple Vector2 class that accepts floats this class demonstrates how you can overload the operators. class Vector2 { public: union { float _f2[2]; struct { float _x; float _y; }; }; inline Vector2(); inline Vector2( float x, float y); inline Vector2( float *pfv ); ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-c-class-implementation-closed\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-08-31T17:01:44+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-c-class-implementation-closed\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-c-class-implementation-closed\\\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#\\\/schema\\\/person\\\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] C++ Class implementation [closed]\",\"datePublished\":\"2022-08-31T17:01:44+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-c-class-implementation-closed\\\/\"},\"wordCount\":160,\"publisher\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#organization\"},\"keywords\":[\"c++\",\"class\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-c-class-implementation-closed\\\/\",\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-c-class-implementation-closed\\\/\",\"name\":\"[Solved] C++ Class implementation [closed] - JassWeb\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#website\"},\"datePublished\":\"2022-08-31T17:01:44+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-c-class-implementation-closed\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-c-class-implementation-closed\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-c-class-implementation-closed\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] C++ Class implementation [closed]\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#website\",\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/jassweb.com\\\/wp-content\\\/uploads\\\/2021\\\/02\\\/jass-website-logo-1.png\",\"contentUrl\":\"https:\\\/\\\/jassweb.com\\\/wp-content\\\/uploads\\\/2021\\\/02\\\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#\\\/schema\\\/logo\\\/image\\\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#\\\/schema\\\/person\\\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/wp-content\\\/litespeed\\\/avatar\\\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400\",\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/wp-content\\\/litespeed\\\/avatar\\\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400\",\"contentUrl\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/wp-content\\\/litespeed\\\/avatar\\\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\\\/\\\/jassweb.com\"],\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/author\\\/jaspritsinghghumangmail-com\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] C++ Class implementation [closed] - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-c-class-implementation-closed\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] C++ Class implementation [closed] - JassWeb","og_description":"[ad_1] Although this is not a Matrix class but rather a simple Vector2 class that accepts floats this class demonstrates how you can overload the operators. class Vector2 { public: union { float _f2[2]; struct { float _x; float _y; }; }; inline Vector2(); inline Vector2( float x, float y); inline Vector2( float *pfv ); ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-c-class-implementation-closed\/","og_site_name":"JassWeb","article_published_time":"2022-08-31T17:01:44+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-c-class-implementation-closed\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-c-class-implementation-closed\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] C++ Class implementation [closed]","datePublished":"2022-08-31T17:01:44+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-c-class-implementation-closed\/"},"wordCount":160,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["c++","class"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-c-class-implementation-closed\/","url":"https:\/\/jassweb.com\/solved\/solved-c-class-implementation-closed\/","name":"[Solved] C++ Class implementation [closed] - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-08-31T17:01:44+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-c-class-implementation-closed\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-c-class-implementation-closed\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-c-class-implementation-closed\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] C++ Class implementation [closed]"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/6010","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=6010"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/6010\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=6010"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=6010"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=6010"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}