Static Data Member Atype of data member that is shared by all objects of a class is known as static data member. Syntax: The static data member is defined in the class with static keyword. static Datatype nameofvariable; static int a; When a data member is defined as static, only one variable is created in the memory even if there are many objects of that class.
3.
Static Data Member Note: •in normal variable declaration, a seperate variable is created in the memory for each object of calss. • Static data member is visible only in the class in which It is defined but its life time starts when the program starts its execution. Its life time ends when the entire program is terminated. • It is normally used to share some data among all objects of a particular class
4.
Static Data Member Difference: •The main difference between normal data member and static data member is that each object has its own variable of normal data member. On the other hand, static data member is shared among all objects of the class. It means only one memory location is created for the static data member that is share among all objects.
5.
Static Data Member(Example)(Header.h) Write a program that counts the number of objects created of particular class. #pragma once #include<iostream> using namespace std; class student { private: static int objects_created; public: student() { objects_created++; } void display(); }; int student::objects_created = 0; void student::display() { cout << " Number of objects are created: " << objects_created << endl; }
Static member functions •A type of member function that can be accessed without any object of class is called static function. • Normally, a member function of any class cannot be accessed or executed without creating an object of that class. • The static data members of a class are not created for each object. • The class creates only one data member for all objects. • The static data member is defined when the program is executed. • The static member functions can be used to access a static data member.
11.
Accessing Static Functions class_name::static_FuncName(); •However, this won’t work on a normal member function; an object and the dot member-access operator are required in such cases. • To access function using only the class name, we must declare it to be a static member function.
12.
Static Member Function(Header.h) #pragma once #include<iostream> using namespace std; class student { private: static int marks1; static int marks2; static int marks3; public: static void display(); }; int student::marks1 = 60; int student::marks2 = 70; int student::marks3 = 80; void student::display() { cout << " Subject 1 marks: " << marks1 << endl; cout << " Subject 2 marks: " << marks2 << endl; cout << " Subject 3 marks: " << marks3 << endl; }
13.
Static Data Function(Source.cpp) #include<iostream> #include "Header.h" using namespace std; int main() { student obj1; cout << " Access with object: " << endl; obj1.display(); cout << 'n'; cout << " Access with class name" << endl; student::display(); cout << 'n'; system("pause"); }
14.
Friend function • Afriend function of a class is defined outside that class’s scope, but it has the right to access all private and protected members of the class. • An ordinary function that is not the member function of a class has no privilege to access the private data members. Still, the friend function does have the capability to access any private data members.
15.
Syntax Friend function classclassName { ... .. ... friend return_type functionName(class name); ... .. ...}; return_type functionName(argument/s) { ... .. ... // Private and protected data of className can be accessed from // this function because it is a friend function of className. ……………..}
16.
Friend Function (Header.h) #pragmaonce #include<iostream> using namespace std; class student { int id; int marks; public: student(); friend void set_data(student& a); friend int get_id(student& b); friend int get_marks(student& c); friend void display(student& d); }; student::student() { id = 0; marks = 0; } void set_data(student &a) { cout << " Enter your id: "; cin >> a.id; cout << " Enter your subject marks: "; cin >> a.marks; } int get_id(student& b) { return b.id; } int get_marks(student &c) { return c.marks; } void display(student& d) { cout << " Student id is: " << d.id << endl; cout << " Student marks is: " << d.marks << endl; }
Friend function (Header.h) #pragmaonce #include<iostream> using namespace std; class student2; class student { int id; int marks; public: student(); friend void set_data(student& a, student2& aa); friend int get_id1(student& b); friend int get_id2(student2& bb); friend int get_marks1(student& c); friend int get_marks2(student2& cc); friend student sum(student& a, student2& aa); friend void display(student& d); }; class student2 { int id2; int marks2; public: student2(); friend void set_data(student& a, student2& aa); friend int get_id1(student& b); friend int get_id2(student2& bb); friend int get_marks1(student& c); friend int get_marks2(student2& cc); friend student sum(student& a, student2& aa); friend void display(student& d); }; student::student() { id = 0; marks = 0; } student2::student2() { id2 = 0; marks2 = 0; } void set_data(student& a, student2& aa) { cout << " Enter your student id : "; cin >> a.id; cout << " Enter your student id2 : "; cin >> aa.id2; cout << " Enter your subject marks : "; cin >> a.marks; cout << " Enter your subject marks2 : "; cin >> aa.marks2; }
20.
Friend function (Header.h) intget_id1(student& b) { return b.id; } int get_id2(student2& bb) { return bb.id2; } int get_marks1(student &c) { return c.marks; } int get_marks2(student2& cc) { return cc.marks2; } student sum(student& a, student2& aa) { student s4; s4.id = a.id + aa.id2; s4.marks = a.marks + aa.marks2; return s4; } void display(student& d) { cout << " Sum of both id is: " << d.id << endl; cout << " Sum of marks is: " << d.marks << endl; }
Friend function (Pointerid) (Header.h) #pragma once #include<iostream> using namespace std; class student2; class student { int *id; int marks; public: student(); friend void set_data(student& a, student2& aa); friend int get_id1(student& b); friend int get_id2(student2& bb); friend int get_marks1(student& c); friend int get_marks2(student2& cc); friend student sum(student& a, student2& aa); friend void display(student& d); }; class student2 { int *id2; int marks2; public: student2(); friend void set_data(student& a, student2& aa); friend int get_id1(student& b); friend int get_id2(student2& bb); friend int get_marks1(student& c); friend int get_marks2(student2& cc); friend student sum(student& a, student2& aa); friend void display(student& d); }; student::student() { id = new int; id = 0; marks = 0; } student2::student2() { id2 = new int; id2 = 0; marks2 = 0; } void set_data(student& a, student2& aa) { a.id = new int; cout << " Enter your student id : "; cin >> *a.id; aa.id2 = new int; cout << " Enter your student id2 : "; cin >> *aa.id2; cout << " Enter your subject marks : "; cin >> a.marks; cout << " Enter your subject marks2 : "; cin >> aa.marks2; }
23.
Friend function (Header.h) intget_id1(student& b) { return *b.id; } int get_id2(student2& bb) { return *bb.id2; } int get_marks1(student &c) { return c.marks; } int get_marks2(student2& cc) { return cc.marks2; } student sum(student& a, student2& aa) { student s4; int save_pointer_sum; save_pointer_sum = *a.id + *aa.id2; s4.id = &save_pointer_sum; s4.marks = a.marks + aa.marks2; return s4; } void display(student& d) { cout << " Sum of both id is: " << *d.id << endl; cout << " Sum of marks is: " << d.marks << endl; }
Class activity Write aC++ program to swap the values of two integer members of different classes using the friend function. ClassA Data Member: value1 (integer) Methods: setdata( ): (a type of void function used to take value of value1 (integer) display() : (a type of void function used to display value of value1 before swaping. exchange (class1 ,class2 ) : Friend Function (swap and display values after swapping) ClassB Data Member: value2 (integer) Methods: setdata( ): (a type of void function used to take value of value2(integer) display() : (a type of void function used to display value of value2 before swapping. exchange (class1 ,class2) :Friend Function (swap and display values after swapping)
26.
Friend class •A friendclass is a class that can access the private and protected members of a class in which it is declared as a friend. •This is needed when we want to allow a particular class to access a class’s private and protected members.
27.
Syntax Friend class classClassB; class ClassA { // ClassB is a friend class of ClassA friend class ClassB; ... .. ... }; class ClassB { ... .. ... }
28.
Friend class (Header.h) #pragmaonce #include<iostream> using namespace std; class student { private: int id; int marks; public: friend class student2; }; class student2 { public: student2(); student object; void set_id(); int get_id(); int get_marks(); void display(); }; student2::student2() { object.id = 0; object.marks = 0; } void student2::set_id() { cout << " Enter your id: "; cin >> object.id; cout << " Enter your marks: "; cin >> object.marks; } int student2::get_id() { return object.id; } int student2::get_marks() { return object.marks; } void student2::display() { cout << " Student id: " << object.id << endl; cout << " Student marks: " << object.marks << endl; }
Class activity • Createa class “data” with attribute distance in centimeter. • Create another class “Data2” that will be a friend class of “data” and it should have the following attributes and member functions: • Length as an attribute • Input function to take the input of distance and length. • Check function to check the values of length and sitance at point’0’. Increment the value by 1 if any of the value or both the values are zero before adding.