I am starting to look at C++ and have done an introductory online course. Based on that I am just poking around trying to find my feet but have come across a little issue that I cannot find a solution to (though I'm sure it's extremely simple and perhaps just down to syntax when you know how).
My issue is that I have created a class Rectangle with a number of member functions. One if these member functions is to return the area of the a rectangle. In the code below I have created a number of rectangles and at the end I have a user input to type the rectangle that you want to return the area of.
Currently, it is returning the area of rectangle 'inner' as a fixed selection.
My question is, using my member function getArea, how can I return the area of the rectangle whose name is within 'selection'?
Rectangle.cpp:
// Rectangle.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
int main()
{
// Define rectangles
Rectangle outer{ 2.3,3.4 }; // The values initialise the rectangle to that size
Rectangle inner{ 5.6,7 };
Rectangle holder{1,2};
double Area{};
double udWidth{};
double udHeight{};
char selection[99]{};
std::string test;
// Dimension rectangles
outer.adjWidth ( 10.2 );
outer.adjHeight ( 5.2 );
inner.adjWidth ( 9.6 );
inner.adjHeight ( 4.6 );
std::cout << "Input width of user defined rectangle: " << std::endl;
std::cin >> udWidth;
std::cout << "Input height of the user defined rectangle: " << std::endl;
std::cin >> udHeight;
Rectangle ud{ udWidth,udHeight };
std::cout << "Outer rectangle dimensions: " << outer.getWidth() << " * " << outer.getHeight() << std::endl;
std::cout << "Inner rectangle dimensions: " << inner.getWidth() << " * " << inner.getHeight() << std::endl;
std::cout << "Holder rectangle dimensions: " << holder.getWidth() << " * " << holder.getHeight() << std::endl;
std::cout << "User defined rectangle dimensions: " << ud.getWidth() << " * " << ud.getHeight() << std::endl;
std::cout << "Select a rectangle to view the area " << std::endl;
std::cin >> selection;
double areaRectangle{ inner.getArea() };
std::cout << "The area of the chosen rectangle is " << areaRectangle << std::endl;
system("pause");
return 0;
}
rescource.h
#pragma once
// Declare Class 'Rectangle'
class Rectangle
{
// Private values cannot be altered externally to the class
private:
double _width;
double _height;
// Public values can be altered externally and member functions written to allow private values to be altered
public:
// Initialise rectangle dimensions
/* This method can be used to create all rectangles with the same default dimensions
Rectangle() : _width{0.0}, _height{0.0} {}
*/
// Initialise with custom initial dimensions
Rectangle(double initWidth, double initHeight)
: _width{initWidth}, _height{initHeight}{}
// Member function to calculate the area of the rectangle
double getArea()
{
returnthis->_width * this->_height; // 'this->' can be omitted from the code
}
// Member fnction to get the width of the rectangle
double getWidth() { return _width; }
// Member function to get the height of the rectangle
double getHeight() { return _height; }
// Member function to resize the rectangle
void resize(double setWidth, double setHeight)
{
_width = setWidth;
_height = setHeight;
}
// Memeber function to adjust the width
void adjWidth(double setwidth)
{
_width = setwidth;
}
// Member function to adjust the height
void adjHeight(double setHeight)
{
_height = setHeight;
}
};
stdafx.h
// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//
#pragma once
#include "targetver.h"
#include <stdio.h>
#include <tchar.h>
#include <string>
// TODO: reference additional headers your program requires here
#include <iostream>
#include "rescource.h"