Computer Notes - Date Class

Embed Size (px)

Citation preview

  • 8/3/2019 Computer Notes - Date Class

    1/25

    Date Class

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Date Class

    2/25

    Date Classclass Date{int day, month, year;

    static Date defaultDate;

    public:

    void SetDay(int aDay);int GetDay() const;

    void AddDay(int x);

    static void SetDefaultDate(

    int aDay,int aMonth, int aYear);

  • 8/3/2019 Computer Notes - Date Class

    3/25

    Date Class...private:

    bool IsLeapYear();};

    int main(){

    Date aDate;

    aDate.IsLeapYear(); //Error

    return 0;

    }

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Date Class

    4/25

    Creating SpecialDate Class

    Special Date

    Date Special Date

    AddSpecialYear...

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Date Class

    5/25

    Creating SpecialDate Classclass SpecialDate: public Date{

    public:void AddSpecialYear(int i){

    ...

    if(day == 29 && month == 2&& !IsLeapyear(year+i)){ //ERROR!

    ...

    }

    }

    };

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Date Class

    6/25

    Modify Access Specifier We can modify access specifier

    IsLeapYear from private to public

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Date Class

    7/25

    Modified Date Classclass Date{

    public:

    ...

    bool IsLeapYear();

    };

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Date Class

    8/25

    Modified AddSpecialYearvoid SpecialDate :: AddSpecialYear

    (int i){...

    if(day == 29 && month == 2&& !IsLeapyear(year+i)){

    ...}

    }http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Date Class

    9/25

    Protected members Protected members can not be

    accessed outside the class Protected members of base class

    become protected member ofderived class in Public inheritance

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Date Class

    10/25

    Modified Date Classclass Date{

    protected:bool IsLeapYear();

    };

    int main(){

    Date aDate;

    aDate.IsLeapYear(); //Error

    return 0;

    }

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Date Class

    11/25

    Modified AddSpecialYearvoid SpecialDate :: AddSpecialYear

    (int i){...

    if(day == 29 && month == 2&& !IsLeapyear(year+i)){

    ...

    }

    }http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Date Class

    12/25

    Disadvantages

    Breaks encapsulation

    The protected member is part of base

    classs implementation as well as

    derived classs implementation

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Date Class

    13/25

    IS A Relationship Public inheritance models the IS A

    relationship

    Derived object IS A kind of base

    object

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Date Class

    14/25

    Exampleclass Person {char * name;

    public: ...const char * GetName();

    };

    class Student: public Person{

    int rollNo;

    public: ...int GetRollNo();

    };http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Date Class

    15/25

    Exampleint main()

    {

    Student sobj;

    cout

  • 8/3/2019 Computer Notes - Date Class

    16/25

    IS A Relationship The base class pointer can point

    towards an object of derived class

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Date Class

    17/25

    int main(){

    Person * pPtr = 0;Student s;

    pPtr = &s;cout GetName();

    return 0;

    }

    Example

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Date Class

    18/25

    ExamplepPtr = &s;

    derived member1

    derived member2...

    base member1

    base member2

    ...

    spPtr

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Date Class

    19/25

    int main(){

    Person * pPtr = 0;Student s;

    pPtr = &s;//Error

    cout GetRollNo();

    return 0;

    }

    Example

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Date Class

    20/25

    Static Type The type that is used to declare a

    reference or pointer is called itsstatic type

    The static type of pPtr is Person

    The static type of s is Student

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Date Class

    21/25

    Member Access The access to members is

    determined by static type The static type of pPtr is Person

    Following call is erroneous

    pPtr->GetRollNo();

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Date Class

    22/25

    IS A Relationship We can use a reference of derived

    object where the reference of baseobject is required

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Date Class

    23/25

    Exampleint main(){

    Person p;Student s;

    Person & refp = s;

    cout

  • 8/3/2019 Computer Notes - Date Class

    24/25

    Examplevoid Play(const Person& p){cout

  • 8/3/2019 Computer Notes - Date Class

    25/25

    Exampleint main(){

    Person p;Student s;

    Play(p);Play(s);

    return 0;

    }http://ecomputernotes.com