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

Lecture Note MA251 Aug 17

This document summarizes key concepts from a lecture on pointers to classes, structs vs classes, friendship, and inheritance in C++. It discusses how pointers can reference objects, how structs are like classes but with public access by default, how friend functions and classes allow accessing private members, and how friendship is not transitive between classes. It also provides examples of using pointers to objects, friend functions, and friend classes and assigns programming tasks on dynamic stacks and binary search sorting.

Uploaded by

Deepak Kumar
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 views9 pages

Lecture Note MA251 Aug 17

This document summarizes key concepts from a lecture on pointers to classes, structs vs classes, friendship, and inheritance in C++. It discusses how pointers can reference objects, how structs are like classes but with public access by default, how friend functions and classes allow accessing private members, and how friendship is not transitive between classes. It also provides examples of using pointers to objects, friend functions, and friend classes and assigns programming tasks on dynamic stacks and binary search sorting.

Uploaded by

Deepak Kumar
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/ 9

MA 251: Data Structures Lab with OOP

Tutorial 3

Partha Sarathi Mandal


Dept. of Mathematics, IIT Guwahati
Pointers to classes
Crectangle *ptrrect; int main(){
CRactangel rect, *ptr1, *ptr2;
//pointer to an object of class CRectangle.
Cractangel *d = new Cractangel[2];
ptr1 = new Cractangel;
Example: ptr2 = ▭
#include<iostrem> rect.set_values(1,2);
using namespace std; ptr1->set_values(3,4);
d->set_values(5,6);
class CRactangel{ d[1].set_values(7,8);
int x, y;
cout<< “rect area”<<rect.area()<< endl;
public:
cout<<“*ptr1 area”<<ptr1->area()<< endl;
void set_values(int a,int b){ cout<< “*ptr2 area”<<ptr2->area()<<endl;
x = a; y = b; cout<<“d[0] area”<<d[0].area()<< endl;
} cout<<“d[1] area”<<d[1].area()<< endl;
int area(){ delete []d;
return(x*y); delete ptr1;
return 0;
}
}
};
P. S. Mandal, IITG
classes defined with
struct & union
• Classes can be defined not only with keyword class, but
also with keywords struct and union.
• structs can also have function members in C++, not only
data members.
• If class is declared with the keyword struct then all
member would be public access by default.
• For all other purposes both keywords are equivalent.
• concept of unions is different from that of classes
declared with struct and class.
• unions only store one data member at a time, but
nevertheless they are also classes and can thus also hold
function members.
• default access in union classes is public.
P. S. Mandal, IITG
Friendship and inheritance
• Friend function
• Friend classes
• Inheritance between classes
• Multiple inheritance

P. S. Mandal, IITG
Friend functions
• In principle, private and protected members of a
class cannot be accessed from outside the same
class in which they are declared. However, this
rule does not affect friends.
• If we want to declare an external function as
friend of a class, thus allowing this function to
have access to the private and protected
members of this class.
• Need to declare a prototype of this external
function within the class, and preceding it with
the keyword friend:

P. S. Mandal, IITG
Example: friend functions
#include<iostrem> int main () {
using namespace std;
CRectangle rect, rectb;
class CRactangel {
int x, y; rect.set_values (2,3);
public: rectb =
void set_values(int, int); duplicate(rect);
int area(){
cout << rectb.area();
return(x*y);
} return 0;
friend CRectangle duplicate(CRectangle); }
};
void CRactangel::set_values(int a, int b){
x = a; y = b;
▪ Notice that neither in the declaration of
}
duplicate() nor in its later use in main()
CRectangle duplicate(CRectangle rect_p){
CRectangle rect_m;
have considered duplicate a member of
rect_m.x = rect_p.x*2;
class CRectangle.
rect_m.y = rect_p.y*2; ▪ It simply has access to its private and
return(rect_m); protected members without being a
P. S. Mandal, IITGmember.
}
Example: Friend classes
#include <iostream> int main () {
using namespace std;
CSquare sqr;
class CSquare; // forward declaration CRectangle rect;
class CRectangle {
int x, y;
public: sqr.set_side(4);
int area () {return (x * y);} rect.convert(sqr);
void convert (CSquare a);
};
cout << rect.area();
class CSquare { return 0;
private: }
int side;
public:
void set_side (int a) {side=a;}
friend class CRectangle; // friend class
};
void CRectangle::convert (CSquare a) { Like friend function, we can also define a class
x = a.side; as friend of another one, yielding that first
y = a.side;
} class (CRectangle) access to the protected and
private members of the second (CSquare) one.

P. S. Mandal, IITG
Friendship is not transitive
• CRectangle is considered as a friend class by
CSquare, but CRectangle does not consider
CSquare to be a friend, so CRectangle can access
the protected and private members of CSquare
but not the reverse way.
• Of course, we could have declared also CSquare
as friend of CRectangle if we wanted to.
• Another property of friendships is that they are
not transitive: The friend of a friend is not
considered to be a friend unless explicitly
specified.

P. S. Mandal, IITG
Assignments to be done in class today
1. Program an array implementation of a dynamic stack with
changing size. Consider initial stack size is 2. When an
element is inserted in the stack which is “FULL”, the stack
capacity dynamically becomes double of the earlier size and
the element is inserted appropriately. In the same way, the
stack should release additional, unused memory that it might
have acquired dynamically.
2. Given integer array of size n, then arrange the elements in
sorted order. To do so read one integer at a time and check if
it is already in the given array, using binary search.

P. S. Mandal, IITG

You might also like