Creating and using member functions - C++ Class

C++ examples for Class:Member Function

Description

Creating and using member functions

Demo Code

#include <cstdlib> 
 #include <iostream> 

 using namespace std; 

 class MyClass //www  .  ja  v a 2  s  .  c o  m
 { 
 public : 
     void SetI(int iValue); 
     int GetI(); 

     void SetF(float fValue); 
     float GetF(); 

 private: 
     int i; 
     float f; 
 }; 


 void MyClass ::SetI(int iValue) 
 { 
     i = iValue; 
 } 


 int MyClass ::GetI() 
 { 
     return (i); 
 } 


 void MyClass ::SetF(float fValue) 
 { 
     f = fValue; 
 } 


 float MyClass ::GetF() 
 { 
      return (f); 
 } 


 int main(int argc, char *argv []) 
 { 
     MyClass anObject, anotherObject; 

     anObject .SetI(10); 
     anObject .SetF(3.14159); 

     anotherObject .SetI(-10); 
     anotherObject .SetF(0.123456); 

     cout << "anObject .i = " << anObject .GetI() << endl; 
     cout << "anObject .f = " << anObject .GetF() << endl; 

     cout << "anotherObject .i = " << anotherObject .GetI() << endl; 
     cout << "anotherObject .f = " << anotherObject .GetF() << endl; 

   
     return (0); 
}

Result


Related Tutorials