Wednesday, March 3, 2010

0

QUIZ 3 OOP ANSWER

  • Wednesday, March 3, 2010
  • Ibnu Mahmud Al-Shirazy
  • Share
  • #include
    using namespace std;

    class Point
    {

    private:
    int x;
    int y;
    public:
    Point (int = 3, int = 3);
    Point operator + (const Point &);
    Point operator * (const Point &);
    friend ostream& operator << ( ostream &, const Point &);
    friend istream& operator >> ( istream &, Point &);
    };

    Point::Point(int xx , int yy )
    {
    x = xx;
    y = yy;
    }


    Point Point::operator +(const Point & a)
    {
    Point temp;
    temp.x = x + a.x;
    temp.y = y + a.y;
    return temp;
    }

    Point Point::operator *(const Point & b)
    {
    Point temp;
    temp.x = x * b.x;
    temp.y = y * b.y;
    return temp;
    }

    ostream& operator << ( ostream &out, const Point & c)
    {
    out << "(" << c.x << "," << c.y << ")" << endl;
    return out;
    }

    istream& operator >> ( istream &in, Point & d)
    {
    in >> d.x >> d.y;
    return in;
    }

    int main()
    {
    Point p1(2,4), p2, p3, p4, p5, p6;

    cout << "p1 = " << p1;
    cout << "p2 = " << p2;

    p3 = p1 + p2;
    p4 = p1 * p2;

    cout << "\n\np3 = p1 + p2 = " << p3;
    cout << "p4 = p1 * p2 = " << p4;

    cout << "\n\nEnter value of x and y point:" << endl;
    cin >> p5;

    cout << "p5 = " << p5;

    p6 = p5 * p3;
    cout << "\n\np6 = p5 * p3 = " << p6;

    return 0;
    }

    0 Responses to “QUIZ 3 OOP ANSWER”

    Post a Comment

    Subscribe