Download as pdf or txt
Download as pdf or txt
You are on page 1of 27

Overview of C++

Static Members
Static is a keyword in C++ used to give special
characteristics to an element.
Static elements are allocated storage only once in a
program lifetime in static storage area.
Static is a keyword which can be used with data members
as well as the member functions.
Static variables are created when the program starts and
destroyed when the program stops.
Visibility is similar to instance variables.
However, most static variables are declared public since
they must be available for users of the class.
• We can define class members static using static
keyword.
• When we declare a member of a class as static it means
no matter how many objects of the class are created,
there is only one copy of the static member.
• A static member is shared by all objects of the class.
• All static data is initialized to zero when the first object
is created, if no other initialization is present.
• We can't put it in the class definition but it can be
initialized outside the class as, using the scope
resolution operator :: to identify which class it belongs
to.
With and Without static
void counter() void counter()
{ {
static int count=0; int count=0;
cout << count++; cout << count++;
} }

int main() int main()


{ {
for(int i=0;i<5;i++) for(int i=0;i<5;i++)
{ {
counter(); counter();
} }
} }
Output Output
01234 00000
Scope resolution operator in C++

#include<iostream> • To access a global variable


using namespace std; when there is a local variable
with same name
int x; // Global x Output

int main()
Value of global x is 0
{
int x = 10; // Local x Value of local x is 10
cout << "Value of global x is " << ::x;
cout << "\nValue of local x is " << x;
return 0;
}
To define a function outside a class use scope resolution operator ::

#include <iostream>
using namespace std;
class A {
public:
// Only declaration
Output
void fun(); fun( ) called
};
// Definition of function outside the
class using ::

void A::fun()
{ cout << "fun() called"; }

int main()
{
A a;
a.fun();
return 0;
}
double Volume() {
#include <iostream>
return length * breadth * height;
}
using namespace std;
private:
class Box {
double length; // Length of a box
public:
double breadth; // Breadth of a box
static int objectCount;
double height; // Height of a box
};
// Constructor definition
// Initialize static member of class Box
Box(double l = 2.0, double b = 2.0, double
int Box::objectCount = 0;
h = 2.0) {
cout <<"Constructor called." << endl;
int main(void) {
length = l;
Box Box1(3.3, 1.2, 1.5); // Declare box1
breadth = b;
Box Box2(8.5, 6.0, 2.0); // Declare box2
height = h;
// Increase every time object is created
// Print total number of objects.
objectCount++;
cout << "Total objects: " <<
}
Box::objectCount << endl;

return 0;
}
Static Function Members

• By declaring a function member as static, you make it


independent of any particular object of the class.
• A static member function can be called even if no objects
of the class exist and the static functions are accessed
using only the class name and the scope resolution
operator ::
• A static member function can only access static data
member, other static member functions and any other
functions from outside the class.
static int getCount()
#include <iostream> { return objectCount;
}
using namespace std;
private:
class Box { double length; // Length of a box
public: double breadth; // Breadth of a box
static int objectCount; double height; // Height of a box
};
// Constructor definition // Initialize static member of class Box
Box(double l = 2.0, double b = 2.0, int Box::objectCount = 0;
double h = 2.0) int main(void)
{
{
// Print total number of objects before creating object.
cout <<"Constructor called." << endl; cout << "Inital Stage Count: " << Box::getCount() <<
length = l; endl;
breadth = b; Box Box1(3.3, 1.2, 1.5); // Declare box1
height = h; Box Box2(8.5, 6.0, 2.0); // Declare box2
objectCount++; // Print total number of objects after creating object.
} cout << "Final Stage Count: " << Box::getCount() <<
double Volume() { endl;
return length * breadth * height;
return 0;
} }
Friend function / class
• One of the important concepts of OOP is data hiding,
• Nonmember function cannot access an object's private or
protected data
• 'friend' function has access to all 'private' members of the
class for which it is a 'friend'.
• To declare a 'friend' function, include its prototype within
the class, preceding it with the C++ keyword 'friend'.
• Single functions or entire classes may be declared as friends of
a class.
• Function definition must not use keyword friend.
• Definition of friend function is specified outside the class body
and is not treated as part of the class.
• The major difference between the member function and the
friend function is that the member function is accessed through
the object , while friend function requires object to be passed as
parameter.
• It is invoked like normal function, it cannot be called using
object of that class.
• Friend function declaration can appear anywhere in the class.
• But a good practice would be where the class ends
COLLISION PROBLEM
Friend functions/class by-pass object data hiding

Friend
function
class TANK
Class Wall

Intermediary function that is


