Week 14 - Wednesday.  What did we talk about last time?  More C++  new  delete  Differences for structs  OOP.

Slides:



Advertisements
Similar presentations
A C++ Crash Course Part II UW Association for Computing Machinery Questions & Feedback.
Advertisements

Chapter 11 Operator Overloading; String and Array Objects Chapter 11 Operator Overloading; String and Array Objects Part I.
Lesson 13 Introduction to Classes CS1 Lesson Introduction to Classes1.
Constructors and Destructors. Constructor Constructor—what’s this? Constructor—what’s this? method used for initializing objects (of certain class) method.
Review What is a virtual function? What can be achieved with virtual functions? How to define a pure virtual function? What is an abstract class? Can a.
Overloading Operators Overloading operators Unary operators Binary operators Member, non-member operators Friend functions and classes Function templates.
Chapter 17 Templates. Generic Algorithms Algorithms in which the actions or steps are defined, but the data types of the items being manipulated are not.
Templated Functions. Overloading vs Templating  Overloaded functions allow multiple functions with the same name.
CMSC 202, Version 2/02 1 Operator Overloading Strong Suggestion: Go over the Array class example in Section 8.8 of your text. (You may ignore the Array.
Introduction to Programming Lecture 31. Operator Overloading.
C++ Programming Languages
C/c++ 4 Yeting Ge.
Wednesday, 10/2/02, Slide #1 CS 106 Intro to CS 1 Wednesday, 10/2/02  QUESTIONS (on HW02 – due at 5 pm)??  Today:  Review of parameters  Introduction.
Rossella Lau Lecture 10, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 10: Operator overload  Operator overload.
1 CS 201 Introduction to c++ (2) Debzani Deb. 2 Classes (1) A class definition – in a header file :.h file A class implementation – in a.cc,.cpp file.
Specification and Implementation Separating the specification from implementation makes it easier to modify programs. Changes in the class’s implementation.
Wednesday, 10/9/02, Slide #1 CS 106 Intro to CS 1 Wednesday, 10/9/02  QUESTIONS ??  Today:  Discuss HW #02  Discuss test question types  Review 
OOP in C. Cfront Original C++ compiler was just a pre- processor –generated C code which could be compiled with an existing C Compiler.
Chapter 16 Templates. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Function Templates  Syntax, defining 
Lecture 9 Concepts of Programming Languages
Object Oriented Programming C++. ADT vs. Class ADT: a model of data AND its related functions C++ Class: a syntactical & programmatic element for describing.
Object Oriented Programming C++. ADT vs. Class ADT: a model of data AND its related functions C++ Class: a syntactical & programmatic element for describing.
Operator Overloading 1. Introduction Let’s define a class for Complex numbers: class Complex { private: double real, image; public: Complex () : real(0.0),
1 Classes- Inheritance Multiple Inheritance It is possible to derive a new class from more than one base class. This is called Multiple Inheritance. Under.
CS (CCN 27241) Software Engineering for Scientific Computing Lecture 5: C++ Key Concepts.
CSE 332: C++ templates This Week C++ Templates –Another form of polymorphism (interface based) –Let you plug different types into reusable code Assigned.
Week 2 - Monday.  What did we talk about last time?  Exceptions  Threads  OOP  Interfaces.
Overloading Binary Operators Two ways to overload –As a member function of a class –As a friend function As member functions –General syntax Data Structures.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Templates An introduction. Simple Template Functions template T max(T x, T y) { if (x > y) { return x; } else { return y; } } int main(void) { int x =
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
Copyright © Curt Hill Generic Classes Template Classes or Container Classes.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 4 – August 30, 2001.
CSC 270 – Survey of Programming Languages C++ Lecture 4 – More on Writing Classes.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
Object Oriented Programming (OOP) Lecture No. 10.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
Simple Classes. ADTs A specification for a real world data item –defines types and valid ranges –defines valid operations on the data. Specification is.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department.
Elementary C++. Procedural Programming Split your problem into simpler parts then solve each part separately Recognize common parts and solve them only.
Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315.
EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
CS 132 Spring 2008 Chapter 2 Object-Oriented Design (OOD) and C++ Ideas: * Inheritance (and protected members of a class) ** Operator overloading Pointer.
(1) ICS 313: Programming Language Theory Chapter 11: Abstract Data Types (Data Abstraction)
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 3 is due Sunday, the 8 th at 7pm. Problems with assn 3? Discuss at your team meeting tonight.
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
Introduction to Classes in C++ Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
1 Data Structures CSCI 132, Spring 2014 Lecture 2 Classes and Abstract Data Types Read Ch Read Style Guide (see course webpage)
ENEE150 – 0102 ANDREW GOFFIN Abstract Data Types.
Chapter 16 Templates Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Mr H Kandjimi 2016/01/03Mr Kandjimi1 Week 3 –Modularity in C++
Object-Oriented Programming Review 1. Object-Oriented Programming Object-Oriented Programming languages vary but generally all support the following features:
CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Templates Linked Lists.
Andy Wang Object Oriented Programming in C++ COP 3330
TK1924 Program Design & Problem Solving Session 2011/2012
Pointer to an Object Can define a pointer to an object:
Week 14 - Wednesday CS222.
Templates.
Lecture 9 Concepts of Programming Languages
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
Name: Rubaisha Rajpoot
Dr. Bhargavi Dept of CS CHRIST
C++ Templates An Introduction to Generic Programming.
Lecture 8 Object Oriented Programming (OOP)
(4 – 2) Introduction to Classes in C++
Object Oriented Programming (OOP) Lecture No. 10
Lecture 9 Concepts of Programming Languages
Presentation transcript:

Week 14 - Wednesday

 What did we talk about last time?  More C++  new  delete  Differences for structs  OOP

If you think C++ is not overly complicated, just what is a protected abstract virtual base pure virtual private destructor, and when was the last time you needed one? Tom Cargill If you think C++ is not overly complicated, just what is a protected abstract virtual base pure virtual private destructor, and when was the last time you needed one? Tom Cargill

 In industrial strength C++ code, the class declaration is usually put in a header file (.h ) while the class definition is in an implementation file (.cpp )  Benefits:  Easy to see members and methods  Header files can be sent to clients without divulging class internals  Separate compilation (faster)  Easier to take care of circular dependencies

class Complex { double real; double imaginary; public: Complex(double realValue = 0, double imaginaryValue = 0); ~Complex(void); double getReal(); double getImaginary(); }; class Complex { double real; double imaginary; public: Complex(double realValue = 0, double imaginaryValue = 0); ~Complex(void); double getReal(); double getImaginary(); };

Complex::Complex(double realValue, double imaginaryValue) { real = realValue; imaginary = imaginaryValue; } Complex::~Complex(void) {} double Complex::getReal() {return real; } double Complex::getImaginary() {return imaginary; } Complex::Complex(double realValue, double imaginaryValue) { real = realValue; imaginary = imaginaryValue; } Complex::~Complex(void) {} double Complex::getReal() {return real; } double Complex::getImaginary() {return imaginary; }

 In C++, you can overload operators, meaning that you can define what + means when used with classes you design  Thus, the following could be legal: Hippopotamus hippo; Sandwich club; Vampire dracula = club + hippo; Hippopotamus hippo; Sandwich club; Vampire dracula = club + hippo;

 But, what does it mean to "add" a Hippopotamus to a Sandwich and get a Vampire ?  Overloading operators is a bad idea  You can get confusing code  Most languages don't allow it  It C++ it is useful in two cases:  To make your objects easy to input/output using iostream  To perform mathematical operations with numerical classes (like Complex !)

Complex& operator=( const Complex& complex ); Complex operator+( const Complex& complex ) const; Complex operator-( const Complex& complex ) const; Complex operator-() const; Complex operator*( const Complex& complex ) const; Complex& operator=( const Complex& complex ); Complex operator+( const Complex& complex ) const; Complex operator-( const Complex& complex ) const; Complex operator-() const; Complex operator*( const Complex& complex ) const;

Complex& Complex::operator= ( const Complex& complex ) { real = complex.real; imaginary = complex.imaginary; return *this; } Complex& Complex::operator= ( const Complex& complex ) { real = complex.real; imaginary = complex.imaginary; return *this; }

 Let's finish the Complex type  Then, we can ask the user to enter two complex numbers  We can do the appropriate operation with them

 const, of course, means constant in C++  In class methods, you'll see several different usages  Const methods make a guarantee that they will not change the members of the object they are called on  int countCabbages() const;  Methods can take const arguments  void insert(const Coin money);  Methods can take const reference arguments  void photograph(const Castle& fortress);  Why take a const reference when references are used to change arguments?

 Allow classes and functions to be written with a generic type or value parameter, then instantiated later  Each necessary instantiation is generated at compile time  Appears to function like generics in Java, but works very differently under the covers  Most of the time you will use templates, not create them

template void exchange(T& a, T& b ) { T temp = a; a = b; b = temp; } template void exchange(T& a, T& b ) { T temp = a; a = b; b = temp; }

 You can make a class using templates  The most common use for these is for container classes  e.g. you want a list class that can be a list of anything  The STL filled with such templates  Unfortunately, template classes must be implemented entirely in the header file  C++ allows template classes to be separate from their headers, but no major compiler fully supports it

template class Pair { private: T x; T y; public: Pair( const T& a, const T& b ) { x = a; y = b; } T getX() const { return x; } T getY() const { return y; } void swap() { T temp = x; x = y; y = temp; } }; template class Pair { private: T x; T y; public: Pair( const T& a, const T& b ) { x = a; y = b; } T getX() const { return x; } T getY() const { return y; } void swap() { T temp = x; x = y; y = temp; } };

 Let's write an ArrayList class with templates!

 STL  Lab 14

 Keep working on Project 6  Due next Friday