Home | | Computer Science 11th std | C++: Referencing class members

Example Programs in C++ - C++: Referencing class members | 11th Computer Science : Chapter 14 : Classes and objects

Chapter: 11th Computer Science : Chapter 14 : Classes and objects

C++: 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.

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


 

Illustration 14.4 C++ program to illustrate the communication of object:

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;

}

Output:

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

 

Tags : Example Programs in C++ , 11th Computer Science : Chapter 14 : Classes and objects
Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
11th Computer Science : Chapter 14 : Classes and objects : C++: Referencing class members | Example Programs in C++

Related Topics

11th Computer Science : Chapter 14 : Classes and objects


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2024 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.