permitted to manipulate the
data members of both Tank
and Wall classes
Syntax for Friend function
#include <iostream>
using namespace std; int main()
class Test
{
{

private: Test obj;


int a;
public : obj.get();
void get() process(obj);
{ obj.disp();
cin>>a;
} return(0);
}
friend void process(Test );
void disp()
{
cout<<"\n a value in current object is =" <<a;
}
}; // class ends here
void process(Test n)
{

n.a=n.a+1;
cout<<"the value of a using friend function is
:"<<n.a;
}
#include <iostream>
using namespace std; void display(car x)
class car {
{ cout<<"\nThe color of the car is : "<<x.color;
private: cout<<"\nThe speed of the car is :
"<<x.speed;
int speed;
}
char color[20];

int main( )
public:
{
void input( )
car mine;
{
mine.input( );
cout<<"\nEnter the color : ";
display(mine); //passing the object ‘mine’ to
cin>>color;
the friend function
cout<<"\nEnter the speed : ";
return 0;
cin>>speed;
}
}
friend void display(car);
};
#include<iostream>
using namespace std; int main()
class base { {
int val1, val2; base obj;
public:
obj.get();
void get() { cout << "\n Mean value is : "
cout << "Enter two values:"; << mean(obj);
cin >> val1>>val2; }
}
friend float mean(base ob);
};

float mean(base ob) {


return float(ob.val1 + ob.val2) / 2;
}
C++ program to compare two object values and given the
higher value by using Friend function
#include<iostream> class bus
using namespace std; {
class bus; int length;
public:
class car
void setvalue(int l)
{
{
int length; length=l;
public: }
void setvalue(int l) friend void max(car,bus);
{ };
length=l; void max(car c,bus b)
} {
friend void max(car,bus); if(c.length>b.length)
cout<<"car is long";
};
else
cout<<"bus is long";
}
int main()
{
car c;
c.setvalue(12);
bus b;
b.setvalue(15);
max(c,b);
return 0;
}
// Add members of two different classes using
friend functions class ClassB
{
private:
#include <iostream> int numB;
using namespace std;
public:
// constructor to initialize numB to 1
class ClassB; ClassB()
{
class ClassA numB=1;
}
{ // friend function declaration
private: friend int add(ClassA, ClassB);
int numA; };
// access members of both classes
public:
int add(ClassA objectA, ClassB objectB)
ClassA() {
{ return (objectA.numA + objectB.numB);
numA=12; }

} int main()
{
// friend function declaration ClassA objectA;
ClassB objectB;
friend int add(ClassA, ClassB); cout << "Sum: " << add(objectA, objectB);
}; return 0;
}
friend Class in C++

• We can also use a friend class in C++ using the friend keyword

class ClassB;

class ClassA
{
// ClassB is a friend class of ClassA
friend class ClassB;
... .. ...
}
class ClassB {
... .. ...
}
– When a class is declared a friend class, all the member functions of
the friend class become friend functions.

– Since ClassB is a friend class, we can access all members of


ClassA from inside ClassB.

– However, we cannot access members of ClassB from inside


ClassA. It is because friend relation in C++ is only granted, not
taken.
#include <iostream>
using namespace std; int main()
class Exmp_A {
{ Exmp_A a;
int i=3; Exmp_B b;
b.display(a);
return 0;
friend class Exmp_B;
}
};
class Exmp_B
{
public:
void display(Exmp_A obj)
{
cout<<"The value of i is : "<<obj.i;
}
};
#include <iostream>
using namespace std;
class XYZ
int main()
{
{
private: ABC obj;
char ch='A'; XYZ obj2;
int num = 11; obj.disp(obj2);
public: return 0;
}
friend class ABC;
};
class ABC
{
public:
void disp(XYZ obj)
{
cout<<obj.ch<<endl;
cout<<obj.num<<endl;
}
};
#include<iostream> class Square
using namespace std; {
int S;
public:
class Rectangle Square()
{
{ S=5;
int L,B; }
void Display(Rectangle Rect)
public: {
Rectangle() cout<<"\n\n\tLength : "<<Rect.L;
cout<<"\n\n\tBreadth : "<<Rect.B;
{ cout<<"\n\n\tSide : "<<S;
L=10; }
};
B=20;
}
int main()
friend class Square; {
};
Rectangle R;
Square S;
S.Display(R);
return 0;
}
Call by value
#include <iostream>
using namespace std;
Output
void change(int data);
int main()
Value of the data is: 3
{
int data = 3;
change(data);
cout << "Value of the data is: " << data<< endl;

return 0;
}
void change(int data)
{
data = 5;
}
Call by reference in C++

#include<iostream>
int main()
using namespace std;
void swap(int *x, int *y)
{
{ int x=500, y=100;
int swap; swap(&x, &y);
swap=*x; cout<<"Value of x is: "<<x<<endl;
*x=*y; cout<<"Value of y is: "<<y<<endl;
*y=swap; return 0;
} }

Output
Value of x is: 100
Value of y is: 500

You might also like