Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 31
Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 13
-
Upload
sameer-hane -
Category
Documents
-
view
22 -
download
2
Transcript of Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 13
Object Oriented ProgrammingObject Oriented Programming(OOP)(OOP)
Lecture No. 13Lecture No. 13
ReviewReview
►Static data membersStatic data members►Static member functionsStatic member functions►Array of objectsArray of objects
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
ExampleExample
class Student{class Student{
……
public:public:
Studen();Studen();
Student(char * aName);Student(char * aName);
void setRollNo(int aNo);void setRollNo(int aNo);
};};
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;
}}
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
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;
}}
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;
}}
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;
}}
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
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
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
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
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;
……};};
InterfacesInterfaces
► getDaygetDay► getMonthgetMonth► getYeargetYear► setDaysetDay► setMonthsetMonth► setYearsetYear
► addDayaddDay► addMonthaddMonth► addYearaddYear► setDefaultDatesetDefaultDate
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
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);……
……};};
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);……};};
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};};
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);
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
}}
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{{}}
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;
}}
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;
}}}}
Helper FunctionHelper Function
class Date{class Date{
……
private:private:
bool leapYear(int x) const;bool leapYear(int x) const;
……
};};
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;
}}
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;}}……
}}
RecapRecap