Friday, 9 September 2016

REFERENCING CLASS MEMBERS


You can't use the member functions of the class directly outside the class. To make use of a Class Specified, we create Objects of type Class.

Syntax: class_name obj1, obj2,...; 
Example: Student s1, s2;
This will create two objects s1, s2 of Student Class type.This will become more clear with the help  of an example.


Example: 

class student
{
        private:
                     int roll_no;
                     char name[20];

        //Private members are only accessible through the member functions of that class

          public: 
                        void take_data()          //For taking values
                        {
                           cin>>roll_no;
                           gets(name);
                        }
                       
                        void show()          //For showing values
                        {
                           cin<<roll_no;
                           cout<<"\n"<<name;
                        }
} ;
student s1, s2;   // This will create objects s1, s2 of Student class type. We can use them anywhere in the program according to our needs.

Only the public Members are called by non-member functions using the Objects.
Syntax: object_name.function_name(actual arguments);

Now to call the function 'take_data' from class student, statement will be:
     s1.take_data();
or s2.take_data();

and for function 'show':  s1.show(); or s2.show();

For more Help: 

No comments:

Post a Comment