EC-241 Object-Oriented Programming

Slides:



Advertisements
Similar presentations
Complete Structure class Date {class Date { private :private : // private data and functions// private data and functions public :public : // public data.
Advertisements

CPS 235 Object Oriented Programming Paradigm
Operator overloading redefine the operations of operators
Lecture 14 Today: Overloading: Revision on this Revision on increment operators the assignment operator the [] operator Book: p , 215,
Pass by Value. COMP104 Pass by Value / Slide 2 Passing Parameters by Value * A function returns a single result (assuming the function is not a void function)
Starting Out with C++, 3 rd Edition 1 Chapter 14 – More About Classes.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14: More About Classes.
Copyright © 2012 Pearson Education, Inc. Chapter 14: More About Classes.
Lesson 14 Classes, Continued CS1 Lesson More Classes1.
Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
Introduction to Programming Lecture 15. In Today’s Lecture Pointers and Arrays Manipulations Pointers and Arrays Manipulations Pointers Expression Pointers.
1 CSC241: Object Oriented Programming Lecture No 21.
Overloading Operators Overloading operators Unary operators Binary operators Member, non-member operators Friend functions and classes Function templates.
1 Chapter 11 Introducing the Class Pages ( )
Chapter 8. Operator Overloading Operator overloading gives the opportunity to redefine C++ Operator overloading refers to redefine C++ operators such.
C++ Classes & Data Abstraction
Win32 Programming Lesson 4: Classes and Structures.
CS 106 Introduction to Computer Science I 03 / 24 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 26 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 23 / 2007 Instructor: Michael Eckmann.
Object Oriented Programming I
1 CSC241: Object Oriented Programming Lecture No 06.
LECTURE 4. Composition Definition: A data member of a class is an object of some other class Example: an AlarmClock object needs to know when it is supposed.
Dale Roberts Object Oriented Programming using Java - Final and Static Keywords Dale Roberts, Lecturer Computer Science, IUPUI
Some odds and ends about Classes and Objects. 6-2 Object-Oriented Programming Object Data (Fields) Methods That Operate on the Data.
C++ Lecture 5 Monday, 18 July Chapter 7 Classes, continued l const objects and const member functions l Composition: objects as members of classes.
1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh.
Constructors, Copy Constructors, constructor overloading, function overloading Lecture 04.
1 CSC241: Object Oriented Programming Lecture No 08.
Dynamic Memory Management & Static Class Members Lecture No 7 Object Oriented Programming COMSATS Institute of Information Technology.
IIT Bombay Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering IIT Bombay Session: Friends.
Static Variables. Function Scope  “Normal” local variables go out of scope and are deallocated when a function terminates.
Object-Oriented Programming Using C++ Third Edition Chapter 7 Using Classes.
LECTURE 3 PASS BY REFERENCE. METHODS OF PASSING There are 3 primary methods of passing arguments to functions:  pass by value,  pass by reference, 
Lecture 6 Object Oriented Programming Using Java
Class and Object Cont’d
9.1 Class (static) Variables and Methods
Examples of Classes & Objects
CSC241: Object Oriented Programming
Creational Pattern: Prototype
CISC181 Introduction to Computer Science Dr
Object-Oriented Programming Using C++
Templates.
Object-Oriented Programming (OOP) Lecture No. 32
Static Data Member and Functions
More Object Oriented Programming
Object-Oriented Programming (OOP) Lecture No. 39
Object-Oriented Programming
Programming -2 برمجة -2 المحاضرة-7 Lecture-7.
Classes & Objects: Examples
Static in Classes CSCE 121 J. Michael Moore.
Object Oriented Programming Using C++
Operator Overloading.
Static is one of the modifiers that determine variable and method characteristics. The static modifier associates a variable or method with its class.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Object-Oriented Programming (OOP) Lecture No. 38
Object-Oriented Programming Using C++
Introduction to Programming
Object-Oriented Programming
foo.h #ifndef _FOO #define _FOO template <class T> class foo{
Submitted By : Veenu Saini Lecturer (IT)
Eighth step for Learning C++ Programming
The Stack.
STATIC DATA MEMBER & MEMBER FUNCTIONS
Lab – 2018/04/12 implement getTotalCount to return how many ‘nMatrix’s are allocated(exist) (modify constructor and destructor, use static variable and.
Object-Oriented Programming (OOP) Lecture No. 34
Object-Oriented Programming (OOP) Lecture No. 23
Constructors and Deconstructor
COP 3330 Object-oriented Programming in C++
Classes Member Qualifiers
Presentation transcript:

EC-241 Object-Oriented Programming LECTURE

static Data Members Normally, each object maintains its own copy of data members. However, if some data item in a class is declared static, only one such item is created for the entire class, no matter how many objects are there. A static data item is useful when all objects of the same class must share a common item of information.

static Data Members A static data member exists even if there are no objects of the class instantiated. Static class member data is used to share information among the objects of a class.

class foo { private: static int count; //only one data item for all objects //Note: declaration only public: foo() {count++; } //increments count when object created int getcount() {return count; } }; int foo::count=0; void main() foo f1, f2, f3; cout<<f1.getcount()<<f2.getcount()<<f3.getcount(); //each object sees the same value }

Inheritance A form of software reuse in which you create a class that absorbs an existing class’s data and behavior and enhances them with new capabilities. In inheritance, a class derives the behavior and structure of another (existing) class. Advantages Saves time Reuse of proven, debugged, high quality software

Inheritance Base Class Defined in Derived Class Feature A Feature B Base Class Feature C Defined in Derived Class Feature B Defined in Base Class but accessible from derived class Feature A Derived Class

Inheritance Base and Derived classes. A derived class inherits the members of its base class. Friends are not inherited Syntax class DerivedClass : kind BaseClass where kind is one of public, private or protected.

Inheritance Examples Base Class Derived Classes Student PGStudent, UGS tudent Shape Circle, Triangle, Rectangle, Sphere, Cube Loan CarLoan, HomeImprovementLoan, MortgageLoan Employee Faculty, Staff Account CurrentAccount, SavingsAccount

Example: Inheritance Hierarchy for university CommunityMember SS Single Inheritance Employee Student Alumnus SS Single Inheritance Faculty Staff Starting from the bottom, each arrow in the hierarchy represents an IS-A relationship SS Single Inheritance Teacher Administrator SS Multiple Inheritance AdministratorTeacher

Kinds of Inheritance Public (Commonly used) Public and protected members of the base class remain, respectively, public and protected members of the derived class. Protected Public and protected members of the base class are inherited as protected members of the derived class. Private Public and protected members of the base class are inherited as private members of the derived class. In all cases, private members of a base class remain private to the base class and cannot be accessed directly by a derived class.

Base class member access specifier protected inheritance Kind of Inheritance public inheritance protected inheritance private inheritance public public in derived class Can be accessed directly by member functions, friend functions, and non-member functions protected in derived class Can be accessed directly by member functions and friend functions private in derived class protected private Hidden in derived class Can be accessed by member functions and friend functions through public or protected member functions of the base class

protected Access Specifier Note: Not to be confused with protected Inheritance A base class’s protected members can be accessed within that base class, by friends and members of that class, and by members and friends of any classes derived from that base class. Offers an intermediate level of protection between public and private access

Access Specifier Accessible from Own class? Accessible from Derived class? Accessible from objects outside the class? public yes protected no private

class counter { protected: int count; public: counter(): count(0) { } counter (int c): count(c){ } int getcount() const { return count; } counter operator ++ () //prefix return counter (++count); } }; class countDn : public counter { public: counter operator –() //prefix { return counter (--count); } }; void main() countDn c1; cout<<c1.getcount(); //0 ++c1; cout<<c1.getcount(); --c1; cout<<c1.getcount(); }

In the previous example we cannot write: countDn c2; c2= --c1; Why?

Answer: You cannot assign a base class object to a derived class object (Reason) Unclean behavior w.r.t. added members in the derived class Note: … but you can assign a derived class object to a base class object (because a derived class object IS A base class object)

INITIALIZING DERIVED CLASS OBJECTS THROUGH DERIVED CLASS CONSTRUCTORS class counter { protected: int count; public: counter(): count (0) {} counter(int c): count(c){} int getcount() const { return count; } counter operator ++() {return counter (++count);} }; class countDn: public counter { public: countDn():counter() { } countDn(int c): counter (c){ } countDn operator – () { return countDn(--count); } }; void main() { countDn c1, c2(100), c3; cout<<c1.getcount(); ++c1; cout<<c1.getcount(); c3= --c2; cout<<c3.getcount(); }

Overriding Member Functions Overriding refers to re-defining the base class member functions within the derived classes. When a derived class has an over-riding version of a base class function, the base class version can still be accessed using the binary scope resolution operator (::)

OVERRIDING MEMBER FUNCTIONS #include <cstdlib> class stack { protected: int a[10]; int top; public: array() { top=-1; } void push(int var) { a[++top]=var; } int pop() { return a[top--]; } }; class stack2 : public stack { public: void push(int var) { if (top>=9) { cout<<“Error: stack full”; exit(1); } stack::push(var);} int pop() { if (top < 0) { cout<<“Error: stack empty”; exit(1);} return stack::pop ();}}; void main() { stack2 s; s.push(15); s.push(10); cout<<s.pop(); }

Order of Execution of Constructors and Destructors in Inheritance When a program creates a derived-class object, the derived-class constructor immediately calls the base-class constructor; the base-class constructor’s body executes, then the derived-class’s member initializers execute and finally the derived-class constructor’s body executes. This process cascades up the hierarchy if it contains more than two levels.

Order of Execution of Constructors and Destructors in Inheritance When a derived-class object is destroyed, the program calls that object’s destructor. This begins a chain(cascade) of destructor calls in which the derived-class destructor and the destructors execute in reverse of the order in which the constructors executed.

Class Work Create an inheritance hierarchy that a bank might use to represent customers’ bank accounts. All customers at this bank can deposit (i.e. credit) money into their accounts and withdraw (i.e. debit) money from their accounts. More specific types of accounts also exist. Savings accounts, for instance, earn interest on the money they hold. Checking accounts, on the other hand, charge a fee per transaction (i.e. credit or debit).

Class Work Create an inheritance hierarchy containing base class Account and derived classes SavingsAccount that inherit from class Account. Base class (Account) members: Data: double, balance Functions: Constructor (argument: initial balance) Credit: add an amount to current balance Debit: withdraw money from account getBalance: return current value of balance

Class Work Derived class SavingsAccount Added data member: double, interest rate Added member functions: Constructor: (argument 1: initial balance, argument 2: interest rate) calculateInterest: returns a double indicating the amount of interest earned by an account. This amount should be determined by multiplying interest rate by account balance.

Class Work Derived class CheckingAccount Added data member: double, fee charged per transaction Added member functions: Constructor: (argument 1: initial balance, argument 2: fee amount) Redefined member functions credit and debit: subtract fee from account balance whenever either transaction is performed successfully These functions should invoke the base class versions to update the account balance

Class Work Write a program that creates objects of each class and tests their member functions.