Sie sind auf Seite 1von 14

Module 17

Partha Pratim
Das
Module 17: Programming in C++
Objectives &
Outline
friend Function and friend Class
friend
function
Matrix-Vector
Multiplication Partha Pratim Das
Linked List

friend class
Linked List
Department of Computer Science and Engineering
Iterator Indian Institute of Technology, Kharagpur
Notes
ppd@cse.iitkgp.ernet.in
Summary

Tanwi Mallick
Srijoni Majumdar
Himadri B G S Bhuyan

NPTEL MOOCs Programming in C++ Partha Pratim Das 1


Module Objectives

Module 17

Partha Pratim Understand friend function and class


Das

Objectives &
Outline

friend
function
Matrix-Vector
Multiplication
Linked List

friend class
Linked List
Iterator

Notes

Summary

NPTEL MOOCs Programming in C++ Partha Pratim Das 2


Module Outline: Lecture-32

Module 17

Partha Pratim friend function


Das
Matrix-Vector Multiplication
Objectives & Linked List
Outline
friend class
friend
function Linked List
Matrix-Vector
Multiplication Iterator
Linked List

friend class friend-ly Notes


Linked List
Iterator

Notes

Summary

NPTEL MOOCs Programming in C++ Partha Pratim Das 3


Program 17.01: friend function – Basic Notion
Ordinary function friend function
Module 17
#include<iostream> #include<iostream>
Partha Pratim using namespace std; using namespace std;
Das
class MyClass { int data_; class MyClass { int data_;
public: public:
Objectives & MyClass(int i) : data_(i) {} MyClass(int i) : data_(i) {}
Outline
friend void display(const MyClass& a);
friend }; };
function void display(const MyClass& a) { void display(const MyClass& a) {
Matrix-Vector cout << "data = " << a.data_; // Error 1 cout << "data = " << a.data_; // Okay
Multiplication
} }
Linked List
int main(){ int main(){
friend class MyClass obj(10); MyClass obj(10);
Linked List
Iterator display(obj); display(obj);

Notes return 0; return 0;


} }
Summary
• display() is a non-member function • display() is a non-member function; but
friend to class MyClass
• Error 1: ’MyClass::data ’ : cannot • Able to access data even though it is private
access private member declared in class in class MyClass
’MyClass’
• Output: data = 10

In the recorded video void display(const MyClass& a); is included in the class MyClass on left by mistake.
This should be ignored. It is corrected here.
NPTEL MOOCs Programming in C++ Partha Pratim Das 4
friend function

Module 17

Partha Pratim A friend function of a class


Das has access to the private and protected members of the class (breaks
the encapsulation)
Objectives &
Outline
must have its prototype included within the scope of the class
prefixed with the keyword friend
friend does not have its name qualified with the class scope
function
Matrix-Vector
is not called with an invoking object of the class
Multiplication can be declared friend in more then one classes
Linked List

friend class A friend function can be a


Linked List global function
Iterator
a member function of a class
Notes
a function template
Summary

NPTEL MOOCs Programming in C++ Partha Pratim Das 5


Program 17.02: Multiply a Matrix with a Vector
#include <iostream> class Matrix { int e_[3][3]; int m_, n_;
Module 17 using namespace std; public:
Matrix(int m, int n) : m_(m), n_(n) {
Partha Pratim class Matrix; // Forward declaration // Arbitrary initialization
Das for (int i = 0; i < m_; ++i)
class Vector { int e_[3]; int n_; for (int j = 0; j < n_; ++j)
public: e_[i][j] = i + j;
Objectives & Vector(int n) : n_(n) { }
Outline // Arbitrary initialization void Show() { //Show the matrix
for (int i = 0; i < n_; ++i) for (int i = 0; i < m_; ++i) {
friend e_[i] = i + 1; for (int j = 0; j < n_; ++j)
function } cout << e_[i][j] << " ";
Matrix-Vector void Clear() { // Set a zero vector cout << endl;
Multiplication
for (int i = 0; i < n_; ++i) }
Linked List
e_[i] = 0; cout << endl;
friend class } }
Linked List void Show() { //Show the vector friend Vector Prod(Matrix *pM,
Iterator for (int i = 0; i < n_; ++i) Vector *pV);
cout << e_[i] << " "; };
Notes cout << endl << endl; Vector Prod(Matrix *pM, Vector *pV) {
} Vector v(pM->m_); v.Clear();
Summary friend Vector Prod(Matrix *pM, for (int i = 0; i < pM->m_; i++)
Vector *pV); for (int j = 0; j < pM->n_; j++)
}; v.e_[i] += pM->e_[i][j] * pV->e_[j];
return v;
}

