Referencing
class members
The
members of a class are referenced (accessed) by using the object of the class
followed by the dot (membership) operator and the name of the member.
The
general syntax for calling the member function is:
Object_name .
function_name(actual parameter);
For
example consider the following illustration
Calling
a member function of an object is also known as sending message to object
or communication with
the object.
#include<iostream>
using namespace std;
class compute
{
int n1,n2; //private by default
public :
int n;
int add (int a, int
b) //inline member function
{
int c=a+b; //int c ;
local variable for this function
return c;
}
int prd (int a, int b) //inline member function
{
int c=a*b;
return c;
}
}; // end of class
specification
compute c1,c2; //global
object
int main()
{
c1.n
=c1.add(12,15); //member function is
called
c2.n =c2.add(8,4);
cout<<"\n
Sum of object-1 "<<c1.n;
cout<<"\n
Sum of object-2 "<<c2.n;
cout<<"\n
Sum of the two objects are "<<c1.n+c2.n;
c1.n=c1.prd(5,4);
c2.n=c2.prd(2,5);
cout<<"\n
Product of object-1 "<<c1.n;
cout<<"\n
Product of object-2 "<<c2.n;
cout<<"\n
Product of the two objects are "<<c1.n*c2.n;
return 0;
}
Sum of object-1 27
Sum of object-2 12
Sum of the two objects are 39
Product of object-1 20
Product of object-2 10
Product of the two objects are 200
Related Topics
Privacy Policy, Terms and Conditions, DMCA Policy and Compliant
Copyright © 2018-2024 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.