45
CSC241 Object-Oriented Programming (OOP) Lecture No. 7

CSC241 Object-Oriented Programming (OOP) Lecture No. 7

Embed Size (px)

Citation preview

Page 1: CSC241 Object-Oriented Programming (OOP) Lecture No. 7

CSC241 Object-Oriented Programming (OOP)

Lecture No. 7

Page 2: CSC241 Object-Oriented Programming (OOP) Lecture No. 7

Review

Constant data members

Constant objects

Static data members

Static member functions

Array of objects

Page 3: CSC241 Object-Oriented Programming (OOP) Lecture No. 7

Pointer to Objects

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

They can also be used to dynamically allocate objects

Page 4: CSC241 Object-Oriented Programming (OOP) Lecture No. 7

Example

class Student{

public:

Student();

Student(char * aName);

void setRollNo(int aNo);

};

Page 5: CSC241 Object-Oriented Programming (OOP) Lecture No. 7

Example

int main(){

Student obj;

Student *ptr;

ptr = &obj;

ptr->setRollNo(10);

return 0;

}

Page 6: CSC241 Object-Oriented Programming (OOP) Lecture No. 7

Allocation with new Operator new operator can be used to create objects at

runtime

Page 7: CSC241 Object-Oriented Programming (OOP) Lecture No. 7

Example

int main(){

Student *ptr;

ptr = new Student;

ptr->setRollNo(10);

return 0;

}

Page 8: CSC241 Object-Oriented Programming (OOP) Lecture No. 7

Example

int main(){

Student *ptr;

ptr = new Student(“Ali”);

ptr->setRollNo(10);

return 0;

}

Page 9: CSC241 Object-Oriented Programming (OOP) Lecture No. 7

Example

int main()

{

Student *ptr = new Student[100];

for (int i = 0; i < 100; i++)

{

ptr->setRollNo(10);

}

return 0;

}

Page 10: CSC241 Object-Oriented Programming (OOP) Lecture No. 7

Breakup of new Operation new operator is decomposed as follows

Allocating space in memory

Calling the appropriate constructor

Page 11: CSC241 Object-Oriented Programming (OOP) Lecture No. 7

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

Page 12: CSC241 Object-Oriented Programming (OOP) Lecture No. 7

Attributes

Attributes that can be seen in this problem statement are

Day

Month

Year

Default date

Page 13: CSC241 Object-Oriented Programming (OOP) Lecture No. 7

Attributes

The default date is a feature shared by all objects

This attribute must be declared a static member

Page 14: CSC241 Object-Oriented Programming (OOP) Lecture No. 7

Attributes in Date.h

class Date

{

int day;

int month;

int year;

static Date defaultDate;

};

Page 15: CSC241 Object-Oriented Programming (OOP) Lecture No. 7

Interfaces

getDay getMonth getYear setDay setMonth setYear

addDay addMonth addYear setDefaultDate

Page 16: CSC241 Object-Oriented Programming (OOP) Lecture No. 7

Interfaces

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

Page 17: CSC241 Object-Oriented Programming (OOP) Lecture No. 7

Interfaces in Date.h

class Date{

public:

void setDay(int aDay);

int getDay() const;

void addDay(int x);

};

Page 18: CSC241 Object-Oriented Programming (OOP) Lecture No. 7

Interfaces in Date.h

class Date{

public:

static void setDefaultDate(int aDay, int aMonth, int aYear);

};

Page 19: CSC241 Object-Oriented Programming (OOP) Lecture No. 7

Constructors and Destructors in Date.h

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

~Date(); //Destructor

};

Page 20: CSC241 Object-Oriented Programming (OOP) Lecture No. 7

Implementation of Date Class The static member variables must be initialized

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

Page 21: CSC241 Object-Oriented Programming (OOP) Lecture No. 7

Constructors

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

if (aDay == 0) {

this->day = defaultDate.day;

}

else{

setDay(aDay);

}

//similarly for other members (aMonth, aYear)

}

Page 22: CSC241 Object-Oriented Programming (OOP) Lecture No. 7

Destructor

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

Date::~Date

{

}

Page 23: CSC241 Object-Oriented Programming (OOP) Lecture No. 7

Getter and Setter

void Date::setMonth(int a){

if (a > 0 && a <= 12){

month = a;

}

}

int Date::getMonth() const{

return month;

}

Page 24: CSC241 Object-Oriented Programming (OOP) Lecture No. 7

addYearvoid Date::addYear(int x){

year += x;

if (day == 29 && month == 2 && !leapyear(year)){

day = 1;

month = 3;

}

}

Page 25: CSC241 Object-Oriented Programming (OOP) Lecture No. 7

Helper Function

class Date{

private:

bool leapYear(int x) const;

};

Page 26: CSC241 Object-Oriented Programming (OOP) Lecture No. 7

Helper Functionbool Date::leapYear(int x) const{

if ((x % 4 == 0 && x % 100 != 0) || (x % 400 == 0)){

return true;

}

return false;

}

Page 27: CSC241 Object-Oriented Programming (OOP) Lecture No. 7

setDefaultDate

void Date::setDefaultDate(int d, int m, int y){

if (d >= 0 && d <= 31){

day = d;

}

}

