• No results found

• Inlined and non-inlined member functions of classes

N/A
N/A
Protected

Academic year: 2022

Share "• Inlined and non-inlined member functions of classes"

Copied!
21
0
0

Loading.... (view fulltext now)

Full text

(1)

IIT Bombay

Computer Programming

Dr. Deepak B Phatak Dr. Supratik Chakraborty

Department of Computer Science and Engineering IIT Bombay

Session: Inline Member Function and Template

(2)

IIT Bombay

• Object-oriented programming with structures and classes

• Self-contained definitions of classes

All member functions defined within class definition

Quick Recap of Relevant Topics

(3)

IIT Bombay

• Inlined and non-inlined member functions of classes

• Further use of scope resolution operator (::)

• Template classes and functions

Overview of This Lecture

(4)

IIT Bombay

Acknowledgment

• Much of this lecture is motivated by the treatment in An Introduction to Programming Through C++

by Abhiram G. Ranade

McGraw Hill Education 2014

• Examples taken from this book are indicated in slides by

the citation AGRBook

(5)

IIT Bombay

Self-contained Class Definition

class V3 { private:

double x, y, z;

double length() const { return sqrt(x*x + y*y + z*z); } public:

V3(double p = 0.0, double q = 0.0, double r = 0.0) { x = p; y = q; z = r; return;

}

~V3() {return; }

… Some more member functions (on next slide) …

(6)

IIT Bombay

Self-contained Class Definition

class V3 {

private: … Data and member functions (from previous slide) ...

public:

… Constructor and destructor (from previous slide) … V3 operator+ (V3 const &b) const {

return V3(x + b.x, y + b.y, z + b.z);

}

V3 operator* (double const factor) const { return V3(x*factor, y*factor, z*factor);

} };

(7)

IIT Bombay

Can We Always Write Self-Contained Classes?

Inline member function

• Member function defined inside the class definition (all member functions we have seen so far)

• Convenient if function definition contains a few lines

• With long and complicated member functions, defining member functions in class definition is cumbersome

• C++ allows member functions to be declared in a class definition, but defined outside the class definition

Very useful if different member functions are developed by

(8)

IIT Bombay

Member Functions Outside Class Definition

class V3 {

private: double x, y, z;

double length() const;

public:

V3(double p=0.0, double q=0.0, double r=0.0);

V3 operator+(V3 const &b) const;

V3 operator*(double factor) const;

~V3() {return;}

};

(Not inline) Member function declaration

Inline member function definition

(9)

IIT Bombay

Non-inline Member Function Definition

V3::V3(double p, double q, double r) { x = p; y = q; z = r; return;

}

V3 V3::operator+(V3 const &b) const { return V3(x+b.x, y+b.y, z+b.z);

}

V3 V3::operator*(double factor) const { return (x*factor, y*factor, z*factor);

}

double V3::length() const {return sqrt(x*x + y*y + z*z);}

Note use of scope resolution operator

::

(10)

IIT Bombay

Motivating Template Class [Ref. AGRBook]

Class for implementing queue of integers (car identifiers)

class IntQueue {

private: int front, nWaiting;

int elements[100];

public:

IntQueue() { front = 0; nWaiting = 0; return; }

~IntQueue() {return;}

bool insert (int value);

bool remove (int &value);

};

(11)

IIT Bombay

Motivating Template Class

bool IntQueue::insert(int value) {

if (nWaiting == 100) { cout << “Q Full!” << endl; return false; } else {

elements[(front + nWaiting)%100] = value; nWaiting++; return true;

} }

bool IntQueue::remove(int &value) {

if (nWaiting == 0) { cout << “Q Empty!” << endl; return false; } else {

value = elements[front]; front = (front + 1)%100; nWaiting--; return true;

}

(12)

IIT Bombay

Motivating Template Class: Queue of 3-D Vectors

class V3Queue {

private: int front, nWaiting;

V3 elements[100];

public:

V3Queue() { front = 0; nWaiting = 0; return; }

~V3Queue() {return;}

bool insert (V3 value);

bool remove (V3 &value);

};

(13)

IIT Bombay

Motivating Template Class: Queue of 3-D Vectors

bool V3Queue::insert(V3 value) {

if (nWaiting == 100) { cout << “Q Full!” << endl; return false; } else {

elements[(front + nWaiting)%100] = value; nWaiting++; return true;

} }

bool V3Queue::remove(V3 &value) {

if (nWaiting == 0) { cout << “Q Empty!” << endl; return false; } else {

value = elements[front]; front = (front + 1)%100; nWaiting--; return true;

}

(14)

IIT Bombay

Motivating Template Class

• Differences between IntQueue and V3Queue only with respect to data types of some members

• Wouldn’t it be nice to be able to define a template for a Queue class with a generic data type T?

• The template can then be instantiated by specifying T to give Queue classes for different data types.

C++ provides a mechanism to do this: Template Class

(15)

IIT Bombay

Template Classes

• Foundation of generic programming

• Programming independent of specific types

• Template is a schema for creating several classes that differ only in data types of members and some parameters

Abstract definition of a class (complete with data

members and member functions) with generic

data types of members

(16)

IIT Bombay

Template Class

template <class T> class Queue { private: int front, nWaiting;

T elements[100];

public:

Queue() { front = 0; nWaiting = 0; return; }

~Queue() {return;}

bool insert (T value);

bool remove (T &value);

};

(17)

IIT Bombay

Template Member Function

template <class T> bool Queue<T> ::insert(T value) { if (nWaiting == 100) {

cout << “Q Full!” << endl; return false;

}

else {

elements[(front + nWaiting)%100] = value;

nWaiting++; return true;

}

}

(18)

IIT Bombay

Template Member Function

template <class T> bool Queue<T>::remove(T &value) { if (nWaiting == 0) {

cout << “Q Empty!” << endl;

return false;

}

else {

value = elements[front];

front = (front + 1)%100; nWaiting--; return true;

} }

(19)

IIT Bombay

Instantiating Template Classes

Queue<int> myIntQueueObject;

Queue<V3> myV3QueueObject;

• Helps reduce repetition of code

• Facilitates generic programming, thinking at an abstract level

• Crucial in C++ Standard Library … to be studied next

(20)

IIT Bombay

Concluding Note About Template Class

Template class definition allows using more than one generic data type and other parameters using a comma-separated list

Class definition:

template <class T, int QueueSize> class Queue { private: int front, nWaiting;

T elements[QueueSize];

public: … };

Instantiation: Queue<V3, 10> myV3QueueObject;

Queue<int, 100> myIntQueueObject;

(21)

IIT Bombay

Summary

• Inline and non-inline member functions of C++ classes

• Use of scope resolution operator in defining non-inline member functions

• Template classes and functions

References

Related documents

Definition of Data Structure, Types of Data Structures, Abstract Data Type (ADT), Algorithms: Algorithm Concepts, Definition of Algorithm, Objectives of Algorithms,

Unit-I Definition of Data Structure, Types of Data Structures, Abstract Data Type (ADT), Algorithms: Algorithm Concepts, Definition of Algorithm, Objectives of

 Interaction among the members of a group brings changes in the personality of an individual member and influences the group as a whole. Members are woven into

Give definition of the following terms: Keywords, Identifiers, Constants, Variables, Data Types, Pre-processor Directives, Built-in Functions, Expression,

1# Declare a class Number that contains two data member value1 and value2 of the type of integer, define constructor to give initial value, and perform addition

Unit I: Introduction to Python- Python data structures, data types, indexing and slicing, vectors, arrays, developing programs, functions, modules and packages, data structures

Introduction to Data Structure : Definition of Data Structure, Types &amp; Characteristics of Data Structures, Abstract Data Type (ADT), Algorithms: Algorithm Concepts, Definition

3: Program to print static data member and method using class name and object.. 4: Program to show Private, Protected and public class