0% found this document useful (0 votes)
12 views17 pages

Lecture 3-DS

The document discusses pointers to functions in C++. It provides an example of defining a function pointer and calling a function using a pointer. It also discusses function templates and template classes in C++. The document explains inheritance in C++ including base classes, derived classes, and access specifiers. It provides an example of inheritance with multiple levels of derived classes.
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)
12 views17 pages

Lecture 3-DS

The document discusses pointers to functions in C++. It provides an example of defining a function pointer and calling a function using a pointer. It also discusses function templates and template classes in C++. The document explains inheritance in C++ including base classes, derived classes, and access specifiers. It provides an example of inheritance with multiple levels of derived classes.
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/ 17

CE-231 DATA STRUCTURES

Instructor: Muhammad Usman Ghani


LECTURE 3: Introduction and Review
Pointer to function

• On function call control is transferred to


function definition
• Pointer is used? Yes

• With this definition, f is the pointer to the


function f(),
• *f is the function itself
• (*f)(7) is a call to the function.
Pointer to function
𝑚

෍ 𝑓(𝑖)
𝑖=𝑛
𝑓 𝑖 = 2𝑖 + 6
// pointer to function
#include<iostream>
#include<cmath>
using namespace std;
double f(double x){
return 2*x+6;
}
double sum(double (*f)(double),int n, int m)
{
double result=0;
for(int i=n;i <=m; i++){
result=result+f(i);
cout<<result<<endl;
}
return result;
}
int main(){
cout<<sum(f,3,5);
}
Templates

• A function or class template is not


an actual function or class, but
instead is a pattern for what could
become a function or class.
• We have a template argument Obj
that can be replaced by any type
to generate a function or class
Template Class

A powerful feature of C++ is the possibility of declaring


generic class. For example, if we need to declare a class that
uses an array for storing some items, then we may declare this
class as

if we need a class that performs the same operations as


intClass except that it operates on float numbers,
Template Class

Initialize genType:
Example
Inheritance

BseClass

Derived1_Level1 Derived2_Level1

Multiple
inheritance
Derived_Level2
Inheritance

Base Class
Inheritance

Derived level 1

Derived2 level 1
Inheritance

Derived level 2
Main function
output
Inheritance

• Hierarchy of classes
• ParentClass/BaseClass,SuperClass
• ChildClass/DrivedClass
• BaseClass can decide which member
functions and data members can be
revealed to derived classes
Derived class can decide which parts of the public and
protected member functions and data members to retain and
use and which to modify

Example, both Derived1Level1and Derived2Level1 define their


own versions of f().
The access to the member function with the same name in any
of the classes higher up in the hierarchy is still possible by
preceding the function with the name of the class and the
scope operator, as shown in the call of BaseClass::f() from f()
in DerivedLevel2
Activity

Consider the code below


▪ Correct errors if any
▪ Write the output
Solution

You might also like