Passing data to a member function

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'?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
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()
	{
		return this->_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"
your right
You could make selection a string and use an if-else statement.
1
2
3
4
5
6
7
string selection;
cin >> selection;

if (selection == "inner")
  std::cout << "Inner rectangle are: " << inner.getArea() << std::endl;
else if (selection == "outer")
  // and so on 
Topic archived. No new replies allowed.