SlideShare ist ein Scribd-Unternehmen logo
1 von 4
OOP Lecture 5, 28th Feb, 2011
                                   M. Anwar-ul-Haq

Question: Create a class Rectangle. The class has attributes length and width, each of
which defaults to 1.

It has member functions that calculate the perimeter and the area of the rectangle. It has
set and get functions for both length and width.

The set functions should verify that length and width are each floating-point numbers
larger 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
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
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
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

Weitere ähnliche Inhalte

Was ist angesagt?

Computer graphics lab manual
Computer graphics lab manualComputer graphics lab manual
Computer graphics lab manualUma mohan
 
Writeup advanced lane_lines_project
Writeup advanced lane_lines_projectWriteup advanced lane_lines_project
Writeup advanced lane_lines_projectManish Jauhari
 
Roberto Gallea: Workshop Arduino, giorno #2 Arduino + Processing
Roberto Gallea: Workshop Arduino, giorno #2 Arduino + ProcessingRoberto Gallea: Workshop Arduino, giorno #2 Arduino + Processing
Roberto Gallea: Workshop Arduino, giorno #2 Arduino + ProcessingDemetrio Siragusa
 
COMPUTER GRAPHICS LAB MANUAL
COMPUTER GRAPHICS LAB MANUALCOMPUTER GRAPHICS LAB MANUAL
COMPUTER GRAPHICS LAB MANUALVivek Kumar Sinha
 
Computer Graphics Lab File C Programs
Computer Graphics Lab File C ProgramsComputer Graphics Lab File C Programs
Computer Graphics Lab File C ProgramsKandarp Tiwari
 
Multi prefix trie
Multi prefix trieMulti prefix trie
Multi prefix triemehdi sa
 
OPTIMAL BINARY SEARCH
OPTIMAL BINARY SEARCHOPTIMAL BINARY SEARCH
OPTIMAL BINARY SEARCHCool Guy
 
Computer graphics lab manual
Computer graphics lab manualComputer graphics lab manual
Computer graphics lab manualAnkit Kumar
 
The Uncertain Enterprise
The Uncertain EnterpriseThe Uncertain Enterprise
The Uncertain EnterpriseClarkTony
 
Computer Graphics Lab
Computer Graphics LabComputer Graphics Lab
Computer Graphics LabNeil Mathew
 
Computer graphics lab report with code in cpp
Computer graphics lab report with code in cppComputer graphics lab report with code in cpp
Computer graphics lab report with code in cppAlamgir Hossain
 
C++ Programming - 4th Study
C++ Programming - 4th StudyC++ Programming - 4th Study
C++ Programming - 4th StudyChris Ohk
 
Computer graphics lab assignment
Computer graphics lab assignmentComputer graphics lab assignment
Computer graphics lab assignmentAbdullah Al Shiam
 
5 1. character processing
5 1. character processing5 1. character processing
5 1. character processing웅식 전
 
บทที่ 3 พื้นฐานภาษา Java
บทที่ 3 พื้นฐานภาษา Javaบทที่ 3 พื้นฐานภาษา Java
บทที่ 3 พื้นฐานภาษา JavaItslvle Parin
 
SE Computer, Programming Laboratory(210251) University of Pune
SE Computer, Programming Laboratory(210251) University of PuneSE Computer, Programming Laboratory(210251) University of Pune
SE Computer, Programming Laboratory(210251) University of PuneBhavesh Shah
 

Was ist angesagt? (20)

Cpp tutorial
Cpp tutorialCpp tutorial
Cpp tutorial
 
Computer graphics lab manual
Computer graphics lab manualComputer graphics lab manual
Computer graphics lab manual
 
Writeup advanced lane_lines_project
Writeup advanced lane_lines_projectWriteup advanced lane_lines_project
Writeup advanced lane_lines_project
 
Roberto Gallea: Workshop Arduino, giorno #2 Arduino + Processing
Roberto Gallea: Workshop Arduino, giorno #2 Arduino + ProcessingRoberto Gallea: Workshop Arduino, giorno #2 Arduino + Processing
Roberto Gallea: Workshop Arduino, giorno #2 Arduino + Processing
 
