Anúncio
Próximos SlideShares
Do the following with class Shape- a- Add prototypes for all required.docx
Carregando em ... 3
1 de 4
Anúncio

### Do the following with class Shape- a- Add prototypes for all required (1).docx

1. Do the following with class Shape: a. Add prototypes for all required methods and operators for the class (the Point class is the one from problem one above). 1/////////////////////////////////////////////////////////////////////////////////////////////// template <class T> class Point { private: TÂ Â X; TÂ Â Y; public: Point() { X=0; Y=0; } T getX() { return X; } void setX(T x) { X = x; } T getY() { return Y; } void setY(T y) { Y = y; }
2. Point operator+(Point a, Point b) { Point r; r.X = a.X+b.X; r.Y = a.Y+b.Y; return r; } }; 2////////////////////////////////////////////////////////////////////////////////////////////////////// template <class T> class Shape { public: private: Point<T> Origin; }; Solution Along with solution i am also correcting the operator + function. The operator + is within the class so it will accept one argument only. Two argument is required in case of friend functions. #include <iostream> using namespace std; template <class T> class Point {
3. private: T X; T Y; public: Point() { X=0; Y=0; } T getX() { return X; } void setX(T x) { X = x; } T getY() { return Y; } void setY(T y) { Y = y; } Point operator + (Point<T> a) { Point<T> r; r.X = a.X+this.X; r.Y = a.Y+this.Y; return r; } }; template <class T> class Shape { public: T area_rectangle(T ,T ); // prototype to calculate area of rectangle T area_circle(T ); // prototype to calculate area of circle Point<T> operator * (Point<T> );// prototype for multiplication used in area of rectangle Point<T> operator / (Point<T> );// prototype for division used in area of circle(22/7 as pie) private: Point<T> Origin; };
Anúncio