floating point - C++ using float numbers, functions not calculating valid data -
writing class rectangle assignment create class rectangle attributes length , width, each of defaults 1. provide member functions calculate perimeter , area of rectangle. also, provide set , functions length , width attributes. set functions should verify length , width each floating-point numbers larger 0.0 , less 20.0.;
the code executes, output incorrect. functions must not calling correctly.
example of output
>the first objects information >the length 1 >the width 1 >the perimeter -1.07374e+08 area -1.07374e+08 >the second objects information >the length -1.07374e+08 >the width -1.07374e+08 >the perimeter -1.07374e+08 >the area -1.07374e+08
header file
#ifndef rectangle_h #define rectangle_h class rectangle { public: rectangle(); rectangle(float length); rectangle(float length, float width); ~rectangle(); float setlengthandwidth(float, float); float setlength(float length); float setwidth(float width); float calculateperimeter(); float calculatearea(); void printinfo(); float getlength(); float getwidth(); private: float length; float width; float area; float perimeter; }; #endif#pragma once
main file
#include <iostream> #include <iomanip> #include <cmath> #include "rectangle.h" using namespace std; int main() { rectangle objectone; rectangle objecttwo(7.1, 3.2); rectangle objectthree(6.3); rectangle objectfour(200, 300); rectangle objectfive = objecttwo; cout << "the first objects information is\n "; objectone.printinfo(); cout << "the second objects information is\n "; objecttwo.printinfo(); cout << "the third objects information is\n "; objectthree.printinfo(); cout << "the fourth objects information is\n "; objectfour.printinfo(); cout << "the fifth objects information is\n "; objectfive.printinfo(); }
member .cpp file
#include <iostream> #include <iomanip> #include <cmath> #include "rectangle.h" using namespace std; rectangle::rectangle() { length = width = 1.0; } rectangle::rectangle(float length) { setlengthandwidth(length, 1.0); } rectangle::rectangle(float length, float width) { setlengthandwidth(length, width); } float rectangle::setlengthandwidth(float len, float wid) { setlength(len); setwidth(wid); } float rectangle::setlength(float length) { if (length >= 0 || length <= 20.0) length = length; else length = 1.0; } float rectangle::setwidth(float width) { if (width >= 0 || width <= 20.0) width = width; else width = 1.0; } float rectangle::calculateperimeter() { perimeter = (length * 2) + (width * 2) ; return perimeter; } float rectangle::calculatearea() { area = length * width; return area; } float rectangle::getlength() { cout << "please enter length" << endl; cin >> length; return length; } float rectangle::getwidth() { cout << "please enter width" << endl; cin >> width; return width; } void rectangle::printinfo() { cout << "the length " << length << endl << "the width " << width << endl; cout << "the perimeter " << perimeter << endl << "the area " << area << endl; } rectangle::~rectangle() { cout << "the object has gone out of scope. "; }
your setlength()
member doesn't set this->length
, , setwidth()
. set parameter, has no permanent effect.
Comments
Post a Comment