* New post Constructor Destructor and Inheritance& Array , Virtual base class in c++ language

Tuesday, 25 March 2014

0 Classes and Objects c++ language

Classes and Objects 
Classes : A class is an expanded concepts of the structure but a difference is that structure is a group of data member only while class is group of data members and member functions.
Declaration of class 
   The classes are declared with a keyword class. The general form of the class declaration is :
   Class class _ name
{
              Access_ specifier 1 : variable declaration ; member declaration ;
              Access _ specifier 2 : variable declaration ; member function declaration ;
              Access _ specifier 3 : variable declaration ; member function declaration ;
          };
        Ex :
Class A
{
     Private : int x;
                            void square ( );
               Public : int y;
                             void cube ( );
             Protected : int z ;
                            void add (int z) 
}; 
 The definition of a class is enclosed within a pair of curly braces and  terminates with a semicolon. Any class can contain 0 or more data members . And all the functions declared in the class are called member function . In c++  A class has three access specifiers .


  • Public : The data members and member functions declared in the public section can be accessed from outside world or from the main function. 
  • Private : The members that are declared in the private section cannot be access outside the class. The keyword private members can be used with in the class . The keyword private is optional , therefore when we declare the data members or member function without any access specifier then these member consider as private members. If we want to use private members outside the class then these can be accessed with the help of public function .
  • Protected : The protected specifier is mainly used in inheritance , if we want to make the data private and interitable in other class then we use protected  specifier. The data members declared in protected section are private for the outside world but they can be accessed into the other class.


Creating Objects

Once a class has been declared we can create any number of objects . These objects are declared in terms of variables of the type class. These variables are called objects. The syntax of object declaration is : class_name  object _ name ;

  Ex : Area A1 , A2 , A3 -------------- An; 

Accessing Class members 
The private data cannot be invoked in to the main ( ). If we want to used private data then firstly we access private data in public function of the class then we call this public function in the main ( ) function . All the public data member and member function are invoked in the main ( ) with helps of objects and dont operator . A member function can be invoked in the main by the following syntax : A1. input  ( 10 , 15 )  , A1. area( ) ;  
Ex : 
# include < iostream . h>
# Include <  conio .h >                           
                                                                            www.programmingking.in
{
                    int h , w ;
            Public :
                        void input ( int x , int y)
                      {
                               h = x ;
                               w = y ;
                       }
                          void area ( )
                       { 
                         cout << " area = " << ( h * w ) /2 ;
                       }
             };
                   void main ( )
                 {
                       Area A1;
                       clrscr ( ) ;
                       A1. input ( 25 , 42 ) ;
                       A1 . area ( ) ;
                       getch ( );
                 }


                       Defining Member function

