Docsity

OOP Lecture 13: Static Members, Object Pointers, and new Operator, Slides of Object Oriented Programming

Partial preview of the text

Download OOP Lecture 13: Static Members, Object Pointers, and new Operator and more Slides Object Oriented Programming in PDF only on Docsity!

Object Oriented Programming

(OOP)

Lecture No. 13

Review

  • Static data members
  • Static member functions
  • Array of objects

Example

class Student{

…

public:

Studen(); Student(char * aName); void setRollNo(int aNo);

};

Example

int main(){

Student obj; Student *ptr; ptr = &obj; ptr->setRollNo(10); return 0;

}

Example

int main(){

Student *ptr;

ptr = new Student;

ptr->setRollNo(10);

return 0;

Example

int main(){

Student *ptr;

ptr = new Student(“Ali”);

ptr->setRollNo(10);

return 0;

Breakup of new Operation

  • new operator is decomposed as

follows

  • Allocating space in memory
  • Calling the appropriate constructor

Case Study

Design a class date through which user must be able to perform following operations

  • Get and set current day, month and year
  • Increment by x number of days, months and year
  • Set default date

Attributes

  • The default date is a feature

shared by all objects

  • This attribute must be declared a static member

Attributes in Date.h

class Date

{

int day; int month; int year; static Date defaultDate;

…

};

Interfaces

  • As the default date is a static

member the interface

setDefaultDate should also be

declared static

Interfaces in Date.h

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

Constructors and

Destructors in Date.h Date(int aDay = 0, int aMonth= 0, int aYear= 0);

~Date(); //Destructor

};

Implementation of Date

Class

  • The static member variables must

be initialized

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