Classes and Objects Friend Function Friend Class (Compatibility Mode)
Classes and Objects Friend Function Friend Class (Compatibility Mode)
Introduction
• Just an extension of idea of structures in C
• A new way of creating and implementing user-
defined data type
2
Extensions to Structures
• In C++, structures can have both variables and functions
struct student
{
char name[20];
int rollno;
char grade[2];
};
struct student krishna; //Allowed in C
student krishna; //Error in C but allowed in C++
• In C++, structure names are stand-alone and can be used like any other type
names
• All these extensions are incorporated in a user defined type called class in C++
• Class is a specially designed data type in C++
• Structure are normally used for holding the data and classes for holding both data
and function
3
Creating classes
• A class is a way to bind data and its associated
functions together
• Class allows data and functions hiding
• When defining a class, we are creating a new
abstract data type
• A class specifications has:
– Class declaration
– Class function definitions
4
Creating classes
keyword
visibility labels
class class_name data members
{
private:
variable declarations;
class members
function declarations;
public:
variable declarations;
class members
function declarations;
};
Member functions
5
Class: STUDENT
Example DATA
roll_number
class student marks
{ …….
int roll_number; FUNCTIONS
insertdata()
float marks;
displaydata()
public: …….
void insertdata(int a, float b);
void displaydata(void); STUDENT
};
inserttdata()
displaydata()
……….
6
Accessing Objects
• Once class has been declared, we can declare
variable of a type by using the class name
student a; //Creates memory for a
• The class variable is known as object. Therefore, a is
an object of type student
student a, b, c ; //More than one object declaration
class student
{
………
……..
……..
}a,b,c; 7
Accessing Class Members
• The private data of a class can be accessed only
through the member functions of that class
• The main() function can not contain statements that
access roll and marks directly
Object-name.function-name(actual-arguments);
• Example
a.insertdata(10, 85.4);
a.displaydata();
insertdata(10, 85.4); // Illegal. Member functions can be invoked only by using object
a.roll_number=10; // Roll number is declared private, so illegal
8
Accessing Class Members
• Objects communicate by sending and receiving
messages a.displaydata(); sends a message to object a
requesting it to display its contents
• A variable declared as public can be accessed by the
objects directly class
{
xyz
int x;
int y;
public:
int z;
};
…….
…….
xyz p;
p.x=10; //Error
9
p.z=100; //Correct, z is public
Defining Member Functions
• Two ways:
– Outside the class definition
– Inside the class definition
10
Defining Member Functions
• Outside the class definition
– Member functions defined inside a class have to be defined separately
outside the class
– The difference between a member function and a normal function is that
a member function incorporates a membership ‘identity label’ in the
header
– This ‘label’ tells the compiler which class the function belongs to
return-type class-name::function-name (argument declaration)
{
Function body
}
– The membership label class-name:: tells the compiler that the function-
name belongs to the class-name
11
Example
void student::insertdata(int a, float b)
{
roll_number = a;
marks = b
}
void student::displaydata(void)
{
cout<<“Roll Number: ”<<roll_number<<“\n”;
cout<<“Marks: marks: ”<<marks<<“\n”;
}
15
An Example (Contd..)
int main()
{
int student_roll;
float student_marks;
student x; //Derive object x from student class
cout<<"Enter Roll Number:\n";
cin>>student_roll;
cout<<"Enter Marks:\n";
cin>>student_marks;
cout<<“Student info:\n";
x.insertdata(student_roll, student_marks); //Call member functions of class student
x.displaydata();
return 0;
}
16
An Example2
class student
{
char name[20];
int roll_number;
float marks;
public:
void insertdata(char a[], int b,float c);
void displaydata(void)
{
cout<<"Name: "<<name<<"\n";
cout<<"Roll Number: "<<roll_number<<"\n";
cout<<"Marks: "<<marks<<"\n\n\n";
}
};
17
An Example2 (Contd..)
void student::insertdata(char a[], int b, float c)
{
for(int i=0;i<20;i++)
{
name[i] = a[i];
}
roll_number = b;
marks = c;
}
18
An Example2 (Contd..)
int main()
{
char student_name[20];
int student_roll;
float student_marks;
student x; //Derive object x from student class
cout<<"Enter name:\n";
cin>>student_name;
cout<<"Enter Roll Number:\n";
cin>>student_roll;
cout<<"Enter Marks:\n";
cin>>student_marks;
cout<<“Student info:\n";
x.insertdata(student_name, student_roll, student_marks); //Call member
x.displaydata(); // functions of class student
return 0;
}
19
Static Data Member & Static Member Function
20
static Class Members
• static data member
– Only one copy of a variable shared by all objects
of a class
• “Class-wide” information
• A property of the class shared by all instances, not a
property of a specific object of the class
– Declaration begins with keyword static
21
Static Data Member
class student int main()
{ {
int roll; student s1; // [roll,marks]
int marks; student s2; // [roll, marks]
}; }
// 2 objects of class student are created here
// s1 will have separate id and marks
// So does s2
//general concept of OOP
22
Static Data Member
• As you create an object from a class
– Each and every object will get a separate variables
of that class
– Methods[functions] are being shared
• If the variable also be needed to be shared by
each and every object in a class
– Static Data members
– Should be common for s1 and s2 [previous
example]
23
Static Data Member
class student // count is not a variable
{ of an object but it is a
int roll;
variable of class
int marks;
static int count; // can be manipulated by
}; any objects of that class
// s1 and s2 will share
common variable count // can be declared only
// will also be shared by once inside the class
future object example : s3
24
Example
#include<iostream> void student:: change(int a,int b)
using namespace std; {
class student roll=a;
{ marks=b;
int roll; count=count+20;
int marks; cout<<count;
static int count; }
public: int main()
void change(int,int); {
}; int val1,val2;
student s1,s2;
int student::count=20; cin>>val1>>val2;
s1.change(val1,val2);
cin>>val1>>val2;
s2.change(val1,val2);
}
25
static Class Members
• static member function
– Is a service of the class, not of a specific object of
the class
• static applied to an item at file scope
– That item becomes known only in that file
– The static members of the class need to be
available from any client code that accesses the
file
• So we cannot declare them static in the definition
part but we declare them static only in declaration.
26
static Class Members
• Use static data members to save storage when a single
copy of the data for all objects of a class will suffice.
• A class’s static data members and static member
functions exist and can be used even if no objects of that class
have been instantiated.
• A static function cannot include non- static function but it can
incorporate other static function
• You do not need to create object in order to call static
function from outside the class example is given
– A static member function is one which can be called directly form
main ( ) without associating it to an object
– static member function can be called without creating instances /
objects of the class.
27
Static Member Function
#include<iostream> void student:: change(int a,int b)
using namespace std; {
class student roll=a;
{ marks=b;
int roll; count=count+20;
int marks; cout<<count;
static int count; }
public: int main()
void change(int,int); {
static int getcount( ) int val1,val2;
{ student s1,s2;
count; // Valid cin>>val1>>val2;
roll; // Not valid s1.change(val1,val2);
} cin>>val1>>val2;
}; s2.change(val1,val2);
int student::count=20; }
28
#include<iostream>
Example 2
void student::getcount()
using namespace std; {
cout<<" My present value is:"<<count;
// display(); // invalid if display is defined as non static function
class student
}
{
int roll; void student::display()
int marks; {
static int count; // cout<< " Present Roll is:"<<roll<< " and marks is:"<<marks<<endl; // error
because static function cannot hold non static data members
public:
cout<<" I am display"<<endl;
void change(int,int); //cout<<count; // valid because not static function can access static variables
static void getcount(); }
static void display();
}; int main()
{
int val1,val2;
int student::count=100; student s1,s2;
cin>>val1>>val2;
void student:: change(int a,int b) s1.change(val1,val2);
{ cin>>val1>>val2;
s2.change(val1,val2);
roll=a;
s2.getcount();
marks=b; s2.display();
count=count+20; // student::display(); // call of static member function outside class
cout<<count; }
}
29
Friend Function
• Encapsulation & data hiding concept is that we can not access
private member of a class directly with the help of object of
that class
– need a public member function to access private member of a class
– Any non member function can not access the private data of a class
• C++ allows a mechanism, in which a non member function can
access the private data of a class. this can be done by
declaring a non member function friend to a class whose
private data is to be accessed.
30
Characteristic of a Friend Function
• 1→ Friend function is not in the scope of the class in which it
has been declare as friend. So friend function cannot be called
with the help of object of that class.
• 2→ Friend function can be invoked like a normal function with
out any help of any object.
• 3→ Friend function cannot access member name directly. It
has to use object name with dot operator.
• 4→ Friend function can be declared either in public or private
part of the class with out affecting its meaning.
• 5→ Friend function can access many object of many class in
which it has been declared as friend.
• 6→ usually , friend function has object as argument.
31
Example 1: declaration in Public Part
#include<iostream> float mean(sample s)
using namespace std; {
class sample return (s.a+s.b)/2.0; // accessing private
{ data with help of object of that class
int a;
}
int b;
main()
public:
void setvalue( )
{
{ sample x;
a=10; x.setvalue();
b=25; cout<<mean(x); // call to friend function
} return 0;
friend float mean(sample s); // Declaration of }
friend function in public part
};
32
Example 2: Declaration in Private Part
#include<iostream> float mean(sample s)
using namespace std; {
class sample return (s.a+s.b)/2.0; // accessing private
{ data with help of object of that class
int a; }
int b; main()
friend float mean(sample s); // {
Declaration of friend function in sample x;
public part
p zz
x.setvalue();
cout<<mean(x); // call to friend function
public: return 0;
void setvalue( ) }
{
a=10;
b=25;
33
}
Example 3: Friend function using multiple class
#include<iostream> float mean(sample1 s1 , sample2 s2)
using namespace std;
// Definition of friend function
class sample2; // called forward declaration
{
class sample1
{
return (s1.a+s2.i)/2.0;
int a; }
public:
void setvalue() main()
{ {
a=10; sample1 x;
} x.setvalue();
friend float mean(sample1 s1,sample2 s2); // Declaration of
sample2 y;
friend function in class sample1
};
y.setvalue();
class sample2 cout<<mean(x,y);// calling of friend
{ function without help of any object
int I; return 0;
public: }
void setvalue()
{
i=15;
}
friend float mean(sample1 s1,sample2 s2); // Declaration of
friend function in class sample1
};
34
Friend Class
• We know that member of one class can not access the private
data of other class. Some time it is needed that we want to
make available private data of one class to other class. In such
case we need to make one class to a friend of other class.
• For example, there is two class, class A & class B. object of
class A can access private data of class B if class A is a friend to
Class B. to make the class A as friend of class B we need to
declare a member function of class A as a friend function to
class B.
• It is a member function of one class and uses object of other
class as arguments so the function has to be called using
corresponding class object and other class object as
arguments.
35
Example
#include<iostream> void test1:: display( test2 t2 ) // definition of
using namespace std; member function of class test1 which friend
class test2; // forward declaration function to class test2
class test1
{
{
int a; cout<<a<<endl;
public: cout<<t2.b<<endl;
void setvalue() }
{
a=30;
} int main()
void display( test2 t2 ); // member function of
class test1
{
}; test1 x;
x.setvalue();
class test2 test2 y;
{ y.setvalue();
int b; x.display(y); // call to member function display()
public: return 0;
void setvalue()
{
}
b=40;
}
friend void test1:: display( test2 t2 ); // member
function of class test1 has been declare as friend
function to test2
}; 36
END of Chapter 4
37