Wednesday, 7 September 2016

CLASS DEFINITION 


Member functions of a class can be defined in two place:
1. Inside the class Definition
2. Outside the class Definition


Inside the Class Definition

Just like we define functions, Same goes goes in case of defining the Classes i.e. you can define them before without giving the Prototype. Just start the Class Definition just like You stats a Function Definition.

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

public:
                  void accept()
                  {
                    cout<<"Enter Roll No: "; cin>>roll_no;
                    cout<<"Enter Name: ";  gets(name);      //For accepting the char string
                   }

                  void show()
                  {
                    cout<<"\nDetails:\n";
                    cout<<"Roll No: "<<roll_no;
                    cout<<"\nName: "<<name;
                  }
}


Outside the Class Definition

Defining the function Sometimes becomes Bulky when your Programs become Complicated or when it is too large. So Many Programmers gives the Class Definition outside the Class.

Syntax:  return-type class_name::function_name(parameter list)
              {
              }
:: is a Scope resolution operator which specifies the  scope of the Function which is inside the Class.

Example: 
                   void Student::show()
                   {
                     cout<<"\nDetails:\n";
                     cout<<"Roll No: "<<roll_no;
                     cout<<"\nName: "<<name;
                    }
where 'Student' is our Class and 'show' is the function with Return type 'void'.

To Know more about Classes and their Definitions: 


No comments:

Post a Comment