├── .gitignore ├── .snapcraft └── travis_snapcraft.cfg ├── .travis.yml ├── CMakeLists.txt ├── README.md ├── _config.yml ├── answers ├── solution-1.cpp └── solution-2.cpp ├── appveyor.yml ├── check.cpp ├── checker.hpp ├── create-exercise.cpp ├── exercise.hpp ├── exercises ├── exercise-1 │ ├── README.md │ ├── solution.cpp │ └── tester.cpp ├── exercise-2 │ ├── README.md │ ├── solution.cpp │ └── tester.cpp └── exercise-3 │ ├── README.md │ ├── solution.cpp │ └── tester.cpp ├── flags.hpp ├── mingw-to-path.bat ├── snap ├── .snapcraft │ └── state └── snapcraft.yaml └── testcpp17.cpp /.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Compiled Object files 5 | *.slo 6 | *.lo 7 | *.o 8 | *.obj 9 | check 10 | create-exercise 11 | 12 | # Precompiled Headers 13 | *.gch 14 | *.pch 15 | 16 | # Compiled Dynamic libraries 17 | *.so 18 | *.dylib 19 | *.dll 20 | 21 | # Fortran module files 22 | *.mod 23 | *.smod 24 | 25 | # Compiled Static libraries 26 | *.lai 27 | *.la 28 | *.a 29 | *.lib 30 | 31 | # Executables 32 | *.exe 33 | *.out 34 | *.app 35 | 36 | 37 | # VSCode 38 | .vscode/* 39 | 40 | # CLion 41 | .idea/* 42 | 43 | # CMake 44 | CMakeLists.txt.user 45 | CMakeCache.txt 46 | CMakeFiles 47 | CMakeScripts 48 | Testing 49 | Makefile 50 | cmake_install.cmake 51 | install_manifest.txt 52 | compile_commands.json 53 | CTestTestfile.cmake 54 | _deps 55 | -------------------------------------------------------------------------------- /.snapcraft/travis_snapcraft.cfg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kerolloz/oop-exercises/84b65305cfa48f02dbae9c1d381c9cd7231dfbc0/.snapcraft/travis_snapcraft.cfg -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | dist: xenial 2 | language: cpp 3 | addons: 4 | apt: 5 | sources: 6 | - ubuntu-toolchain-r-test 7 | packages: 8 | - gcc-8 9 | - g++-8 10 | before_script: 11 | - export CC='gcc-8' CXX='g++-8' TRAVIS_COMPILER='g++-8' CXX_FOR_BUILD='g++-8' 12 | script: cmake . && make 13 | after_success: 14 | - openssl aes-256-cbc -K $encrypted_4ce617354043_key -iv $encrypted_4ce617354043_iv 15 | -in .snapcraft/travis_snapcraft.cfg -out .snapcraft/snapcraft.cfg -d 16 | sudo: required 17 | services: 18 | - docker 19 | deploy: 20 | 'on': 21 | branch: master 22 | provider: script 23 | script: docker run -v $(pwd):$(pwd) -t snapcore/snapcraft sh -c " 24 | sudo -E apt-get -yq --no-install-suggests --no-install-recommends install software-properties-common -y; 25 | sudo -E apt-add-repository -y "ppa:ubuntu-toolchain-r/test"; 26 | sudo apt-get update -y; 27 | cd $(pwd) && export CC='gcc-8' CXX='g++-8' CXX_FOR_BUILD='g++-8' && snapcraft && snapcraft push *.snap --release edge" 28 | skip_cleanup: true 29 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.0) 2 | project(oop-exercises) 3 | 4 | set(CMAKE_CXX_STANDARD 17) 5 | set(CMAKE_RUNTIME_OUTPUT_DIRECTORY bin/) 6 | 7 | #add_executable(check check.cpp) 8 | #add_executable(create-exercise create-exercise.cpp) 9 | add_executable(cpp17 testcpp17.cpp) 10 | 11 | target_link_libraries(cpp17 stdc++fs) 12 | 13 | #install(TARGETS check create-exercise DESTINATION bin) 14 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # C++ Object-Oriented Programming exercises 2 | [![Snap Status](https://build.snapcraft.io/badge/kerolloz/oop-exercises.svg)](https://build.snapcraft.io/user/kerolloz/oop-exercises) [![OOP Exercises](https://snapcraft.io/oop-exercises/badge.svg)](https://snapcraft.io/oop-exercises) [![Build Status](https://travis-ci.com/kerolloz/oop-exercises.svg?branch=master)](https://travis-ci.com/kerolloz/oop-exercises) [![Build status](https://ci.appveyor.com/api/projects/status/m6hoai6ry1yc9793?svg=true)](https://ci.appveyor.com/project/kerolloz/oop-exercises) 3 | >Coding challenges for those who want to learn Object Oriented Programming in C++ :fire: 4 | 5 | ## Contents 6 | - [Installation](#installation) 7 | - [Windows](#windows) 8 | - [Linux](#linux) 9 | - [Usage](#usage) 10 | - [Exercises](/exercises) 11 | 12 | ## Installation 13 | ### Linux 14 | [![Get it from the Snap Store](https://snapcraft.io/static/images/badges/en/snap-store-black.svg)](https://snapcraft.io/oop-exercises) 15 | ### Windows 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /_config.yml: -------------------------------------------------------------------------------- 1 | theme: jekyll-theme-cayman 2 | title: OOP Exercises 3 | plugins: 4 | - jemoji -------------------------------------------------------------------------------- /answers/solution-1.cpp: -------------------------------------------------------------------------------- 1 | 2 | class Point 3 | { 4 | private: 5 | int x, y; 6 | int square(int number){ 7 | return number * number; 8 | } 9 | public: 10 | Point(int x, int y){ 11 | this->x = x; 12 | this->y = y; 13 | } 14 | int getDistance(Point p2){ 15 | return sqrt(square(p2.x - this->x) + square(p2.y - this->y)); 16 | } 17 | }; 18 | 19 | -------------------------------------------------------------------------------- /answers/solution-2.cpp: -------------------------------------------------------------------------------- 1 | class Time{ 2 | 3 | private: 4 | int hours, min, second; 5 | 6 | public: 7 | Time() : hours(0), min(0), second(0) {} 8 | Time(int hours, int min, int second){ 9 | this->hours = hours; 10 | this->min = min; 11 | this->second = second; 12 | } 13 | 14 | void Display() const { 15 | cout << hours << ":" << min << ":" << second << endl; 16 | } 17 | 18 | int getHours() { return this->hours; } 19 | int getMin() { return this->min; } 20 | int getSec() { return this->second; } 21 | 22 | void Add(Time t1, Time t2){ 23 | int H, M, S; 24 | H = M = S = 0; 25 | 26 | M += (t1.second + t2.second) / 60; 27 | S += (t1.second + t2.second) % 60; 28 | 29 | H += (t1.min + t2.min) / 60; 30 | M += (t1.min + t2.min) % 60; 31 | 32 | H += t1.hours + t2.hours; 33 | 34 | this->hours = H, this->min = M, this->second = S; 35 | } 36 | }; 37 | -------------------------------------------------------------------------------- /appveyor.yml: -------------------------------------------------------------------------------- 1 | version: 1.1.{build} 2 | image: Visual Studio 2017 3 | install: 4 | - call "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvars32.bat" 5 | before_build: 6 | - cmake . 7 | build_script: 8 | - dir 9 | - msbuild ALL_BUILD.vcxproj /std:c++latest 10 | after_build: 11 | - xcopy /s exercises bin\exercises\ 12 | - copy mingw-to-path.bat bin\ 13 | - xcopy /s answers bin\answers\ 14 | 15 | artifacts: 16 | - path: bin/ 17 | name: oop-exercises 18 | 19 | deploy: 20 | - provider: GitHub 21 | artifacts: oop-exercises 22 | auth_token: 23 | secure: aIiYr0MeanCLsD/pLbbRO0XtDuSTpRor3ABjJMGPqjvlmLDvAl49TG5VtphFXSnF 24 | -------------------------------------------------------------------------------- /check.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "checker.hpp" 5 | #define endl '\n' 6 | 7 | using namespace std; 8 | 9 | template < typename T > std::string to_string( const T& n ){ 10 | std::ostringstream stm ; 11 | stm << n ; 12 | return stm.str() ; 13 | } 14 | 15 | void verify(int questionNumber) { 16 | string exercisesDir = "exercises/"; 17 | string questionDir = "exercise-" + to_string(questionNumber) + "/"; 18 | Checker c(exercisesDir + questionDir + "tester.cpp"); 19 | cout << "Testing Question #" << questionNumber << ":" << endl; 20 | if(Compilation::SUCCESS == c.compile() && Test::SUCCESS == c.test()) 21 | cout << "\tCORRECT Answer!\n"; 22 | else 23 | cout << "\tWRONG Answer.\n" 24 | "\tTry Harder!\n"; 25 | } 26 | 27 | int main (int argc, char ** argv) 28 | { 29 | string input; 30 | if(argc == 1){ 31 | cout << "Please enter the number(s) of the question(s) you want to verify \n" 32 | "comma separated e.g: 1,2,3\n" 33 | ">> "; 34 | cin >> input; 35 | 36 | }else if(argc == 2){ 37 | input = argv[1]; 38 | } 39 | 40 | char * pch = strtok (const_cast(input.c_str()),","); 41 | 42 | while (pch != nullptr) { 43 | verify(atoi(pch)); 44 | pch = strtok (nullptr, ","); 45 | } 46 | 47 | system("pause"); 48 | 49 | return 0; 50 | } 51 | 52 | -------------------------------------------------------------------------------- /checker.hpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "flags.hpp" 4 | 5 | using namespace std; 6 | 7 | class Checker{ 8 | private: 9 | string tester; 10 | string compilerExecutable; 11 | string cwd; 12 | std::string get_cwd(){ 13 | // returns current working directory 14 | char temp[1000]; 15 | return ( getcwd(temp, sizeof(temp)) ? std::string( temp ) : std::string("") ); 16 | } 17 | public: 18 | 19 | explicit Checker(string test) { 20 | tester = move(test); 21 | cwd = get_cwd(); 22 | compilerExecutable = "g++"; 23 | } 24 | 25 | Compilation compile(){ 26 | std::string command = compilerExecutable + " " + cwd + "/" + tester + " -o " + cwd + "/test"; 27 | int compilationState = system(command.c_str()); 28 | return (compilationState)? Compilation::FAILED : Compilation::SUCCESS ; 29 | } 30 | 31 | Test test(){ 32 | std::string command = "./test"; 33 | int returnValue = system(command.c_str()); 34 | system("rm ./test"); // remove the test binary after testing 35 | return (returnValue == 0)? Test::SUCCESS : Test::FAILED; 36 | 37 | } 38 | 39 | }; -------------------------------------------------------------------------------- /create-exercise.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "exercise.hpp" 3 | #define endl '\n' 4 | 5 | using namespace std; 6 | 7 | 8 | 9 | int main (int argc, char** argv) { 10 | 11 | Exercise exercise; 12 | switch (argc){ 13 | case 1: 14 | break; 15 | case 2: 16 | exercise = Exercise(atoi(argv[1])); 17 | break; 18 | case 3: 19 | exercise = Exercise(atoi(argv[1]), atoi(argv[2])); 20 | break; 21 | default: 22 | cout << "ERROR max parameters: 2" << endl; 23 | return 0; 24 | } 25 | 26 | exercise.create(); 27 | system("pause"); 28 | return 0; 29 | } -------------------------------------------------------------------------------- /exercise.hpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | template < typename T > std::string to_string( const T& n ){ 9 | std::ostringstream stm ; 10 | stm << n ; 11 | return stm.str() ; 12 | } 13 | 14 | bool IsPathExist(const std::string &s) 15 | { 16 | struct stat buffer; 17 | return (stat (s.c_str(), &buffer) == 0); 18 | } 19 | 20 | using namespace std; 21 | 22 | class Exercise{ 23 | private: 24 | int from; 25 | int to; 26 | string folderNameTemplate; 27 | 28 | void createExercise(){ 29 | if(!IsPathExist("exercises/")) 30 | system("mkdir exercises/"); 31 | 32 | chdir("exercises/"); 33 | string command = "mkdir " + folderNameTemplate + to_string(from); 34 | 35 | if(system(command.c_str()) == 0) { 36 | string filesCreationCommand = 37 | "cd " + folderNameTemplate + to_string(from) + "/ &&" 38 | "echo '#include \"solution.cpp\"' > tester.cpp &&" 39 | "echo '# Exercise " + to_string(from) + "' > README.md &&" 40 | "touch solution.cpp && cd .."; 41 | system(filesCreationCommand.c_str()); 42 | 43 | cout << folderNameTemplate << from << " created!" << endl; 44 | cout 45 | << "\tCreated solution.cpp" << endl 46 | << "\tCreated tester.cpp" << endl 47 | << "\tCreated README.md" << endl; 48 | } 49 | } 50 | 51 | 52 | public: 53 | 54 | Exercise(){ 55 | folderNameTemplate = "exercise-"; 56 | this->from =this->to = calculateNextExerciseNumber("exercises/"); 57 | } 58 | Exercise(int num){ 59 | this->from =this->to = num; 60 | } 61 | Exercise(int from, int to){ 62 | if(from > to) 63 | swap(from, to); 64 | this->from = from; 65 | this->to = to; 66 | } 67 | 68 | bool isExerciseDir(string dirName){ 69 | if(dirName.size() <= folderNameTemplate.size()) return false; 70 | for(size_t i = 0; i < folderNameTemplate.size(); i++) 71 | { 72 | if(folderNameTemplate[i] != dirName[i]) return false; 73 | } 74 | return true; 75 | 76 | } 77 | int calculateNextExerciseNumber(const char *path) { 78 | // cout << path << endl; 79 | struct dirent *entry; 80 | DIR *dir = opendir(path); 81 | 82 | if (dir == NULL) { 83 | return 1; 84 | } 85 | int maxExerciseNumber = 0; 86 | while ((entry = readdir(dir)) != NULL) { 87 | string fileName(entry->d_name); 88 | // cout << fileName << endl; 89 | if(isExerciseDir(fileName)){ 90 | string exerciseNumber = fileName.substr(folderNameTemplate.size(), fileName.size()); 91 | maxExerciseNumber = max(maxExerciseNumber, atoi(exerciseNumber.c_str())); 92 | } 93 | 94 | } 95 | closedir(dir); 96 | return maxExerciseNumber+1; 97 | } 98 | void create(){ 99 | while(from <= to){ 100 | createExercise(); 101 | from++; 102 | } 103 | } 104 | 105 | 106 | }; -------------------------------------------------------------------------------- /exercises/exercise-1/README.md: -------------------------------------------------------------------------------- 1 | # Exercise 1 2 | ## Create a class called Point 3 | This class should have 2 integers(x, y) as private data members.
4 | The constructor should set the values for x and y through parameters.
5 | The class should have a public method called getDistance.
6 | That takes 1 parameter a Point and returns the distance between the 2 points
7 | ![equation](https://latex.codecogs.com/gif.latex?%5Cinline%20%5Cdpi%7B150%7D%20%5Csqrt%7B%5Cleft%20%28%20x_2%20-%20x_1%20%5Cright%20%29%5E%7B2%7D%20+%20%5Cleft%20%28%20y_2%20-%20y_1%20%5Cright%20%29%5E%7B2%7D%7D) 8 | -------------------------------------------------------------------------------- /exercises/exercise-1/solution.cpp: -------------------------------------------------------------------------------- 1 | 2 | class Point 3 | { 4 | 5 | 6 | }; 7 | 8 | -------------------------------------------------------------------------------- /exercises/exercise-1/tester.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "solution.cpp" 3 | 4 | #define endl '\n' 5 | 6 | int main (){ 7 | Point p1(5, 5); 8 | Point p2(7, 7); 9 | if(p1.getDistance(p2) == int(sqrt(8))) 10 | return 0; 11 | else 12 | return 1; 13 | } 14 | -------------------------------------------------------------------------------- /exercises/exercise-2/README.md: -------------------------------------------------------------------------------- 1 | # Exercise 2 2 | ## Create a class called time 3 | has separate int member data for hours, minutes, and seconds.(make thim private)
4 | One constructor should initialize this data to 0
5 | Another constructor should initialize it to fixed values.
6 | Another member function should display it, in 11:59:59 format.
7 | Make three member function to return every member data name them ``getHours``, ``getMin`` & ``getSec``
8 | 9 | The final member function should add two objects of type time passed as arguments.
10 | Make The appropriate member function const.
11 | -------------------------------------------------------------------------------- /exercises/exercise-2/solution.cpp: -------------------------------------------------------------------------------- 1 | using namespace std; 2 | 3 | 4 | class Time{ 5 | 6 | // Start conding... 7 | }; 8 | -------------------------------------------------------------------------------- /exercises/exercise-2/tester.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "solution.cpp" 3 | using namespace std; 4 | 5 | int main() { 6 | 7 | Time t1(2, 30, 35), t2(1, 59, 25); 8 | Time t3; 9 | 10 | t3.Add(t1, t2); 11 | 12 | if(t3.getHours() == 4 && t3.getMin() == 30 && !t3.getSec()) 13 | return 0; 14 | else 15 | return 1; 16 | } -------------------------------------------------------------------------------- /exercises/exercise-3/README.md: -------------------------------------------------------------------------------- 1 | # Exercise 3 2 | -------------------------------------------------------------------------------- /exercises/exercise-3/solution.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kerolloz/oop-exercises/84b65305cfa48f02dbae9c1d381c9cd7231dfbc0/exercises/exercise-3/solution.cpp -------------------------------------------------------------------------------- /exercises/exercise-3/tester.cpp: -------------------------------------------------------------------------------- 1 | #include "solution.cpp" 2 | -------------------------------------------------------------------------------- /flags.hpp: -------------------------------------------------------------------------------- 1 | enum class Compilation{ 2 | SUCCESS, 3 | FAILED 4 | }; 5 | 6 | enum class Test{ 7 | SUCCESS, 8 | FAILED 9 | }; 10 | -------------------------------------------------------------------------------- /mingw-to-path.bat: -------------------------------------------------------------------------------- 1 | setx path "%path%;C:\Program Files (x86)\CodeBlocks\MinGW\bin\;C:\Mingw\bin" 2 | -------------------------------------------------------------------------------- /snap/.snapcraft/state: -------------------------------------------------------------------------------- 1 | !GlobalState 2 | assets: 3 | build-packages: [] 4 | build-snaps: [] 5 | -------------------------------------------------------------------------------- /snap/snapcraft.yaml: -------------------------------------------------------------------------------- 1 | name: oop-exercises 2 | version: git 3 | summary: OOP Exercises in C++ 4 | description: Exercises for those who to learn Object-Oriented Programming in C++ 5 | grade: stable 6 | confinement: devmode 7 | 8 | apps: 9 | check: 10 | command: bin/check 11 | plugs: [personal-files] 12 | 13 | create-exercise: 14 | command: bin/create-exercise 15 | plugs: [personal-files] 16 | 17 | parts: 18 | app: 19 | source: https://github.com/kerolloz/oop-exercises.git 20 | plugin: cmake 21 | stage-packages: [g++] # needed inside the container 22 | -------------------------------------------------------------------------------- /testcpp17.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | 4 | 5 | namespace fs = std::filesystem; 6 | 7 | int main() 8 | { 9 | // create files of different kinds 10 | fs::create_directory("sandbox"); 11 | return 0; 12 | } --------------------------------------------------------------------------------