TU-Programmieren_2/exercise6/task1.main.cpp
2025-04-09 10:22:44 +02:00

58 lines
1.9 KiB
C++

/// @file
/// @brief Task1: "single-file" excutable C++ program
#include "task1.hierarchy.hpp" // task1::Interface, task1::Circle, task1::Rectangle
#include <iostream>
#include <vector> // for std::vector
/// @todo Include standard library headers as needed
/// @todo Implement a main funtion conducting the following tasks in this order:
/// - create an empty vector of base pointers 'std::vector<task1::Interface *>'
/// - dynamically allocate 5 Circles with radii set to 1,2,3,4, and 5
/// - push the pointers to the dynamically allocated circles into the vector
/// - dynamically allocate 5 Squares with length set to 1,2,3,4, and 5
/// - push the pointers to the dynamically allocated squares into the vector
/// - use a loop to iterate over the vector and count how many elements have an area less than 8.0
/// - print the result to the console
/// - use a loop to deallocate all 10 elements in the vector
/// @file
/// @brief Task1: "single-file" excutable C++ program
/// @todo Implement a main funtion conducting the following tasks in this order:
int main() {
// Create an empty vector of base pointers
std::vector<task1::Interface*> shapes;
// Dynamically allocate 5 Circles
for (int i = 1; i <= 5; ++i) {
shapes.push_back(new task1::Circle(i));
std::cout << "malloc - Circle with radius " << i << std::endl;
}
// Dynamically allocate 5 Squares
for (int i = 1; i <= 5; ++i) {
shapes.push_back(new task1::Square(i));
std::cout << "malloc - Square with length " << i << std::endl;
}
// Count elements with area less than 8.0
int count = 0;
for (auto* shape : shapes) {
if (shape->area() < 8.0) {
count++;
}
}
std::cout << "Number of shapes with area less than 8.0: " << count << std::endl;
// Deallocate all elements in the vector
for (auto* shape : shapes) {
delete shape;
std::cout << "free - Shape" << std::endl;
}
return 0;
}