c++ - Overloading operators in a Rectangle Class -
my program not run , giving me error messages. @ first, forgot place semicolon after }
in header file. went , added 1 visual studio keeps giving me errors.
please ignore part 2c below:
link error messages : http://pastebin.com/s11rs1du
#ifndef rectangle_h #define rectangle_h using namespace std; // class declaration class rectangle { private: double length; double width; public: rectangle(); rectangle(double, double); rectangle operator-(rectangle &); rectangle operator*(rectangle &); friend istream& operator>>(istream &, rectangle &); }; #endif #include "stdafx.h" #include <iostream> #include "rectangle.h" using namespace std; // default constructor rectangle::rectangle() { length = 0; width = 0; } // constructor rectangle::rectangle(double len, double wid) { length = len; width = wid; } // overload - operator rectangle rectangle::operator-(rectangle &otherrect) { rectangle temp; temp.length = this->length - otherrect.length; temp.width = this->width - otherrect.width; return temp; } // overload * operator rectangle rectangle::operator*(rectangle &otherrect) { rectangle temp; temp.length = this->length * otherrect.length; temp.width = this->width * otherrect.width; return temp; } // overload cin operator istream& operator>>(istream &is, rectangle& r) { // prompt user length cout << "enter length: "; >> r.length; // prompt user width cout << "enter width: "; >> r.width; return is; } #include "stdafx.h" #include "rectangle.h" #include <iostream> using namespace std; int main() { rectangle r1(3,5); rectangle r3, r4, r5, r6; rectangle r2(r1); // copy constructor cin >> r2; // read in value r2 , overloaded r3 = r1 – r2; cout << r3; r4 = r1 * r2; cout << r4; system("pause"); return 0;
this student 982217279. please ignore message of instructors come across post. have been advised avoid type of plagiarism issues.
you have 2 problems. first didn't declare or define insertion operator. using here:
cout << r3;
and
cout << r4;
your second problem seems line try subtract 2 rectangles on has character isn't minus symbol:
r3 = r1 – r2 // ^this isn't subtraction, it's hyphen or something. r3 = r1 - r2 // ^see difference?
after adding insertion operator overload, , fixing minus symbol, code compiled.
Comments
Post a Comment