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

Slides:



Advertisements
Similar presentations
Introduction to Programming Lecture 39. Copy Constructor.
Advertisements

This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
OBJECT ORIENTED PROGRAMMING Instructor: Rashi Garg Coordinator: Gaurav Saxena.
Classes & Objects classes member data and member function access control and data hiding instantiation of objects class constructor and destructor objects.
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
Copy Constructors Shallow Copy: –The data members of one object are copied into the data members of another object without taking any dynamic memory pointed.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Programming Languages and Paradigms Object-Oriented Programming.
Chapter 10 Defining Classes. User-defined Types Are data types defined by programmers. Include: – typedef: simple data definition – struct: a structure.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
CSC241 Object-Oriented Programming (OOP) Lecture No. 12.
CSC241 Object-Oriented Programming (OOP) Lecture No. 10.
 200 Total Points ◦ 74 Points Writing Programs ◦ 60 Points Tracing Algorithms and determining results ◦ 36 Points Short Answer ◦ 30 Points Multiple Choice.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
Object Oriented Programming Lect. Dr. Daniel POP Universitatea de Vest din Timişoara Facultatea de Matematică şi Informatică.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
SEN 909 OO Programming in C++ Final Exam Multiple choice, True/False and some minimal programming will be required.
Dale Roberts 1 Classes Constructors & Destructors Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer.
C++ Lecture 4 Tuesday, 15 July Struct & Classes l Structure in C++ l Classes and data abstraction l Class scope l Constructors and destructors l.
1 CSC241: Object Oriented Programming Lecture No 22.
Pointers and Dynamic Memory Allocation Copyright Kip Irvine 2003, all rights reserved. Revised 10/28/2003.
CSC241 Object-Oriented Programming (OOP) Lecture No. 8.
Object Oriented Programming (OOP) Lecture No. 11.
Object Oriented Programming (OOP) Lecture No. 10.
Object Oriented Programming (OOP) Lecture No. 8. Review ► Class  Concept  Definition ► Data members ► Member Functions ► Access specifier.
CSC241 Object-Oriented Programming (OOP) Lecture No. 6.
CSC241 Object-Oriented Programming (OOP) Lecture No. 4.
More C++ Features True object initialisation
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
1 Overview of C++ CS Data Structure. 2 Value parameters int abc (int a, int b, int c) // a, b, and c are the { // formal parameters a = a * 2; return.
2 Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Chapter 11 Friends and Overloaded Operators. Introduction to function equal // Date.h #ifndef _DATE_H_ #define _DATE_H_ class CDate { public: CDate();
C++ Lecture 5 Monday, 18 July Chapter 7 Classes, continued l const objects and const member functions l Composition: objects as members of classes.
CSC241 Object-Oriented Programming (OOP) Lecture No. 5.
1 CSC241: Object Oriented Programming Lecture No 05.
Introduction to Programming Lecture 40. Class Class is a user defined data type.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
C ++ Programming Languages Omid Jafarinezhad Lecturer: Omid Jafarinezhad Fall 2013 Lecture 3 Department of Computer Engineering 1.
1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh.
1 Memory as byte array Pointers Arrays relationship to pointers Operator ‘new’ Operator ‘delete’ Copy ctor Assignment operator ‘this’ const pointer Allocating.
CIS162AD Constructors Part 2 08_constructors.ppt.
Previous lecture Introduction to OOP and C++ Data Abstraction –String example.
1 Recall that... char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
CSC241 Object-Oriented Programming (OOP) Lecture No. 14.
Programming Fundamentals1 Chapter 7 INTRODUCTION TO CLASSES.
Dynamic Memory Management & Static Class Members Lecture No 7 Object Oriented Programming COMSATS Institute of Information Technology.
MAITRAYEE MUKERJI Object Oriented Programming in C++: Hierarchy / Inheritance.
Object-Oriented Programming (OOP) Lecture No. 24.
Object Oriented Programming (OOP) Lecture No.2 Downloaded From:
Object-Oriented Programming (OOP) Lecture No. 15
Object Oriented Programming (OOP) Lecture No. 8
CSC241: Object Oriented Programming
Chapter 15 Pointers, Dynamic Data, and Reference Types
Object Oriented Programming (OOP) Lecture No. 9
Object-Oriented Programming (OOP) Lecture No. 14
Department of Computer and Information Science, School of Science, IUPUI CSCI 265 Classes Dale Roberts, Lecturer Computer Science, IUPUI
How Dynamic Memory Works with Memory Diagram
Object Oriented Programming (OOP) Lecture No. 13
Introduction of Programming
Object-Oriented Programming (OOP) Lecture No. 22
Web Design & Development Lecture 4
Object Oriented Programming (OOP) Lecture No. 11
Object-Oriented Programming (OOP) Lecture No. 23
How Dynamic Memory Works with Memory Diagram
Destructors, Copy Constructors & Copy Assignment Operators
Destructors, Copy Constructors & Copy Assignment Operators
Object Oriented Programming (OOP) Lecture No. 10
Presentation transcript:

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

