C++ Programs Coding Vol.1

download C++ Programs Coding Vol.1

of 24

Transcript of C++ Programs Coding Vol.1

  • 8/8/2019 C++ Programs Coding Vol.1

    1/24

    /* Write a program of swapping two numbers without using third variable using class module with

    parameters. */

    #include

    #include

    Class sample

    {

    Public:

    float fx, fy;

    Void swap(float fx, float fy)

    {

    fx=fx+fy;

    fy=fx-fy;

    fx=fx-fy;

    cout

  • 8/8/2019 C++ Programs Coding Vol.1

    2/24

    /* Write a program of student marksheet where Physics, Chemistry and Maths are the subject as

    input. */

    #include

    #include

    class marks

    {

    int r, p, c, m;

    char name[20];

    public:

    void input();

    void dis();

    };

    void marks::input()

    {

    coutname;

    coutr;

    coutp;

    coutc;

    coutm;

    }

    void marks::dis()

    {

    int n;

  • 8/8/2019 C++ Programs Coding Vol.1

    3/24

  • 8/8/2019 C++ Programs Coding Vol.1

    4/24

    if(per>=60)

    cout

  • 8/8/2019 C++ Programs Coding Vol.1

    5/24

    /* Define a class "Bank" with following data members Acc-no, Name of Depositor, A/C Balance,

    Wirte member function to perform following task.

    i) Input Values to data members.

    ii) Display the information (Acc no, Name, Balance).

    iii) Depositor Amt. withdrawn Amt. */

    #include

    #include

    class Bank

    {

    int acc, bal, cash, withdraw;

    char name[20], ch;

    public:

    void input();

    void display();

    void input();

    void credit();

    };

    void Bank::input()

    {

    coutname;

    coutacc;

    coutbal;

    }

  • 8/8/2019 C++ Programs Coding Vol.1

    6/24

    void Bank::display()

    {

    cout

  • 8/8/2019 C++ Programs Coding Vol.1

    7/24

    for(i=0; i>ch;

    switch(ch)

    {

    case 1:obj.input();

    obj.display();

    break;

    case 2:obj.credit();

    obj.display();

    break;

    }

    }

    getch();

    }

    --------------------------x-------------------------------x----------------------------------

  • 8/8/2019 C++ Programs Coding Vol.1

    8/24

    /*Program to increment the value of x by defining it as static variable. */

    #include

    #include

    class test

    {

    static int x;

    public:

    void show()

    {

    x++;

    cout

  • 8/8/2019 C++ Programs Coding Vol.1

    9/24

    /*Program for parametrized constructors. */

    #include

    #include

    class test

    {

    int a, b;

    public:

    test(int x, int y)

    {

    a=x;

    b=y;

    }

    void show()

    {

    cout

  • 8/8/2019 C++ Programs Coding Vol.1

    10/24

    /* Write a class "test" with three float variables fx, fy, fz. Write a parameterized

    constructor, assign values of variables. Write a function sum, which calculates sum of

    three varaibles and return the results of sum to the calling portion. Display the result

    in calling portion. */

    #include

    #include

    class test

    {

    float fx, fy, fz;

    public:

    test(float a, float b, float c)

    {

    fx=a;

    fy=b;

    fz=c;

    }

    float show()

    {

    float sum;

    sum=fx+fy+fz;

    return(sum);

    }

    };

    void main()

    {

    clrscr();

    test obj(1.2, 1.4, 2.5)

  • 8/8/2019 C++ Programs Coding Vol.1

    11/24

    cout

  • 8/8/2019 C++ Programs Coding Vol.1

    12/24

    /* Take 3 varaibles a,b,c in class sample. Write 2 parameterized constructors where the

    first constructor receives values of a and b, the second constructor receives values of

    a,b and c and print the value accordingly. */

    #include

    #include

    class sample

    {

    int a,b,c;

    public:

    sample(int x, int y)

    {

    a=x;

    b=y;

    cout

  • 8/8/2019 C++ Programs Coding Vol.1

    13/24

    sample obj(x,y);

    sample obj(x,y,z);

    getch();

    }

    ----------------------------x----------------------------x---------------------------------------

  • 8/8/2019 C++ Programs Coding Vol.1

    14/24

    /* Create a class "test" with 2 float values fx and fy. Write 2 functions input() and show()

    to accept and print value respectively. Access the function in main memory using pointer

    technique. */

    #include

    #include

    class test

    {

    float fx, fy;

    public:

    void input();

    void show();

    };

    void test::input()

    {

    coutfx>>fy;

    }

    void test::show()

    {

    cout

  • 8/8/2019 C++ Programs Coding Vol.1

    15/24

    ptr->show();

    getch();

    }

    --------------------------x------------------------------x----------------------------------

  • 8/8/2019 C++ Programs Coding Vol.1

    16/24

    /* Create a class "cal" with 3 integer variables a,b and c. Write two functions input(),

    cat(). input() accept the value while the function "cal" finds the total and average

    of number and displays it. Use pointer technique access members of class. */

    #include

    #include

    class cal

    {

    int a, b, c, av;

    public:

    void input()

    {

    couta>>b;

    }

    void cal()

    {

    c=a+b;

    av=c/2;

    cout

  • 8/8/2019 C++ Programs Coding Vol.1

    17/24

    ptr->input();

    ptr->cal();

    getch();

    }

    -----------------------------x------------------------------ x--------------------------------------

  • 8/8/2019 C++ Programs Coding Vol.1

    18/24

    /* WAP to use reference varaible. */

    #include

    #include

    void main()

    {

    clrscr();

    int x=10;

    int &a=x;

    cout

  • 8/8/2019 C++ Programs Coding Vol.1

    19/24

    /* WAP to swap 2 integer values using call by reference technique in main function. */

    #include

    #include

    void swap(int a, int b)

    {

    int *t;

    *t=*a;

    *a=*b;

    *b=*t;

    }

    void main()

    {

    clrscr();

    int x, y;

    coutx>>y;

    cout

  • 8/8/2019 C++ Programs Coding Vol.1

    20/24

    /* WAP to pass 2 objects to member function of class "test" has one int private data sem()

    receive value of x for 2 objects sums them and print the result. input() receives the values

    of x for both the objects. */

    #include

    #include

    class test

    {

    int x;

    public:

    void input(int a)

    {

    x=a;

    }

    void show()

    {

    cout

  • 8/8/2019 C++ Programs Coding Vol.1

    21/24

    test obj1, obj2, obj3;

    obj1.input(10);

    obj2.input(20);

    obj1.show();

    obj2.show();

    obj3.sum(obj1, obj2);

    getch();

    }

    -----------------------------x------------------------------x---------------------------------

  • 8/8/2019 C++ Programs Coding Vol.1

    22/24

    /* WAP to pass 2 objects to member function sum of class test for one integer private data.

    sum receives values of x for 2 objects, sum them and return the object to calling portion.

    input() receives values of x for both the objects. */

    #include

    #include

    class test

    {

    int x;

    public:

    void input(int a)

    {

    x=a;

    }

    void show()

    {

    cout

  • 8/8/2019 C++ Programs Coding Vol.1

    23/24

    test obj1, obj2, obj3;

    obj1.input(10);

    obj2.input(20);

    obj3=obj3.sum(obj1, obj2);

    obj1.show();

    obj2.show();

    obj3.show();

    getch();

    }

    ---------------------------x-----------------------------x--------------------------------

  • 8/8/2019 C++ Programs Coding Vol.1

    24/24

    /* Create a class "sample" with 2 static function input() and display(). input() receives

    values at run time from user while display() prints the values. */

    #include

    #include

    class sample

    {

    static int a, b;

    public:

    static void input();

    static void display();

    };

    void sample::input()

    {

    couta>>b;

    }

    void sample::display()

    {

    cout