1 / 22

CMSC 202

CMSC 202. Computer Science II for Majors. Topics. Objects and Classes Project 2 description. Objects. What are objects ? Real-world objects E.g. Car, Fruit A real-world object is an entity in the world An object is an entity in an OO system OO programming approach

talon-pena
Download Presentation

CMSC 202

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CMSC 202 Computer Science II for Majors

  2. Topics • Objects and Classes • Project 2 description

  3. Objects • What are objects ? • Real-world objects • E.g. Car, Fruit • A real-world object is an entity in the world • An object is an entity in an OO system • OO programming approach • Programming problem is in terms of objects and communication between them

  4. Class • A Class is a user-defined data type whose instances are objects • Objects are variables of type class • Thus, class is an abstraction of objects of similar type • E.g. automobile is a class whose objects could be Bob’s car, Mary’s car etc.

  5. Class … cont • One important feature of class is Data hiding or Encapsulation • A class wraps data and functions into a single unit • Thus data is typically not accessible to outside world • Member functions provide an interfacebetween object’s data and program

  6. Class … cont • An Interface Member Variables ( Data ) User of objects Member Functions Object

  7. Class … cont • Syntax • class class-name { • access-specifier : • data and functions • access-specifier : • data and functions • access-specifier : • data and functions • … • } ;

  8. Class … cont • Access – specifier • private : Private to class • public : Accessible to other parts of program • protected : Needed in inheritance (later) • By default, all members are private

  9. Class … cont • E.g. of a class named Date class Date { public : // public member functions Date (int month=1, int day=1, int year=2003); void Print ( void ) const; private : // private data members int m_month; int m_day; int m_year; };

  10. Class … cont • Defining constructor & member functions Date :: Date (int month, int day, int year) { m_month = month; m_day = day; m_year = year; } void Date :: Print ( void ) const { cout << month << “-” << day << “-” << year; }

  11. Class … cont • In C++, you can add new features to existing structure of object / class • Suppose we need to add a member function called NextDay • NextDay increments day by 1 • Where do we make changes in the class ?

  12. Class … cont class Date { public : // public member functions Date (int month=1, int day=1, int year=2003); void Print ( void ) const; void NextDay( void ); private : // private data members int m_month; int m_day; int m_year; };

  13. Class … cont • Defining NextDay member function void Date :: NextDay ( void ) { .. // logic to increment date .. // Also need to consider incrementing to next // month and next year }

  14. Class … cont • Declaring an object as data member of another class • A class is a data-type, a class can use an object as a data member • This is known as aggregation or composition • Consider adding a Time object in our Date class • Exercise 6.10 on page 466

  15. Class … cont class Time { public : // Constructor and all member function will come // here // Function to increment time by one second void Increment ( void ); private : int m_hour; int m_minute; int m_second; };

  16. Class … cont class Date { public : // Constructor and all member function will come // here // Add member function Tick which calls Increment void Tick ( void ); private : Time currentTime; // Declare Time object in Date // other data members };

  17. Class … cont • How is the Tick() member function defined ? void Date :: Tick ( void ) { .. // logic to increment date // This is how we make use of Increment function of Time class currentTime.Increment(); } • But you still cannot access private members of Time class from Date class - Encapsulation

  18. Class … cont m_month m_day m_year User Member Functions m_hour m_minute m_second Member Func Time Object Date Object

  19. Project 2 • Objective • To implement and use objects • To understand aggregation • To practice formatted output, makefiles • Given a quadrilateral, it can be a • Parallelogram • Rectangle • Square • Trapezoid • Irregular

  20. Project 2 … cont • Classes • A Point class • Encapsulates x and y co-ordinates of a point • A Quadrilateral class • Encapsulates a quadrilateral • Uses Point class for each of its four corners • The corners are referred as ‘UpperRight’, ‘UpperLeft’, ‘LowerLeft’ and ‘LowerRight’

  21. Project 2 … cont • What you need to do ? • Accept x and y co-ordinates of four corners • Classify the quadrilateral as one of the five types • Compute and display its area and perimeter • Assume • Co-ordinate values are integers • Base is parallel to x axis • Area of irregular quadrilateral is 0.0000

  22. Project 2 … cont • Sample run (taken from Project 2 description) linux3[3]% Proj2 This is my optional greeting Please enter x- and y-coordinates of the UpperLeft corner: 6 4 Please enter x- and y-coordinates of the LowerLeft corner: 9 1 Please enter x- and y-coordinates of the LowerRight corner: 16 1 Please enter x- and y-coordinates of the UpperRight corner: 13 4 The Quadrilateral's Corners Upper Left: ( 6, 4) Upper Right: (13, 4) Lower Left: ( 9, 1) Lower Right: (16, 1) This Quadrilateral is a Parallelogram Area: 21.0000 Perimeter: 22.4853 linux3[4]%

More Related