Modeco80/Cool Hello World code: Difference between revisions

From Computernewb Wiki
Jump to navigation Jump to search
m (syntax highlighting)
(No difference)

Revision as of 15:40, 11 December 2021

// ==== Hello World Enterprise Edition ===
//
// (c) 2018 FakeCorp Ltd. All rights reserved.
//
// hwent.cc: Main entry point for the application.

#include <iostream>
#include <string>
#include <sstream>
using std::cout;
using std::endl;
using std::string;
using std::stringstream;

namespace HelloWorld {
	
	class Stream {
	public:
		Stream(string str){
			mp_stream << str; // Push str into the underlying stream.
		}
		void pushString(string str) { /* Push a string into the stream. */ mp_stream << str; }
		string toString() { /* Return the resulting string buffer. */ return mp_stream.str(); }
	private:
		stringstream mp_stream;
	};
	
	class StreamWriter {
	public:
		virtual void writeStream(Stream& stream, string buffer) {}
	};
	
	class HelloWorldStreamWriter : public StreamWriter {
	public:
		void writeStream(Stream& stream, string buffer){
			stream.pushString(buffer); // Push the string to the stream.
		}
	};
	
	class HelloWorldApplication {
	public:
		int Run(){
			Stream f_Stream("");
			mp_Hw.writeStream(f_Stream, "Hello World!"); // Start the stream writer.
			cout << f_Stream.toString() << endl; // Print the resulting stream buffer to the standard console output
			return 0; // Return a successful exit code.
		}
	private:
		HelloWorldStreamWriter mp_Hw;
	};
}

int main(){
	HelloWorld::HelloWorldApplication hwapp; // Initalize the application.
	return hwapp.Run(); // Run the application.
}