When a function is defined inside the class, then it is called inline funct
ion . In the previous example the function are defined inside the class. 
Defining member function outside 
Defining the member function outside the class and the use of the scope resolution operator (::). When the member function are defined outside the class , then it is called outside definition of member function . A member function can be defined outside the class by using the function return type , function name , class name and the scope resolution operator . A function can be defined outside the class as :
 return type class name :: function name (arguments)
{
               ------------------
              -------------------    // Function code
               -------------------
 Ex
Void area : : input ( int x, int y)
 {
                h = x;
                w = y ;
            }
                             or 
              void area : : area ( )
         {
                       cout << " area = "<< ( h * w)/ 2;
         }
 * //Write a program to input the data of a passenger and then display  the data *?
# include<iostream.h>
# include < conio.h>
class Passenger
{
                  char N[20] ;
                  int age ;
                  float h;
           public :
                    void input ( ) 
                    void output ( ) 
          };
    void passenger : : input ( ) 
{
                 cout << " enter name " ;
                 cin >> N;
                 cout << " enter age =";
                 cin >> age ;
                 cout <<"enter height = ";
                 cin >> h;
}
Void passenger  : : output ( )
{
            cout << " name = "<<N<< end l ;
            cout << "age = "<<age<< end l ;
            cout << " height = "<<H<< end l ;
}
void main ( )
{
            Passenger p1;
            clrscr ( );
            P1 . intput ( )
            cout << " records of passenger \n" ;
            p1 . output ( );
            getch ( );
* Program to perform arithmetic operation using class *>
           # include <iostream .h>
              class Arithmetic 
            {
                  int a , b , A , M , D , R ,S = 0;
                Public :
                           void input ( )
                           { 
                               cout << " enter two number \n" ;
                               cin >> a >>b;
                           }
                           void calculate ( )
                           {
                             A = a + b;
                             M = a * b;
                             D = a/b; 
                             R = a % b ;
                             S = a -b;
                            }
                            Void display ( )
                            {
                               cout << "sum =" << A << end l ;
                               cout << " difference =" << s << end l;
                               cout << "Multiply =" << M << end l ;
                               cout << " Quotient =" << D<< end l;
                               cout <<" modulus = "<< R << end l ;
                   }
         }; 
                                void main ( )
                              {
                                  Arithmetic A1 ;
                                  clrscr ( ) ;
                                 A1. input ( );
                                 A1.calculate ( );
                                 A1. display ( );
                                 getch ( );
                              } 
Inline Function : when a function is defined inside the class , it is treated as inline function . Normally functions are defined inside the class . when we define a member function outside the class and make it inline by just using inline keyword in the function definition .
When we declare a function as inline the compiler replaces the function call with the function code , and the compiler does not jump from function declaration to function definition . The inline function executes very fastly . The general  form of function declaration is :
    inline return type class name : : function _ name (argument)
             {
                        function body ;
             } 
 * Program to calculate square of a number* ?
                  Class square 
                  { 
                       int A ;
                      Public :
                               void getdata ( );
                               void sqre( );
                  };
              inline void square : : getdata ( )
 {
             cout << " enter a number =" ;
             cin >>A;
          }
           inline void square : : sqre ( )
          {
                 cout << "square  = "<< A * A;
          } 
  Void main ( )
           {
                square s1;
                clrscr ( );
                s1.getdata ( );
                s1.sqre ( );
                getch  ( );
       } 


Nesting of  member function : When a function called by using it;s name inside another member function of the same class . This is known as nesting of member function.
   Class set
{
                int m ,n ;
            Public :
                       void input ( ) ;
                      Int Largest ( ) ;
                      void display ( ) ;
};
             Void set : : input ( )
{
              cout << "Enter two numbers = ";
              cin >> m >>n ;
}
     int Set : : Largest ( )
{
                 if m > n                          // www.programmingking.in
                        return m;
                 else
                        return n;
}
     Void set : : display ( )
{
    cout <<"largest value is ="<<largest ( ) ;
}
Void main ( )
{
  Set s1;
            clrscr ( );
           s1.input ( );
           s1. display ( );
getch ( );
  Passing object to function or Objects as function Arguments
 Like the other data type , the objects can be pass to a function as function arguments . An object as a function arguments can be used in two ways:
  1. A copy of entire objects is passed to the function.
  2. Only the address of the object is transfered to the function.
The first method is called by value . Since a copy of object is passed to the function . The change made to the object inside the function do not affect the original object used to call to the function do not affect the original object used to call to the function .
The second method is called pass by reference or call by reference , when the address of any objects passed , then the called function works directly on actual object used in the call . 
Ex :
          # include <iostream .h>
# include <conio .h>
Class time
{
             int hour;
             int minute;
          Public :
                     Void get time (int h , int m)
        {
                         hour = h ;
                         minutes = m ; 
        }
                      Void put time ( )
      {
                      cout <<hour<<"hour and << minutes << "minutes"<<end l ;
      }
                    Void sum ( time t1 , time t2)
      {
                  minutes = t1.minutes + t2.minutes
                  hour = minutes / 60 ;
                  minutes = minutes % 60;
                  hour = hours + t1 . hours + t2. hours
       };
        Void main ( )
       {
                     time T1 ,T2 ,T3 ;
                       Clrscr ( ) ;
                     T1 . get time ( 6 , 45 );
                     T2 . get time (7 , 40 );
                     T3. sum ( T1 , T2) ;
                    cout <<"Time for object T ,\n";
                    T1. put time ( ) ;
                   cout << " time for object T2 \n";
                   T2. Put time ( );
                    Cout << "time for object T3 \n';
                    getch ( ) ;
     }
 
  

 


0 comments:

Post a Comment

 

PROGRAMMINGqueen Copyright © 2011 - |- Template created by O Pregador - |- Powered by Blogger Templates

Blogger Widgets