C++ Lecture Note
C++ Lecture Note
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 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 {
};
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 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 {
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
second class
{
friend class one;
};
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;
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
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.
Example:
Machine Translated by Google
Class A
{
public
const int f(){...}
int f(){...}
// IT'S BAD...
{
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)
{
…
};
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.).
Class Point
double x,y;
Public:
return 1;
else
return 0;
};
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;
OPERATOR +
1) Global function
p.x+=1;
p.y+=l;
return p;
}
point p = x this;
px += l;
py += l;
return p;
}