Monday 16 December 2013

Oops with c++

Functions:

Functions are generally the building blocks of a program.The starting point to any function is the main.A normal function can have three parts namely
  • Declaration section
  • Definition section 
  • Calling section
General syntax:
   <data_type><function_name><arguments>
Example:
  void fun() 
 Declaration section:

            Declaration section of a function is optional.(i.e)The user can declare the function in a class and define it in outside the class, or the user can directly define the function inside the class itself.

Example:
class sample
           {
                   private:
                        int num;
                   public:
                    /* Declaration part*/
                           void getdata();
                           void showdata();
           } 
Definition section:

A function can be defined either inside or outside the class.when a function declared inside a class, it must defined using a scope resolution operator.This operator reveals the scope of a variable or a function.

Example 1:

class sample
          {
                   private:
                           int age;
                   public:
                          void getdata()
                          {
                                cout<<"enter age":
                                cin>>data;
                            }
                            void showdata()
                             {
                                    cout<<"AGE:"<<age;
                              }
               };
Example 2:
                class sample
                {
                        private:
                                   int age;
                        public:
                               /*Declaring the function*/
                                void getdata();
                                void showdata();
                 };
                 /*Defining the function outside the class*/
                void sample::getdata()
                {
                      cout<<"enter age";
                      cin>>age;
                 }
                void sample::showdata()
                {
                    cout<<"age:"<<age;
                 }        
Calling functions:
When the function declared and defined,it must be called in main function,otherwise the function will not be executed.
Example:
 class sample
                {
                        private:
                                   int age;
                        public:
                               /*Declaring the function*/
                                void getdata();
                                void showdata();
                 };
                 /*Defining the function outside the class*/
                void sample::getdata()
                {
                      cout<<"enter age";
                      cin>>age;
                 }
                void sample::showdata()
                {
                    cout<<"age:"<<age;
                 }        
                           void main()
                           {
                                  /*object declaration*/
                                  sample s;
                                  /*calling function*/
                                   s.getdata();
                                   s.showdata();
                            }
Various types of functions:
  • Passing nothing and returning nothing 
  •  Passing parameters and returning nothing
  • Passing parameters and returning something
Parameters are something that is given inside the brackets.parameters are the channel through which data flows  from call statement to function and vice versa.
Passing nothing and returning nothing:
      
         #include<iostream.h>
         #include<conio.h>
          class sample
           {
                        private:
                                   int age;
                        public:
                               /*Declaring the function*/
                                void getdata();
                                void showdata();
      };

      /*Defining the function outside the class*/
       void sample::getdata()
       {
                      cout<<"enter age";
                      cin>>age;
        }
         void sample::showdata()
         {
                    cout<<"age:"<<age;
          }        
                   void main()
                   {
                        /*object declaration*/
                          sample s;
                          /*calling function*/
                           s.getdata();
                            s.showdata();
             } 
Passing parameters and returning nothing:
      
#include<iostream.h>
#include<conio.h>
void sum(int x,int y) //Passing parameters
{
        int z;
        z=x+y;
        cout<"Addition is"<<z;
}
void main()
{
    int a,b;
    cout<<"enter two numbers";
    cin>>a,b;
    sum(a,b); //calling functions
}

Sample output:
enter two numbers
2 4
Addition is 6

Passing parameters and returning something:

#include<iostream.h>
#include<conio.h>
int sum(int a, int b) //passing parameters
{
     int c=a+b;
     return c;
}
void main()
{
    int a,b,c;
    int sum(int,int);
    cout<<"Enter two numbers";
    cin>>a>>b;
    c=sum(a,b); //calling function
    cout<<C;
}

Sample output:
Enter two numbers
4 6
10 


No comments:

Post a Comment