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

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

View Full Document Right Arrow Icon
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++? Answer: allows a class to access private attributes and methods 2 Overload << Operator (outcome 5) Please overload the << operator to print the attributes of class Person . Answer: #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; 1
Background image of page 1

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

View Full DocumentRight Arrow Icon
} }; int main(int argc, char * argv[]) { Person p1("John", "123 Main Street"); Person p2("Amy", "567 First Avenue"); 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. Answer: Vector operator - () { return Vector (-v_x, -v_y, -v_z); } #include <iostream> #include <string> using namespace std; class Vector { private: 2
Background image of page 2
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[]) { 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. Answer: Vector operator * { return Vector (v.getX() * d, v.getY() * d, v.getZ() * d); } #include <iostream> #include <string> using namespace std; // do not change the Vector class class Vector { 3
Background image of page 3

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

View Full DocumentRight Arrow Icon
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; 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? Answer:
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the 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.

Page1 / 16

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

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