Machine Translated by Google
Computer Programming, Semester 3, Lecture No. 5, 18.11.2011
FRIENDSHIP
Familiarization is a method of breaking down encapsulation and objectivity. A given class / function / method
has full access to the friend class - including private elements. We define friending in a class sharing its
resources with a "friend".
void fun();
class B {
.
.
.
};
class C
{
public:
void met();
};
class A
{
private:
int a;
public:
friend void fun();
friend class B;
friend void C::met();
};
• Names must already be declared when using friend declarations. • Friendship is not transitive My
friend's friend is not my friend. • Friendship is not mutual - if class A is friends with class B, then class
B is NOT friends with class A. • Friendship is not hereditary - friend of my parents is not my friend.
Machine Translated by Google
class Four; // prototype
int spr; // prototype
class point {
double x,y;
Public:
Point(double ax, double ay) {
x=ax;
y=ay;
}
...
friend int spr (Point &p, Four &c); // friend one function spr.
};
class Four {
double x1, y1, x2, y2;
Public:
Four( double ax1, double ax2, double ay1, double ay2 ) { };
};
int spr (Point &p, Quad &c ) {
if ((px >= c.x1) && (px <= c.x2) && (py >= p.y1) && (py <= c.y2))
returns 2;
else
return 0;
};
Thanks to friendship, only the spr function has access to data from the Point class.
Machine Translated by Google
class Four; // prototype int
spr; // prototype
class point {
double x,y;
Public:
Point(double ax, double ay) {
x=ax;
y=ay;
}
...
friend int Four::spr (Point &p,); // make friends with a particular method of one class.
};
class Four {
double x1, y1, x2, y2;
Public:
Four( double ax1, double ax2, double ay1, double ay2 ) { };
int spr (&p point) {
if ((px >= c.x1) && (px <= c.x2) && (py >= p.y1) && (py <= c.y2))
returns 2;
else
return 0;
};
};
Now the spr method of the Four class, and ONLY this method of this class can access the private data of the Point
class. Other methods from the Four class, as well as the Four class itself, have no access to the private data of the
Point class.
Machine Translated by Google
Two-way friendship
class two; // PROTOTYPE
class first {
friend class two;
};
second class
{
friend class one;
};
static, const, volatile modifiers
STATIC
Class A
{
static int a;
Public:
static int b;
void met()
{
a=10;
}
};
int A::a = 0;
intA::b=10;
// WHETHER A VARIABLE IS PRIVATE OR PUBLIC, IT CAN BE DECLARED.
A::ob.b = 4;
Aob1;
cout << ob1.b; // will print 4
Machine Translated by Google
STATIC VARIABLE IS ONE FOR ALL OBJECTS OF A GIVEN CLASS. THAT MEANS IF I CHANGE IT IN
ONE OBJECT, ALL OTHER OBJECTS OF THE CLASS WILL FEEL IT. THIS VARIABLE OCCUPATES ONE
SLOT IN MEMORY FOR ALL OBJECTS AT ONCE.
It's like a global variable, but when it's in a private section, only objects from that class can use it.
We can call a static method even if there are no objects of the class. For example, a static function, but we
can't use the this pointer in it because there is no object to point to.
CONST
const void print()
{
b++ // WRONG
cout << b
void cos()
{
…
};
const ob.print();
ob.cos() // WRONG
VOLATILE
volatile int a;
This prohibits the compiler from optimizing the variable/object/function. Useful when some other
program wants to use our variable / object / function.
Modifiers are not resolved when using overloaded functions.
Example:
Machine Translated by Google
Class A
{
public
const int f(){...}
int f(){...}
// IT'S BAD...
THANKS TO ROLLING, YOU CAN GET RID OF MODIFIERS
class X
{
int m;
Public:
const void fun 1()
{
m++ // error
}
const void fun2()
{
((X*)this)>m++; // THAT'S GOOD
...
OPERATORS
They can be defined in:
1) class method
2) global function
type...
operator @ (arg)
{
…
};
Use common sense - do not make operators, e.g. +, which subtract.
Machine Translated by Google
We can overload all operators except:
. .* :: sizeof
Only as methods we can overload operators:
() [ ] ÿ new delete (type, e.g. integer or something), ==
We cannot make operators from non-standard ASCI characters that are not normally used (heart, etc.). We
always define the operator for the argument to its left.
The operators += , + and = are 3 different operators, each defined separately. The same applies to other multi-
operand operators ( ==, <=, >=, etc.).
Defining operators in a class:
Class Point
double x,y;
Public:
int operator <= (Point &p)
if ((x<=px) && (y<=py))
return 1;
else
return 0;
};
point p1, p2;
int a = (p1<=p2); // 1 way
int a = p1.operator<=(p2); // 2nd way
Defining an operator as a global function. It is advisable to make friends of the function with the class to which the
operator will apply.
Machine Translated by Google
fun
int operator <=(point &p1, point &p2); // (left argument, right argument);
if ((p1x<=p2.x)&&(p1.y<=p2.y))
return 1;
else
return 0;
point p1, p2;
int a = (p1<=p2); // 1 way
int a = p1.operator<=(p1,p2); // 2nd way
OPERATOR +
1) Global function
Operator point + (Point p, int l)
{
p.x+=1;
p.y+=l;
return p;
}
2) The method in the object
operator point + (int l)
{
point p = x this;
px += l;
py += l;
return p;
}
by Kozak127, IT, Rybnik