C++ TEMPLATES, Lecture notes of C programming

Complete the line of code to create a new. BSTNode object with int data on the heap and assign nodePtr to point to it. 9. BSTNode<int>* nodePtr. BST, with ...

Typology: Lecture notes

2021/2022

Uploaded on 09/12/2022

ekambar
ekambar 🇺🇸

4.8

(25)

264 documents

1 / 17

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
C++ TEMPLATES
Problem Solving with Computers-II
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download C++ TEMPLATES and more Lecture notes C programming in PDF only on Docsity!

C++ TEMPLATES

Problem Solving with Computers-II

Pa02 released!

Its about implementing a BST with a movie data set, collecting and

analyzing running time!

Part of the assignment involves writing a report, explaining the trends

in your data

Due 06/

Start early!

Midterm grades released!

Max: 55/50 (5 students)

Median: 90%

Mean: 88%

Announcements

Suppose your program uses 100,000,000 different data types,

and you need a maximum function for each...

int maximum(Knafn a, Knafn b)

if (a > b)

return a;

else

return b;

One Hundred Million Functions...

int maximum(Foo a, Foo b)

if (a > b)

return a;

else

return b;

int maximum(Poo a, Poo b) }

if (a > b)

return a;

else

return b;

int maximum(Noo a, Noo b)

if (a > b)

return a;

else

return b;

int maximum(Moo a, Moo b)

if (a > b)

return a;

else

return b;

int maximum(Loo a, Loo b)

if (a > b)

return a;

else

return b;

int maximum(Koo a, Koo b)

if (a > b)

return a;

else

return b;

int maximum(Joo a, Joo b)

if (a > b)

return a;

else

return b;

int maximum(Ioo a, Ioo b)

if (a > b)

return a;

else

return b;

int maximum(Hoo a, Hoo b)

if (a > b)

return a;

else

return b;

int maximum(Goo a, Goo b)

if (a > b)

return a;

else

return b;

int maximum(Doo a, Doo b)

if (a > b)

return a;

else

return b;

int maximum(Coo a, Coo b)

if (a > b)

return a;

else

return b;

int maximum(Boo a, Boo b)

if (a > b)

return a;

else

return b;

int maximum(Knafn a, Knafn b)

if (a > b)

return a;

else

return b;

int maximum(Foo a, Foo b)

if (a > b)

return a;

else

return b;

int maximum(Poo a, Poo b) }

if (a > b)

return a;

else

return b;

int maximum(Noo a, Noo b)

if (a > b)

return a;

else

return b;

int maximum(Moo a, Moo b)

if (a > b)

return a;

else

return b;

int maximum(Loo a, Loo b)

if (a > b)

return a;

else

return b;

int maximum(Koo a, Koo b)

if (a > b)

return a;

else

return b;

int maximum(Joo a, Joo b)

if (a > b)

return a;

else

return b;

int maximum(Ioo a, Ioo b)

if (a > b)

return a;

else

return b;

int maximum(Hoo a, Hoo b)

if (a > b)

return a;

else

return b;

int maximum(Goo a, Goo b)

if (a > b)

return a;

else

return b;

int maximum(Doo a, Doo b)

if (a > b)

return a;

else

return b;

int maximum(Coo a, Coo b)

if (a > b)

return a;

else

return b;

int maximum(Boo a, Boo b)

if (a > b)

return a;

else

return b;

int maximum(Knafn a, Knafn b)

if (a > b)

return a;

else

return b;

int maximum(Foo a, Foo b)

if (a > b)

return a;

else

return b;

int maximum(Poo a, Poo b)

if (a > b)

return a;

else

return b;

int maximum(Noo a, Noo b)

if (a > b)

return a;

else

return b;

int maximum(Moo a, Moo b)

if (a > b)

return a;

else

return b;

int maximum(Loo a, Loo b)

if (a > b)

return a;

else

return b;

int maximum(Koo a, Koo b)

if (a > b)

return a;

else

return b;

int maximum(Joo a, Joo b)

if (a > b)

return a;

else

return b;

int maximum(Ioo a, Ioo b)

if (a > b)

return a;

else

return b;

int maximum(Hoo a, Hoo b)

if (a > b)

return a;

else

return b;

int maximum(Goo a, Goo b)

if (a > b)

return a;

else

return b;

int maximum(Doo a, Doo b)

if (a > b)

return a;

else

return b;

int maximum(Coo a, Coo b)

if (a > b)

return a;

else

return b;

int maximum(Boo a, Boo b)

if (a > b)

return a;

else

return b;

int maximum(int a, int b)

{

if (a > b)

return a;

else

return b;

}

When you write a template function, you choose a data type

for the function to depend upon...

template

Item maximum(Item a, Item b)

{

if (a > b)

return a;

else

return b;

}

A Template Function for Maximum

BST, with templates:

template

class BSTNode {

public:

BSTNode left;*

BSTNode right;*

BSTNode parent;*

Data const data;

BSTNode( const Data & d ) :

data(d) {

left = right = parent = nullptr ;

}

};

How would you create a BSTNode object on

the runtime stack?

A. BSTNode n(10);

B. BSTNode n;

C. BSTNode n(10);

D. BSTNode n = new BSTNode(10);

E. More than one of these will work

{ } syntax OK too

How would you create a pointer to

BSTNode with integer data?

A. BSTNode* nodePtr;

B. BSTNode nodePtr;

C.BSTNode* nodePtr;

BST, with templates:

template

class BSTNode {

public:

BSTNode left;*

BSTNode right;*

BSTNode parent;*

Data const data;

BSTNode( const Data & d ) :

data(d) {

left = right = parent = nullptr ;

}

};

Working with a BST

template

class BST {

private:

BSTNode* root; //Pointer to the root of this BS

public:

/** Default constructor. Initialize an empty BST. */

BST() : root(nullptr){ }

void insertAsLeftChild(BSTNode* parent, const Data& item){

// Your code here

}

Working with a BST: Insert

//Assume this is inside the definition of the class

void insertAsLeftChild(BSTNode* parent, const Data& item)

{

// Your code here

}

Which line of code correctly inserts the data item into the BST as the left

child of the parent parameter.

A.parent.left = item;

B.parent->left = item;

C.parent->left = BSTNode(item);

D.parent->left = new BSTNode(item);

E.parent->left = new Data(item);

template

Item maximum(Item a, Item b)

{

if (a > b)

return a;

else

return b;

}

What is difference between templates and typedefs?

typedef int item;

item maximum(item a, item b)

{

if (a > b)

return a;

else

return b;

}

Demo maximum.cpp

Template classes: Non-member functions

template

BST operator+(const BST& b1, const BST&b2);

BST operator+(const BST& b1, const BST&b2);

Template classes: Including the implementation

//In bst.h

class BST{

//code

};

#include "bst.cpp"

Review and demo an example