Professional Documents
Culture Documents
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();
}
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++.
}
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.
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";
}
};
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