Tutorial 8 extra
This commit is contained in:
parent
8195919a20
commit
6ae92c3768
|
@ -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)
|
|
@ -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] << " ";
|
||||
}
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
//
|
||||
// Created by ivs on 10/30/24.
|
||||
//
|
||||
|
||||
#ifndef MAIN_H
|
||||
#define MAIN_H
|
||||
|
||||
#include <iostream>
|
||||
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
|
|
@ -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--) {
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,47 @@
|
|||
//
|
||||
// Created by Marcus Vinicius de Carvalho
|
||||
//
|
||||
|
||||
#ifndef MAIN_H
|
||||
#define MAIN_H
|
||||
|
||||
#include <string>
|
||||
|
||||
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
|
|
@ -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)
|
|
@ -0,0 +1,31 @@
|
|||
//
|
||||
// Created by Marcus Vinícius de Carvalho
|
||||
//
|
||||
|
||||
#include "Polygon.h"
|
||||
#include <iostream>
|
||||
|
||||
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;
|
||||
};
|
|
@ -0,0 +1,33 @@
|
|||
//
|
||||
// Created by Marcus Vinícius de Carvalho
|
||||
//
|
||||
|
||||
#ifndef POLYGON_H
|
||||
#define POLYGON_H
|
||||
|
||||
#include <string>
|
||||
|
||||
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
|
|
@ -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;
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
//
|
||||
// Created by Marcus Vinícius de Carvalho
|
||||
//
|
||||
|
||||
#ifndef RECTANGLE_H
|
||||
#define RECTANGLE_H
|
||||
|
||||
#include "Polygon.h"
|
||||
#include <string>
|
||||
|
||||
class Rectangle final : public Polygon {
|
||||
public:
|
||||
Rectangle(string name, float width, float height);
|
||||
float calculateArea() override;
|
||||
};
|
||||
|
||||
#endif //RECTANGLE_H
|
|
@ -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;
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
//
|
||||
// Created by Marcus Vinícius de Carvalho
|
||||
//
|
||||
|
||||
#ifndef TRIANGLE_H
|
||||
#define TRIANGLE_H
|
||||
|
||||
#include "Polygon.h"
|
||||
#include <string>
|
||||
|
||||
class Triangle final : public Polygon {
|
||||
public:
|
||||
Triangle(string name, float width, float height);
|
||||
float calculateArea() override;
|
||||
};
|
||||
|
||||
#endif //TRIANGLE_H
|
|
@ -0,0 +1,24 @@
|
|||
//
|
||||
// Created by Marcus Vinicius de Carvalho
|
||||
//
|
||||
|
||||
#include "main.h"
|
||||
#include <iostream>
|
||||
|
||||
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);
|
||||
}
|
|
@ -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
|
Loading…
Reference in New Issue