Object Oriented Programming (OOP) Lecture No. 13

Slides:



Advertisements
Similar presentations
Chapter 4 Constructors and Destructors. Objectives Constructors – introduction and features The zero-argument constructor Parameterized constructors Creating.
Advertisements

Introduction to Programming Lecture 39. Copy Constructor.
EC-241 Object-Oriented Programming
C++ Classes & Data Abstraction
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.
Object Oriented Programming.  OOP Basic Principles  C++ Classes  September 2004  John Edgar 22.
Engineering Problem Solving With C++ An Object Based Approach Chapter 8 Introduction to Classes.
1 Class Constructors a class constructor is a member function whose purpose is to initialize the private data members of a class object the name of a constructor.
Classes in C++ Bryce Boe 2012/08/15 CS32, Summer 2012 B.
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
More C++ Bryce Boe 2013/07/18 CS24, Summer 2013 C.
Programming Languages and Paradigms Object-Oriented Programming.
CSC241 Object-Oriented Programming (OOP) Lecture No. 12.
Polymorphism. Introduction ‘one name multiple forms’ Implemented using overloaded functions and operators Early binding or static binding or static linking.
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 Inside the Vector Class: with additional needed methods CPS212CPS212 Gordon College.
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. 7.
Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315.
CSC241 Object-Oriented Programming (OOP) Lecture No. 5.
1 CSC241: Object Oriented Programming Lecture No 05.
1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh.
Class Definitions and Writing Methods Chapter 3 3/31/16 & 4/4/16.
Abstract classes only used as base class from which other classes can be inherit cannot be used to instantiate any objects are incomplete Classes that.
1 Lecture Material Declarations and definitions of classes in C++ Member functions and variables Constructors The this pointer Destructor Function and.
Object Oriented Programming (OOP) Lecture No.2 Downloaded From:
Yan Shi CS/SE 2630 Lecture Notes
Object-Oriented Programming (OOP) Lecture No. 15
CSC241: Object Oriented Programming
Chapter 9 Type Conversions
CMPE 135: Object-Oriented Analysis and Design September 14 Class Meeting Department of Computer Engineering San Jose State University Fall 2017 Instructor:
Andy Wang Object Oriented Programming in C++ COP 3330
Object Oriented Programming (OOP) Lecture No. 8
Class Definitions and Writing Methods
CISC181 Introduction to Computer Science Dr
"A class is where we teach an object how to behave." --Rich Pattis
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Class Operations Pointer and References with class types
CS Computer Science IB: Object Oriented Programming
CMPE Data Structures and Algorithms in C++ February 22 Class Meeting
solve the following problem...
Memberwise Assignment / Initialization
HKCT Java OOP Unit 02 Object Oriented Programming in Java Unit 02 Methods, Classes, and Objects 1.
// simple Date // guarantee initialization with constructor // provide some notational convenience struct Date {           int y, m, d;                            //
Object-Oriented Programming (OOP) Lecture No. 36
Object Oriented Programming (OOP) Lecture No. 9
Classes Short Review of Topics already covered Constructor
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
How Classes Work with Memory Diagram
Introduction of Programming
Lecture 16 Oct 30, 02.
C++ Programming ㅎㅎ String OOP Class Constructor & Destructor.
Object oriented programming (OOP) Lecture No. 7
CS148 Introduction to Programming II
CMSC202 Computer Science II for Majors Lecture 07 – Classes and Objects (Continued) Dr. Katherine Gibson Based on slides by Chris Marron at UMBC.
NAME 436.
Data Structures & Algorithms
Operator Overloading; String and Array Objects
Object Oriented Programming (OOP) Lecture No. 11
Object oriented programming (OOP) Lecture No. 6
Object-Oriented Programming (OOP) Lecture No. 23
How Dynamic Memory Works with Memory Diagram
How Classes Work with Memory Diagram
Object Oriented Programming (OOP) Lecture No. 10
Lecture 4 – Data collection List ADT
Presentation transcript:

Object Oriented Programming (OOP) Lecture No. 13

Review 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: Studen(); 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,2005);

Constructors Date::Date(int aDay, int aMonth, int aYear) { if(aDay==0) { this->day = defaultDate.day; } else{ setDay(aDay); //similarly for other members

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

Recap