CPP_lessons

Document Sample
CPP_lessons Powered By Docstoc
					                                                 ‫ا‬         ‫ا ا‬

                                            ‫ف ‪class‬‬                ‫ا‬

     ‫دوال‬             ‫ﻡ ﻡ‬         ‫دة وﻡ ﺕ‬        ‫اع‬    ‫ا‬         ‫ات , ﻡ‬               ‫ﻡ ا‬            ‫ا‬
                                                                                                     ‫فه ﻡ‬
                                                                                                   ‫ﻡ ا‬
                                                 ‫فإ ء عﺝ ی .‬                           ‫ﺱ لا‬       ‫•‬
     ‫وﺹ ت ‪ atribute‬و‬                             ‫اﺥ ا ع ه آ ﺉ ی‬                         ‫أو‬   ‫• ا عه‬
                                                                                       ‫رات .‬   ‫وﻡ‬
                       ‫ﺵــــــ ﻡ .‬          ‫ات‬         ‫اﺥ ا وال + ا‬                   ‫‪class‬‬  ‫• ی يا‬

                                                                              ‫:‪- : encapsulation‬‬                  ‫ا‬

                  ‫.‬     ‫آ ﺉ وا‬             ‫تآ نﻡ‬             ‫ﻡ ت و رات وﻡ‬                   ‫ﺕ ی آ ا‬         ‫رة‬

                                                                                  ‫:‬         ‫ءا‬        ‫‪: Clients‬‬


                      ‫‪encapsulation‬‬                    ‫ا‬     ‫ی‬                    ‫ا‬         ‫ه ﺹ ف أو دوال ﺕ‬

