Rectangle Struct
Define a Rectangle struct with member variables width and height. Add member functions float get_area() and float get_perimeter() to the struct to calculate the area and perimeter of the rectangle, respectively. Demonstrate them working.
- Mangaing class: create a team class with a vector holding a list of player class instances (with the variables name, age, and height). Add a function "void add_player" that adds a player to the team, a
bool remove_player(name)that removes a player from the team based on their name, and ateam overview that prints a graph like this [each line has name | age | info, etc. \n].
Hint
Define the member functions inside the struct for easy access to the member variables.
Answer
#include <iostream>
struct Rectangle {
float width;
float height;
float get_area() {
return width * height;
}
float get_perimeter() {
return 2 * (width + height);
}
};
int main() {
Rectangle myRectangle;
std::cout << "Enter the width of the rectangle: ";
std::cin >> myRectangle.width;
std::cout << "Enter the height of the rectangle: ";
std::cin >> myRectangle.height;
std::cout << "Area of the rectangle: " << myRectangle.get_area() << std::endl;
std::cout << "Perimeter of the rectangle: " << myRectangle.get_perimeter() << std::endl;
return 0;
}