[go: up one dir, main page]

0% found this document useful (0 votes)
77 views6 pages

Lecture 25 Handout PDF

This document discusses topics covered in an intro to C++ lecture, including virtual function tables, abstract base classes with pure virtual functions, and templates for functions and classes. It provides examples of how virtual functions are looked up using virtual function tables and how abstract base classes must define pure virtual functions. It also demonstrates how templates can be used to define generic functions and classes that can work with different data types.

Uploaded by

chuck212
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
77 views6 pages

Lecture 25 Handout PDF

This document discusses topics covered in an intro to C++ lecture, including virtual function tables, abstract base classes with pure virtual functions, and templates for functions and classes. It provides examples of how virtual functions are looked up using virtual function tables and how abstract base classes must define pure virtual functions. It also demonstrates how templates can be used to define generic functions and classes that can work with different data types.

Uploaded by

chuck212
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

ECE

220 Computer Systems & Programming

Lecture 25 – Intro to C++: Virtual Function Table, Template


December 5, 2017

• Final exam conflict sign-up due 10pm on Sunday, 12/10


• C programming competition next Tuesday, 12/12, 6pm in ECEB 3013
What we’ve learned so far in C++

§ Class vs. Struct


§ Dynamic Memory Allocation
§ Basic I/O
§ Pass by Value vs. Pass by Reference vs. Pass by Address (pointer)
§ Operator Overloading
§ Base Class & Derived Class
§ Declared Type vs. Actual Type

2
Virtual Function Table
§ stores pointers to all virtual functions
§ created for each class that uses virtual functions
§ lookup during the function call

class Shape{
protected:
int width, height;
public:
Shape(int a, int b) { width = a; height = b; }
virtual int area() { cout << “Base class area.” << endl; return 0; }
};
class Rectangle : public Shape{
public:
Rectangle(int a, int b) : Shape(a,b){}
int area() {
cout << “Rectangle class area.” << endl;
return width*height; }
}; 3
Abstract Base Class & Pure Virtual Functions

class Shape{
protected:
int width, height;
public:
Shape(int a, int b) { width = a; height = b; }
virtual int area()=0; //pure virtual function – it has no body
};

int main(){
Shape shape1(2,4); // this will cause compiler error!
Shape *p_shape1; // this is allowed
}

§ derived class must define a body for this virtual function, or it will also be
considered an abstract base class
4
Function Template
//functions can have the same names (overload)
int sum(int a, int b){
return a+b;
}

double sum(double a, double b){


return a+b;
}

//define function with generic type instead!


template <class T>
T sum (T a, T b){
return a+b;
}

int main(){
cout << sum(5,7) << endl;
cout << sum(1.5, 2.7) << endl;
} 5
Class Template
template <class T>
class mypair {
T a, b;
public:
mypair (T first, T second)
{a=first; b=second;}
T getmax ();
};
template <class T>
T mypair<T>::getmax () {
T retval;
retval = a>b? a : b;
return retval;
}
int main () {
mypair <int> myobject (100, 75);
cout << myobject.getmax();
return 0;
} 6

You might also like