OOP Lecture 4 – 23rd Feb, 2011Solution 1:#include <iostream>using namespace std;struct phone{      int area; //area code (...
Solution 3:#include <iostream>using namespace std;class Rectangle {public:Rectangle( double = 1.0, double = 1.0 );double p...
Upcoming SlideShare
Loading in...5
×

oop Lecture 4

181

Published on

Published in: Technology, Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
181
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

oop Lecture 4

  1. 1. OOP Lecture 4 – 23rd Feb, 2011Solution 1:#include <iostream>using namespace std;struct phone{ int area; //area code (3 digits) int exchange; //exchange (3 digits) int number; //number (4 digits)};int main(){ phone ph1 = { 212, 767, 8900 }; //initialize phone number phone ph2; //define phone number // get phone no from user cout << "nEnter your area code, exchange, and number"; cout << "n(Don’t use leading zeros): "; cin >> ph2.area >> ph2.exchange >> ph2.number; cout << "nMy number is " << ( << ph1.area << ") " << ph1.exchange << - << ph1.number; cout << "nYour number is " << ( << ph2.area << ") "<< ph2.exchange << - << ph2.number << endl; return 0;}Solution 2:#include <iostream>using namespace std;struct point{int xCo; //X coordinateint yCo; //Y coordinate};int main(){ point p1, p2, p3; //define 3 points cout << “nEnter coordinates for p1: “; //get 2 points cin >> p1.xCo >> p1.yCo; //from user cout << “Enter coordinates for p2: “; cin >> p2.xCo >> p2.yCo; p3.xCo = p1.xCo + p2.xCo; //find sum of p3.yCo = p1.yCo + p2.yCo; //p1 and p2 cout << “Coordinates of p1+p2 are: “ //display the sum << p3.xCo << “, “ << p3.yCo << endl; return 0;}
  2. 2. Solution 3:#include <iostream>using namespace std;class Rectangle {public:Rectangle( double = 1.0, double = 1.0 );double perimeter( void );double area( void );void setWidth( double w );void setLength( double l );double getWidth( void );double getLength( void );private: double length; double width; };Rectangle::Rectangle( double w, double l ) { setWidth(w); setLength(l);} double Rectangle::perimeter( void ) { return 2 * ( width + length ); }double Rectangle::area( void ) { return width * length; } void Rectangle::setWidth( double w ) { width = w > 0 && w < 20.0 ? w : 1.0; }void Rectangle::setLength( double l ){ length = l > 0 && l < 20.0 ? l : 1.0;}double Rectangle::getWidth( void ) { return width; }double Rectangle::getLength( void ) { return length;}int main() { Rectangle a, b( 4.0, 5.0 ), c( 67.0, 888.0 ); // output Rectangle a cout << "a: length = " << a.getLength()<< "; width = " <<a.getWidth()<< "; perimeter = " << a.perimeter() << "; area = " <<a.area() << n; // output Rectangle b cout << "b: length = " << b.getLength() << "; width = " <<b.getWidth() << "; perimeter = " << b.perimeter() << "; area = " <<b.area() << n; // output Rectangle c; bad values attempted cout << "c: length = " << c.getLength() << "; width = " <<c.getWidth() << "; perimeter = " << c.perimeter() << "; area = " <<c.area() << n; return 0;}

×