CMSC 202 UMBC
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
CMSC 202 UMBC
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.
CMSC 202 UMBC
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
CMSC 202 UMBC
Class … cont
Object
User
of objects
Mem
ber
Fu
nct
ion
s
Member
Variables
( Data )
• An Interface
CMSC 202 UMBC
Class … cont
Syntaxclass class-name {
access-specifier : data and functionsaccess-specifier : data and functionsaccess-specifier : data and functions
…} ;
CMSC 202 UMBC
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
CMSC 202 UMBC
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;
};
CMSC 202 UMBC
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;}
CMSC 202 UMBC
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 ?
CMSC 202 UMBC
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 membersint m_month;int m_day;int m_year;
};
CMSC 202 UMBC
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
}
CMSC 202 UMBC
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
CMSC 202 UMBC
Class … cont
class Time {public :
// Constructor and all member function will come // here
// Function to increment time by one secondvoid Increment ( void );
private :int m_hour; int m_minute; int m_second;
};
CMSC 202 UMBC
Class … cont
class Date {
public :// Constructor and all member function will come // here
// Add member function Tick which calls Incrementvoid Tick ( void );
private : Time currentTime; // Declare Time object in Date// other data members
};
CMSC 202 UMBC
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
CMSC 202 UMBC
Class … cont
m_month
m_day
m_year
m_hour
m_minute
m_second
Date Object
Time Object
User
Mem
ber
Fun
ctio
ns
Mem
ber
Fun
c
CMSC 202 UMBC
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
CMSC 202 UMBC
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’
CMSC 202 UMBC
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
CMSC 202 UMBC
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]%