C++ இல் உள்ள எடுத்துக்காட்டு நிரல்கள் - C++: இனக்குழு உறுப்புகளை அணுகுதல் | 11th Computer Science : Chapter 14 : Classes and objects
இனக்குழு உறுப்புகளை அணுகுதல்
இனக்குழுவின் - உறுப்புகளை அணுகுவதற்கு புள்ளி (. DOT operator) செயற்குறியானது பயன்படுகிறது. இனக்குழு பொருளின் பெயரைத் தொடர்ந்து புள்ளி செயற்குறியும், அதைத் தொடர்ந்து அணுக வேண்டிய தரவு உறுப்பு / உறுப்பு செயற்கூறின் பெயரைத் தர வேண்டும். இனக்குழுவின் உறுப்புகளை அணுகப் பயன்படும் புள்ளி செயற்குறியின் தொடரியல்:
பொருளின் பெயர். உறுப்பு செயற்கூறின் பெயர் (மெய்யான அளபுருக்கள்);
(எடுத்துக்காட்டு)
விளக்க நிரல் 14.4 பொருளைத் தொடர்புகொள்வதை விளக்கும் நிரல்
பொருளின் உறுப்புச் செயற்கூறை அழைத்தல் என்பது பொருளுக்கு செய்தியை அனுப்புவது அல்லது தொடர்பு கொள்ளுதல் ஆகும்.
#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
குறிப்பு
இனக்குழு உறுப்புகளை (பொருள்களை) வரிசையாகக் கொண்ட அணிகள் பொருள்களின் அணிகள் எனப்படும். மற்ற வகையான அணிகளை அறிவித்து வரையறுப்பது போன்றே பொருள்களின் அணிகளையும் உருவாக்கலாம்.
(எ.கா) student s [10] ;
10 இனக்குழு பொருள்கள் உருவாக்கப்படுகிறது