Search This Blog

If you like any posts in this blog then click this icon which is present under the post

Wednesday 12 October 2011

Adding Variables and Methods


Adding variables
                 To a variable in a class it must be defined inside the class. At once a variable is added to a class it becomes instance variable.
Example
                class Student
                {
                                int reg_no;
                                 int mark_1;
                                int mark_2;
                }
Where
                Student                                                -              class name
                reg_no,mark_1,mark_  -              variables
                No memory space will be allocated to the above class. Because class definitions create only the template.

Adding Methods

                Methods are used to access the data fields in a class. To add a method it must be defined after the data field definition. At once a method is added to a class it becomes an instance method. The general form is,
Datatype method_name(parameter list)
{
        Body of the method
}

Where
                Datatype             -              available data types such as int, float etc.
                method_name -              user defined name
                parameter list    -              list of variables separated by comma.
Rules
1. Data type represents the type of the data returned by the method.
2. The value of the parameter list should be supplied.
3. The parameter list may be empty.

No comments:

Post a Comment

Followers