{"id":1056,"date":"2013-09-03T21:00:38","date_gmt":"2013-09-03T21:00:38","guid":{"rendered":"https:\/\/raspberry-projects.com\/pi\/?p=1056"},"modified":"2020-07-22T22:08:52","modified_gmt":"2020-07-22T22:08:52","slug":"creating-a-class","status":"publish","type":"post","link":"https:\/\/raspberry-projects.com\/pi\/programming-in-c\/classes\/creating-a-class","title":{"rendered":"Creating A Class"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">Very Simple Class<\/h4>\n\n\n\n<h5 class=\"wp-block-heading\">Header file<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/******************************\n\/\/******************************\n\/\/********** MY CLASS **********\n\/\/******************************\n\/\/******************************\nclass MyClassName\n{\npublic:\n    unsigned char MyClassByte;\n    unsigned int MyClassInt;\n\n\t\/\/***** CONSTRUCTOR *****\n\tMyClassName()\n\t{\n\t\tthis->MyClassByte = 0;\n\t\tthis->MyClassInt = 0;\n\t}\n\t\n\t\/\/***** DECONSTRUCTOR *****\n\t~MyClassName() {};\n};<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">Using it<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>\tMyClassName MyClassName1;\n\tMyClassName1.MyClassByte = 24;\n\tMyClassName1.MyClassInt = 25;\n\n\/\/If you want to pass it to a function:\n\tvoid my_function (MyClassName *MyClassName1)\n\t{\n\t\tMyClassName1->MyClassByte = 24;\n\t\tMyClassName1->MyClassInt = 25;\n\t}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">More Complex Class<\/h4>\n\n\n\n<h5 class=\"wp-block-heading\">Header file<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/******************************\n\/\/******************************\n\/\/********** MY CLASS **********\n\/\/******************************\n\/\/******************************\nclass MyClassName\n{\npublic:\n    MyClassName();\n    MyClassName(std::string MyClassSetupString, unsigned char MyClassByte_set, unsigned int MyClassInt_set);\n    ~MyClassName();\n    \n\t\/\/int MyClassPublicFunction(void);\n\nprivate:\n    unsigned char MyClassByte;\n    unsigned int MyClassInt;\n\n    int MyClassPrivateFunction(std::string SetupString);\n};<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">CPP File<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>\n\/\/*****************************************\n\/\/*****************************************\n\/\/********** DEFAULT CONSTRUCTOR **********\n\/\/*****************************************\n\/\/*****************************************\nMyClassName::MyClassName()\n{\n\tthis->MyClassByte = 0;\n\tthis->MyClassInt = 0;\n\tthis->MyClassPrivateFunction(std::string(\"Hello\"));\n}\n\n\/\/********************************************\n\/\/********************************************\n\/\/********** OVERLOADED CONSTRUCTOR **********\n\/\/********************************************\n\/\/********************************************\nMyClassName::MyClassName(std::string MyClassSetupString, unsigned char MyClassByte_set, unsigned int MyClassInt_set)\n{\n\tthis->MyClassByte = MyClassByte_set;\n\tthis->MyClassInt = MyClassInt_set;\n\n\tthis->MyClassPrivateFunction(MyClassSetupString);\n}\n\n\/\/***********************************\n\/\/***********************************\n\/\/********** DECONSTRUCTOR **********\n\/\/***********************************\n\/\/***********************************\nMyClassName::~MyClassName()\n{\n    \n}\n\n\/\/********************************\n\/\/********************************\n\/\/********** A FUNCTION **********\n\/\/********************************\n\/\/********************************\nint MyClassName::MyClassPrivateFunction(std::string SetupString)\n{\n\t\/*\n    if (something)\n\t{\n        perror(\"Could not do something\");\n        exit(1);\n    }\n\t*\/\n\n    return(0);\n}<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">Using it<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>\tMyClassName MyClassName1;\n\tMyClassName1.MyClassPrivateFunction(\"Test\");<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Very Simple Class Header file Using it More Complex Class Header file CPP File Using it<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[70],"tags":[],"class_list":["post-1056","post","type-post","status-publish","format-standard","hentry","category-classes"],"_links":{"self":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/1056","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/comments?post=1056"}],"version-history":[{"count":8,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/1056\/revisions"}],"predecessor-version":[{"id":3280,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/1056\/revisions\/3280"}],"wp:attachment":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/media?parent=1056"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/categories?post=1056"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/tags?post=1056"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}