Upcoming SlideShare
×

# oop Lecture 5

858 views
817 views

Published on

0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total views
858
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
0
0
Likes
0
Embeds 0
No embeds

No notes for slide

### oop Lecture 5

1. 1. OOP Lecture 5, 28th Feb, 2011 M. Anwar-ul-HaqQuestion: Create a class Rectangle. The class has attributes length and width, each ofwhich defaults to 1.It has member functions that calculate the perimeter and the area of the rectangle. It hasset and get functions for both length and width.The set functions should verify that length and width are each floating-point numberslarger than 0.0 and less than 20.0.Solution:#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;} OOP, Spring 2011, Engr. Anwar, Foundation University (FUIEMS), Islamabad
2. 2. 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;} OOP, Spring 2011, Engr. Anwar, Foundation University (FUIEMS), Islamabad
3. 3. Version 2:#include <iostream>using namespace std;class Rectangle {public: Rectangle () { length = 1.0; width = 1.0; cout<<"Constructor without parameter is called"<<endl; } Rectangle( double l, double w ) { setWidth(w); setLength(l); cout<<"Constructor with two parameter is called"<<endl; } double perimeter( void ) { return 2 * ( width + length ); } double area( void ) { return width * length; } void setWidth( double w ) { if (w > 0 && w < 20.0) width = w; else width = 1.0; } void setLength( double l ) { if (l > 0 && l < 20.0) length = l; else length = 1.0; } double getWidth( void ) { return width; } double getLength( void ) { return length; } OOP, Spring 2011, Engr. Anwar, Foundation University (FUIEMS), Islamabad
4. 4. private: double length; double width;};/*Rectangle::Rectangle( double w, double l ) { } double Rectangle::perimeter( void ) {}double Rectangle::area( void ) {}void Rectangle::setWidth( double w ) {}void Rectangle::setLength( double l ){}double Rectangle::getWidth( void )double Rectangle::getLength( void ) { }*/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(); cout << "; perimeter = " << a.perimeter() << "; area = " <<a.area() << n; // output Rectangle b cout << "b: length = " << b.getLength() << "; width = " <<b.getWidth(); cout << "; perimeter = " << b.perimeter() << "; area = " <<b.area() << n; // output Rectangle c; bad values attempted cout << "c: length = " << c.getLength() << "; width = " <<c.getWidth() ; cout << "; perimeter = " << c.perimeter() << "; area = " <<c.area() << n; return 0;} OOP, Spring 2011, Engr. Anwar, Foundation University (FUIEMS), Islamabad