Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 13

28
Object Oriented Programming Object Oriented Programming (OOP) (OOP) Lecture No. 13 Lecture No. 13

Transcript of Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 13

Page 1: Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 13

Object Oriented ProgrammingObject Oriented Programming(OOP)(OOP)

Lecture No. 13Lecture No. 13

Page 2: Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 13

ReviewReview

►Static data membersStatic data members►Static member functionsStatic member functions►Array of objectsArray of objects

Page 3: Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 13

Pointer to ObjectsPointer to Objects

►Pointer to objects are similar as Pointer to objects are similar as pointer to built-in typespointer to built-in types

►They can also be used to dynamically They can also be used to dynamically allocate objectsallocate objects

Page 4: Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 13

ExampleExample

class Student{class Student{

……

public:public:

Studen();Studen();

Student(char * aName);Student(char * aName);

void setRollNo(int aNo);void setRollNo(int aNo);

};};

Page 5: Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 13

ExampleExample

int main(){int main(){

Student obj;Student obj;

Student *ptr;Student *ptr;

ptr = &obj;ptr = &obj;

ptr->setRollNo(10);ptr->setRollNo(10);

return 0;return 0;

}}

Page 6: Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 13

Allocation with new OperatorAllocation with new Operator

►new operator can be used to create new operator can be used to create objects at runtimeobjects at runtime

Page 7: Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 13

ExampleExample

int main(){int main(){

Student *ptr;Student *ptr;

ptr = new Student;ptr = new Student;

ptr->setRollNo(10);ptr->setRollNo(10);

return 0;return 0;

}}

Page 8: Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 13

ExampleExample

int main(){int main(){

Student *ptr;Student *ptr;

ptr = new Student(“Ali”);ptr = new Student(“Ali”);

ptr->setRollNo(10);ptr->setRollNo(10);

return 0;return 0;

}}

Page 9: Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 13

ExampleExample

int main()int main(){{

Student *ptr = new Student[100];Student *ptr = new Student[100];for(int i = 0; i < 100;i++)for(int i = 0; i < 100;i++){{

ptr->setRollNo(10);ptr->setRollNo(10);}}return 0;return 0;

}}

Page 10: Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 13

Breakup of new OperationBreakup of new Operation

►new operator is decomposed as new operator is decomposed as followsfollows Allocating space in memoryAllocating space in memory Calling the appropriate constructorCalling the appropriate constructor

Page 11: Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 13

Case StudyCase Study

Design a class date through which user must Design a class date through which user must be able to perform following operationsbe able to perform following operations Get and set current day, month and yearGet and set current day, month and year Increment by x number of days, months and yearIncrement by x number of days, months and year Set default dateSet default date

Page 12: Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 13

AttributesAttributes

►Attributes that can be seen in this Attributes that can be seen in this problem statement areproblem statement are DayDay MonthMonth YearYear Default dateDefault date

Page 13: Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 13

AttributesAttributes

►The default date is a feature shared by The default date is a feature shared by all objectsall objects This attribute must be declared a static This attribute must be declared a static

membermember

Page 14: Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 13

Attributes in Date.hAttributes in Date.h

class Dateclass Date{{int day;int day;int month;int month;int year;int year;static Date defaultDate;static Date defaultDate;

……};};

Page 15: Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 13

InterfacesInterfaces

► getDaygetDay► getMonthgetMonth► getYeargetYear► setDaysetDay► setMonthsetMonth► setYearsetYear

► addDayaddDay► addMonthaddMonth► addYearaddYear► setDefaultDatesetDefaultDate

Page 16: Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 13

InterfacesInterfaces

►As the default date is a static member As the default date is a static member the interface setDefaultDate should the interface setDefaultDate should also be declared staticalso be declared static

Page 17: Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 13

Interfaces in Date.hInterfaces in Date.h

class Date{class Date{……public:public:void setDay(int aDay);void setDay(int aDay);int getDay() const;int getDay() const;void addDay(int x);void addDay(int x);……

……};};

Page 18: Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 13

Interfaces in Date.hInterfaces in Date.h

class Date{class Date{……public:public:static void setDefaultDate(static void setDefaultDate(

int aDay,int aMonth, int aYear);int aDay,int aMonth, int aYear);……};};

Page 19: Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 13

Constructors and Destructors in Constructors and Destructors in Date.hDate.h

Date(int aDay = 0, Date(int aDay = 0, int aMonth= 0, int aYear= 0);int aMonth= 0, int aYear= 0);

~Date(); //Destructor~Date(); //Destructor};};

Page 20: Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 13

Implementation of Date ClassImplementation of Date Class

►The static member variables must be The static member variables must be initializedinitialized

Date Date::defaultDate (07,3,2005);Date Date::defaultDate (07,3,2005);

Page 21: Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 13

ConstructorsConstructors

Date::Date(int aDay, int aMonth, Date::Date(int aDay, int aMonth, int aYear) {int aYear) {

if(aDay==0) {if(aDay==0) {this->day = defaultDate.day;this->day = defaultDate.day;

}}else{else{

setDay(aDay);setDay(aDay);}}//similarly for other members//similarly for other members

}}

Page 22: Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 13

DestructorDestructor

►We are not required to do any house We are not required to do any house keeping chores in destructorkeeping chores in destructor

Date::~DateDate::~Date{{}}

Page 23: Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 13

Getter and SetterGetter and Setter

void Date::setMonth(int a){void Date::setMonth(int a){if(a > 0 && a <= 12){if(a > 0 && a <= 12){

month = a;month = a;}}int getMonth() const{int getMonth() const{return month;return month;

}}

Page 24: Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 13

addYearaddYear

void Date::addYear(int x){void Date::addYear(int x){year += x;year += x;if(day == 29 && month == 2if(day == 29 && month == 2

&& && !leapyear(year)){!leapyear(year)){day = 1;day = 1;month = 3;month = 3;

}}}}

Page 25: Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 13

Helper FunctionHelper Function

class Date{class Date{

……

private:private:

bool leapYear(int x) const;bool leapYear(int x) const;

……

};};

Page 26: Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 13

Helper FunctionHelper Function

bool Date::leapYear(int x) const{bool Date::leapYear(int x) const{if((x%4 == 0 && x%100 != 0)if((x%4 == 0 && x%100 != 0)

|||| (x%400==0)){(x%400==0)){return true;return true;

}}return false;return false;

}}

Page 27: Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 13

setDefaultDatesetDefaultDate

void Date::setDefaultDate( void Date::setDefaultDate( int d, int m, int y){int d, int m, int y){if(d >= 0 && d <= 31){if(d >= 0 && d <= 31){

day = d;day = d;}}……

}}

Page 28: Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 13

RecapRecap