COMPUTER GRAPHICS LAB MANUAL
COMPUTER GRAPHICS LAB MANUALCOMPUTER GRAPHICS LAB MANUAL
COMPUTER GRAPHICS LAB MANUAL
 
Computer Graphics Lab File C Programs
Computer Graphics Lab File C ProgramsComputer Graphics Lab File C Programs
Computer Graphics Lab File C Programs
 
Multi prefix trie
Multi prefix trieMulti prefix trie
Multi prefix trie
 
OPTIMAL BINARY SEARCH
OPTIMAL BINARY SEARCHOPTIMAL BINARY SEARCH
OPTIMAL BINARY SEARCH
 
Computer graphics lab manual
Computer graphics lab manualComputer graphics lab manual
Computer graphics lab manual
 
The Uncertain Enterprise
The Uncertain EnterpriseThe Uncertain Enterprise
The Uncertain Enterprise
 
Computer Graphics Lab
Computer Graphics LabComputer Graphics Lab
Computer Graphics Lab
 
C sharp 8
C sharp 8C sharp 8
C sharp 8
 
Computer graphics lab report with code in cpp
Computer graphics lab report with code in cppComputer graphics lab report with code in cpp
Computer graphics lab report with code in cpp
 
Computer graphics
Computer graphicsComputer graphics
Computer graphics
 
C++ Programming - 4th Study
C++ Programming - 4th StudyC++ Programming - 4th Study
C++ Programming - 4th Study
 
Computer graphics lab assignment
Computer graphics lab assignmentComputer graphics lab assignment
Computer graphics lab assignment
 
5 1. character processing
5 1. character processing5 1. character processing
5 1. character processing
 
บทที่ 3 พื้นฐานภาษา Java
บทที่ 3 พื้นฐานภาษา Javaบทที่ 3 พื้นฐานภาษา Java
บทที่ 3 พื้นฐานภาษา Java
 
Code Brevity in Scala
Code Brevity in ScalaCode Brevity in Scala
Code Brevity in Scala
 
SE Computer, Programming Laboratory(210251) University of Pune
SE Computer, Programming Laboratory(210251) University of PuneSE Computer, Programming Laboratory(210251) University of Pune
SE Computer, Programming Laboratory(210251) University of Pune
 

Andere mochten auch

Storage classes arrays & functions in C Language
Storage classes arrays & functions in C LanguageStorage classes arrays & functions in C Language
Storage classes arrays & functions in C LanguageJenish Bhavsar
 
Object Oriented Programming in Swift Ch0 - Encapsulation
Object Oriented Programming in Swift Ch0 - EncapsulationObject Oriented Programming in Swift Ch0 - Encapsulation
Object Oriented Programming in Swift Ch0 - EncapsulationChihyang Li
 

Andere mochten auch (8)

oop Lecture 17
oop Lecture 17oop Lecture 17
oop Lecture 17
 
oop Lecture 16
oop Lecture 16oop Lecture 16
oop Lecture 16
 
oop Lecture19
oop Lecture19oop Lecture19
oop Lecture19
 
oop Lecture 10
oop Lecture 10oop Lecture 10
oop Lecture 10
 
oop Lecture 9
oop Lecture 9oop Lecture 9
oop Lecture 9
 
Storage classes arrays & functions in C Language
Storage classes arrays & functions in C LanguageStorage classes arrays & functions in C Language
Storage classes arrays & functions in C Language
 
Object Oriented Programming in Swift Ch0 - Encapsulation
Object Oriented Programming in Swift Ch0 - EncapsulationObject Oriented Programming in Swift Ch0 - Encapsulation
Object Oriented Programming in Swift Ch0 - Encapsulation
 
oop Lecture 11
oop Lecture 11oop Lecture 11
oop Lecture 11
 

Ähnlich wie oop Lecture 5

Abstract Classes Interface Exceptional Handling Java
Abstract Classes Interface Exceptional Handling JavaAbstract Classes Interface Exceptional Handling Java
Abstract Classes Interface Exceptional Handling JavaSyedShahroseSohail
 
Example for Abstract Class and Interface.pdf
Example for Abstract Class and Interface.pdfExample for Abstract Class and Interface.pdf
Example for Abstract Class and Interface.pdfrajaratna4
 