• Vector Prod(Matrix*, Vector*); is a global function


• Vector Prod(Matrix*, Vector*); is friend of class Vector as well as class Matrix
• This function accesses the private data members of both these classes
NPTEL MOOCs Programming in C++ Partha Pratim Das 6
Program 17.02: Multiply a Matrix with a Vector
int main() { Output:
Module 17 Matrix M(2, 3);
Vector V(3); 0 1 2
Partha Pratim 1 2 3
Das Vector PV = Prod(&M, &V);
1 2 3
M.Show();
Objectives & 8 14
V.Show();
Outline
PV.Show();
friend
function return 0;
}
Matrix-Vector
Multiplication
Linked List
• Vector Prod(Matrix*, Vector*); is a global function
friend class • Vector Prod(Matrix*, Vector*); is friend of class Vector as well as class Matrix
Linked List • This function accesses the private data members of both these classes
Iterator

Notes

Summary

NPTEL MOOCs Programming in C++ Partha Pratim Das 7


Program 17.03: Linked List
#include <iostream> void List::display() {
Module 17 using namespace std; Node *ptr = head;
while (ptr) {
Partha Pratim class Node; // Forward declaration cout << ptr->info << " ";
Das ptr = ptr->next;
class List { }
Node *head; // Head of the list }
Objectives & Node *tail; // Tail of the list
Outline public: void List::append(Node *p) {
List(Node *h = 0): if (!head) head = tail = p;
friend head(h), else {
function tail(h) {} tail->next = p;
Matrix-Vector void display(); tail = tail->next;
Multiplication
void append(Node *p); }
Linked List
}; }
friend class
Linked List class Node { int main() {
Iterator int info; // Data of the node List l; // Init null list
Node *next; // Ptr to next node Node n1(1), n2(2), n3(3); // Few nodes
Notes public: l.append(&n1); // Add nodes to list
Node(int i): info(i), next(0) { } l.append(&n2);
Summary friend void List::display(); l.append(&n3);
friend void List::append(Node *); l.display(); // Show list
}; return 0;
}

• List is built on Node. Hence List needs to know the internals of Node
• void List::append(Node *); needs the internals of Node – hence friend member function is used
• void List::display(); needs the internals of Node – hence friend member function is used
• We can do better with friend classes
NPTEL MOOCs Programming in C++ Partha Pratim Das 8
friend class

Module 17

Partha Pratim A friend class of a class


Das has access to the private and protected members of the class (breaks
the encapsulation)
Objectives &
Outline
does not have its name qualified with the class scope (not a nested
class)
friend can be declared friend in more then one classes
function
Matrix-Vector
Multiplication A friend class can be a
Linked List
class
friend class class template
Linked List
Iterator

Notes

Summary

NPTEL MOOCs Programming in C++ Partha Pratim Das 9


Program 17.04: Linked List
#include <iostream> void List::display() {
Module 17 using namespace std; Node *ptr = head;
while (ptr) {
Partha Pratim class Node; // Forward declaration cout << ptr->info << " ";
Das ptr = ptr->next;
class List { }
Node *head; // Head of the list }
Objectives & Node *tail; // Tail of the list
Outline public: void List::append(Node *p) {
List(Node *h = 0): if (!head) head = tail = p;
friend head(h), else {
function tail(h) {} tail->next = p;
Matrix-Vector void display(); tail = tail->next;
Multiplication
void append(Node *p); }
Linked List
}; }
friend class
Linked List class Node { int main() {
Iterator int info; // Data of the node List l; // Init null list
Node *next; // Ptr to next node Node n1(1), n2(2), n3(3); // Few nodes
Notes public: l.append(&n1); // Add nodes to list
Node(int i): info(i), next(0) { } l.append(&n2);
Summary //friend void List::display(); l.append(&n3);
//friend void List::append(Node *);
friend class List; l.display(); // Show list
}; return 0;
}

