#ifndef __FORMAT_HPP__ #define __FORMAT_HPP__ #include #include #include // base class class format { protected: unsigned int width; private: virtual void justify(const std::string&) = 0; public: format(int w) { width = w; } virtual ~format() {} void output() { std::string line, word; std::ifstream inFile("quote.txt"); inFile >> word; line = word; while (inFile >> word) { if (line.length() + word.length() + 1 > width) { // we have obtained one line justify(line); line = ""; line = word; } else { line = line + " "+ word; } } justify(line); inFile.close(); } }; // child class 1 class LeftJustify : public format { public: LeftJustify(int w) :format(w){} private: virtual void justify(const std::string& line) { std::cout << line << std::endl; } }; // child class 2 class RightJustify : public format { public: RightJustify(int w) :format(w) {} private: virtual void justify(const std::string& line) { for (int i = 0 ; i < width - line.length(); i++) { std::cout << " "; } std::cout << line << std::endl; } }; // child class 3 class CenterJustify : public format { public: CenterJustify(int w) :format(w) {} private: virtual void justify(const std::string& line) { for (int i=0; i < (width - line.length())/2; i++) { std::cout << " "; } std::cout << line << std::endl; } }; #endif