‫رة‬    ‫دة ا‬                   ‫)ﻡ‬              ‫آ‬        ‫أو ﻡ‬      ‫دون ﻡ‬
                                                                 ‫ﺕ ﺹ‬       ‫ﺱ‬        ‫ءا‬
                                                                  ‫.(‬           ‫دون ا ه م‬
 ‫ت‬        ‫ء أو‬        ‫ات أ‬        ‫ءﻡ‬        ‫أ أ‬         ‫ات ا ا ة داﺥ ا‬         ‫• ی ر‬
                                                            ‫ء ‪. member data‬‬       ‫أ‬
 ‫ء ‪member‬‬                    ‫ا وال ا‬         ‫ء وﺕ‬    ‫ات ا‬     ‫ا‬      ‫ا‬   ‫ا وال‬   ‫• ﺕ‬
                                                  ‫‪. methods‬‬      ‫ا‬     ‫‪ function‬أو‬

                                                                              ‫:‬         ‫ا‬        ‫ن‬     ‫ا‬    ‫آ‬

                                           ‫‪Class class name‬‬

                                             ‫{‬

                                       ‫ء‬         ‫ات + ا وال ا‬             ‫ا‬

                                                  ‫;}‬
             ‫تا‬       ‫‪ class‬وﻡ ا‬             ‫فﻡ ه ا‬             ‫ا‬         ‫ذاآ ة إ ی‬      ‫ی‬                 ‫نا‬    ‫إ‬
                                                              ‫وری‬          ‫وﻡ ي ا اآ ة ا‬               ‫و‬         ‫ی‬

      ‫ﺝ‬           ‫ی ﻡ ا‬           ‫–‪- www.cb4a.com‬‬                 ‫ا ﺱ ا‬              ‫ه اا بﻡ ﻡ آ‬  ‫ﺕ ﺕ‬
                                                                                  ‫ا ی رﺕ .‬ ‫ﻡ تا ﺱ ،ﺕ‬

                                                       ‫1‬
                                                                : object               ‫ا‬

                                  ‫ا‬   ‫ی‬    ‫ی فا ﺉ ﻡ ا عا ی آ‬
                                              class‫أل‬ ‫إذا آ ن‬

                  Class employee
                           {
                      int age ;
                   string name ;
                     salary( );
                          };
                   :      ‫ آ‬employee ‫آ ﺉ ﻡ ا ع‬                 ‫ﺕ ی‬
                  Employee A ;

                                  employee ‫ ﻡ ا ع‬a               ‫آﺉ‬

‫ء‬    ‫ء وا وال ا‬    ‫ات ا‬       ‫ا‬       ‫ﺹ لإ‬    ‫ا ﺉ‬          ‫ی‬
                                                                               .           ‫ا‬

                                              :        ‫ام ﻡ ﺙ ).( آ‬        ‫ﺱ‬           ‫وذ‬

       A. age = 22;

      A. name = " ali"

                                                       : ‫ا ص اذاء ا م‬

                                                  ‫نا‬           ‫آ ت أﺱ ﺱ‬

                          Public
                          private

private ‫ات + دوال ( ﺕ ن ﺥ ﺹ‬           ‫)ﻡ‬     ‫ءا‬      ‫آ أ‬
                                                  public ‫ﻡ‬            ‫وﺽ‬           ‫ی‬
                      Class employee
                              {
                          public:
                          int age;
                       string name;
                         salary( );
                             };

                                  2
     ‫أو ا ﺹ ل إ‬       ‫ﻡ‬     ‫ﻡ ﻡ ی‬   alary ( ) ‫ و‬name ‫ و‬age ‫ن‬    ‫أﺹ‬
                                                ‫ی ا ﺉ ت دون أﺥ ء‬

                           constructor& destructor : ‫م‬    ‫دوال ا ء وا‬


  ‫ف دا‬     ‫ا‬    ‫ﺕ‬   ‫ء ا‬       ‫ء وا وال ا‬      ‫تا‬      ‫ا‬   ‫ﺕ‬
‫ا ﺝ و‬          ‫وری‬  ‫اﺥ وﺱ ﺉ ا‬      ‫وه درة‬         ‫ ا‬constructor      ‫ﺕ‬
                             void        ‫و‬      ‫إرﺝ ع أی‬    ‫درة‬
                            : ‫ ﻡ‬class            ‫ﺕ ﺥ اﺱ ا‬      ‫وا ا ا‬
                       Employee ( ).
‫ﺕ م ﺥ ء ا اآ ة ا‬     destructor ‫ه دﻡ‬       ‫ن‬    ‫ی ا‬              ‫آ أ‬
           .   ‫ﺕ ﺥ أی وﺱ ﺉ و ﺕ ﺝ أی‬         ‫) ~ ( وه أی‬      ‫ﺕ وﺕ‬

                          ~ employee( )

                                                          :‫ﻡ ل‬

                    #include <iostream.h>
                        class employee
                                 {
                             public:
               employ(int age ); \\ constructor
                         ~employee( );
                          int getage( );
                            private :
                           int itsage ;
                 \\ constructor of employee
                    employee : : (int age);
                                 {
                         its age = age ;
                                 }

          employee : : ~ employee( ) \\ destructor
                int employee : : getage ()
                            {
                      return itsage ;

                                }


                                                    :‫ا ﻥ ت‬       ‫طﺕ‬
                                3
        ‫ن ی آ ﺉ آ ﻡ وﺝ ه‬                            ‫ا‬         ‫ء‬   ‫ا‬        ‫ء ا ﺉ ت أﻡ‬     ‫ا‬   ‫ﺕ‬
                                                                                       ‫ﺱ ل .‬
                                                ‫:‬       ‫اﺙ‬                ‫آﺉ ی‬    ‫ل‬    ‫ا‬    ‫ی‬
                                                                          ‫‪age‬‬    ‫• ﺕﺥ ا و ا‬
                                  ‫ﺕ ﺥ أي ﺵ ء .‬                        ‫ا ﺱ ‪ name‬أﻡ ا‬      ‫• وا‬
                           ‫ا دﻡ ت .‬    ‫ﺕ‬                               ‫ت و‬     ‫ا‬   ‫طﺕ‬

                                                                                       ‫:‬         ‫ﺏﻥ ا‬

‫ر‬         ‫ﻡ‬     ‫ا ﺉ‬                             ‫آ ﻡ ةﺕ‬    ‫ﺱ ا اﺽ ﺕ‬                                ‫ف‬          ‫ی ما‬
        ‫ه اا ﺉ .‬                     ‫ف‬          ‫ا‬   ‫ﻡ ﺝ ی‬   ‫ا ا أو ﺥ رﺝ آ‬                        ‫إ‬            ‫ا ﺉ‬

‫ن‬         ‫ﺙ‬       ‫ﺝ‬            ‫وی‬          ‫ا‬        ‫ﺉ ﻡ‬        ‫راﻡ ا وا ا ﻡ ﺝ‬                    ‫تا‬          ‫ﺕﺥ آ‬
                                                             ‫ر ﻡ :‬    ‫ی ا ﺉ ا‬                    ‫ﺕ ج‬            ‫ا‬

                      ‫;)‪Employee (const employee& the employee‬‬
                       ‫ﺉ ‪ employee‬اﺥ ي.‬       ‫‪ employee‬ﻡ ﺝ ﺙ‬                                           ‫ه أﺥ ت ا‬
                                ‫ﻡ ‪. the employee‬‬      ‫ه إ اث‬                                         ‫ا‬     ‫وه ف‬



                                                              ‫ف ‪ class‬ه إ ا ع ﺝ ی ة‬                      ‫ا‬

                      ‫ة ﺹ ف أﺥ ي .‬               ‫ات ﻡ‬        ‫ء وه ﻡ‬            ‫تأ‬            ‫ا‬          ‫ی‬

              ‫ﻡ ﻡ‬                   ‫‪ methods‬وﺕ‬                    ‫ءﺕ‬    ‫دوال أ‬               ‫ا‬          ‫ی‬
                                                             ‫ء ‪. meber data‬‬                  ‫تا‬         ‫ا‬

                               ‫‪public or private‬‬              ‫ﻡ او ﺥ ﺹ‬                ‫تا‬      ‫ﺕ ن‬

    ‫ء‬         ‫وال ا‬    ‫ة‬        ‫ﻡ‬          ‫ﻡ ,ﻡ ا ﺹ‬               ‫يﺝ ءﻡ ا‬             ‫ا ﻡ‬        ‫ﺕ‬

                                                                        ‫.‬       ‫ا ا‬         ‫داﺥ ا‬
        ‫آ ﺉ ت أﺥ ي‬                   ‫ﺕ‬         ‫ءﺹ فﻡ‬                  ‫تا‬                   ‫طا‬           ‫ی‬

              ‫.‬            ‫ا‬             ‫وا ة ﺕ م‬            ‫فی‬       ‫نا‬                         ‫ﺕ‬     ‫إذا‬



                                          ‫ات ‪pointer‬‬                       ‫ا‬
                                                         ‫4‬
                                           variable
.




          0101010001101100010100111



                                           :

                       : (&) address of
                  Int main
                      {

                 Int age = 5
    Cout << "address of age:\t<<&age<<"\n;
                  Return 0
                       }

                                       :



                                                   .

                Int age = 30 ;
                 Int *p = 0

           P = &age;

                                               :

     :                           (*)
                                                       –1
                         5
                                                       –2

   *
        .

                         : ‫ات‬       ‫لا‬        ‫اﺱ‬       ‫دوا‬

                               :

free store                                             –1

    class                                              –2

                     .                                 –3

                                    ‫نا‬       ‫س وا‬        ‫ا‬
            :

                                                       –1

                                                       –2

                                   register            –3

                                                       –4

                                                       –5

    .
            ,                                      ,

                . free store



                                         :

 new
  .
                 6
delete
                           .

                                         : ‫ﺕ ــــ ب ا اآ ة‬



          Int *p = new short int ;
                 *p = 72 ;
            p = new short int ;
                  *p = 84


            :

             int *p = new int;

                    p = 72;
                          ;

                delete p ;

                p = new int;


                    *p = 84;


         . delete                  new




         references            ‫ا‬     ‫ا‬
                                                       *
                       7
:                    &


Int &rage = someint;

                          .   int                            rage

                         #include<iostream.h>

                                int main ( )
                                     {

                                    int age ;

                              int &rage = age ;

                                age = 10 ;
                      cout << " age"<< age<<endl;
                  cout << " rage"<<r age<<endl;


         rage = 12;

                      cout << " age"<< age<<endl;
                  cout << " rage"<<r age<<endl;

        return 0;
                }
                                             :      ‫ا‬    ‫ان ﻡ ا‬      ‫ا‬          ‫ﻡ‬
    : ‫ه اف‬   ‫رة‬      ‫اﺝ ه أﺱ ء ﻡ‬        ‫نا‬       ‫ وذ‬target   ‫ﺝ ه ه‬       ‫ان ا‬


                         #include<iostream.h>

                                int main ( )
                                     {

                                       8
                                           ‫; ‪int age‬‬

                                     ‫; ‪int &rage = age‬‬

                                      ‫; 01 = ‪age‬‬
                            ‫;‪cout << " age"<< age<<endl‬‬
                        ‫;‪cout << " rage"<<r age<<endl‬‬


                           ‫;‪cout <<"& age"<< &age<<endl‬‬
                       ‫;‪cout << "& rage"<<&r age<<endl‬‬

              ‫;0 ‪return‬‬
                      ‫}‬


                             ‫ﺙ‬        ‫ا ﺉ تا‬      ‫ذ‬           ‫أي آ ﺉ‬   ‫ا ﺝ عإ‬          ‫ی‬



  ‫ﺝ‬       ‫ﻡ‬        ‫و‬      ‫ﺵ ﻡ‬    ‫ا‬     ‫وا ﺱ د‬          ‫لو‬     ‫ﺱ‬        ‫اﺝ أﺱ‬    ‫أن ا‬       ‫*‬
     ‫ﺵ ات ﺕ‬            ‫اﺱ ل ا ﺵ ات ن ا‬                        ‫أآ ﻡ ﻡ ة و‬  ‫ﺱ دا‬
  ‫اﺝ أن‬                 ‫‪ null‬و ی‬    ‫أن ﺕ ن‬             ‫ﻡ و اآ ر أ ﺹ ا ﺱ ل وی‬
                                                           ‫.‬ ‫إ دة ﺕ‬   ‫و ی‬   ‫ﺕ ن‬



                                 ‫ء‬     ‫ا‬    ‫ﻡ ﺹ‬         ‫و ﺕ‬

‫و .‬   ‫ا‬        ‫ا‬




                                              ‫9‬

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:0
posted:7/28/2012
language:
pages:9