Page 28: CSC241 Object-Oriented Programming (OOP) Lecture No. 7

CompositionConsider the following implementation of the student class:

gpa : floatrollNo : intname : char *

Student(char * = NULL, int = 0, float = 0.0);

Student(const Student &)GetName() const : const char *SetName(char *) : void~Student()

Student

Page 29: CSC241 Object-Oriented Programming (OOP) Lecture No. 7

Compositionclass Student{

private:

float gpa;

char * name;

int rollNumber;

public:

Student(char * = NULL, int = 0, float = 0.0);

Student(const Student & st);

const char * GetName() const;

~Student();

};

Page 30: CSC241 Object-Oriented Programming (OOP) Lecture No. 7

CompositionStudent::Student(char * _name, int roll, floatg){

cout << "Constructor::Student..\n";

if (!_name){

name = new char[strlen(_name) + 1];

strcpy(name, _name);

}

else

name = NULL;

rollNumber = roll;

gpa = g;

}

Page 31: CSC241 Object-Oriented Programming (OOP) Lecture No. 7

Composition

Student::Student(const Student & st){

if (str.name != NULL){

name = new char[strlen(st.name) + 1];

strcpy(name, st.name);

}

else

name = NULL;

rollNumber = st.roll;

gpa = st.g;

}

Page 32: CSC241 Object-Oriented Programming (OOP) Lecture No. 7

Compositionconst char * Student::GetName(){

return name;

}

Student::~Student(){

delete[] name;

}

Page 33: CSC241 Object-Oriented Programming (OOP) Lecture No. 7

Composition►C++: “its all about code reuse”►Composition:

Creating objects of one class inside another class

►“Has a” relationship:Bird has a beakStudent has a name

Page 34: CSC241 Object-Oriented Programming (OOP) Lecture No. 7

CompositionConceptual notation:

String()SetString(char *) : voidGetString() const : const char *~String()…

gpa : floatrollNo : intname : StringStudent(char * = NULL, int = 0,

float = 0.0);Student(const Student &)GetName() const : StringGetNamePtr() const : const char *SetName(char *) : void~Student()

Student

string : char *

String

Page 35: CSC241 Object-Oriented Programming (OOP) Lecture No. 7

Compositionclass String{

private:

char * ptr;

public:

String();

String(const String &);

void SetString(char *);

const char * GetString() const;

~String()

};

Page 36: CSC241 Object-Oriented Programming (OOP) Lecture No. 7

CompositionString::String(){

cout << "Constructor::String..\n";

ptr = NULL;

}

String::String(const String & str){

if (str.ptr != NULL){

string = new char[strlen(str.ptr) + 1];

strcpy(ptr, str.ptr);

}

else ptr = NULL;

}

Page 37: CSC241 Object-Oriented Programming (OOP) Lecture No. 7

Composition

void String::SetString(char * str){

if (ptr != NULL){

delete[] ptr;

ptr = NULL;

}

if (str != NULL){

ptr = new char[strlen(str) + 1];

strcpy(ptr, str);

}

}

Page 38: CSC241 Object-Oriented Programming (OOP) Lecture No. 7

Compositionconst char * String::GetString()const{

return ptr;

}

String::~String(){

delete[] ptr;

cout << "Destructor::String..\n";

}

Page 39: CSC241 Object-Oriented Programming (OOP) Lecture No. 7

Compositionclass Student{

private:

float gpa;

int rollNumber;

String name;

public:

Student(char* = NULL, int = 0, float = 0.0);

Student(const Student &);

void SetName(const char *);

String GetName() const;

const char * GetNamePtr const();

~Student();

};

Page 40: CSC241 Object-Oriented Programming (OOP) Lecture No. 7

CompositionStudent::Student(char * _name, int roll, float g){

cout << "Constructor::Student..\n";

name.SetString(_name);

rollNumber = roll;

gpa = g;

}

Page 41: CSC241 Object-Oriented Programming (OOP) Lecture No. 7

CompositionStudent::Student(const Student & s){

name.Setname(s.name.GetString());

gpa = s.gpa;

rollNo = s.rollNo;

}

const char * Student::GetNamePtr() const{

return name.GetString();

}

Page 42: CSC241 Object-Oriented Programming (OOP) Lecture No. 7

Compositionvoid Student::SetName(const char * n){

name.SetString(n);

}

Student::~Student(){

cout << "Destructor::Student..\n";

}

Page 43: CSC241 Object-Oriented Programming (OOP) Lecture No. 7

CompositionMain Function:

void main(){

Student aStudent("Fakhir", 899, 3.1);

cout << endl;

cout << “Name:” << aStudent.GetNamePtr() << “\n”;

}

Page 44: CSC241 Object-Oriented Programming (OOP) Lecture No. 7

CompositionOutput:

Constructor::String..

Constructor::Student..

Name: Fakhir

Destructor::Student..

Destructor::String..

Page 45: CSC241 Object-Oriented Programming (OOP) Lecture No. 7

Composition

►Constructors of the sub-objects are always executed before the constructors of the master class

►Example:Constructor for the sub-object name is executed before the constructor of Student