C++ OOP Hello World

From D3xt3r01.tk
Jump to navigationJump to search

INFO

This is my first attempt to write a C++ class. It has a constructor ( to initialize default values to variabiles ), a setter function ( to set values in private variables ) and a getter function ( to get the values ). I think the easiest way to learn is by trying to edit simple codes and see what they do and if they still work.

CODE

#include <iostream>
#include <string>

using namespace std;

class thingy{
        private:
                string text;

        public:
                thingy(){
                        this->text = "";
                }
                void SetText(string text){
                        this->text = text;
                }
                string GetText(void){
                        return(this->text);
                }
};

int main (void){
        thingy moo;
        cout << "Initial text value is: " << moo.GetText() << endl;
        moo.SetText("Hello World");
        cout << "Text value now is: " << moo.GetText() << endl;
        return 0;
}

should output

 Initial text value is:
 Text value now is: Hello World