├── LICENSE ├── PacktCppByExample-master ├── 1_3_Basic_Cpp_Syntax │ ├── CMakeLists.txt │ └── src │ │ └── main.cpp ├── 2_Virtual_Die │ ├── CMakeLists.txt │ └── src │ │ └── main.cpp ├── 3_Data_Algorithms │ ├── CMakeLists.txt │ └── src │ │ └── main.cpp ├── 4_Classes_Structures │ ├── CMakeLists.txt │ └── src │ │ └── main.cpp ├── 5_Library_Management │ ├── CMakeLists.txt │ ├── data │ │ └── books.csv │ ├── include │ │ ├── application.h │ │ ├── book.h │ │ ├── book_library.h │ │ └── book_library_parser.h │ └── src │ │ ├── application.cpp │ │ ├── book_library.cpp │ │ ├── book_library_parser.cpp │ │ └── main.cpp ├── 6_First_GUI │ ├── CMakeLists.txt │ ├── include │ │ └── mainwindow.h │ ├── src │ │ ├── main.cpp │ │ └── mainwindow.cpp │ └── ui │ │ └── mainwindow.ui ├── 7_Text_Editor │ ├── CMakeLists.txt │ ├── data │ │ └── test.txt │ ├── include │ │ └── mainwindow.h │ ├── src │ │ ├── main.cpp │ │ └── mainwindow.cpp │ └── ui │ │ └── mainwindow.ui └── CMakeLists.txt └── README.md /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Packt 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /PacktCppByExample-master/1_3_Basic_Cpp_Syntax/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.11 FATAL_ERROR) 2 | 3 | project(BasicSyntax) 4 | 5 | set(project_source 6 | src/main.cpp) 7 | 8 | add_executable(${PROJECT_NAME} 9 | ${project_source}) 10 | -------------------------------------------------------------------------------- /PacktCppByExample-master/1_3_Basic_Cpp_Syntax/src/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(int argc, char* argv[]) 4 | { 5 | auto f = 1.23f; // 32 bits 6 | double d = 1.23; // 64 bits 7 | long double ld = 1.342349287492837492892873492347; // large 8 | 9 | int i = 0; // usually 32 bits 10 | short s = 32000; // usually 16 bits 11 | long l = 18000000000; // usually 64 bits 12 | long long ll = 1292309230983; // 128 bits 13 | char c = 0; // usually 8 bits 0-255 14 | 15 | auto b = false; // usually 8 bits, but check with sizeof(bool) 16 | 17 | std::cout << "Size of float and double: " << sizeof(float) 18 | << ", " << sizeof(double) << std::endl; 19 | 20 | // this is a "block" 21 | { 22 | int j = 1; 23 | int t = j; 24 | } 25 | 26 | if(b) 27 | { 28 | std::cout << "B is true!" << std::endl; 29 | } 30 | else if(!b) 31 | { 32 | std::cout << "B is false!" << std::endl; 33 | } 34 | else 35 | { 36 | std::cout << "B is false!" << std::endl; 37 | } 38 | 39 | switch(i) 40 | { 41 | case 0: 42 | std::cout << "switch 0" << std::endl; 43 | break; 44 | case 1: 45 | std::cout << "switch 1" << std::endl; 46 | break; 47 | case 2: 48 | std::cout << "switch 2" << std::endl; 49 | break; 50 | default: 51 | break; 52 | } 53 | 54 | // for loop 55 | for(unsigned int count = 0; count < 10; count++) 56 | { 57 | if (count == 5) continue; 58 | std::cout << "Count is: " << count << std::endl; 59 | } 60 | 61 | // range based for loop 62 | std::string str = "hello"; 63 | for(char character : str) 64 | { 65 | std::cout << character << std::endl; 66 | } 67 | 68 | int while_count = 0; 69 | while(while_count < 10) 70 | { 71 | std::cout << "While count: " << while_count << std::endl; 72 | while_count++; 73 | // or... 74 | // if(while_cout >= 10) break; 75 | } 76 | 77 | getchar(); 78 | 79 | return 0; 80 | } 81 | -------------------------------------------------------------------------------- /PacktCppByExample-master/2_Virtual_Die/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.11 FATAL_ERROR) 2 | 3 | project(VirtualDie) 4 | 5 | set(project_sources 6 | src/main.cpp) 7 | 8 | add_executable(${PROJECT_NAME} ${project_sources}) -------------------------------------------------------------------------------- /PacktCppByExample-master/2_Virtual_Die/src/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int roll_dice(const int number_faces) 6 | { 7 | std::random_device random_device; 8 | std::default_random_engine random_engine(random_device()); 9 | std::uniform_int_distribution uniform_int_distribution(1, number_faces); 10 | 11 | auto random_int = uniform_int_distribution(random_engine); 12 | return random_int; 13 | } 14 | 15 | int main(int argc, char* argv[]) 16 | { 17 | int number_faces = 6; 18 | int number_rolls = 10; 19 | 20 | std::cout << "Enter the number of faces: "; 21 | std::cin >> number_faces; 22 | 23 | std::cout << std::endl; 24 | 25 | std::cout << "Enter the number of times to roll: "; 26 | std::cin >> number_rolls; 27 | printf("Rolling dice %d times.\n", number_rolls); 28 | auto sum = 0; 29 | auto min_roll = 10000000; 30 | auto max_roll = -10000000; 31 | for(auto i = 0; i < number_rolls; i ++) 32 | { 33 | auto random_int = roll_dice(number_faces); 34 | sum += random_int; 35 | printf("You rolled a %d\n", random_int); 36 | if (random_int < min_roll) min_roll = random_int; 37 | if (random_int > max_roll) max_roll = random_int; 38 | } 39 | 40 | auto average = (double) sum / 10.0; 41 | printf("Your average roll was %f\n", average); 42 | printf("You min roll was %d and your max %d\n", min_roll, max_roll); 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /PacktCppByExample-master/3_Data_Algorithms/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.11.0 FATAL_ERROR) 2 | 3 | project(DataStructuresAlgorithms) 4 | 5 | set(CMAKE_CXX_STANDARD 17) 6 | 7 | set(project_sources 8 | src/main.cpp) 9 | 10 | add_executable(${PROJECT_NAME} ${project_sources}) -------------------------------------------------------------------------------- /PacktCppByExample-master/3_Data_Algorithms/src/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #pragma region Helper functions 9 | template 10 | void print_container(Container container) 11 | { 12 | std::copy(container.begin(), container.end(), 13 | std::ostream_iterator(std::cout, " ")); 14 | std::cout << std::endl; 15 | } 16 | template 17 | void print_vector(std::vector data, std::string info_text = "", bool with_new_line = true) 18 | { 19 | if (!info_text.empty()) std::cout << info_text.c_str(); 20 | /* Print path vector to console */ 21 | std::copy(data.begin(), data.end(), 22 | std::ostream_iterator(std::cout, " ")); 23 | if (with_new_line) std::cout << std::endl; 24 | } 25 | #pragma endregion 26 | 27 | using contact = std::tuple; 28 | using contacts = std::vector; 29 | 30 | contacts demo_contacts = 31 | { 32 | {"Javon", "Bernard", "555-667-2345"}, 33 | {"Kiana", "Lester", "234-745-4534"}, 34 | {"Lia", "Baldwin", "546-234-7578"}, 35 | {"Ryleigh", "Liu", "094-242-7745"}, 36 | {"Shea", "Gibbs", "251-647-8941"}, 37 | {"Sabrina", "Galvan", "345-798-3168"}, 38 | {"Glenn", "Henry", "654-984-8664"}, 39 | {"Lucia", "Wright", "134-798-6464"}, 40 | {"Annika", "Carr", "461-641-7497"}, 41 | {"Bruno", "Frank", "465-065-4610"} 42 | }; 43 | 44 | inline std::ostream& operator<<(std::ostream& output, const contact &person) 45 | { 46 | output << std::get<1>(person) << ", " << std::get<0>(person) 47 | << ": " << std::get<2>(person); 48 | return output; 49 | } 50 | 51 | bool compare_by_first_name(const contact& first, const contact &second) 52 | { 53 | return std::get<0>(first) < std::get<0>(second); 54 | } 55 | 56 | bool compare_by_last_name(const contact& first, const contact& second) 57 | { 58 | return std::get<1>(first) < std::get<1>(second); 59 | } 60 | 61 | bool compare_by_phone_number(const contact& first, const contact& second) 62 | { 63 | return std::get<2>(first) < std::get<2>(second); 64 | } 65 | 66 | void print_contacts(const contacts& list) 67 | { 68 | std::for_each(list.begin(), list.end(), 69 | [](const contact& contact) 70 | { 71 | std::cout << contact << std::endl; 72 | }); 73 | } 74 | 75 | int main(int argc, char* argv[]) 76 | { 77 | std::cout << "Sort by first name..." << std::endl; 78 | std::sort(demo_contacts.begin(), demo_contacts.end(), compare_by_first_name); 79 | print_contacts(demo_contacts); 80 | std::cout << std::endl; 81 | 82 | std::cout << "Sort by last name..." << std::endl; 83 | std::sort(demo_contacts.begin(), demo_contacts.end(), compare_by_last_name); 84 | print_contacts(demo_contacts); 85 | std::cout << std::endl; 86 | 87 | std::cout << "Sort by phone number..." << std::endl; 88 | std::sort(demo_contacts.begin(), demo_contacts.end(), compare_by_phone_number); 89 | print_contacts(demo_contacts); 90 | std::cout << std::endl; 91 | 92 | std::cout << "Searching for people..." << std::endl; 93 | std::vector people_with_b; 94 | std::copy_if(demo_contacts.begin(), demo_contacts.end(), 95 | std::back_inserter(people_with_b), [](const contact& person) 96 | { 97 | auto first_name = std::get<0>(person); 98 | return first_name.find('L') != std::string::npos; 99 | }); 100 | 101 | print_contacts(people_with_b); 102 | 103 | return 0; 104 | } 105 | -------------------------------------------------------------------------------- /PacktCppByExample-master/4_Classes_Structures/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.11.0 FATAL_ERROR) 2 | 3 | project(ClassesAndStructures) 4 | 5 | set(CMAKE_CXX_STANDARD 17) 6 | 7 | set(project_sources 8 | src/main.cpp) 9 | 10 | add_executable(${PROJECT_NAME} ${project_sources}) -------------------------------------------------------------------------------- /PacktCppByExample-master/4_Classes_Structures/src/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | struct player 6 | { 7 | std::string name; 8 | int total_money{ 0 }; 9 | }; 10 | 11 | struct wager_result 12 | { 13 | bool did_win = false; 14 | int money_change_amount{ 0 }; 15 | int wager{ 0 }; 16 | int guess{ 0 }; 17 | }; 18 | std::ostream& operator<<(std::ostream& output, const wager_result &wager) 19 | { 20 | output << "You bet " << std::to_string(wager.wager) << " and guessed " 21 | << std::to_string(wager.guess) << std::endl; 22 | output << "You "; 23 | if(wager.did_win) 24 | { 25 | output << "won "; 26 | } 27 | else 28 | { 29 | output << "lost "; 30 | } 31 | output << std::to_string(std::abs(wager.money_change_amount)) << std::endl; 32 | return output; 33 | } 34 | 35 | class number_generator 36 | { 37 | public: 38 | number_generator(const int &min, const int &max) 39 | : random_engine_(random_device_()), uniform_int_distribution_(min, max) 40 | { 41 | 42 | } 43 | int generate_random_number() 44 | { 45 | return uniform_int_distribution_(random_engine_); 46 | } 47 | private: 48 | std::random_device random_device_; 49 | std::default_random_engine random_engine_; 50 | std::uniform_int_distribution uniform_int_distribution_; 51 | }; 52 | 53 | class game 54 | { 55 | player player_; 56 | number_generator number_generator_; 57 | public: 58 | game(const player& player) 59 | :number_generator_(1, 10) 60 | { 61 | player_ = player; 62 | } 63 | 64 | void start_game() 65 | { 66 | std::cout << "Enter your name: "; 67 | std::getline(std::cin, player_.name); 68 | std::cout << "Enter start money amount: "; 69 | std::cin >> player_.total_money; 70 | } 71 | 72 | bool keep_playing() 73 | { 74 | std::cout << "Play again? (Y/N): "; 75 | char response; 76 | std::cin >> response; 77 | return response == 'y' || response == 'Y'; 78 | } 79 | 80 | int get_bet_amount() 81 | { 82 | std::cout << "Enter bet amount (1 - " + 83 | std::to_string(player_.total_money) + "): "; 84 | int wager; 85 | std::cin >> wager; 86 | return wager; 87 | } 88 | 89 | int get_guess() 90 | { 91 | std::cin.get(); 92 | std::cout << "Enter guess (1-10): "; 93 | int guess; 94 | std::cin >> guess; 95 | return guess; 96 | } 97 | wager_result play_round() 98 | { 99 | const auto wager = get_bet_amount(); 100 | const auto guess = get_guess(); 101 | 102 | wager_result result; 103 | result.wager = wager; 104 | result.guess = guess; 105 | const auto number = number_generator_.generate_random_number(); 106 | if(number == guess) 107 | { 108 | result.did_win = true; 109 | result.money_change_amount = wager * 10; 110 | } 111 | else 112 | { 113 | result.did_win = false; 114 | result.money_change_amount = -1 * wager; 115 | } 116 | 117 | player_.total_money += result.money_change_amount; 118 | return result; 119 | } 120 | 121 | bool game_over() 122 | { 123 | return player_.total_money <= 0; 124 | } 125 | }; 126 | 127 | int main() 128 | { 129 | std::cout << "----- Casino! -----" << std::endl; 130 | const player m_player; 131 | game game(m_player); 132 | game.start_game(); 133 | do 134 | { 135 | const auto result = game.play_round(); 136 | std::cout << result << std::endl; 137 | if (game.game_over()) { 138 | std::cout << "You lost, game over" << std::endl; 139 | break; 140 | } 141 | 142 | } while (!game.game_over() && game.keep_playing()); 143 | 144 | std::cout << "Game over! Thanks for playing!" << std::endl; 145 | return 0; 146 | } -------------------------------------------------------------------------------- /PacktCppByExample-master/5_Library_Management/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.11) 2 | 3 | project(BooksLibrary) 4 | 5 | set(project_sources 6 | src/application.cpp 7 | src/book_library.cpp 8 | src/book_library_parser.cpp 9 | src/main.cpp) 10 | 11 | set(project_headers 12 | include/book.h 13 | include/book_library.h 14 | include/book_library_parser.h 15 | include/application.h) 16 | 17 | add_executable(${PROJECT_NAME} ${project_sources} ${project_headers}) 18 | 19 | target_include_directories(${PROJECT_NAME} 20 | PUBLIC 21 | ${CMAKE_CURRENT_SOURCE_DIR}/include) 22 | 23 | target_compile_definitions(${PROJECT_NAME} PRIVATE DATA_DIRECTORY="${CMAKE_CURRENT_SOURCE_DIR}/data") -------------------------------------------------------------------------------- /PacktCppByExample-master/5_Library_Management/data/books.csv: -------------------------------------------------------------------------------- 1 | Book1,No-one,Fiction 2 | Book3,No-one,Fiction 3 | Book2,Someone,Non-Fiction 4 | Book5,Someone,Non-Fiction 5 | Book8,Him,Thriller 6 | Book9,She,Thriller 7 | Book12,Him,Mystery 8 | RandomBookTile,RandomAuthor,Action 9 | BookTitle,Author,Suspense -------------------------------------------------------------------------------- /PacktCppByExample-master/5_Library_Management/include/application.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | class application 4 | { 5 | 6 | public: 7 | enum class app_action 8 | { 9 | none, search_book, search_author, sort, save_library 10 | }; 11 | bool continue_running(); 12 | char get_action(); 13 | app_action get_action_input(char input); 14 | }; -------------------------------------------------------------------------------- /PacktCppByExample-master/5_Library_Management/include/book.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | 5 | struct book 6 | { 7 | std::string title; 8 | std::string author; 9 | std::string genre; 10 | }; 11 | 12 | inline std::ostream& operator<<(std::ostream& output, const book &book) 13 | { 14 | output << "Title: " << book.title << " By: " << book.author << " Genre: " << book.genre; 15 | return output; 16 | } 17 | 18 | inline bool operator<(const book& left, const book& right) 19 | { 20 | return std::tie(left.title, left.author, left.genre) < std::tie(right.title, right.author, right.genre); 21 | } -------------------------------------------------------------------------------- /PacktCppByExample-master/5_Library_Management/include/book_library.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include "book.h" 4 | 5 | class book_library 6 | { 7 | std::vector books_; 8 | 9 | public: 10 | book_library(const std::vector &books); 11 | 12 | book find_book_by_title(const std::string& title); 13 | std::vector find_books_by_author(const std::string& author); 14 | std::vector books() const; 15 | std::vector sort_books(); 16 | }; 17 | -------------------------------------------------------------------------------- /PacktCppByExample-master/5_Library_Management/include/book_library_parser.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "book_library.h" 3 | 4 | class book_library_parser 5 | { 6 | public: 7 | static book_library load_book_library(const std::string& path); 8 | static bool save_book_libray(const book_library&library, const std::string &output_path); 9 | }; -------------------------------------------------------------------------------- /PacktCppByExample-master/5_Library_Management/src/application.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include "application.h" 3 | #include 4 | 5 | 6 | bool application::continue_running() 7 | { 8 | std::cout << "Anything else? (Y/N): "; 9 | char response; 10 | std::cin >> response; 11 | return response == 'Y' || response == 'y'; 12 | } 13 | 14 | char application::get_action() 15 | { 16 | std::cout << "What would you like to do next? (Choose one): " << std::endl; 17 | std::cout << "1 (show books) " << std::endl; 18 | std::cout << "2 (sort books) " << std::endl; 19 | std::cout << "3 (search for book by title) " << std::endl; 20 | std::cout << "4 (search for books by author) " << std::endl; 21 | std::cout << "5 (save current library)" << std::endl; 22 | char action; 23 | std::cin >> action; 24 | return action; 25 | } 26 | 27 | application::app_action application::get_action_input(char input) 28 | { 29 | switch(input) 30 | { 31 | case '1': 32 | return app_action::none; 33 | case '2': 34 | return app_action::sort; 35 | case '3': 36 | return app_action::search_book; 37 | case '4': 38 | return app_action::search_author; 39 | case '5': 40 | return app_action::save_library; 41 | default: 42 | return app_action::none; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /PacktCppByExample-master/5_Library_Management/src/book_library.cpp: -------------------------------------------------------------------------------- 1 | #include "book_library.h" 2 | 3 | #include 4 | book_library::book_library(const std::vector& books) 5 | :books_(books) 6 | { 7 | 8 | } 9 | 10 | book book_library::find_book_by_title(const std::string& title) 11 | { 12 | auto book_iterator = std::find_if(books_.begin(), books_.end(), [&](const book& book) 13 | { 14 | return book.title == title; 15 | }); 16 | 17 | if(book_iterator != std::end(books_)) 18 | { 19 | return *book_iterator; 20 | } 21 | 22 | return book{}; 23 | } 24 | 25 | std::vector book_library::find_books_by_author(const std::string& author) 26 | { 27 | std::vector results; 28 | std::copy_if(books_.begin(), books_.end(), std::back_inserter(results), [&](const book &book) 29 | { 30 | return book.author == author; 31 | }); 32 | return results; 33 | } 34 | 35 | std::vector book_library::books() const 36 | { 37 | return books_; 38 | } 39 | 40 | std::vector book_library::sort_books() 41 | { 42 | std::sort(books_.begin(), books_.end()); 43 | return books_; 44 | } 45 | 46 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /PacktCppByExample-master/5_Library_Management/src/book_library_parser.cpp: -------------------------------------------------------------------------------- 1 | #include "book_library_parser.h" 2 | 3 | #include 4 | #include 5 | inline std::vector split_string(const std::string &str) 6 | { 7 | std::istringstream input(str); 8 | std::string part; 9 | std::vector parts; 10 | while (std::getline(input, part, ',')) 11 | { 12 | parts.emplace_back(part); 13 | } 14 | 15 | return parts; 16 | } 17 | book_library book_library_parser::load_book_library(const std::string &path) 18 | { 19 | std::vector books; 20 | 21 | std::ifstream input(path); 22 | if(input.is_open() && input.good()) 23 | { 24 | std::string line; 25 | while(std::getline(input, line)) 26 | { 27 | book book; 28 | auto parts = split_string(line); 29 | book.title = parts[0]; 30 | book.author = parts[1]; 31 | book.genre = parts[2]; 32 | books.emplace_back(book); 33 | } 34 | return book_library(books); 35 | } 36 | return book_library({}); 37 | } 38 | 39 | bool book_library_parser::save_book_libray(const book_library& library, const std::string& output_path) 40 | { 41 | std::ofstream output(output_path); 42 | if(output.is_open() && output.good()) 43 | { 44 | const auto books = library.books(); 45 | for (const auto &book : books) 46 | { 47 | output << book.title << "," << book.author << "," 48 | << book.genre << std::endl; 49 | } 50 | 51 | output.close(); 52 | return true; 53 | } 54 | return false; 55 | } 56 | -------------------------------------------------------------------------------- /PacktCppByExample-master/5_Library_Management/src/main.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | 5 | #include "application.h" 6 | #include "book_library_parser.h" 7 | 8 | void print_books(const std::vector &books) 9 | { 10 | std::copy(books.begin(), books.end(), 11 | std::ostream_iterator(std::cout, "\n")); 12 | } 13 | 14 | int main() 15 | { 16 | std::cout << "---------- Book Library ----------" << std::endl; 17 | application app; 18 | book_library_parser parser; 19 | const auto path = std::string(DATA_DIRECTORY) + "//books.csv"; 20 | auto library = parser.load_book_library(path); 21 | do 22 | { 23 | // get user input 24 | auto input = app.get_action(); 25 | auto action = app.get_action_input(input); 26 | 27 | switch(action) 28 | { 29 | case application::app_action::none: 30 | if(input == '1') 31 | { 32 | print_books(library.books()); 33 | } 34 | break; 35 | case application::app_action::sort: 36 | { 37 | auto sorted_books = library.sort_books(); 38 | print_books(sorted_books); 39 | } 40 | break; 41 | case application::app_action::search_book: 42 | { 43 | std::cin.get(); 44 | std::cout << "Enter book title: "; 45 | std::string book_title; 46 | std::getline(std::cin, book_title); 47 | auto book = library.find_book_by_title(book_title); 48 | if(book.title == book_title) 49 | { 50 | std::cout << "Found book: " << book << std::endl; 51 | } 52 | else 53 | { 54 | std::cout << "No book found with title: " << book_title << std::endl; 55 | } 56 | } 57 | break; 58 | case application::app_action::search_author: 59 | { 60 | std::cin.get(); 61 | std::cout << "Enter author name: "; 62 | std::string author_name; 63 | std::getline(std::cin, author_name); 64 | auto books = library.find_books_by_author(author_name); 65 | if(!books.empty()) 66 | { 67 | std::cout << "Found " + std::to_string(books.size()) << " books by " 68 | << author_name << std::endl; 69 | print_books(books); 70 | } 71 | else 72 | { 73 | std::cout << "No books found by: " << author_name << std::endl; 74 | } 75 | } 76 | break; 77 | case application::app_action::save_library: 78 | { 79 | parser.save_book_libray(library, "books.csv"); 80 | } 81 | break; 82 | default: 83 | break; 84 | } 85 | 86 | // do something with user input 87 | } while (app.continue_running()); 88 | 89 | return 0; 90 | } -------------------------------------------------------------------------------- /PacktCppByExample-master/6_First_GUI/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.11) 2 | 3 | project(HelloQt5) 4 | 5 | find_package(Qt5 REQUIRED COMPONENTS Core Gui widgets) 6 | 7 | set(project_sources 8 | src/main.cpp 9 | src/mainwindow.cpp) 10 | 11 | set(project_headers 12 | include/mainwindow.h) 13 | 14 | set(project_ui 15 | ui/mainwindow.ui) 16 | 17 | qt5_wrap_cpp(project_sources_moc ${project_headers}) 18 | qt5_wrap_ui(project_ui_wrap ${project_ui}) 19 | 20 | add_executable(${PROJECT_NAME} 21 | ${project_sources} 22 | ${project_headers} 23 | ${project_sources_moc} 24 | ${project_ui_wrap}) 25 | 26 | target_link_libraries(${PROJECT_NAME} 27 | PUBLIC 28 | Qt5::Core Qt5::Gui Qt5::Widgets) 29 | 30 | target_include_directories(${PROJECT_NAME} 31 | PUBLIC 32 | ${CMAKE_CURRENT_SOURCE_DIR}/include 33 | PRIVATE 34 | ${CMAKE_CURRENT_BINARY_DIR}) 35 | 36 | add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD 37 | COMMAND ${CMAKE_COMMAND} -E copy_if_different $ $ 38 | COMMAND ${CMAKE_COMMAND} -E copy_if_different $ $ 39 | COMMAND ${CMAKE_COMMAND} -E copy_if_different $ $ 40 | ) -------------------------------------------------------------------------------- /PacktCppByExample-master/6_First_GUI/include/mainwindow.h: -------------------------------------------------------------------------------- 1 | #ifndef MAINWINDOW_H 2 | #define MAINWINDOW_H 3 | 4 | #include 5 | 6 | namespace Ui { 7 | class MainWindow; 8 | } 9 | 10 | class MainWindow : public QMainWindow 11 | { 12 | Q_OBJECT 13 | 14 | public: 15 | explicit MainWindow(QWidget *parent = 0); 16 | ~MainWindow(); 17 | 18 | public slots: 19 | void showText(); 20 | 21 | private: 22 | Ui::MainWindow *ui; 23 | }; 24 | 25 | #endif // MAINWINDOW_H 26 | -------------------------------------------------------------------------------- /PacktCppByExample-master/6_First_GUI/src/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "mainwindow.h" 3 | 4 | int main(int argc, char* argv[]) 5 | { 6 | QApplication app(argc, argv); 7 | 8 | MainWindow main_window; 9 | main_window.setWindowTitle("Hello World"); 10 | main_window.show(); 11 | 12 | return app.exec(); 13 | } 14 | -------------------------------------------------------------------------------- /PacktCppByExample-master/6_First_GUI/src/mainwindow.cpp: -------------------------------------------------------------------------------- 1 | #include "mainwindow.h" 2 | #include "ui_mainwindow.h" 3 | 4 | MainWindow::MainWindow(QWidget *parent) : 5 | QMainWindow(parent), 6 | ui(new Ui::MainWindow) 7 | { 8 | ui->setupUi(this); 9 | QObject::connect(ui->pushButton, SIGNAL(clicked()), 10 | this, SLOT(showText())); 11 | } 12 | 13 | MainWindow::~MainWindow() 14 | { 15 | delete ui; 16 | } 17 | 18 | void MainWindow::showText() 19 | { 20 | ui->label->setText(tr("HELLO")); 21 | } 22 | -------------------------------------------------------------------------------- /PacktCppByExample-master/6_First_GUI/ui/mainwindow.ui: -------------------------------------------------------------------------------- 1 | 2 | 3 | MainWindow 4 | 5 | 6 | 7 | 0 8 | 0 9 | 800 10 | 600 11 | 12 | 13 | 14 | MainWindow 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | Qt::AlignCenter 25 | 26 | 27 | 28 | 29 | 30 | 31 | Click Me 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 0 41 | 0 42 | 800 43 | 17 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | -------------------------------------------------------------------------------- /PacktCppByExample-master/7_Text_Editor/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.11) 2 | 3 | project(TextEditor) 4 | 5 | find_package(Qt5 REQUIRED COMPONENTS Core Gui widgets) 6 | 7 | set(project_sources 8 | src/main.cpp 9 | src/mainwindow.cpp) 10 | 11 | set(project_headers 12 | include/mainwindow.h) 13 | 14 | set(project_ui 15 | ui/mainwindow.ui) 16 | 17 | qt5_wrap_cpp(project_sources_moc ${project_headers}) 18 | qt5_wrap_ui(project_ui_wrap ${project_ui}) 19 | 20 | add_executable(${PROJECT_NAME} 21 | ${project_sources} 22 | ${project_headers} 23 | ${project_sources_moc} 24 | ${project_ui_wrap}) 25 | 26 | target_compile_definitions(${PROJECT_NAME} 27 | PRIVATE 28 | DATA_PATH="${CMAKE_CURRENT_SOURCE_DIR}/data/test.txt") 29 | 30 | target_link_libraries(${PROJECT_NAME} 31 | PUBLIC 32 | Qt5::Core Qt5::Gui Qt5::Widgets) 33 | 34 | target_include_directories(${PROJECT_NAME} 35 | PUBLIC 36 | ${CMAKE_CURRENT_SOURCE_DIR}/include 37 | PRIVATE 38 | ${CMAKE_CURRENT_BINARY_DIR}) 39 | 40 | add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD 41 | COMMAND ${CMAKE_COMMAND} -E copy_if_different $ $ 42 | COMMAND ${CMAKE_COMMAND} -E copy_if_different $ $ 43 | COMMAND ${CMAKE_COMMAND} -E copy_if_different $ $ 44 | ) -------------------------------------------------------------------------------- /PacktCppByExample-master/7_Text_Editor/data/test.txt: -------------------------------------------------------------------------------- 1 | hello there 2 | hello there 3 | hello there -------------------------------------------------------------------------------- /PacktCppByExample-master/7_Text_Editor/include/mainwindow.h: -------------------------------------------------------------------------------- 1 | #ifndef MAINWINDOW_H 2 | #define MAINWINDOW_H 3 | 4 | #include 5 | 6 | namespace Ui { 7 | class MainWindow; 8 | } 9 | 10 | class MainWindow : public QMainWindow 11 | { 12 | Q_OBJECT 13 | 14 | public: 15 | explicit MainWindow(QWidget *parent = 0); 16 | ~MainWindow(); 17 | 18 | public slots: 19 | void onSaveActionTriggered(); 20 | void onOpenActionTriggered(); 21 | 22 | private: 23 | Ui::MainWindow *ui; 24 | }; 25 | 26 | #endif // MAINWINDOW_H 27 | -------------------------------------------------------------------------------- /PacktCppByExample-master/7_Text_Editor/src/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "mainwindow.h" 3 | 4 | int main(int argc, char* argv[]) 5 | { 6 | QApplication app(argc, argv); 7 | 8 | MainWindow main_window; 9 | main_window.show(); 10 | return app.exec(); 11 | } 12 | -------------------------------------------------------------------------------- /PacktCppByExample-master/7_Text_Editor/src/mainwindow.cpp: -------------------------------------------------------------------------------- 1 | #include "mainwindow.h" 2 | #include "ui_mainwindow.h" 3 | 4 | #include 5 | 6 | #include 7 | #include 8 | #include 9 | 10 | MainWindow::MainWindow(QWidget *parent) : 11 | QMainWindow(parent), 12 | ui(new Ui::MainWindow) 13 | { 14 | ui->setupUi(this); 15 | 16 | QObject::connect(ui->actionSave, SIGNAL(triggered()), 17 | this, SLOT(onSaveActionTriggered())); 18 | 19 | QObject::connect(ui->actionOpen, SIGNAL(triggered()), 20 | this, SLOT(onOpenActionTriggered())); 21 | } 22 | 23 | void MainWindow::onSaveActionTriggered() 24 | { 25 | const auto file_path = QFileDialog::getSaveFileName(this, 26 | tr("Save Text File"), 27 | QApplication::applicationDirPath(), 28 | "Text files (*.txt)"); 29 | QFile file(file_path); 30 | if(file.open(QIODevice::WriteOnly)) 31 | { 32 | QTextStream text_stream(&file); 33 | const auto current_text = ui->plainTextEdit->toPlainText(); 34 | text_stream << current_text; 35 | } 36 | } 37 | 38 | void MainWindow::onOpenActionTriggered() 39 | { 40 | const auto file_path = QFileDialog::getOpenFileName(this, 41 | tr("Open Text File"), 42 | QApplication::applicationDirPath(), 43 | "Text files (*.txt)"); 44 | QFile file(file_path); 45 | if(file.open(QIODevice::ReadWrite)) 46 | { 47 | QTextStream text_stream(&file); 48 | const auto text = text_stream.readAll(); 49 | ui->plainTextEdit->setPlainText(text); 50 | } 51 | } 52 | 53 | MainWindow::~MainWindow() 54 | { 55 | delete ui; 56 | } 57 | -------------------------------------------------------------------------------- /PacktCppByExample-master/7_Text_Editor/ui/mainwindow.ui: -------------------------------------------------------------------------------- 1 | 2 | 3 | MainWindow 4 | 5 | 6 | 7 | 0 8 | 0 9 | 524 10 | 347 11 | 12 | 13 | 14 | MainWindow 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 0 27 | 0 28 | 524 29 | 17 30 | 31 | 32 | 33 | 34 | File 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | Open 45 | 46 | 47 | Ctrl+O 48 | 49 | 50 | 51 | 52 | Save 53 | 54 | 55 | Ctrl+S 56 | 57 | 58 | 59 | 60 | 61 | 62 | -------------------------------------------------------------------------------- /PacktCppByExample-master/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.11 FATAL_ERROR) 2 | 3 | project(CppByExample) 4 | 5 | add_subdirectory(1_3_Basic_Cpp_Syntax) 6 | add_subdirectory(2_Virtual_Die) 7 | add_subdirectory(3_Data_Algorithms) 8 | add_subdirectory(4_Classes_Structures) 9 | add_subdirectory(5_Library_Management) 10 | add_subdirectory(6_First_GUI) 11 | add_subdirectory(7_Text_Editor) -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # C++ Programming By Example [Video] 2 | This is the code repository for [C++ Programming By Example [Video]](https://www.packtpub.com/application-development/c-programming-example-video?utm_source=github&utm_medium=repository&utm_campaign=9781788395595), published by [Packt](https://www.packtpub.com/?utm_source=github). It contains all the supporting project files necessary to work through the video course from start to finish. 3 | ## About the Video Course 4 | C++ is a flexible and generic language that offers a wide range of benefits with key strengths being software infrastructure and resource-constrained applications. This course is an introductory guide to C++ that will help you learn the language through multiple hands-on examples. 5 | You’ll begin by diving into the C++ basics, syntax, and generic programming features. We’ll then move on to using data structures and algorithms with C++. Next, you’ll delve into the object-oriented features of C++ with another practical example. 6 | Finally, you’ll further enhance your C++ programming skills by creating multiple GUI, desktop applications using Qt5. By the end of this course, you will have gained knowledge of core programming concepts in C++, and how to implement them effectively. 7 | 8 |

What You Will Learn

9 |
10 |
    11 |
  • C++ syntax and industry-standard style 12 |
  • Develop and architect C++ apps in a modular and maintainable way 13 |
  • Implement the use of modern language features that make code readable and concise 14 |
  • Develop efficient and well-designed applications 15 |
  • Develop a simple yet fun desktop GUI application using the Qt5 framework
16 | 17 | ## Instructions and Navigation 18 | ### Assumed Knowledge 19 | To fully benefit from the coverage included in this course, you will need:
20 | This course is designed for professionals who would like to learn the C++ programming language practically and quickly. Any sort of programming experience would be helpful, but not mandatory. 21 | ### Technical Requirements 22 | This course has the following software requirements:
23 | Latest C++ Compiler 24 | 25 | ## Related Products 26 | * [C++17 STL Solutions [Video]](https://www.packtpub.com/application-development/c17-stl-solutions-video?utm_source=github&utm_medium=repository&utm_campaign=9781789535273) 27 | 28 | * [Practical Windows Penetration Testing [Video]](https://www.packtpub.com/networking-and-servers/practical-windows-penetration-testing-video?utm_source=github&utm_medium=repository&utm_campaign=9781788396653) 29 | 30 | * [Machine Learning for Android App development Using ML Kit [Video]](https://www.packtpub.com/application-development/machine-learning-android-app-development-using-ml-kit-video?utm_source=github&utm_medium=repository&utm_campaign=9781789539875) 31 | 32 | --------------------------------------------------------------------------------