code 1 - } }; void main () { vector k; // vector () is...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
vector operator * (double a) { vector r; r.x = x * a; r.y = y * a; return r; } }; void main (void) { vector a, b; a.x = 3; b.y = 5; b = a.its_oposite(); cout << "Vector a : " << a.x << ", " << a.y << endl; cout << "Vector b : " << b.x << ", " << b.y << endl; b.be_oposited(); cout << "Vector b : " << b.x << ", " << b.y << endl; a.be_calculated (7, 8, 3, 2); cout << "Vector a : " << a.x << ", " << a.y << endl; a = b * 2; cout << "Vector a : " << a.x << ", " << a.y << endl; a = b.its_oposite() * 2; cout << "Vector a : " << a.x << ", " << a.y << endl; class vector { public: double x; double y; vector () // same name as class { x = 0; y = 0; } vector (double a, double b) { x = a; y = b;
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: } }; void main () { vector k; // vector () is called cout << "vector k : " << k.x << ", " << k.y << endl << endl; vector m (45, 2); // vector (double, double) is called cout << "vector m : " << m.x << ", " << m.y << endl << endl; k = vector (23, 2); // vector created, copied to k, then erased cout << "vector k : " << k.x << ", " << k.y << endl << endl; } It is a good practice to try not to overload the constructors. Best is to declare only one constructor and give it default parameters wherever possible : #include <iostream.h> class vector { public: double x; double y; vector (double a = 0, double b = 0) {}...
View Full Document

This note was uploaded on 02/26/2012 for the course MBA IT DOM1 taught by Professor Kviswanathan during the Spring '12 term at Indian Institute of Technology, Chennai.

Ask a homework question - tutors are online