CMPE 180A Data Structures and Algorithms in C++ March 8 Class Meeting

Slides:



Advertisements
Similar presentations
Chapter 16 Templates. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Function Templates  Syntax, defining 
Advertisements

CSE 332: C++ Classes From Procedural to Object-oriented Programming Procedural programming –Functions have been the main focus so far Function parameters.
CSE 332: C++ templates and generic programming I Motivation for Generic Programming in C++ We’ve looked at procedural programming –Reuse of code by packaging.
CSE 425: Object-Oriented Programming II Implementation of OO Languages Efficient use of instructions and program storage –E.g., a C++ object is stored.
Week 14 - Monday.  What did we talk about last time?  Introduction to C++  Input and output  Functions  Overloadable  Default parameters  Pass.
March 6, 2014CS410 – Software Engineering Lecture #10: C++ Basics IV 1 Structure Pointer Operator For accessing members in structures and classes we have.
Inheritance One of the most powerful features of C++
Chapter 9 Questions 1. What are the difference between constructors and member functions? 2. Design and implement a simple class as you want, with constructors.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 16: Introduction to C++
OOP using C Abstract data types How to accomplish the task??? Requirements Details Input, output, process Specify each task in terms of input.
Virtual FunctionstMyn1 Virtual Functions A virtual function is declared in a base class by using the keyword virtual. A function that you declare as virtual.
CS 152: Programming Language Paradigms March 19 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
1 CSC241: Object Oriented Programming Lecture No 17.
Overview of C++ Polymorphism
Chapter 16 Templates Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
1 Inheritance and Polymorphism Chapter Getting Started Continue the Cat Management example from previous presentation.
MAITRAYEE MUKERJI Object Oriented Programming in C++
Motivation for Generic Programming in C++
“Generic Programming” ECE 297
C++ Template.
C++ Lesson 1.
Pointer to an Object Can define a pointer to an object:
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
CMPE 135: Object-Oriented Analysis and Design September 26 Class Meeting Department of Computer Engineering San Jose State University Fall 2017 Instructor:
CMPE Data Structures and Algorithms in C++ September 14 Class Meeting
CMPE 135: Object-Oriented Analysis and Design October 3 Class Meeting
CMPE Data Structures and Algorithms in C++ October 5 Class Meeting
CMPE Data Structures and Algorithms in C++ September 28 Class Meeting
References as Function Arguments
CS3340 – OOP and C++ L. Grewe.
Polymorphism.
CMPE 135: Object-Oriented Analysis and Design October 17 Class Meeting
CMPE 135: Object-Oriented Analysis and Design October 17 Class Meeting
CMPE Data Structures and Algorithms in C++ February 22 Class Meeting
Finally! Discussing a language!
CMPE 180A Data Structures and Algorithms in C++ February 15 Class Meeting Department of Computer Engineering San Jose State University Spring 2018 Instructor:
PRG 218 Week 5 Individual Assignment Coding: Derived Classes Please click here to buy ( (
group work #hifiTeam
Object Oriented Analysis and Design
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Programming II Polymorphism A.AlOsaimi.
CMPE 135: Object-Oriented Analysis and Design November 27 Class Meeting Department of Computer Engineering San Jose State University Fall 2018 Instructor:
CISC/CMPE320 - Prof. McLeod
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Inheritance: Polymorphism and Virtual Functions
C++ Templates CSE 333 Summer 2018
Today’s Objectives 10-Jul-2006 Announcements Quiz #3
Overview of C++ Polymorphism
CS 144 Advanced C++ Programming March 14 Class Meeting
CMPE 152: Compiler Design February 28 / March 5 Lab
CMSC 341 C++ and OOP.
Eighth step for Learning C++ Programming
C++ Programming CLASS This pointer Static Class Friend Class
CS 144 Advanced C++ Programming February 12 Class Meeting
CS 144 Advanced C++ Programming February 21 Class Meeting
CMPE 135 Object-Oriented Analysis and Design March 7 Class Meeting
C++ Templates CSE 333 Winter 2019
CS 144 Advanced C++ Programming March 28 Class Meeting
CMPE 152: Compiler Design April 18 – 30 Labs
Final Exam Review Inheritance Template Functions and Classes
CMSC 341 C++ and OOP.
CS 144 Advanced C++ Programming March 21 Class Meeting
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
CS 144 Advanced C++ Programming April 30 Class Meeting
CS 144 Advanced C++ Programming April 30 Class Meeting
Overloading the << Operator
CS410 – Software Engineering Lecture #6: Advanced C++ I
CMPE 152: Compiler Design March 7/12 Lab
Lecture 6: Polymorphism
Static Binding Static binding chooses the function in the class of the base class pointer, ignoring any versions in the class of the object actually.
Presentation transcript:

CMPE 180A Data Structures and Algorithms in C++ March 8 Class Meeting Department of Computer Engineering San Jose State University Spring 2018 Instructor: Ron Mak www.cs.sjsu.edu/~mak

Today Midterm until 7:15 Break until 7:30 Then a short lecture about templates, inheritance, and polymorphism.

Function exchange A useful function that exchanges the values of its two parameters: This version only works with integers. Can we define a version that works with multiple types? void exchange(int& first, int& second) { int temp = first; first = second; second = temp; }

Templates ExchangeTemplate.cpp template <typename T> void exchange(T& first, T& second) { T temp = first; first = second; second = temp; } void print(T first, T second) cout << first << " " << second << endl; This is not actual code – it’s a template (mold) for the compiler to generate source code on an as-needed basis.

Templates, cont’d ExchangeTemplate.cpp template <typename T> void exchange(T& first, T& second); void print(T first, T second); int main() {     int i = 5, j = 7;     print(i, j);     exchange(i, j);     cout << endl; ... } Generate int versions of the exchange and print functions

Templates, cont’d ExchangeTemplate.cpp ... double pi = 3.14, e = 2.72;     print(pi, e);     exchange(pi, e);     cout << endl;     string a = "alpha", b = "beta";     print(a, b);     exchange(a, b); } Generate double versions of the exchange and print functions 5 7 7 5 3.14 2.72 2.72 3.14 alpha beta beta alpha Generate string versions of the exchange and print functions Demo

Template Class Example Pair.h template <typename T1, typename T2> class Pair { public:     Pair(T1 a_value, T2 b_value);     T1 first()  const;     T2 second() const; private:     T1 a;     T2 b; }; Pair<T1, T2>::Pair(T1 a_value, T2 b_value) : a(a_value), b(b_value) {} T1 Pair<T1, T2>::first()  const { return a; } T2 Pair<T1, T2>::second() const { return b; } Keep all the code for a template class together in a .h file.

Template Class Example, cont’d PairTests.cpp #include "Pair.h" using namespace std; template <typename T1, typename T2> ostream& operator <<(ostream &outs, Pair<T1, T2>& p); int main() { Pair<int, double> p1(2, 3.14); Pair<double, string> p2(3.14, "Hello"); Pair<string, string> p3("Bob", "Ron");    cout << p1 << endl;     cout << p2 << endl;     cout << p3 << endl; } ostream& operator <<(ostream &outs, Pair<T1, T2>& p)     outs << p.first() << " " << p.second();     return outs; 2 3.14 3.14 Hello Bob Ron Demo

A “Safe” Array Type: Version 7 SafeArray7.h template <typename T> class SafeArray { public: SafeArray(); SafeArray(int len); SafeArray(const SafeArray<T>& other); // copy constructor ~SafeArray(); int get_length() const; SafeArray<T>& operator =(const SafeArray<T>& rhs); T& operator [](int i) const; private: int length; T *elements; };

A “Safe” Array Type: Version 7, cont’d SafeArray7.h template <typename T> SafeArray<T>::SafeArray() : length(0), elements(nullptr) {} SafeArray<T>::SafeArray(int len) : length(len), elements(new T[length]) {} SafeArray<T>::SafeArray(const SafeArray<T>& other)     : length(other.length), elements(new T[length]) {     for (int i = 0; i < length; i++) elements[i] = other.elements[i]; } SafeArray<T>::~SafeArray()     if (elements != nullptr) delete[] elements;

A “Safe” Array Type: Version 7, cont’d SafeArray7.h template <typename T> int SafeArray<T>::get_length() const { return length; } SafeArray<T>& SafeArray<T>::operator =(const SafeArray<T>& rhs) {     if (this == &rhs) return *this;     if (elements != nullptr) delete[] elements;     length = rhs.length;     elements = new T[length];     for (int i = 0; i < length; i++) elements[i] = rhs.elements[i];     return *this; } T& SafeArray<T>::operator [](int i) const     assert((i >= 0) && (i < length));     return elements[i];

A “Safe” Array Type: Version 7, cont’d SafeArrayTests7.cpp template <typename T> void print(SafeArray<T> a); void test_int(); void test_string(); void test_birthday(); int main() {     test_int();       cout << endl;     test_string();    cout << endl;     test_birthday();  cout << endl;     cout << "Done!" << endl;     return 0; } void print(SafeArray<T> a)     for (int i = 0; i < a.get_length(); i++) cout << " " << a[i];     cout << endl;

A “Safe” Array Type: Version 7, cont’d SafeArrayTests7.cpp void test_int() {     SafeArray<int> a1(10), a2, a3;     for (int i = 0; i < 10; i++) a1[i] = 10*i;     a3 = a2 = a1;     a1[4] = -a1[4];     cout << "a1 ="; print(a1);     cout << "a2 ="; print(a2);     cout << "a3 ="; print(a3); } a1 = 0 10 20 30 -40 50 60 70 80 90 a2 = 0 10 20 30 40 50 60 70 80 90 a3 = 0 10 20 30 40 50 60 70 80 90

A “Safe” Array Type: Version 7, cont’d SafeArrayTests6.cpp void test_string() {     SafeArray<string> a1(4), a2, a3;     a1[0] = "Fee";     a1[1] = "Fie";     a1[2] = "Foe";     a1[3] = "Fum";     a3 = a2 = a1;     a1[2] = "XXX";     cout << "a1 ="; print(a1);     cout << "a2 ="; print(a2);     cout << "a3 ="; print(a3); } a1 = Fee Fie XXX Fum a2 = Fee Fie Foe Fum a3 = Fee Fie Foe Fum

A “Safe” Array Type: Version 7, cont’d SafeArrayTests7.cpp void test_birthday() {     Birthday bd0;     Birthday bd1(1981, 9, 2);     Birthday bd2(1992, 5, 8);     Birthday bd3(1963, 1, 1);     SafeArray<Birthday> a1(3), a2, a3;     a1[0] = bd0;     a1[1] = bd1;     a1[2] = bd2;     a3 = a2 = a1;     a1[2] = bd3;     cout << "a1 ="; print(a1);     cout << "a2 ="; print(a2);     cout << "a3 ="; print(a3); } a1 = 0/0/0 9/2/1981 1/1/1963 a2 = 0/0/0 9/2/1981 5/8/1992 a3 = 0/0/0 9/2/1981 5/8/1992 Demo

Object-Oriented Programming Encapsulation Classes Inheritance Subclasses Polymorphism Virtual functions

Inheritance A very powerful and important feature of object-oriented programming. A new class (the derived class) is created from another class (the base class). A derived class is also known as a child class. The base class is the parent class. A child class is also known as a subclass.

Inheritance, cont’d A child class inherits member variables and functions from its parent class. Person.cpp class Person { public: string activity() { return "Eat and sleep."; } }; class Student : public Person string study() { return "Study and study." ; } A Student “is a” Person.

Inheritance, cont’d Let s be type Student. Valid: s.study() Person.cpp class Person { public: string activity() { return "Eat and sleep."; } }; class Student : public Person string study() { return "Study and study." ; } Let s be type Student. Valid: s.study() Valid: s.activity()

Inheritance, cont’d Person.cpp class Person { public: string activity() { return "Eat and sleep."; } }; class Student : public Person string study() { return "Study and study." ; } Subclass Student inherits the member function activity from its parent class. The Student class can also override the definition of function activity by defining its own version.

Subclasses Variable k is a Kitty. What is the output? Animal.cpp class Animal { public: string speak() { return "Shhh!"; } }; class Mammal : public Animal string speak() { return "Grrr!" ; } class Cat : public Mammal string speak() { return "Roar!"; } class Kitty : public Cat string speak() { return "Meow!"; } string make_sound(Cat& c) { return c.speak(); } int main() Kitty k; cout << make_sound(k) << endl; } Subclasses Variable k is a Kitty. k is also a Cat, a Mammal, and an Animal. Each subclass overrides the definition of member function speak. What is the output? The type of parameter c is a Cat. Roar! Demo

Polymorphism Polymorphism is the ability of a variable to have different behaviors at run time. How the variable behaves depends not on the type of the variable, but on the type of its value at run time. Polymorphism is implemented in C++ with virtual functions.

Polymorphism, cont’d What is the output? Person.cpp Polymorphism, cont’d class Person { public: virtual string activity() { return "Eat and sleep."; } }; class Student : public Person string activity() { return "Study and study." ; } class EngineeringMajor : public Student string activity() { return "Design and build."; } class SoftwareMajor : public EngineeringMajor string activity() { return "Code and test."; } string do_it(Student& s) { return s.activity(); } int main() SoftwareMajor sw; cout << do_it(sw) << endl; } What is the output? Member function activity is virtual in Person and all subclasses. The type of parameter s is Student. The type of the value of s is SoftwareMajor. Code and test.

Polymorphism, cont’d Person.cpp string activity(Student& s) { return s.activity(); } int main() { SoftwareMajor sw; cout << do_it(sw) << endl; EngineeringMajor em; cout << do_it(em) << endl; Student st; cout << do_it(st) << endl; } Code and test. Design and build. Study and study. Demo

Virtual Destructors From now on, make destructors virtual. Example: A virtual destructor ensures that the correct destructor is called for an object when the object is being destroyed. virtual ~Foo();