You are on page 1of 36

C++ (2)

principles of object-oriented programming:


• Encapsulation
• Abstraction
• Data hiding
• --
• --
ENCAPSULATION, ABSTRACTION and DATA
HIDING
• Encapsulation is a process of combining data members and functions
in a single unit called class. This is to prevent the access to the data
directly, the access to them is provided through the functions of the
class. It is one of the popular feature of Object Oriented
Programming(OOPs) that helps in data hiding.
• Data abstraction refers to providing only essential information to the
outside world and hiding their background details, i.e., to represent
the needed information in program without presenting the details.
• Data abstraction is a programming (and design) technique that relies
on the separation of interface and implementation.
Data abstraction provides two important
advantages −
• Class internals are protected from inadvertent user-level errors, which
might corrupt the state of the object.
• The class implementation may evolve over time in response to
changing requirements or bug reports without requiring change in
user-level code.
• Array in class
• Array of objects..

Exercise:

Redefine class students with Marks as array of size 3 and declare array
of objects of this class
Today..
• Object as parameter to member function/outside function
• Returning object from a function
• Calling non member functions from within a class
• ‘this’ keyword
• Member function overloading
• Friend function
Object as parameter to a (member) function
The objects of a class can be passed as arguments to member functions
as well as nonmember functions either by value or by reference. When
an object is passed by value, a copy of the actual object is created
inside the function. This copy is destroyed when the function
terminates. Moreover, any changes made to the copy of the object
inside the function are not reflected in the actual object. On the other
hand, in pass by reference, only a reference to that object (not the
entire object) is passed to the function. Thus, the changes made to the
object within the function are also reflected in the actual object.
Object as parameter to a (member) function
Define class weight to accept weight in Kg and grams . Write a non
member function to find sum of weights of two 2 objects of class
weight.
Object as parameter to a (member) function
class weight
{
int kilogram;
int gram;
public:
void getdata();
void putdata();
int getKG(){ return kilogram;}
int getGM() { return gram;}
};
void sum_weight(weight wl, weight w2)
{
int G = wl.getGM() + w2.getGM();
int KG =G/1000;
G = G%1000;
KG = KG+ wl.getKG()+w2.getKG();

cout<<"TOTAL WT IS : "<<KG<<"Kg and "<<G<<"gm";


getch();
}
Object as parameter to a (member) function
• Write compare method with object as parameter to compare weight
of calling object with passed object.
void weight::compare(weight w)
{
if(kilogram > w.getKG())
// data member of calling object can
cout<<"MORE"; be directly accessed from within the
class but for the passed object method
else if(kilogram < w.getKG()) has to be used.
cout<<"LESS";
else if(gram > w.getGM())
cout<<"MORE";
else if(gram < w.getGM())
cout<<"LESS";
else
cout<<"EQUAL";
getch();
}
int main()
{
weight w1,w2;
w1.getdata(100,250);
w2.getdata(45,679);
w1.putdata();
w2.putdata();
w1.compare(w2);
return 0;
}
Returning object from a function
Modify sum_weight method to return object of type weight
class weight
{
int kilogram;
int gram;
public:
void getdata(int,int);
void putdata();
int getKG(){ return kilogram;}
int getGM() { return gram;}
};
weight weight::sum_weight(weight wl, weight w2)
{
weight totalwt;
int G = wl.getGM() + w2.getGM();
int KG =G/1000;
G = G%1000;
KG = KG+ wl.getKG()+w2.getKG();
totalwt.getdata(KG,G);
return totalwt;

}
int main(){

weight W = sum_weight(w1,w2);
W.putdata();
‘this’ keyword
In C++ programming, ‘this’ is a keyword that refers to the current
instance of the class. There can be 3 main usage of this keyword in C++.

It can be used to pass current object as a parameter to another method.


It can be used to refer current class instance variable.
It can used to return current class instance variable.
Passing pointers to objects and returning
pointer to object
class weight
{
int kilogram;
int gram;
public:
void getdata();
void putdata();
int getKG(){ return kilogram;}
int getGM() { return gram;}
weight* compare(weight*); //returns pointer to an object with more weight
};
weight* weight::compare(weight *w){
if(kilogram > w->getKG())
return this;
else (kilogram < w->getKG()) -> operator is used
return w; to refer members of
a class
void diff(this,w);

}
void weight :: getdata(){
cout<<"/nKilograms:";
cin>>kilogram;
cout<<"Grams:";
cin>>gram;
}
void weight :: putdata ()
{
cout<<kilogram<<" Kgs. and"<<gram<<" gros.\n";
}
int main()
{
weight w1,w2;
w1.getdata();
w2.getdata();
w1.putdata();
w2.putdata();
weight* w3 = w1.compare(&w2);
w3->putdata();
getch();
return 0;
}
Accessor and Mutator Function
• An accessor doesn't need arguments
• An accessor has the same type as the retrieved variable
• The name of the accessor begins with the Get prefix
• A naming convention is preferred
• While an accessor function makes a data member accessible, it does
not make it editable. Modification of a protected data member
requires a mutator function.
• Because they provide direct access to protected data, mutator and
accessor functions must be written and used carefully.
Friend function/class
• One of the important concepts of OOP is data hiding, i.e., a
nonmember function cannot access an object's private or protected
data.

• But, sometimes this restriction may force programmer to write long


and complex codes. So, there is mechanism built in C++ programming
to access private or protected data from non-member functions.

• This is done using a friend function or/and a friend class.


Friend function/class
If a function is defined as a friend function then, the private and
protected data of a class can be accessed using the function.

The complier knows a given function is a friend function by the use of


the keyword friend.

For accessing the data, the declaration of a friend function should be


made inside the body of the class (can be anywhere inside class either
in private or public section) starting with keyword friend.
Declaration of friend function in C++
class class_name
{
... .. ...
friend return_type function_name(argument/s);
... .. ...
}
return_type functionName(argument/s)
{
... .. ...
// Private and protected data of className can be accessed from
// this function because it is a friend function of class.
... .. ...
}
Define a non-member function changeWT(int,int) to increase/decrease
weight data members of a object by given values

W1 200Kg 375gm
changeWT(12,50)
new W1 = 212Kg 425gm
class weight
{ Accessor Function
int kilogram; An accessor function in C++ and
int gram; the mutator function are like the
public: set and get functions . They are
used instead of making a
void getdata(); class member variable public and
void putdata(); changing it directly within an
/*int getKG(){ return kilogram;} object. To access a private object
int getGM() { return gram;}*/ member, an accessor function
must be called.
friend void modifyWT(weight,int,int);
};
void weight :: getdata()
{
cout<<"/nKilograms:";
cin>>kilogram;
cout<<"Grams:";
cin>>gram;
}
void weight :: putdata ()
{
cout<<kilogram<<" Kgs. and"<<gram<<" gros.\n";
}

void modifyWT(weight w, int k, int g)


{
w.kilogram+=k;
w.gram+=g;
w.kilogram = w.kilogram + w.gram/1000;
w.gram%=1000;
w.putdata();
}
void main()
{
weight w1;
w1.getdata();
modifyWT(w1,20,10);
getch();
}
• A class can have many friends!
• a function can be friend of many classes!
• A friend can be a
• function
• function template
• member function
• a class
• class template, in which case the entire class and all of its
members are friends.
Friend Functions and Friend Classes C++
Friend Functions and Friend Classes C++ allows you to declare another class
to be a “friend” of the current class to make it easier to access variables.
OOP purists have criticized this feature as weakening the principles of
encapsulation and information hiding.
You can do everything you might want to do without using the “friend”
feature.
For example, Java is not friendly. There is no friend feature.
However, this feature can make some code easier to write (at least in the
short term; arguably not in the long term). You may also see other code that
uses friends, so it is good to know what it means.
member function of class B as friendof class A
class A
{
private:
int a,b;
friend void class B::f1(int,int);
};
class B
{
..
..
void f1(int,int) // only this function can access all members of class A

};
Friend class
class A
{
private:
int a,b;
friend class B;
};
class B //all functions of class B can access members of class A
{

void somefunction(){
cout<<A::a<<A::b;
};
const member function
• If a member function does not alter any data of a class then we can
declare it as const

void getWeight(int,int) const;

You might also like