220 likes | 318 Views
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
E N D
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 • Programming problem is in terms of objects and communication between them
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.
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
Class … cont • An Interface Member Variables ( Data ) User of objects Member Functions Object
Class … cont • Syntax • class class-name { • access-specifier : • data and functions • access-specifier : • data and functions • access-specifier : • data and functions • … • } ;
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
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; };
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; }
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 ?
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; };
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 }
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
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; };
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 };
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
Class … cont m_month m_day m_year User Member Functions m_hour m_minute m_second Member Func Time Object Date Object
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
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’
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
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]%