From 6ae92c3768c860609d63a30395414d3c8a7aa924 Mon Sep 17 00:00:00 2001 From: Ivsucram Date: Wed, 30 Oct 2024 07:36:34 +0800 Subject: [PATCH] Tutorial 8 extra --- T8Q1-1/CMakeLists.txt | 7 +++++++ T8Q1-1/main.cpp | 39 +++++++++++++++++++++++++++++++++++ T8Q1-1/main.h | 23 +++++++++++++++++++++ T8Q1/main.cpp | 5 ----- T8Q2/main.cpp | 4 ++-- T8Q2_1/main.h | 47 +++++++++++++++++++++++++++++++++++++++++++ T8Q2_2/CMakeLists.txt | 13 ++++++++++++ T8Q2_2/Polygon.cpp | 31 ++++++++++++++++++++++++++++ T8Q2_2/Polygon.h | 33 ++++++++++++++++++++++++++++++ T8Q2_2/Rectangle.cpp | 14 +++++++++++++ T8Q2_2/Rectangle.h | 17 ++++++++++++++++ T8Q2_2/Triangle.cpp | 14 +++++++++++++ T8Q2_2/Triangle.h | 17 ++++++++++++++++ T8Q2_2/main.cpp | 24 ++++++++++++++++++++++ T8Q2_2/main.h | 12 +++++++++++ 15 files changed, 293 insertions(+), 7 deletions(-) create mode 100644 T8Q1-1/CMakeLists.txt create mode 100644 T8Q1-1/main.cpp create mode 100644 T8Q1-1/main.h create mode 100644 T8Q2_1/main.h create mode 100644 T8Q2_2/CMakeLists.txt create mode 100644 T8Q2_2/Polygon.cpp create mode 100644 T8Q2_2/Polygon.h create mode 100644 T8Q2_2/Rectangle.cpp create mode 100644 T8Q2_2/Rectangle.h create mode 100644 T8Q2_2/Triangle.cpp create mode 100644 T8Q2_2/Triangle.h create mode 100644 T8Q2_2/main.cpp create mode 100644 T8Q2_2/main.h diff --git a/T8Q1-1/CMakeLists.txt b/T8Q1-1/CMakeLists.txt new file mode 100644 index 0000000..ae7ee96 --- /dev/null +++ b/T8Q1-1/CMakeLists.txt @@ -0,0 +1,7 @@ +cmake_minimum_required(VERSION 3.29) +project(T8Q1_1) + +set(CMAKE_CXX_STANDARD 20) + +add_executable(T8Q1_1 main.cpp + main.h) diff --git a/T8Q1-1/main.cpp b/T8Q1-1/main.cpp new file mode 100644 index 0000000..a1e7345 --- /dev/null +++ b/T8Q1-1/main.cpp @@ -0,0 +1,39 @@ +// +// Created by Marcus Vinícius de Carvalho. +// + +#include "main.h" + +// Learn about const class objects and member functions here: https://www.learncpp.com/cpp-tutorial/const-class-objects-and-const-member-functions/ + +void BubbleSort::sort() const { + for (int i = _size - 2; i >= 0; i--) { + for (int j = 0; j <= i; j++) { + if (_numArray[j] > _numArray[j + 1]) { + const int t = _numArray[j]; + _numArray[j] = _numArray[j + 1]; + _numArray[j + 1] = t; + } + } + } +}; + +int main(int argc, const char * argv[]) { + int a[100], n, i; + + std::cout << "Enter number of Integers elements to be sorted: "; + std::cin >> n; + + for (i = 0; i < n; i++) { + std::cout << "Enter integer value for elements no." << i + 1 << ": "; + std::cin >> a[i]; + } + + const BubbleSort b(a, n); + b.sort(); + + std::cout << "\nFinally sorted array is: "; + for (i = 0; i < n; i++) { + std::cout << a[i] << " "; + } +} \ No newline at end of file diff --git a/T8Q1-1/main.h b/T8Q1-1/main.h new file mode 100644 index 0000000..8e81861 --- /dev/null +++ b/T8Q1-1/main.h @@ -0,0 +1,23 @@ +// +// Created by ivs on 10/30/24. +// + +#ifndef MAIN_H +#define MAIN_H + +#include +using namespace std; + +class BubbleSort { + int _size; + int *_numArray; + +public: + BubbleSort(int nums[], const int size) : _size(size), _numArray(nums) {}; + ~BubbleSort() = default; + void sort() const; +}; + +int main(int argc, const char * argv[]); + +#endif //MAIN_H diff --git a/T8Q1/main.cpp b/T8Q1/main.cpp index 2e432b0..3789809 100644 --- a/T8Q1/main.cpp +++ b/T8Q1/main.cpp @@ -17,11 +17,6 @@ class BubbleSort { _numArray = nums; } - ~BubbleSort() { - // This is a destructor. It was introduced in C++ 20. If you are programming on an older version, you will - // have to manually clean the memory - } - void sort() { int t; for (int i = _size - 2; i >= 0; i--) { diff --git a/T8Q2/main.cpp b/T8Q2/main.cpp index f0324c1..c4e1d09 100644 --- a/T8Q2/main.cpp +++ b/T8Q2/main.cpp @@ -9,7 +9,7 @@ using namespace std; enum KindOfPolygon {POLY_PLAIN, POLY_RECT, POLY_TRIANG}; -inline string StringKindofPolygon[] = {"POLY_PLAIN", "POLY_RECT", "POLY_TRIANG"}; +string StringKindofPolygon[] = {"POLY_PLAIN", "POLY_RECT", "POLY_TRIANG"}; class Polygon { @@ -20,7 +20,7 @@ protected: KindOfPolygon polytype; public: - Polygon(string name, float width, float height) : name(std::move(name)), width(width), height(height) { + Polygon(string name, float width, float height) : name(name), width(width), height(height) { polytype = POLY_PLAIN; } diff --git a/T8Q2_1/main.h b/T8Q2_1/main.h new file mode 100644 index 0000000..cdf380e --- /dev/null +++ b/T8Q2_1/main.h @@ -0,0 +1,47 @@ +// +// Created by Marcus Vinicius de Carvalho +// + +#ifndef MAIN_H +#define MAIN_H + +#include + +using namespace std; +enum KindOfPolygon {POLY_PLAIN, POLY_RECT, POLY_TRIANG}; + +inline string StringKindofPolygon[] = {"POLY_PLAIN", "POLY_RECT", "POLY_TRIANG"}; + +class Polygon { +protected: + string name; + float width; + float height; + KindOfPolygon polytype; + +public: + virtual ~Polygon() = default; + + Polygon(string name, float width, float height); + [[nodiscard]] KindOfPolygon getPolytype() const; + void setPolytype(KindOfPolygon polytype); + string getName(); + virtual float calculateArea(); + void printWidthHeight() const; +}; + +class Rectangle final : public Polygon { +public: + Rectangle(string name, float width, float height); + float calculateArea() override; +}; + +class Triangle final : public Polygon { +public: + Triangle(string name, float width, float height); + float calculateArea() override; +}; + +int main(int argc, const char * argv[]); + +#endif //MAIN_H diff --git a/T8Q2_2/CMakeLists.txt b/T8Q2_2/CMakeLists.txt new file mode 100644 index 0000000..a7980cb --- /dev/null +++ b/T8Q2_2/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 3.29) +project(T8Q2_2) + +set(CMAKE_CXX_STANDARD 20) + +add_executable(T8Q2_2 main.cpp + Polygon.cpp + Polygon.h + Rectangle.cpp + Rectangle.h + Triangle.cpp + main.h + Triangle.h) diff --git a/T8Q2_2/Polygon.cpp b/T8Q2_2/Polygon.cpp new file mode 100644 index 0000000..85a882a --- /dev/null +++ b/T8Q2_2/Polygon.cpp @@ -0,0 +1,31 @@ +// +// Created by Marcus Vinícius de Carvalho +// + +#include "Polygon.h" +#include + +Polygon::Polygon(string name, const float width, const float height) + : name(std::move(name)), width(width), height(height) { + polytype = POLY_PLAIN; +} + +KindOfPolygon Polygon::getPolytype() const { + return polytype; +} + +void Polygon::setPolytype(KindOfPolygon polytype) { + this->polytype = polytype; +} + +string Polygon::getName() { + return name; +} + +float Polygon::calculateArea() { + return 0.0; +} + +void Polygon::printWidthHeight() const { + cout << "Name: " << name << "; Width: " << width << "; Height: " << height << endl; +}; \ No newline at end of file diff --git a/T8Q2_2/Polygon.h b/T8Q2_2/Polygon.h new file mode 100644 index 0000000..21429a6 --- /dev/null +++ b/T8Q2_2/Polygon.h @@ -0,0 +1,33 @@ +// +// Created by Marcus Vinícius de Carvalho +// + +#ifndef POLYGON_H +#define POLYGON_H + +#include + +using namespace std; +enum KindOfPolygon {POLY_PLAIN, POLY_RECT, POLY_TRIANGLE}; + +inline string StringKindofPolygon[] = {"POLY_PLAIN", "POLY_RECT", "POLY_TRIANGLE"}; + +class Polygon { +protected: + string name; + float width; + float height; + KindOfPolygon polytype; + +public: + virtual ~Polygon() = default; + + Polygon(string name, float width, float height); + [[nodiscard]] KindOfPolygon getPolytype() const; + void setPolytype(KindOfPolygon polytype); + string getName(); + virtual float calculateArea(); + void printWidthHeight() const; +}; + +#endif //POLYGON_H diff --git a/T8Q2_2/Rectangle.cpp b/T8Q2_2/Rectangle.cpp new file mode 100644 index 0000000..adea94b --- /dev/null +++ b/T8Q2_2/Rectangle.cpp @@ -0,0 +1,14 @@ +// +// Created by Marcus Vinícius de Carvalho +// + +#include "Rectangle.h" + +Rectangle::Rectangle(string name, const float width, const float height) + : Polygon(std::move(name), width, height) { + polytype = POLY_RECT; +} + +float Rectangle::calculateArea() { + return width * height; +} \ No newline at end of file diff --git a/T8Q2_2/Rectangle.h b/T8Q2_2/Rectangle.h new file mode 100644 index 0000000..a6d17d1 --- /dev/null +++ b/T8Q2_2/Rectangle.h @@ -0,0 +1,17 @@ +// +// Created by Marcus Vinícius de Carvalho +// + +#ifndef RECTANGLE_H +#define RECTANGLE_H + +#include "Polygon.h" +#include + +class Rectangle final : public Polygon { +public: + Rectangle(string name, float width, float height); + float calculateArea() override; +}; + +#endif //RECTANGLE_H diff --git a/T8Q2_2/Triangle.cpp b/T8Q2_2/Triangle.cpp new file mode 100644 index 0000000..f396cc6 --- /dev/null +++ b/T8Q2_2/Triangle.cpp @@ -0,0 +1,14 @@ +// +// Created by Marcus Vinícius de Carvalho +// + +#include "Triangle.h" + +Triangle::Triangle(string name, const float width, const float height) + : Polygon(std::move(name), width, height) { + polytype = POLY_TRIANGLE; +} + +float Triangle::calculateArea() { + return width * height * 0.5f; +} \ No newline at end of file diff --git a/T8Q2_2/Triangle.h b/T8Q2_2/Triangle.h new file mode 100644 index 0000000..ae97f60 --- /dev/null +++ b/T8Q2_2/Triangle.h @@ -0,0 +1,17 @@ +// +// Created by Marcus Vinícius de Carvalho +// + +#ifndef TRIANGLE_H +#define TRIANGLE_H + +#include "Polygon.h" +#include + +class Triangle final : public Polygon { +public: + Triangle(string name, float width, float height); + float calculateArea() override; +}; + +#endif //TRIANGLE_H diff --git a/T8Q2_2/main.cpp b/T8Q2_2/main.cpp new file mode 100644 index 0000000..e8c22c9 --- /dev/null +++ b/T8Q2_2/main.cpp @@ -0,0 +1,24 @@ +// +// Created by Marcus Vinicius de Carvalho +// + +#include "main.h" +#include + +class TestPolygon { +public: + static void printArea(Polygon& poly) { + const float area = poly.calculateArea(); + cout << "The area of the " << StringKindofPolygon[poly.getPolytype()] << " is " << area << endl; + } +}; + +int main(int argc, const char * argv[]) { + Rectangle rectangle1("Rectangle1", 3.0f, 4.0f); + rectangle1.printWidthHeight(); + TestPolygon::printArea(rectangle1); + + Triangle triangle1("Triangle1", 3.0f, 4.0f); + triangle1.printWidthHeight(); + TestPolygon::printArea(triangle1); +} \ No newline at end of file diff --git a/T8Q2_2/main.h b/T8Q2_2/main.h new file mode 100644 index 0000000..472bc8f --- /dev/null +++ b/T8Q2_2/main.h @@ -0,0 +1,12 @@ +// +// Created by Marcus Vinícius de Carvalho +// + +#ifndef MAIN_H +#define MAIN_H + +#include "Polygon.h" +#include "Rectangle.h" +#include "Triangle.h" + +#endif //MAIN_H