Docsity

Pointer to Objects-Object Oriented Programming-Lecture Slides, Slides of Object Oriented Programming

Partial preview of the text

Download Pointer to Objects-Object Oriented Programming-Lecture Slides 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

Example

int

main(){

Student

*ptr;

ptr

new

Student(“Ali”);

ptr->setRollNo(10);return

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 ableto 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 allobjects^ 

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 theinterface setDefaultDate should also bedeclared 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 beinitialized

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