CSC241: Object Oriented Programming

Slides:



Advertisements
Similar presentations
Operator overloading redefine the operations of operators
Advertisements

1 CSC241: Object Oriented Programming Lecture No 21.
Introduction to Programming Lecture 39. Copy Constructor.
Win32 Programming Lesson 4: Classes and Structures.
1 Stacks Chapter 4. 2 Objectives You will be able to: Describe a stack as an ADT. Build a dynamic-array-based implementation of stacks. Build a linked-list.
1 CSC241: Object Oriented Programming Lecture No 28.
Lecture 6 Feb 12 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 22 - C++ Templates Outline 22.1Introduction 22.2Class Templates 22.3Class Templates and Non-type.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - Templates Outline 11.1 Introduction 11.2 Function Templates 11.3 Overloading Function Templates.
Stacks  Standard operations: IsEmpty … return true iff stack is empty Top … return top element of stack Push … add an element to the top of the stack.
Data Structures Chapter 2 Stacks Andreas Savva. 2 Stacks A stack is a data structure in which all insertions and deletions of entries are made at one.
CS 1031 C++: Object-Oriented Programming Classes and Objects Template classes Operator Overloading Inheritance Polymorphism.
1 CSC241: Object Oriented Programming Lecture No 27.
1 CSC241: Object Oriented Programming Lecture No 13.
1 Object-Oriented Programming Using C++ CLASS 11 Honors.
1 CSC241: Object Oriented Programming Lecture No 12.
Programming Languages by Ravi Sethi Chapter 6: Groupings of Data and Operations.
1 CSC241: Object Oriented Programming Lecture No 16.
1 Lecture 04 Structural Programming in C++ You will learn: i) Operators: relational and logical ii) Conditional statements iii) Repetitive statements.
Object Oriented Programming in C++ Chapter 6 Inheritance.
Data Structures Stack Namiq Sultan 1. Data Structure Definition: Data structures is a study of different methods of organizing the data and possible operations.
Object-Oriented Programming Simple Stack Implementation.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 12 - Templates Outline 12.1Introduction 12.2Function Templates 12.3Overloading Template Functions.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
1 CSC241: Object Oriented Programming Lecture No 02.
1 CSC241: Object Oriented Programming Lecture No 11.
1 Data Structures CSCI 132, Spring 2014 Lecture 6 Applications using Stacks.
1 CSC241: Object Oriented Programming Lecture No 05.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 22 - C++ Templates Outline 22.1Introduction.
TEMPLATESTEMPLATES BCAS,Bapatla B.mohini devi. Templates Outline 22.1Introduction 22.2Class Templates 22.3Class Templates and Non-type Parameters 22.4Templates.
1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh.
1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh.
1 CSC241: Object Oriented Programming Lecture No 03.
1 CSC241: Object Oriented Programming Lecture No 17.
1 Classes classes and objects - from object-oriented programming point of view class declaration class class_name{ data members … methods (member functions)
1 Data Structures CSCI 132, Spring 2016 Notes 6 Applications using Stacks.
 2000 Deitel & Associates, Inc. All rights reserved. 12.1Introduction Templates - easily create a large range of related functions or classes –function.
Dynamic Memory Management & Static Class Members Lecture No 7 Object Oriented Programming COMSATS Institute of Information Technology.
Data Structure Lecture 1.  The Logical Organization of Data is called data structures  organize data  more efficient programs.  More powerful computers.
MAITRAYEE MUKERJI Object Oriented Programming in C++
Chapter 22 - C++ Templates
Popping Items Off a Stack Using a Function Lesson xx
Visit for more Learning Resources
Reference: R.Sebesta, Chapter 12
CSC241: Object Oriented Programming
Institute of Business & Technology (BIZTEK)
CSC241: Object Oriented Programming
CSC241: Object Oriented Programming
Stack and Queue APURBO DATTA.
CSC241: Object Oriented Programming
Stack Memory 1 (also called Call Stack)
Ambiguity Resolution in Inheritance
CMPE 152: Compiler Design September 18 Class Meeting
Popping Items Off a Stack Lesson xx
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
CSC 143 Stacks [Chapter 6].
Object Oriented Programming Using C++
5th Chapter Pointers in C++.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Object-Oriented Programming (OOP) Lecture No. 22
Statements and flow control
Java Programming Language
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Object-Oriented Programming (Part 2)
Chapter 22 - C++ Templates
Constructor, Destructor Mehroz Sadiq CS Faculty, UMT Department of CS&E, UET.
Chapter 22 - C++ Templates
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Objects as Function Arguments
Functions Chapter No. 5.
Presentation transcript:

CSC241: Object Oriented Programming Lecture No 14

Previous Lecture Protected members Generalization in UML representation Example program Counter CountDn Derived class Constructor Destructor Function overriding class A ~A() { } class B ~B() { } class C ~C() { } class A A() { } class B B() { } class C C() { } ~A() {..} A() {..} A() {..} A() {..} A a1; ~B() {..} B() {..} B() {..} C *c1 = new C; … delete c1; B b1; ~C() {..} C() {..} C c1;

Today’s Lecture Function overriding Class hierarchy Example program – Distance class Class hierarchy Employee program Public and private Inheritance Level of inheritance

Overriding Member Functions Member functions in a derived class can be override, i.e. have the same name as those in the base class Stack, a simple data storage medium. It allowed you to push integers onto the stack and pop them off class A abc(int x) { } class B

Example - stack S1 1 -1 2 13 12 11 Go to program top st[3] class Stack { protected: int st[3]; int top; public: Stack() { top = -1; } void push(int var) { st[++top] = var; } int pop() { return st[top--]; }; Example - stack class Stack2 : public Stack{ public: void push(int var1){ if(top >= 3 -1){ cout << “\nError: stack is full”; exit(1); } Stack::push(var); int pop() { if(top < 0) { cout << “\nError: stack is empty”; return Stack::pop(); }; top st[3] S1 1 -1 2 Stack2 s1; s1.push(11); s1.push(22); s1.push(33); 13 cout <<s1.pop(); 12 11 Go to program cout <<s1.pop();

Which function is called? s1.push(11); Which function is executed? Derive or Base class Rule: When the same function exists in both the base class and the derived class, the function in the derived class will be executed This is true for objects of the derived class Objects of the base class don’t know anything about the derived class and will always use the base class functions

Scope Resolution with Overridden Functions How do push() and pop() in Stack2 access push() and pop() in Stack? They use the scope resolution operator, ::, in the statements Stack::push(var); and return Stack::pop(); Without the scope resolution operator, the compiler would think the push() and pop() functions in Stack2 were calling themselves

Distance Class class DistSign : public Distance { private: DSign sign; DistSign() : Distance() { sign = pos; } DistSign(int ft, float in, DSign sg=pos): Distance(ft, in) { sign = sg; } void getdist() { Distance::getdist(); char ch; cout << “Enter sign (+ or -): “; cin >> ch; sign = (ch==’+’) ? pos : neg; void showdist() const { cout << ( (sign==pos) ? “(+)” : “(-)” ); Distance::showdist(); }; enum DSign { pos, neg }; class Distance{ protected: int feet; float inches; public: Distance() : feet(0), inches(0.0) { } Distance(int ft, float in) : feet(ft), inches(in) { } void getdist() { cout << “\nEnter feet: “; cin >> feet; cout << “Enter inches: “; cin >> inches; } void showdist() const { cout << feet << “ : ” << inches ; };

Cont. pos 0.0 sign feet inches alpha pos 3 4.75 pos 11 6.25 sign feet 0.0 sign feet inches alpha pos 3 4.75 main() { DistSign alpha; alpha.getdist(); DistSign beta(11, 6.25); DistSign gamma(100, 5.5, neg); cout << “\nalpha = “; alpha.showdist(); cout << “\nbeta = “; beta.showdist(); cout << “\ngamma = “; gamma.showdist(); } pos 11 6.25 sign feet inches beta neg 100 5.5 sign feet inches gamma 7 6.5 feet inches d2 Go to program

Class Hierarchies So far we have seen examples to add functionality to an existing class Let’s look at an example where inheritance is used for a different purpose: as part of the original design of a program Widget company has employees: For simplicity, only three kinds of employees are considered Managers manage, Scientists perform research to develop better widgets, and Laborers operate the stamping presses

UML Diagram Scientist Employee Manager Laborer Publication Name Tittle Number Manager Tittle Club dues Scientist Publication Laborer

C ++ classes Employee Manager Scientist Laborer

const int LEN = 80; class employee{ private: char name[LEN]; unsigned long number; public: void getdata(){ cout << “\n Enter last name: “; cin >> name; cout << “ Enter number: “; cin >> number; } void putdata() const { cout << “\n Name: “ << name; cout << “\n Number: “ << number; }; class manager : public employee { private: char title[LEN]; double dues; public: void getdata() { employee::getdata(); cout << “ Enter title: “; cin >> title; cout << “ Enter golf club dues: “; cin >> dues; } void putdata() const{ employee::putdata(); cout << “Title: “ << title; cout << “Club dues:“ << dues; };

Cont. class scientist : public employee { private: int pubs; public: void getdata() { employee::getdata(); cout << “ Enter number of pubs: “; cin >> pubs; } void putdata() const { employee::putdata(); cout << “\n Number of publications: “ << pubs; }; class laborer : public employee{ };

Cont.. Go to program main() { manager m1, m2; scientist s1; laborer l1; cout << “\nEnter data for manager 1”; m1.getdata(); cout << “\nEnter data for manager 2”; m2.getdata(); cout << “\nEnter data for scientist 1”; s1.getdata(); cout << “\nEnter data for laborer 1”; l1.getdata(); cout << “\n manager 1”; m1.putdata(); cout << “\n manager 2”; m2.putdata(); cout << “\n scientist 1”; s1.putdata(); cout << “\n laborer 1”; l1.putdata(); } Go to program