Week 14 wednesday
1 / 38

CS222 - PowerPoint PPT Presentation

  • Uploaded on

Week 14 - Wednesday. CS222. Last time. What did we talk about last time? More C++ new d elete Differences for structs Lab 13. Questions?. Project 6. Quotes.

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
Download Presentation

PowerPoint Slideshow about ' CS222' - ledell

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

Last time
Last time

  • What did we talk about last time?

  • More C++

    • new

    • delete

    • Differences for structs

  • Lab 13


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

C warmup
C++ Warmup

  • Let's write a complete C++ program that reads in:

    • A string

    • An integer

  • Then, it prints out the string however many times the integer specified

Object oriented programming
Object Oriented Programming

  • Let's see how objects work in C++ by looking at classically important elements of OOP

    • Encapsulation

    • Dynamic dispatch

    • Polymorphism

    • Inheritance

    • Self-reference


  • Information hiding

  • We want to bind operations and data tightly together

  • Consequently, we don't want you to touch our privates

  • Encapsulation in C++ is provided by the private and protected keywords

    • Unlike Java, you mark sections as public, private, or protected, not individual members and methods

  • Hardcore OOP people think that all data should be private and most methods should be public

Encapsulation example
Encapsulation example

class A



int a;




return a;


voidsetA(int value)


a = value;




  • Allows code reuse

  • Is thought of as an "is-a" relationship

  • C++ allows multiple inheritance, but you usually only use it if you know what you're doing, usually as part of a design pattern

  • Deriving a subclass usually means creating a "refined" or "more specific" version of a superclass

Inheritance example
Inheritance example

class B : public A

{ //has member and methods from A


class C : public A


private: //has A stuff and more



intgetC(){ return c; }

void increment() { c++; }



  • A confusing word whose underlying concept many programmers misunderstand

  • Polymorphism is when code is designed for a superclass but can be used with a subclass

  • If AudiRS5 is a subtype of Car, then you can use an AudiRS5 where you could use a Car

Polymorphism example
Polymorphism example

void drive( Car* c );

//defined somewhere

class AudiRS5 : public Car


Car car;

AudiRS5 audi;

drive( &audi ); //okay

drive( &car ); //okay

Dynamic dispatch
Dynamic dispatch

  • Polymorphism can be used to extend the functionality of an existing method using dynamic dispatch

  • In dynamic dispatch, the method that is actually called is not known until run time

Dynamic dispatch example
Dynamic dispatch example

class A {

public: virtualvoid print()

{ cout << "A"; }


class B : public A


public: void print()

{ cout << "B"; }


Dynamic dispatch example1
Dynamic dispatch example

A a;

B b;

A* p;

a.print(); // A

b.print(); // B

p = &a;

p->print(); // A

p = &b;

p->print(); // B

Self reference

  • Objects are able to refer to themselves

  • This can be used to explicitly reference variables in the class

  • Or, it can be used to provide the object itself as an argument to other methods

  • Self-reference in C++ is provided in part through the this keyword

    • this is a pointer to the object you're inside of

Self reference example
Self reference example

class Stuff



int things;


voidsetThings(int things)


this->things = things;



Self reference example1
Self reference example




voidaddToList(List& list)





Dividing up code
Dividing up code

  • 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

Dividing up code header
Dividing up code header

class Complex


double real;

double imaginary;


Complex(doublerealValue = 0, doubleimaginaryValue = 0);





Dividing up code implementation
Dividing up code implementation

Complex::Complex(doublerealValue, doubleimaginaryValue)


real = realValue;

imaginary = imaginaryValue;




double Complex::getReal()

{ return real; }

double Complex::getImaginary()

{ return imaginary; }

Overloading operators
Overloading operators

  • 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;

Vampiredracula = club + hippo;

Overloading operators1
Overloading operators

  • 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!)

Partial overloading operators header
(Partial) overloading operators header

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;

Partial overloading operators implementation
(Partial) overloading operators implementation

Complex& Complex::operator=

( const Complex& complex )


real = complex.real;

imaginary = complex.imaginary;

return *this;


What s all that const
What's all that const?

  • 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

    • intcountCabbages() const;

  • Methods can take const arguments

    • void insert(const Coin money);

  • Methods cant 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 method example
Template method example

template<class T> void exchange(T& a, T& b )


T temp = a;

a = b;

b = temp;


Template classes
Template classes

  • 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 example
Template class example

template<class T> class Pair {


T x;

T y;


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;



Next time
Next time…

  • STL

  • Lab 14


  • Keep working on Project 6

    • Due next Friday