• List class is now a friend of Node class. Hence it has full visibility into the internals of Node
• When multiple member functions need to be friends, it is better to use friend class

NPTEL MOOCs Programming in C++ Partha Pratim Das 10


Program 17.05: Linked List with Iterator
#include <iostream> void Iterator::begin(List *l) {
Module 17 using namespace std; list = l; node = l->head; // Set list & Init
}
class Node; class List; bool Iterator::end() { return node == 0; }
Partha Pratim
class Iterator { void Iterator::next() { node = node->next; }
Das
Node *node; // Current Node int Iterator::data() { return node->info; }
List *list; // Current List
Objectives & public: void List::append(Node *p) {
Outline Iterator() : node(0), list(0) {} if (!head)
void begin(List *); // Init head = tail = p;
friend bool end(); // Check end else {
function void next(); // Go to next tail->next = p;
Matrix-Vector int data(); // Get node data tail = tail->next;
Multiplication
}; }
Linked List
class List { Node *head, *tail; }
friend class public: int main() {
Linked List List(Node *h=0): head(h), tail(h) {} List l; Node n1(1), n2(2), n3(3);
Iterator void append(Node *p); l.append(&n1); l.append(&n2); l.append(&n3);
friend class Iterator;
Notes }; Iterator i;
class Node { int info; Node *next; for (i.begin(&l); !i.end(); i.next()) {
Summary public: cout << i.data() << " ";
Node(int i) : info(i), next(0) { } }
friend class List;
friend class Iterator; return 0;
}; }

• An Iterator now traverses over the elements of the List


• void List::display() is dropped from List and can be written in main()
• List class is a friend of Node class
• Iterator class is a friend of List and Node classs
NPTEL MOOCs Programming in C++ Partha Pratim Das 11
friend-ly Notes

Module 17 friend-ship is neither commutative nor transitive


Partha Pratim A is a friend of B does not imply that B is a friend of A
Das A is a friend of B and B is a friend of C does not imply that A is a
friend of C
Objectives &
Outline Visibility and Encapsulation
friend public: a declaration that is accessible to all
function protected: a declaration that is accessible only to the class itself and
Matrix-Vector
Multiplication its subclasses
Linked List private: a declaration that is accessible only to the class itself
friend class friend: a declaration that is accessible only to friend’s of a class.
Linked List friend’s tend to break data hiding and should be used judiciously.
Iterator
Like:
Notes
A function needs to access the internals of two (or more)
Summary
independent classes (Matrix-Vector Multiplication)
A class is built on top of another (List-Node Access, List
Iterator)
Certain situations of operator overloading (like streaming
operators)

NPTEL MOOCs Programming in C++ Partha Pratim Das 12


Module Summary

Module 17

Partha Pratim Introduced the notion of friend function


Das
Introduced the notion of friend class
Objectives &
Outline Studied the use of friend function and friend class with
friend
function
examples
Matrix-Vector
Multiplication friend introduces visibility hole by breaking encapsulation
Linked List
– should be used with care
friend class
Linked List
Iterator

Notes

Summary

NPTEL MOOCs Programming in C++ Partha Pratim Das 13


Instructor and TAs

Module 17

Partha Pratim
Das Name Mail Mobile
Objectives &
Partha Pratim Das, Instructor ppd@cse.iitkgp.ernet.in 9830030880
Outline Tanwi Mallick, TA tanwimallick@gmail.com 9674277774
friend
Srijoni Majumdar, TA majumdarsrijoni@gmail.com 9674474267
function Himadri B G S Bhuyan, TA himadribhuyan@gmail.com 9438911655
Matrix-Vector
Multiplication
Linked List

friend class
Linked List
Iterator

Notes

Summary

NPTEL MOOCs Programming in C++ Partha Pratim Das 14

Das könnte Ihnen auch gefallen