wa2 - ECE 462 Written Assignment 2 Due: 8:30AM, October 24,...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: ECE 462 Written Assignment 2 Due: 8:30AM, October 24, 2011 Name: PUID: 1 friend in C++ (outcome 5) What does friend mean in C++? 2 Overload << Operator (outcome 5) Please overload the << operator to print the attributes of class Person . #include <iostream> #include <string> using namespace std; class Person { private: string p_name; string p_address; public: Person(string n, string a) { p_name = n; p_address = a; } // >>>>> // overload << operator // <<<<< { os << "name: " << p.p_name << endl; os << "address: " << p.p_address << endl << endl; return os; } }; int main(int argc, char * argv) { Person p1("John", "123 Main Street"); Person p2("Amy", "567 First Avenue"); 1 Person p3("Tom", "873 North Street"); Person p4("Mary", "952 South Second Street"); cout << p1; cout << p2; cout << p3; cout << p4; / * output: name: John address: 123 Main Street name: Amy address: 567 First Avenue name: Tom address: 873 North Street name: Mary address: 952 South Second Street * / return 0; } 3 Overload- Operator (outcome 5) Please overload the- operator to change the direction of a vector object. #include <iostream> #include <string> using namespace std; class Vector { private: double v_x; double v_y; double v_z; public: Vector(double x, double y, double z) { v_x = x; v_y = y; v_z = z; } // >>>>> // overload - (negation) operator // <<<<< void print() { cout << "(" << v_x << "," << v_y << "," << v_z << ")" << endl; } }; int main(int argc, char * argv) { 2 Vector v1(2, 3.1, 5.7); Vector v2 = -v1; Vector v3(-0.8, 0.4, 7.5); v1.print(); v2.print(); v3.print(); v3 = -v1; v3.print(); / * output: (2,3.1,5.7) (-2,-3.1,-5.7) (-0.8,0.4,7.5) (-2,-3.1,-5.7) * / return 0; } 4 Overload * Operator (outcome 5) Please overload the * operator between a Vector object and double. #include <iostream> #include <string> using namespace std; // do not change the Vector class class Vector { private: double v_x; double v_y; double v_z; public: // do not change the public methods Vector(double x, double y, double z) { v_x = x; v_y = y; v_z = z; } void print() { cout << "(" << v_x << "," << v_y << "," << v_z << ")" << endl; } double getX() const { return v_x; } double getY() const { return v_y; } double getZ() const { return v_z; } }; // >>>>> // overload operator * for multiplication between a Vector object // and double. return a Vector object // <<<<< int main(int argc, char * argv) { Vector v1(2, 3.1, 5.7); Vector v2 = v1 * 0.5; 3 v1.print(); v2.print(); / * output: (2,3.1,5.7) (1,1.55,2.85) * / return 0; } 5 Overload = , < , > Operators (outcome 5) What is the output of this program?...
View Full Document

This note was uploaded on 02/19/2012 for the course ECE 462 taught by Professor Samuelmidkiff during the Fall '11 term at Purdue University-West Lafayette.

Page1 / 13

wa2 - ECE 462 Written Assignment 2 Due: 8:30AM, October 24,...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online