Review  Constant data members  Constant objects  Static data members  Static member functions  Array of objects

Pointer to Objects  Pointer to objects are similar as pointer to built-in types  They can also be used to dynamically allocate objects

Example class Student{ … public: Student(); Student(char * aName); void setRollNo(int aNo); };

Example int main(){ Student obj; Student *ptr; ptr = &obj; ptr->setRollNo(10); return 0; }

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

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; }

Example int main() { Student *ptr = new Student[100]; for (int i = 0; i < 100; i++) { 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  Attributes that can be seen in this problem statement are  Day  Month  Year  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  getDay  getMonth  getYear  setDay  setMonth  setYear  addDay  addMonth  addYear  setDefaultDate

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); … … };

Interfaces in Date.h class Date{ … public: static void setDefaultDate(int aDay, int aMonth, int aYear); … };

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, 2015);

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) }

Destructor  We are not required to do any house keeping chores in destructor Date::~Date { }

Getter and Setter void Date::setMonth(int a){ if (a > 0 && a <= 12){ month = a; } } int Date::getMonth() const{ return month; }

addYear void Date::addYear(int x){ year += x; if (day == 29 && month == 2 && !leapyear(year)){ day = 1; month = 3; } }

Helper Function class Date{ … private: bool leapYear(int x) const; … };

Helper Function bool Date::leapYear(int x) const{ if ((x % 4 == 0 && x % 100 != 0) || (x % 400 == 0)){ return true; } return false; }

setDefaultDate void Date::setDefaultDate(int d, int m, int y){ if (d >= 0 && d <= 31){ day = d; } … }

Composition Consider the following implementation of the student class: gpa : float rollNo : int name : char * Student(char * = NULL, int = 0, float = 0.0); Student(const Student &) GetName() const : const char * SetName(char *) : void ~Student() … Student

Composition class 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(); … };

Composition Student::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; }

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; }

Composition const char * Student::GetName(){ return name; } Student::~Student(){ delete[] name; }

Composition ► C++: “ its all about code reuse” ► Composition:  Creating objects of one class inside another class ► “Has a” relationship:  Bird has a beak  Student has a name

Composition Conceptual notation: String() SetString(char *) : void GetString() const : const char * ~String() … gpa : float rollNo : int name : String Student(char * = NULL, int = 0, float = 0.0); Student(const Student &) GetName() const : String GetNamePtr() const : const char * SetName(char *) : void ~Student() … Student string : char * String

Composition class String{ private: char * ptr; public: String(); String(const String &); void SetString(char *); const char * GetString() const; ~String() … };

Composition String::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; }

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); } }

Composition const char * String::GetString()const{ return ptr; } String::~String(){ delete[] ptr; cout << "Destructor::String..\n"; }

Composition class 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(); … };

Composition Student::Student(char * _name, int roll, float g){ cout << "Constructor::Student..\n"; name.SetString(_name); rollNumber = roll; gpa = g; }

Composition Student::Student(const Student & s){ name.Setname(s.name.GetString()); gpa = s.gpa; rollNo = s.rollNo; } const char * Student::GetNamePtr() const{ return name.GetString(); }

Composition void Student::SetName(const char * n){ name.SetString(n); } Student::~Student(){ cout << "Destructor::Student..\n"; }

Composition Main Function: void main(){ Student aStudent("Fakhir", 899, 3.1); cout << endl; cout << “Name:” << aStudent.GetNamePtr() << “\n”; }

Composition Output: Constructor::String.. Constructor::Student.. Name: Fakhir Destructor::Student.. Destructor::String..

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