麻省理工C++公开教学课程(二)
麻省理工C++公开教学课程(二)麻省理工C++公开教学课程(二)
麻省理工C++公开教学课程(二)ProCharm
 
You still work for packaging company that makes boxes and cylindrical.docx
 You still work for packaging company that makes boxes and cylindrical.docx You still work for packaging company that makes boxes and cylindrical.docx
You still work for packaging company that makes boxes and cylindrical.docxajoy21
 
Circle.javaimport java.text.DecimalFormat;public class Circle {.pdf
Circle.javaimport java.text.DecimalFormat;public class Circle {.pdfCircle.javaimport java.text.DecimalFormat;public class Circle {.pdf
Circle.javaimport java.text.DecimalFormat;public class Circle {.pdfANJALIENTERPRISES1
 
CSC139 Chapter 10 Lab Assignment (2)PolymorphismObjectivesIn.docx
CSC139 Chapter 10 Lab Assignment (2)PolymorphismObjectivesIn.docxCSC139 Chapter 10 Lab Assignment (2)PolymorphismObjectivesIn.docx
CSC139 Chapter 10 Lab Assignment (2)PolymorphismObjectivesIn.docxfaithxdunce63732
 
C# v8 new features - raimundas banevicius
C# v8 new features - raimundas baneviciusC# v8 new features - raimundas banevicius
C# v8 new features - raimundas baneviciusRaimundas Banevičius
 
Hive Functions Cheat Sheet
Hive Functions Cheat SheetHive Functions Cheat Sheet
Hive Functions Cheat SheetHortonworks
 
Please follow the cod eand comments for description CODE #incl.pdf
Please follow the cod eand comments for description CODE #incl.pdfPlease follow the cod eand comments for description CODE #incl.pdf
Please follow the cod eand comments for description CODE #incl.pdfannaielectronicsvill
 
Software Engineering for Indies #gcmuc18
Software Engineering for Indies #gcmuc18Software Engineering for Indies #gcmuc18
Software Engineering for Indies #gcmuc18Andreas Pohl
 
Pads lab manual final
Pads lab manual finalPads lab manual final
Pads lab manual finalAhalyaR
 
Chapter27 polymorphism-virtual-function-abstract-class
Chapter27 polymorphism-virtual-function-abstract-classChapter27 polymorphism-virtual-function-abstract-class
Chapter27 polymorphism-virtual-function-abstract-classDeepak Singh
 
Better Software: introduction to good code
Better Software: introduction to good codeBetter Software: introduction to good code
Better Software: introduction to good codeGiordano Scalzo
 
Object Oriented Principle&rsquo;s
Object Oriented Principle&rsquo;sObject Oriented Principle&rsquo;s
Object Oriented Principle&rsquo;svivek p s
 

Ähnlich wie oop Lecture 5 (20)

Abstract Classes Interface Exceptional Handling Java
Abstract Classes Interface Exceptional Handling JavaAbstract Classes Interface Exceptional Handling Java
Abstract Classes Interface Exceptional Handling Java
 
oop Lecture 4
oop Lecture 4oop Lecture 4
oop Lecture 4
 
Example for Abstract Class and Interface.pdf
Example for Abstract Class and Interface.pdfExample for Abstract Class and Interface.pdf
Example for Abstract Class and Interface.pdf
 
C++ Inheritance
C++ InheritanceC++ Inheritance
C++ Inheritance
 
麻省理工C++公开教学课程(二)
麻省理工C++公开教学课程(二)麻省理工C++公开教学课程(二)
麻省理工C++公开教学课程(二)
 
You still work for packaging company that makes boxes and cylindrical.docx
 You still work for packaging company that makes boxes and cylindrical.docx You still work for packaging company that makes boxes and cylindrical.docx
You still work for packaging company that makes boxes and cylindrical.docx
 
Oop1
Oop1Oop1
Oop1
 
Circle.javaimport java.text.DecimalFormat;public class Circle {.pdf
Circle.javaimport java.text.DecimalFormat;public class Circle {.pdfCircle.javaimport java.text.DecimalFormat;public class Circle {.pdf
Circle.javaimport java.text.DecimalFormat;public class Circle {.pdf
 
Oop objects_classes
Oop objects_classesOop objects_classes
Oop objects_classes
 
CSC139 Chapter 10 Lab Assignment (2)PolymorphismObjectivesIn.docx
CSC139 Chapter 10 Lab Assignment (2)PolymorphismObjectivesIn.docxCSC139 Chapter 10 Lab Assignment (2)PolymorphismObjectivesIn.docx
CSC139 Chapter 10 Lab Assignment (2)PolymorphismObjectivesIn.docx
 
C# v8 new features - raimundas banevicius
C# v8 new features - raimundas baneviciusC# v8 new features - raimundas banevicius
C# v8 new features - raimundas banevicius
 
Hive Functions Cheat Sheet
Hive Functions Cheat SheetHive Functions Cheat Sheet
Hive Functions Cheat Sheet
 
oop objects_classes
oop objects_classesoop objects_classes
oop objects_classes
 
Please follow the cod eand comments for description CODE #incl.pdf
Please follow the cod eand comments for description CODE #incl.pdfPlease follow the cod eand comments for description CODE #incl.pdf
Please follow the cod eand comments for description CODE #incl.pdf
 
Software Engineering for Indies #gcmuc18
Software Engineering for Indies #gcmuc18Software Engineering for Indies #gcmuc18
Software Engineering for Indies #gcmuc18
 
Pads lab manual final
Pads lab manual finalPads lab manual final
Pads lab manual final
 
Chapter27 polymorphism-virtual-function-abstract-class
Chapter27 polymorphism-virtual-function-abstract-classChapter27 polymorphism-virtual-function-abstract-class
Chapter27 polymorphism-virtual-function-abstract-class
 
Better Software: introduction to good code
Better Software: introduction to good codeBetter Software: introduction to good code
Better Software: introduction to good code
 
Object Oriented Principle&rsquo;s
Object Oriented Principle&rsquo;sObject Oriented Principle&rsquo;s
Object Oriented Principle&rsquo;s
 
Bc0037
Bc0037Bc0037
Bc0037
 

Kürzlich hochgeladen

Market Sizes Sample Report - 2024 Edition
Market Sizes Sample Report - 2024 EditionMarket Sizes Sample Report - 2024 Edition
Market Sizes Sample Report - 2024 EditionMintel Group
 
Traction part 2 - EOS Model JAX Bridges.
Traction part 2 - EOS Model JAX Bridges.Traction part 2 - EOS Model JAX Bridges.
Traction part 2 - EOS Model JAX Bridges.Anamaria Contreras
 
APRIL2024_UKRAINE_xml_0000000000000 .pdf
APRIL2024_UKRAINE_xml_0000000000000 .pdfAPRIL2024_UKRAINE_xml_0000000000000 .pdf
APRIL2024_UKRAINE_xml_0000000000000 .pdfRbc Rbcua
 
Pitch Deck Teardown: Geodesic.Life's $500k Pre-seed deck
Pitch Deck Teardown: Geodesic.Life's $500k Pre-seed deckPitch Deck Teardown: Geodesic.Life's $500k Pre-seed deck
Pitch Deck Teardown: Geodesic.Life's $500k Pre-seed deckHajeJanKamps
 
The-Ethical-issues-ghhhhhhhhjof-Byjus.pptx
The-Ethical-issues-ghhhhhhhhjof-Byjus.pptxThe-Ethical-issues-ghhhhhhhhjof-Byjus.pptx
The-Ethical-issues-ghhhhhhhhjof-Byjus.pptxmbikashkanyari
 
Ten Organizational Design Models to align structure and operations to busines...
Ten Organizational Design Models to align structure and operations to busines...Ten Organizational Design Models to align structure and operations to busines...
Ten Organizational Design Models to align structure and operations to busines...Seta Wicaksana
 
Annual General Meeting Presentation Slides
Annual General Meeting Presentation SlidesAnnual General Meeting Presentation Slides
Annual General Meeting Presentation SlidesKeppelCorporation
 
Kenya’s Coconut Value Chain by Gatsby Africa
Kenya’s Coconut Value Chain by Gatsby AfricaKenya’s Coconut Value Chain by Gatsby Africa
Kenya’s Coconut Value Chain by Gatsby Africaictsugar
 
PSCC - Capability Statement Presentation
PSCC - Capability Statement PresentationPSCC - Capability Statement Presentation
PSCC - Capability Statement PresentationAnamaria Contreras
 
Call Us 📲8800102216📞 Call Girls In DLF City Gurgaon
Call Us 📲8800102216📞 Call Girls In DLF City GurgaonCall Us 📲8800102216📞 Call Girls In DLF City Gurgaon
Call Us 📲8800102216📞 Call Girls In DLF City Gurgaoncallgirls2057
 
Guide Complete Set of Residential Architectural Drawings PDF
Guide Complete Set of Residential Architectural Drawings PDFGuide Complete Set of Residential Architectural Drawings PDF
Guide Complete Set of Residential Architectural Drawings PDFChandresh Chudasama
 
(Best) ENJOY Call Girls in Faridabad Ex | 8377087607
(Best) ENJOY Call Girls in Faridabad Ex | 8377087607(Best) ENJOY Call Girls in Faridabad Ex | 8377087607
(Best) ENJOY Call Girls in Faridabad Ex | 8377087607dollysharma2066
 
Church Building Grants To Assist With New Construction, Additions, And Restor...
Church Building Grants To Assist With New Construction, Additions, And Restor...Church Building Grants To Assist With New Construction, Additions, And Restor...
Church Building Grants To Assist With New Construction, Additions, And Restor...Americas Got Grants
 
Memorándum de Entendimiento (MoU) entre Codelco y SQM
Memorándum de Entendimiento (MoU) entre Codelco y SQMMemorándum de Entendimiento (MoU) entre Codelco y SQM
Memorándum de Entendimiento (MoU) entre Codelco y SQMVoces Mineras
 
Youth Involvement in an Innovative Coconut Value Chain by Mwalimu Menza
Youth Involvement in an Innovative Coconut Value Chain by Mwalimu MenzaYouth Involvement in an Innovative Coconut Value Chain by Mwalimu Menza
Youth Involvement in an Innovative Coconut Value Chain by Mwalimu Menzaictsugar
 
Kenya Coconut Production Presentation by Dr. Lalith Perera
Kenya Coconut Production Presentation by Dr. Lalith PereraKenya Coconut Production Presentation by Dr. Lalith Perera
Kenya Coconut Production Presentation by Dr. Lalith Pereraictsugar
 
8447779800, Low rate Call girls in Shivaji Enclave Delhi NCR
8447779800, Low rate Call girls in Shivaji Enclave Delhi NCR8447779800, Low rate Call girls in Shivaji Enclave Delhi NCR
8447779800, Low rate Call girls in Shivaji Enclave Delhi NCRashishs7044
 

Kürzlich hochgeladen (20)

Market Sizes Sample Report - 2024 Edition
Market Sizes Sample Report - 2024 EditionMarket Sizes Sample Report - 2024 Edition
Market Sizes Sample Report - 2024 Edition
 
Traction part 2 - EOS Model JAX Bridges.
Traction part 2 - EOS Model JAX Bridges.Traction part 2 - EOS Model JAX Bridges.
Traction part 2 - EOS Model JAX Bridges.
 
APRIL2024_UKRAINE_xml_0000000000000 .pdf
APRIL2024_UKRAINE_xml_0000000000000 .pdfAPRIL2024_UKRAINE_xml_0000000000000 .pdf
APRIL2024_UKRAINE_xml_0000000000000 .pdf
 
Pitch Deck Teardown: Geodesic.Life's $500k Pre-seed deck
Pitch Deck Teardown: Geodesic.Life's $500k Pre-seed deckPitch Deck Teardown: Geodesic.Life's $500k Pre-seed deck
Pitch Deck Teardown: Geodesic.Life's $500k Pre-seed deck
 
The-Ethical-issues-ghhhhhhhhjof-Byjus.pptx
The-Ethical-issues-ghhhhhhhhjof-Byjus.pptxThe-Ethical-issues-ghhhhhhhhjof-Byjus.pptx
The-Ethical-issues-ghhhhhhhhjof-Byjus.pptx
 
Ten Organizational Design Models to align structure and operations to busines...
Ten Organizational Design Models to align structure and operations to busines...Ten Organizational Design Models to align structure and operations to busines...
Ten Organizational Design Models to align structure and operations to busines...
 
Annual General Meeting Presentation Slides
Annual General Meeting Presentation SlidesAnnual General Meeting Presentation Slides
Annual General Meeting Presentation Slides
 
Japan IT Week 2024 Brochure by 47Billion (English)
Japan IT Week 2024 Brochure by 47Billion (English)Japan IT Week 2024 Brochure by 47Billion (English)
Japan IT Week 2024 Brochure by 47Billion (English)
 
Kenya’s Coconut Value Chain by Gatsby Africa
Kenya’s Coconut Value Chain by Gatsby AfricaKenya’s Coconut Value Chain by Gatsby Africa
Kenya’s Coconut Value Chain by Gatsby Africa
 
PSCC - Capability Statement Presentation
PSCC - Capability Statement PresentationPSCC - Capability Statement Presentation
PSCC - Capability Statement Presentation
 
Call Us 📲8800102216📞 Call Girls In DLF City Gurgaon
Call Us 📲8800102216📞 Call Girls In DLF City GurgaonCall Us 📲8800102216📞 Call Girls In DLF City Gurgaon
Call Us 📲8800102216📞 Call Girls In DLF City Gurgaon
 
Guide Complete Set of Residential Architectural Drawings PDF
Guide Complete Set of Residential Architectural Drawings PDFGuide Complete Set of Residential Architectural Drawings PDF
Guide Complete Set of Residential Architectural Drawings PDF
 
(Best) ENJOY Call Girls in Faridabad Ex | 8377087607
(Best) ENJOY Call Girls in Faridabad Ex | 8377087607(Best) ENJOY Call Girls in Faridabad Ex | 8377087607
(Best) ENJOY Call Girls in Faridabad Ex | 8377087607
 
No-1 Call Girls In Goa 93193 VIP 73153 Escort service In North Goa Panaji, Ca...
No-1 Call Girls In Goa 93193 VIP 73153 Escort service In North Goa Panaji, Ca...No-1 Call Girls In Goa 93193 VIP 73153 Escort service In North Goa Panaji, Ca...
No-1 Call Girls In Goa 93193 VIP 73153 Escort service In North Goa Panaji, Ca...
 
Church Building Grants To Assist With New Construction, Additions, And Restor...
Church Building Grants To Assist With New Construction, Additions, And Restor...Church Building Grants To Assist With New Construction, Additions, And Restor...
Church Building Grants To Assist With New Construction, Additions, And Restor...
 
Memorándum de Entendimiento (MoU) entre Codelco y SQM
Memorándum de Entendimiento (MoU) entre Codelco y SQMMemorándum de Entendimiento (MoU) entre Codelco y SQM
Memorándum de Entendimiento (MoU) entre Codelco y SQM
 
Youth Involvement in an Innovative Coconut Value Chain by Mwalimu Menza
Youth Involvement in an Innovative Coconut Value Chain by Mwalimu MenzaYouth Involvement in an Innovative Coconut Value Chain by Mwalimu Menza
Youth Involvement in an Innovative Coconut Value Chain by Mwalimu Menza
 
Kenya Coconut Production Presentation by Dr. Lalith Perera
Kenya Coconut Production Presentation by Dr. Lalith PereraKenya Coconut Production Presentation by Dr. Lalith Perera
Kenya Coconut Production Presentation by Dr. Lalith Perera
 
8447779800, Low rate Call girls in Shivaji Enclave Delhi NCR
8447779800, Low rate Call girls in Shivaji Enclave Delhi NCR8447779800, Low rate Call girls in Shivaji Enclave Delhi NCR
8447779800, Low rate Call girls in Shivaji Enclave Delhi NCR
 
Corporate Profile 47Billion Information Technology
Corporate Profile 47Billion Information TechnologyCorporate Profile 47Billion Information Technology
Corporate Profile 47Billion Information Technology
 

oop Lecture 5

  • 1. OOP Lecture 5, 28th Feb, 2011 M. Anwar-ul-Haq Question: Create a class Rectangle. The class has attributes length and width, each of which defaults to 1. It has member functions that calculate the perimeter and the area of the rectangle. It has set and get functions for both length and width. The set functions should verify that length and width are each floating-point numbers larger 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. 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. 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. 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