מונחה עצמים- תרגול 1 | C vs. C++

  • Upload
    ron

  • View
    219

  • Download
    0

Embed Size (px)

Citation preview

  • 7/30/2019 ++C vs. C | - 1

    1/50

    Object Oriented ProgrammingTirgul 1

    C and C++

  • 7/30/2019 ++C vs. C | - 1

    2/50

  • 7/30/2019 ++C vs. C | - 1

    3/50

    Difference between C and C++

    C++,,COOP.

    CC++,)(

    C)-C++(.

    C++g++)gcc.(-.h,.cpp

    '.

  • 7/30/2019 ++C vs. C | - 1

    4/50

    Difference between C and C++

    ,.

    C++class.classdata types.

    ,OOP),'(

    C++.

    .

  • 7/30/2019 ++C vs. C | - 1

    5/50

    Difference between C and C++

    C"/* */C++.

    C++//:.

    intadd(int a, int b){

    // This is a C++ commentint i = a + b;

    /* This is a C comment

    that works also in c++ */

    return i;

    }

  • 7/30/2019 ++C vs. C | - 1

    6/50

    C C++

    i.

    .

    ",.

    {int i;

    // Can access i

    for (i=0;i

  • 7/30/2019 ++C vs. C | - 1

    7/50

    :

    2.

    3?

    intfloat?

    Difference between C and C++

    Function overloading -

    int add(int a, int b){

    return a+b;

    {

    int add3(int a, int b, int c){

    return a+b+c;

    {

    int addIF(int a, float b){

    return a+b;

    {

  • 7/30/2019 ++C vs. C | - 1

    8/50

    :

    )(.

    .

    )addIF,int-float,"

    ".(

    !

    Difference between C and C++

    Function overloading -

  • 7/30/2019 ++C vs. C | - 1

    9/50

    .

    .

    C,.

    C++,.

    Difference between C and C++

    Function overloading -

  • 7/30/2019 ++C vs. C | - 1

    10/50

    C++"+.

    ""

    .'""

    "".

    Difference between C and C++

    Function overloading -

  • 7/30/2019 ++C vs. C | - 1

    11/50

    int add(int a, int b){

    return a+b;

    }

    float add(int a, int b){

    return a+b;

    }

    :

    :

    -type!

    ),

    (

    Difference between C and C++

    Function overloading -int add(int a, int b){

    return a+b;

    {int add(int a, int b, int c){

    return a+b+c;

    {int add(int a, float b){

    return a+b;

    {

  • 7/30/2019 ++C vs. C | - 1

    12/50

    -Overload Resolution

    .

    ".

    ".

    ,

    )implicit conversion(".

    "".

    Difference between C and C++

    Function overloading -

  • 7/30/2019 ++C vs. C | - 1

    13/50

    -Overload Resolution

    :

    Difference between C and C++

    Function overloading -

    int add(float a, int b);

    int add(int a, float b);

    int x,y;

    add(x,y);

    //error - more than one best viable function

    /* error message:

    call of overloaded `add(int&, int&)' is ambiguous

    candidates are: int add(float, int)

    int add(int, float) */

    add(x,(float)y);

    //OK - calls the 2nd function

    add((float)x,y);

    //OK - calls the 1st function

  • 7/30/2019 ++C vs. C | - 1

    14/50

    :

    C++.

    .

    .

    Difference between C and C++

    Function overloading -

  • 7/30/2019 ++C vs. C | - 1

    15/50

    :

    2.

    3.:

    ,2.

    Difference between C and C++

    intadd(int a, int b);

    intadd(int a, int b);

    intadd(int a, int b, int c);

  • 7/30/2019 ++C vs. C | - 1

    16/50

    :

    .

    ,2

    3. 2.32c0.

    function overloading.

    Difference between C and C++

    intadd(int a, int b, int c=0){

    return a+b+c;

    }

  • 7/30/2019 ++C vs. C | - 1

    17/50

    C++.

    )"-

    ).h(..(

    ).(

    ..

    Difference between C and C++

  • 7/30/2019 ++C vs. C | - 1

    18/50

    :

    ?

    Difference between C and C++

    intfunc (int a, int b, int c)

    {

    return a+b+c;

    }

    intfunc ()

    intfunc (int a)

    intfunc (int a, int b)

    intfunc (int a, int b, int c);

    intfunc (int a=10, int b = 20, int c=30);

    file.h

    file.cpp

  • 7/30/2019 ++C vs. C | - 1

    19/50

    :

    x: x=func(1,2,3)

    x=func(1,2)

    x=func(1)

    x=func()

    Difference between C and C++

    6

    33

    51

    60

    intfunc (int a, int b, int c)

    {

    return a+b+c;

    }

    intfunc (int a=10, int b = 20, int c=30);

    file.h

    file.cpp

  • 7/30/2019 ++C vs. C | - 1

    20/50

    C,int")0=,=.(

    C++-)bool.(2:\)true \ false.(

    :

    Difference between C and C++

    bool isRich (int money){

    bool rich = money > 200000;

    if (rich) //or if (rich==true)

    return true;

    else //if (!rich) //or if (rich==false)

    return false;

    {

  • 7/30/2019 ++C vs. C | - 1

    21/50

    2.

    .

    ,,.

    .int4bool

    .

    Difference between C and C++

  • 7/30/2019 ++C vs. C | - 1

    22/50

    Cmalloc)(free

    ".

    )",

    sizeof().(casting.

    C++new-delete.

    Difference between C and C++

  • 7/30/2019 ++C vs. C | - 1

    23/50

    C C++

    )

    .(

    casting.

    #include

    int *pi = (int*)malloc(sizeof(int));

    if(pi == NULL){

    ... /* out of memory */

    }

    *pi = 6;

    ...free(pi);

    Difference between C and C++

    int main() {

    int *pi = new int;*pi=6;

    delete pi;

    return 0;

    {

  • 7/30/2019 ++C vs. C | - 1

    24/50

    C)malloc(.

    .

    ...

    casting.

    Difference between C and C++

    -

    int arrSize = 10;

    int *pi = (int*)malloc(sizeof(int) * arrSize);

    if(pi == NULL){ ... /* out of memory */ }

    pi[3] = 6;

    ...

    free(pi);

  • 7/30/2019 ++C vs. C | - 1

    25/50

    ,C++,.

    ...)

    .(

    casting.

    Difference between C and C++

    -

    int main() {

    int arraySize = 10;int *pi = new int[arraySize];

    pi[7]=4;

    delete[] pi;

    return 0;

    {

  • 7/30/2019 ++C vs. C | - 1

    26/50

    :),(

    C++mallocfree.

    ,new\deletemalloc\free.

    newdeletenew[]delete[].

    delete-NULL.

    Difference between C and C++

    -

  • 7/30/2019 ++C vs. C | - 1

    27/50

    C)&(-indirection.(*)

    C++:-reference.-reference.

    :

    ,ji) .&jj.(

    ij.

    Difference between C and C++

    Reference

    int i=1;

    int& j=i;

  • 7/30/2019 ++C vs. C | - 1

    28/50

    :-i-j

    . .k,

    -i

    i.reference

    .!

    ,.

    Difference between C and C++

    Reference

    int i = 10 ;

    int& j = i;

    int k = i;i = 7;

    j++;

    j = k;

    j = 4;

    // i=j=7, k=10

    // i=j=8, k=10

    // i=j=10, k=10

    // i=j=4, k=10

  • 7/30/2019 ++C vs. C | - 1

    29/50

    -referencereferenceparameters.

    reference,.

    , ,,.

    reference parameters.

    Difference between C and C++

    Reference parameters

  • 7/30/2019 ++C vs. C | - 1

    30/50

    :

    isRich.

    "

    isRich(15000);,,int money=15000;.

    .

    .

    Difference between C and C++

    Reference parameters

    bool isRich (int money){

    bool rich = money > 20000;

    if (rich) //or if (rich==true)return true;

    else //if (!rich) //of if (rich==false)

    return false;

    }

  • 7/30/2019 ++C vs. C | - 1

    31/50

    :2.

    ?"swap (x,y);

    ,int& a=x;-int& b=y;.

    .reference-x-y

    .

    Difference between C and C++

    Reference parameters

    voidswap(int& a, int& b){int temp=a;

    a=b;

    b=temp;

    }

  • 7/30/2019 ++C vs. C | - 1

    32/50

    by reference by pointer

    .) .

    "(

    int*

    .

    Difference between C and C++

    reference

    voidswap(int& a, int& b){int temp=a;

    a=b;

    b=temp;

    }

    voidswap(int* a, int* b){

    int temp=*a;

    *a=*b;*b=temp;

    }

  • 7/30/2019 ++C vs. C | - 1

    33/50

    :

    reference

    .

    referencex,

    !

    Difference between C and C++

    Reference parameters

    int& add(int a, int b){

    int x = a+b;

    return x;}

  • 7/30/2019 ++C vs. C | - 1

    34/50

    reference

    .

    NULL.

    .

    .

    .

    NULL.

    .

    Difference between C and C++

    reference

  • 7/30/2019 ++C vs. C | - 1

    35/50

    ,C++OOP,

    ..

    ,C++',:

    istreamcin. ostreamcout.

    .

    "

    Difference between C and C++

    #include

    usingnamespace std;

  • 7/30/2019 ++C vs. C | - 1

    36/50

    istream.

    .

    -C.

    Difference between C and C++

  • 7/30/2019 ++C vs. C | - 1

    37/50

    :

    ,)(,.

    .?

    .

    Difference between C and C++

    char c;

    cin>>c;

    int n;

    cin>>n;

  • 7/30/2019 ++C vs. C | - 1

    38/50

    c; int n;cin>>n;

  • 7/30/2019 ++C vs. C | - 1

    39/50

    cout.

    >>

    .char

    char.

    intint

    .

    Difference between C and C++

    char c='a';

    cout

  • 7/30/2019 ++C vs. C | - 1

    40/50

    .:

    2.123

    321?x=123 y=321

    cin,cout.,,

    .

    Difference between C and C++

    int x,y;

    cin>>x>>y;

    cout

  • 7/30/2019 ++C vs. C | - 1

    41/50

    C C++

    Difference between C and C++

    int x,y;cin>>x>>y;

    cout

  • 7/30/2019 ++C vs. C | - 1

    42/50

    C++.C,"#DEFINE.

    ,,125"A."

    C++.

    const..-const!

    Difference between C and C++

    const

    constint ci=125;

    #define A 125

  • 7/30/2019 ++C vs. C | - 1

    43/50

    :

    Difference between C and C++

    const

    //OK. arr[3]=default=0

    //error: assignment of read-only variable `ci'

    //error: uninitialized const `cj'

    //OK

    //OK

    //error:

    //too many initializers for `const int[2]'

    const int ci=125; //initializationci=55;

    const int cj;

    const int intArray[3] = {3,12,135};

    const int intArray2[2] = {3,12,135};

    const int intArray3[4] = {3,12,135};

    int i=ci;

  • 7/30/2019 ++C vs. C | - 1

    44/50

    const.

    -cpiconst int,!

    ,cpi.

    .

    Difference between C and C++

    const -

    constint* cpi;

  • 7/30/2019 ++C vs. C | - 1

    45/50

    )"(,

    .

    ?

    ,,a,

    .

    Difference between C and C++

    const -

    intdontChange(constint* a);

  • 7/30/2019 ++C vs. C | - 1

    46/50

    :

    const int* pc;-pcconst.

    pcconst,.

    int* const cpcpconst.,

    ., cp.

    const int* const cpccpcconstconst)2.(

    cp.

    Difference between C and C++

    const -

  • 7/30/2019 ++C vs. C | - 1

    47/50

    ?

    Difference between C and C++

    const -

    constint ci = 25;constint* pc = &ci;

    *pc = 5;

    int j = 9;

    int* pi = &j;

    pc = &j;pc = pi;

    pi = pc;

    Error: assignment of read-only location

    Error: invalid conversion from `const int*' to `int*'

  • 7/30/2019 ++C vs. C | - 1

    48/50

    swap(x,y):

    123456

    ?

    x=123 y=456

    Difference between C and C++

    int x,y;

    cin>>x>>y;

    swap(x,y);

    cout

  • 7/30/2019 ++C vs. C | - 1

    49/50

    swap(x,y):

    123456

    ?

    x=456 y=456

    Difference between C and C++

    int x,y;

    cin>>x>>y;

    swap(x,y);

    cout

  • 7/30/2019 ++C vs. C | - 1

    50/50

    C C++

    Difference between C and C++

    Hello World!

    #include using namespace std;

    int main() {

    cout