Download presentation
Presentation is loading. Please wait.
Published byOsborne Caldwell Modified over 9 years ago
1
CMSC 202 Computer Science II for Majors
2
CMSC 202UMBC Topics Objects and Classes Project 2 description
3
CMSC 202UMBC 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
CMSC 202UMBC 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
CMSC 202UMBC 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 interface between object’s data and program
6
CMSC 202UMBC Class … cont Object User of objects Member Functions Member Variables ( Data ) An Interface
7
CMSC 202UMBC Class … cont Syntax class class-name { access-specifier : data and functions access-specifier : data and functions access-specifier : data and functions … } ;
8
CMSC 202UMBC 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
CMSC 202UMBC 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
CMSC 202UMBC 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
CMSC 202UMBC 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
CMSC 202UMBC 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
CMSC 202UMBC 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
CMSC 202UMBC 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
CMSC 202UMBC 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
CMSC 202UMBC 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
CMSC 202UMBC 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
CMSC 202UMBC Class … cont m_month m_day m_year m_hour m_minute m_second Date Object Time Object User Member Functions Member Func
19
CMSC 202UMBC 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
CMSC 202UMBC 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
CMSC 202UMBC 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
CMSC 202UMBC 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]%
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.