Consider the class of points in the xy plane. The location of each point is determined by the real numbers (x, y) specifying the cartesian coordinates. The class definition is: #include using namespace std; class point{ public: point(); point(double value_x, double value_y); double get_x() const; double get_y() const; void print() const; void move(double dx, double dy); private: double x, y; }; point::point(){ x = 0.0; y = 0.0;} point::point(double a, double b){ x = a; y = b; } void point::print() const{ cout< A = point(1, 2); A.print(); A.move(4, -5); A.print(); pointB(3.2, 4.9); cout << B.get_x() <<""<< B.get_y() << endl ; point C("day", "young"); C.print(); C.move("s","ster"); C.print(); return 0;
#include<iostream>
#include<string>
using namespace std;
template<class type>
class point{
public:
point();
point(type value_x, type value_y);
type get_x() const;
type get_y() const;
void print() const;
void move(type dx, type dy);
private:
type x, y;
};
template<class type>
point<type>::point(){
x = NULL;
y = NULL;
}
template<class type>
point<type>::point(type a, type b){
x = a;
y = b;
}
template<class type>
void point<type>::print() const{
cout<<x<<" "<<y<< endl;
}
template<class type>
type point<type>::get_x() const{
return x;
}
template<class type>
type point<type>::get_y() const{
return y;
}
template<class type>
void point<type>::move(type dx, type dy){
x = x+dx;
y = y+dy;
}
int main(){
point<int> A = point<int>(1, 2);
A.print();
A.move(4, -5);
A.print();
point<float>B(3.2, 4.9);
cout << B.get_x() <<" "<< B.get_y() << endl ;
point<string> C("day", "young");
C.print();
C.move("s","ster");
C.print();
return 0;
}
Comments
Leave a comment