Class
Class
class class_name
{
private: private members or methods
…
…
…
public:
… Public members or methods
…
…
};
Private:
only members of that class have accessibility
can be accessed only through member functions of
that class.
Private members and methods are for internal
use only.
Public:
• Accessible from outside the class
• can be accessed through member function of any
class in the same program.
Protected:
• Stage between private and public access.
• They cannot be accessed from outside the class, but
can be accessed from the derived class(inheritance)
Class Example
• This class example shows how we can encapsulate
(gather) information into one package (unit or class)
float age;
public:
void getdata(void);
};
int main()
employee manager[3];
employee worker[3];
manager[i].getdata();
}
Objects as Function arguments
• This can be done in two ways:
A copy of the entire object is passed to the function( pass
by value)
Only the address of the object is transferred to the
function(pass by reference)
In first way, a copy of the object is passed to function, any
changes made to the object inside the function do not
affect the object used to call the function.
In second method, address of object is passed, the called
function directly works on actual object used in call. So any
changes made to object inside the function will reflect in
the actual object
#include<iostream>
using namespace std;
class time int main()
{ {
int hours; Time T1, T2, T3;
int minutes; T1.gettime(2,45); //get T1
public: T1.gettime(3,30); //get T2
void gettime(int h, int m) T3. sum(T1, T2); // T3= T1+T2
{ cout<<“T1=“; T1.puttime(); //display T1
hours=h; minutes=m;} cout<<“T2=“; T2.puttime(); //display T2
void puttime(void) cout<<“T3=“; T3.puttime(); //display T3
{ return 0;
cout<<hours<<“hours and”; }
cout<<minutes<< “minutes”<< “\n”;
} Output:
void sum(time, time); // declaration with objects T1= 2 hours and 45 minutes
as arguments T2= 3 hours and 30 minutes
}; T3= 6 hours and 15 minutes
void time:: sum(time t1, time t2) //t1,t2 are
objects
{
minutes=t1.minutes + t2.minutes;
hours= minutes/60;
minutes= minutes%60;
hours= hours + t1.hours + t2.hours;
}
Friend function
• To make outside function “friendly” to a class,
declare this function as a friend of the class :
class ABC
{
..............
public:
............
friend void xyz(void); //declaration
};
Friend function
• Function definition does not use either the keyword
friend or the scope operator ::
• A function can be declared as a friend in any
number of classes.
• A friend function, although not a member function,
has full access rights to the private members of the
class.
A friend function possesses certain special characteristics:
• It is not in the scope of the class to which it has been declared
as friend.
• Since it is not in the scope of the class, it cannot be called
using the object of that class.
• It can be invoked like a normal function without the help of
any object.
• Unlike member functions, it cannot access the member names
directly and has to use an object name and dot membership
operator with each member name (e.g., A.x)
• It can be declared either in public or private part of a class
without affecting its meaning.
• Usually, it has the objects as arguments.
# include<iostream.h>
using namespace std;
class sample
{ Output:
int a; Mean value = 32.5
int b;
public:
void setvalue() { a=25; b=40; }
friend float mean(sample s);
};
float mean(sample s)
{
return float(s.a+s.b)/2.0;
}
int main)
{
sample X; //object X
X.setvalue();
cout<<“MeanValue=“<<mean(X)<<“\n”;
// passes the object X by value to the
friend function
return 0;
}
• Member functions of one class can be friend functions of
another class. In such cases , they are defined using scope
resolution operator as shown below:
class X
{
.........
int fun1(); // member function of X
};
class Y
{
friend int X:: fun1(); // fun1() of X is friend of Y
};
• Also declare all the member functions of one class as the
friend functions of another class. In such cases, the class is
called a friend class.
• A friend class is a class whose members have access to the
private or protected members of another class
• This can be specified as follows:
class Z
{
.........
friend class X; //all member functions of X are
// friends to Z
Using friend function to Add data objects
of two different classes
{
#incude<iostream.h> data=value;
class ABC; //forward declaration }
class XYZ friend void add ( XYZ, ABC);
{ };
int data; void add (XYZ obj1, ABC obj2)
public: {
void setvalue( int value) cout<<“ sum of data
{ value=“<<obj1.data + obj2.data;
data = value; }
} int main()
friend void add (XYZ, ABC); // friend function {
declaration XYZ x;
}; ABC a;
class ABC x.setvalue(5);
{ a.setvalue(50);
int data; add (x,a);
public: return 0;
void setvalue(int value) {
Function friendly to two classes
# include<iostream>
using namespace std;
class ABC; //forward declaration
class XYZ
{
int x; int main()
public: {
void setvalue(int i) { x=i;} ABC abc;
friend void max(XYZ, ABC); abc.setvalue(10);
}; XYZ xyz;
class ABC xyz.setvalue(20)
{ max(xyz,abc);
int a; return 0;
public: }
void setvalue(int i) { a=i;}
friend void max(XYZ, ABC); OUTPUT:
}; 20
void max(XYZ m, ABC n) //Definition of
friend
{
if(m.x>= n.a)
cout<<m.x;
else
cout<<n.a;
}
Swapping Private data of classes
# include<iostream.h> C1.indata(100);
using namespace std; C2.indata(200);
class class_2;
class class_1 cout<<“Values before
{ exchange”<“\n”;
int value1; C1.display();
public: C2.display();
void intdata(int a) { value1=a;} exchange(C1,C2); //swapping
void display(void) { cout<<value1<<‘\n”; }
friend void exchange(classes_1 &, cout<<“values after exchange”<<“\n”;
classes_2 &); C1.display();
}; C2.display();
void exchange(class_1 & x, class_2 & y) return 0;
{ }
int temp = x.value1; OUTPUT:
x.value1 = y.value2; Values before exchange
y.value2 = temp; 100
} 200
int main() Values after exchange
{ 200
class_1 C1; 100
class_2 C2;
Returning Objects
• A function cannot only receive objects as arguments but also
can return them.
# include<iostream.h> void complex :: show(complex c)
using namespace std; {
class complex cout<<c.x<<“+ j”<<c.y<<“\n”;
{ }
float x,y; int main()
public: {
void input(float real, float imag) complex A,B,C;
{ x=real; y=imag; }
friend complex sum (complex, complex); A.input(3.1, 5.65);
void show(complex); B. input(2.75, 1.2);
};
complex sum(complex c1, complex c2) C= sum(A,B);
{ cout<<“A=“; show(A); //sum() is a friend
complex c3; cout<<“B=“; show(B);
c3.x=c1.x+c2.x; cout<<“C=“; show(c);
c3.y=c1.y+c2.y; return 0;
return(c3); }
}
Pointer to Objects
• A pointer to a C++ class is done exactly the same way as a pointer to a
structure and to access members of a pointer to a class you use the
member access operator -> operator, just as you do with pointers to
structures. Also as with all pointers, you must initialize the pointer before
using it.
#include <iostream> objectPointer = &ob; // get address of ob
using namespace std; cout << objectPointer->getInt();
class myclass { // use -> to call getInt()
int i; return 0;
public: }
myclass(int j) {
i = j;
}
int getInt() {
return i;
}
};
int main()
{
myclass ob(88), *objectPointer;
Pointers to Members
• It is possible to take address of a member of a class and assign
it to a pointer.
• The address of a member can be obtained by applying the
operator & to a “fully qualified “ class member name.
• A class member pointer can be declared using the operator ::*
with the class name.
Ex: class A
{
private:
int m;
public:
void show();
};
• Define a pointer to the member m as follows:
int A ::* ip = &A :: m;
– ip pointer created thus acts like a class member in
that it must be invoked with a class object.
– A::* means “pointer-to-member” of A class.
– &A::m means the “address of the m member of A
class”.
– ip now be used to access the member m inside
member functions.
• The dereferencing operator ->* is used to access a
member when we use pointers to both the object
and the member.
• The dereferencing operator .* is used when object
itself is used with the member pointer.
Example
#include <iostream> // create an object of class type X
using namespace std;
X xobject;
class X
{ // initialize data member
public: xobject.*ptiptr = 10;
int a;
void f(int b) cout << "The value of a is " << xobject.*ptiptr<<
{ endl;
cout << "The value of b is "<< b << endl;
}
};
// call member function
int main() (xobject.*ptfptr) (20);
{ }
// declare pointer to data member
int X::*ptiptr = &X::a;