C++ Program Concepts

Embed Size (px)

Citation preview

  • 7/31/2019 C++ Program Concepts

    1/31

    C++ Programming

    19/01/12

  • 7/31/2019 C++ Program Concepts

    2/31

    A simple C++ program

    A simple C++ program which prints "HelloWorld!" on the screen

    #include // need this header//file to support the C++ I/O system

    using namespace std; // telling the//compiler to use namespace "std",

    // where the entire C++ library is declared.

  • 7/31/2019 C++ Program Concepts

    3/31

    int main()

    {

    // Print out a sentence on the screen.// "

  • 7/31/2019 C++ Program Concepts

    4/31

    Program for Variables

    #include using namespace std;

    // declaring a constant. It's value cannot be changed.

    const int CONST_VAL = 5;

    int main()

    {

    int iValue; // An integer variable

    float fValue; // A floating point variablechar cValue; // A character variable

  • 7/31/2019 C++ Program Concepts

    5/31

    iValue = 1234; // Assigns 1234 to iValue

    fValue = 1234.56; // Assigns 1234.56 to fValue

    cValue = 'A'; // Assigns A to cValue

    // Now print them out on the screen:

    cout

  • 7/31/2019 C++ Program Concepts

    6/31

    Program for operation with variables

    #include

    using namespace std;

    int main ()

    {

    // declaring variables:

    int a, b;

    int result;

  • 7/31/2019 C++ Program Concepts

    7/31

    // process:

    a = 5;b = 2;

    a = a + 1;

    result = a - b; // print out the result:

    cout

  • 7/31/2019 C++ Program Concepts

    8/31

    Converts gallons to liters

    #include

    using namespace std;

    int main(){

    float gallons, liters;

    cout

  • 7/31/2019 C++ Program Concepts

    9/31

    cin >> gallons; // Read the inputs from the user

    liters = gallons * 3.7854; // convert to liters

    cout

  • 7/31/2019 C++ Program Concepts

    10/31

    Program for Array

    #include

    using namespace std;

    int main ()

    {int age[10];int i,sum=0, avg=0;

    int max=0,min=100;

  • 7/31/2019 C++ Program Concepts

    11/31

    for(i=0;i

  • 7/31/2019 C++ Program Concepts

    12/31

    if(age[i]>max)

    {max=age[i];

    }

    if(age[i]

  • 7/31/2019 C++ Program Concepts

    13/31

    avg=sum/10;

    cout

  • 7/31/2019 C++ Program Concepts

    14/31

    Program for calculating length of string

    #include

    using namespace std;

    int main()

    {

    char name[15];

    int i=0;

    cout name;

  • 7/31/2019 C++ Program Concepts

    15/31

    while(name[i]!='\0')

    {

    i++;}

    cout

  • 7/31/2019 C++ Program Concepts

    16/31

    Program for Display a triangle using for

    loopfor (int i = 0; i= 0 ; j--)

    {

    cout

  • 7/31/2019 C++ Program Concepts

    17/31

    calculates the average of the elements

    of the row of the two dimensional

    array#includeusing namespace std;

    int main ()

    {

    int age[2][5]= { {12,13,14,15,15}, { 12,16,17,13,12}};

    int i,j;int sum=0,avg=0;

  • 7/31/2019 C++ Program Concepts

    18/31

    for(i=0;i

  • 7/31/2019 C++ Program Concepts

    19/31

    A program which illustrates the

    working of pointer arithmetic

    #include

    using namespace std;

    Int main ()

    {

    int *x;

    int *p,*q;

    int c=100,a;

    x=&c;

    p=x+2;

    q=x-2;

    a=p-q;

  • 7/31/2019 C++ Program Concepts

    20/31

    cout

  • 7/31/2019 C++ Program Concepts

    21/31

    A program which illustrates the

    working of arrays and pointers

    #include

    using namespace std;

    int main()

    {int age[5];

    int *p;

    int sum=0,i;

    char yes='y';p=age;

  • 7/31/2019 C++ Program Concepts

    22/31

    for(i=0;i

  • 7/31/2019 C++ Program Concepts

    23/31

    Program for Function Prototuping

    #includeusing namespace std;

    int factorial(int n);

    int main (){

    int n1,fact;

    cout

  • 7/31/2019 C++ Program Concepts

    24/31

    fact=factorial(n1);

    cout

  • 7/31/2019 C++ Program Concepts

    25/31

    else

    {for(i=1;i

  • 7/31/2019 C++ Program Concepts

    26/31

    Parameter passing mechanism

    Pass by value

    Pass by reference

  • 7/31/2019 C++ Program Concepts

    27/31

    Pass by value

    #include

    using namespace std;

    int add(int n);

    int main(){

    int number,result;

    number=5;

    cout

  • 7/31/2019 C++ Program Concepts

    28/31

    cout

  • 7/31/2019 C++ Program Concepts

    29/31

    Pass by reference

    #includeusing namespace std;

    int add(int &number);

    int main ()

    {

    int number;

    int result;

    number=5;

    cout

  • 7/31/2019 C++ Program Concepts

    30/31

    result=add(&number);

    cout

  • 7/31/2019 C++ Program Concepts

    31/31

    Thats all .

    Thanks !!