Tips & tricks

C++

Hello world example

#include <iostream>

using namespace std;

class Address {
    public:
        void setStreet( string street ) { this->_street = street; }
        void setNo( string no) { this->_no = no; }
        void setCity( string city ) { this->_city = city; }
        string getStreet() { return this->_street; }
        string getNo() { return this->_no; }
        string getCity() { return this->_city; }

        string prettyAddress() { return this->_street + ' ' + this->_no + ", " + this->_city; }
    private:
        string _street;
        string _no;
        string _city;
};

class User {

    public:
        void setName( string name ) { this->_name = name; }
        void setAddress( Address address ) { this->_address = address; }
        string getName() { return this->_name; }
        Address getAddress() { return this->_address; }

    private:
        string _name;
        Address _address;

};

int main() {
    User user = User();
    user.setName("John Doe");
    Address addr = Address();
    addr.setStreet("High Street");
    addr.setNo("77");
    addr.setCity("Minitown");
    user.setAddress(addr);
    cout << "Hello world by " << user.getName() << " from " << user.getAddress().prettyAddress() << endl;
    return 0;
}

(c) 2017 horvoje.net