c++ Programs Internet 1

  • Upload
    rahul-r

  • View
    214

  • Download
    0

Embed Size (px)

Citation preview

  • 8/8/2019 c++ Programs Internet 1

    1/15

    1. Language Basics

    1.1 Introduction( 4 ) 1. 9. namespace( 14 )

    1. 2. main function( 2 ) 1. 10. global namespace( 2 )

    1. 3. Variables( 6 ) 1. 11. comments( 1 )

    1. 4. Variable size( 1 ) 1.12. static( 3 )

    1. 5. global variable( 2 ) 1. 13. using( 2 )

    1. 6. block scope variable( 7 ) 1. 14. mutable( 2 )1. 7. Expression( 1 ) 1. 15. extern( 1 )

    1. 8. const( 3 )

    --------------------------------------------------------------

    1. 1. Introduction

    1. 1. 1. This is a simple C++ program

    1. 1. 2. Calculating with integer constants

    1. 1. 3. Working with integer variables

    1. 1. 4. Using the assignment operator--------------------------------------------------------------

    1. 1. 1. This is a simple C++ program#include

    using namespace std;

    int main()

    {

    cout

  • 8/8/2019 c++ Programs Internet 1

    2/15

    1. 3. Variables

    1. 3. 1.Assign value to a variable

    1. 3. 2. Dynamic initialization.

    1. 3. 3.A scoping example

    1. 3. 4. Using the unary scope resolution operator

    1. 3. 5. Finding maximum and minimum values for data types1. 3. 6. Comparing data values

    ---------------------------------------------

    1. 3. 1. Assign value to a variable

    #include

    using namespace std;

    int main()

    {

    int length; // this declares a variablelength = 7; // this assigns 7 to length

    cout

  • 8/8/2019 c++ Programs Internet 1

    3/15

    1. 3. 5. Finding maximum and minimum values for data types

    #include

    #include

    using std::cout;

    using std::endl;

    using std::numeric_limits;

    int main() {cout

  • 8/8/2019 c++ Programs Internet 1

    4/15

    void func2()

    {

    int count;

    for(count=0; count

  • 8/8/2019 c++ Programs Internet 1

    5/15

    output:

    inner i: 50

    outer i: 10

    -------------------------------

    1. 6. 4. Names in inner scopes can hide names in outer scopes.

    #include

    #include

    int main()

    {

    for (int i = 0; i < 10; ++i)

    {

    int x = 2;if (x < i)

    {double x = 3.4;

    std::cout

  • 8/8/2019 c++ Programs Internet 1

    6/15

    int n::counter::n = 7;

    int main()

    {

    int counter = 0;

    int n = 10;

    std::cout

  • 8/8/2019 c++ Programs Internet 1

    7/15

    int main()

    {

    int a[] = { 10, 20, 30 };

    f( a );

    cout

  • 8/8/2019 c++ Programs Internet 1

    8/15

    output:

    10 9 8 7 6 5 4 3 2 1 0

    20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0100 99 98 97 96 95 94 93 92 91 90

    ----------------------------

    1. 9. 3. A nested namespace

    #include

    using namespace std;

    namespace MyNamespace1 {int i;

    namespace MyNamespace2 { // a nested namespace

    int j;

    }

    }

    int main(){MyNamespace1::i = 19;

    MyNamespace1::MyNamespace2::j = 10; // this is right

    cout

  • 8/8/2019 c++ Programs Internet 1

    9/15

    namespace second

    {

    double var = 3.1416;

    }

    int main () {{

    using namespace first;

    cout

  • 8/8/2019 c++ Programs Internet 1

    10/15

    namespace Example

    {

    const double PI = 3.14159;

    void printValues();

    namespace Inner{

    enum Years { FISCAL1 = 1990, FISCAL2, a };

    }

    }

    namespace

    {

    double doubleInUnnamed = 88.22;}

    int main()

    {

    Example::printValues();

    return 0;

    }

    void Example::printValues()

    {cout

  • 8/8/2019 c++ Programs Internet 1

    11/15

    ("input error in readAndProcessSum()");

    }

    // return sum

    return sum;

    }

    }

    a

    I/O exception: input error in readAndProcessSum()

    ---------------------------------

    1. 9. 12. Using unnamed namespaces.

    #include

    #include

    namespace {

    int i = 10;}

    namespace {

    int j; // same unnamed namespace

    namespace X {

    int i = 20;

    }

    namespace Y = X;

    int f() { return i; }}

    int main(){

    std::cout

  • 8/8/2019 c++ Programs Internet 1

    12/15

    60

    2

    2

    5

    -----------------------------------1. 10. global namespace

    1. 10. 1. Use explicit std:: qualification

    1. 10. 2. Bring only a few names into the global

    namespace

    --------------------------------------

    1. 10. 1. Use explicit std:: qualification

    #include

    int main()

    {

    int val;

    std::cout > val;

    std::cout

  • 8/8/2019 c++ Programs Internet 1

    13/15

    }

    1 2 3 4 5 67 8 9 10 11 12 13

    14 15 16 17 18 19 20

    21 22 23 24 25 26

    27 28 29 30

    --------------------------------------

    1. 12. 2. Use static variable to compute a running average of

    numbers entered by the user

    #include

    using namespace std;

    int f(int i);

    int main(){int num;

    do {

    cout > num;

    if(num != -1)

    cout

  • 8/8/2019 c++ Programs Internet 1

    14/15

    const int count = sizeof boxes/sizeof boxes[0];

    cout

  • 8/8/2019 c++ Programs Internet 1

    15/15

    return 0;

    }

    1900"

    ---------------------

    1. 14. 2. Demonstrating storage-class specifier mutable

    #include

    using std::cout;

    using std::endl;

    class MyClass {public:

    MyClass( int v )

    {

    value = v;

    }

    int getValue() const{

    return value++;

    }

    private:

    mutable int value;

    };

    int main()

    {const MyClass test( 99 );

    cout