iii eee lab manual

Upload: vijayponraj

Post on 07-Apr-2018

225 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/3/2019 III Eee Lab Manual

    1/34

    Functions with default arguments:

    #include

    #include

    void repchar(char=*, int=3);

    void main()

    {

    repchar();

    repchar(x,2);

    repchar(&,4);

    }

    void repchar( char c, int n)

    {

    for(int i=1;i

  • 8/3/2019 III Eee Lab Manual

    2/34

    Output :

    *

    *

    *

    x

    x

    &

    &

    &

    &

  • 8/3/2019 III Eee Lab Manual

    3/34

    Classes with objects

    #include

    class cl

    {

    int i;

    public:

    int get_i();

    void put_i(int j);

    };

    int cl::get_i()

    {

    return i;

    }

    void cl::put_i(int j)

    {

    i=j;

    }

    int main()

    {

    cl s;

    s.put_i(10);

    cout

  • 8/3/2019 III Eee Lab Manual

    4/34

    Output :

    10

  • 8/3/2019 III Eee Lab Manual

    5/34

    Friend Functions

    #include

    class sample

    {

    private:

    float a,b;

    public:

    void getdata()

    { cin>>a>>b;

    }

    friend float mean(sample s)

    { return (s.a+s.b)/2.0;

    }

    };

    void main()

    {

    sample s1;

    cout

  • 8/3/2019 III Eee Lab Manual

    6/34

    Output :

    Enter two float inputs:

    15

    22

    Mean value=18.5

  • 8/3/2019 III Eee Lab Manual

    7/34

    Constructor with default arguments:

    #include

    #include

    class test

    {

    private:

    float a,b,c;

    public:

    test(float x=1.1, float y=2.2, float z=3.3)

    {a=x;

    b=y;

    c=z;

    }

    void display()

    {cout

  • 8/3/2019 III Eee Lab Manual

    8/34

    t3.display();

    t4.display();

    t5.display();

    getch();

    }

  • 8/3/2019 III Eee Lab Manual

    9/34

    Output :

    1.1 2.2 3.3

    1.1 2.2 3.3

    6.6 2.2 3.3

    6.6 7.7 3.3

    6.6 7.7 8.8

  • 8/3/2019 III Eee Lab Manual

    10/34

    Copy Constructor:

    #include

    #include

    class test

    {

    private:

    int a,b,c;

    public:

    test()

    {

    a=5;

    b=10;

    c=15;

    }

    test(int x, int y, int z)

    {

    a=x;

    b=y;

    c=z;

    }

    void display()

    {

    cout

  • 8/3/2019 III Eee Lab Manual

    11/34

    void main()

    {

    clrscr();

    test t1;

    test t2=t1; // copy constructor

    test t3(4,8,12);

    test t4(t3); // copy constructor

    t1.display();

    t2.display();

    t3.display();

    t4.display();

    getch();

    }

  • 8/3/2019 III Eee Lab Manual

    12/34

    Output :

    5 10 15

    5 10 15

    4 8 12

    4 8 12

  • 8/3/2019 III Eee Lab Manual

    13/34

    Assignment Operator Overloading

    # include

    #include

    class sample

    {

    private:

    int x;

    float y;

    public:

    sample(int, float);

    void operator = (sample abc);

    void display();

    };

    sample::sample(int one,float two)

    {

    x=one;

    y=two;

    }

    void sample::operator =(sample abc)

    {

    x=abc.x;

    y=abc.y;

    }

    void sample::display()

    {

    cout

  • 8/3/2019 III Eee Lab Manual

    14/34

    }

    void main()

    {

    clrscr();

    sample s1(10,15.5);

    sample s2(20,25.5);

    s1.display();

    s2.display();

    s1=s2;

    s1.display();

    s2.display();

    getch();

    }

  • 8/3/2019 III Eee Lab Manual

    15/34

    Output :

    10 15.5

    20 25.5

    20 25.5

    20 25.5

  • 8/3/2019 III Eee Lab Manual

    16/34

    Type Conversion:

    #include

    #include

    void main()

    {

    int a;

    float b,c;

    clrscr();

    couta;

    coutb;

    c=float(a)+b;

    cout

  • 8/3/2019 III Eee Lab Manual

    17/34

    Output :

    Enter the value of a: 10

    Enter the value of b: 12.5

    The value of C is: 22.5

  • 8/3/2019 III Eee Lab Manual

    18/34

    Inheritance using C++:

    # include

    #include

    class base

    {

    private:

    char n[20];

    int rno;

    public:

    void getdata()

    {

    coutrno;

    } void display();};

    class deri:public base

    {

    private:

    float ht, wt;

    public:

    void getdata()

    { base::getdata();

    coutwt;

    }

    void display();

    };

  • 8/3/2019 III Eee Lab Manual

    19/34

    void base::display()

    {

    cout

  • 8/3/2019 III Eee Lab Manual

    20/34

    Output :

    Enter inputs.

    Enter name and roll number

    raja

    34

    Enter height and weight

    180

    67

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

    name rolno height weight

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

    raja 34 180 67

  • 8/3/2019 III Eee Lab Manual

    21/34

    Function Template :

    #include

    #include

    template

    void swap(T &x, T&y)

    {

    T t;

    t=x;

    x=y;

    y=t;

    }

    void main()

    {

    char ch1,ch2;

    int a,b;

    float c,d;

    clrscr();

    coutch1>>ch2;

    swap(ch1,ch2);

    cout

  • 8/3/2019 III Eee Lab Manual

    22/34

    coutc>>d;

    swap(c,d);

    cout

  • 8/3/2019 III Eee Lab Manual

    23/34

    Output :

    Enter the character values of ch1 and ch2:

    a

    z

    On swapping ch1 and ch2

    z a

    Enter the integer values of a and b:

    2

    3

    On swapping a and b

    3 2

    Enter the real values of c and d:

    2.6

    7.8

    On swapping c and d

    7.8 2.6

  • 8/3/2019 III Eee Lab Manual

    24/34

    Abstract Class:

    abstract class one

    {

    }

    class two extends one

    {

    void display()

    {

    System.out.println("Hai");

    }

    }

    class test

    {

    public static void main(String args[])

    {

    two t1= new two();

    t1.display();

    }

    }

  • 8/3/2019 III Eee Lab Manual

    25/34

    Output

    C:\javaex>javac test.java

    C:\javaex>java test

    Hai

  • 8/3/2019 III Eee Lab Manual

    26/34

    Interface Inheritance

    interface int1

    {

    int i1();

    }

    interface int2

    {double i2();

    }

    interface int3 extends int1,int2

    {boolean i3();

    }

    class test implements int3

    {public int i1()

    {

    return 100;

    }

    public double i2()

    {return 25.5;

    }

    public boolean i3()

    {

    return true;

    }

    }

    class inter

    {

  • 8/3/2019 III Eee Lab Manual

    27/34

    public static void main(String args[])

    {

    test t= new test();

    System.out.println("Interface1\t"+t.i1());

    System.out.println("Interface2\t"+t.i2());

    System.out.println("Interface3\t"+t.i3());

    }

    }

  • 8/3/2019 III Eee Lab Manual

    28/34

    Output

    C:\javaex>javac inter.java

    C:\javaex>java inter

    Interface1 100

    Interface2 25.5

    Interface3 true

  • 8/3/2019 III Eee Lab Manual

    29/34

    Exception Handling

    class Sample

    {

    static void Method1()

    {

    try

    {

    System.out.printIn("Method1-try");

    throw new RuntimeException("Sample");

    }

    System.out.printIn("Method1-finally");

    }

    }

    static void Method2()

    {

    try

    {

    System.out.printIn("Method2-try");

    return;

    }

    finally

    {

    System.out.printIn("Method2-finally");

    }

    }

    static void Method3()

  • 8/3/2019 III Eee Lab Manual

    30/34

    {

    try

    {

    System.out.printIn("Method3-try");

    }

    finally

    {

    System.out.printIn("Method3-finally");

    }

    }

    public static void main(String s[])

    {

    Method1();

    }

    }

    catch(Exception e)

    {

    system.out.printin("Exception caught");

    }

    Method2();

    Method3();

    }

    }

  • 8/3/2019 III Eee Lab Manual

    31/34

    Output

    Method1-try

    Method1-finally

    Exception caught

    Method2-try

    Method2-finally

    Method3-try

    Method3-finally

  • 8/3/2019 III Eee Lab Manual

    32/34

    Design of Multi-Threaded Programs in JAVA

    Program

    class Newthread extends Thread

    {

    Newthread()

    {

    super("Demo Thread");

    System.out.printIn("Child thread: " + this);

    start();

    }

    public void run()

    {try

    {

    for(int i=5;i>0;i--) {

    System.out.printIn("Child Thread: " +i);

    Thread.sleep(500);

    }

    }

    Catch (InterruptedException e)

    {

    System.out.printIn("child interrupted.");}

    System.out.printIn("Exiting child thread.");

    }

    }

    class ExtendThread

    {

    public static void main(string args[])

    {

    new NewThread();

    try

    {

    for (int i =5; i>0;i--)

    {

    System.out.printIn("Main Thread: " +i);

    Thread.sleep(1000);

    }

  • 8/3/2019 III Eee Lab Manual

    33/34

    }

    catch (InterruptedException e)

    {

    System.out.printIn("Main thread interrupted.");

    }

    System.out.printIn ("Main thread exiting.");

    }

    }

  • 8/3/2019 III Eee Lab Manual

    34/34

    Output:

    Child thread: Thread (Demo Thread, 5, main)

    Main Thread: 5

    Child Thread: 5

    Child Thread: 4

    Main Thread: 4

    Child Thread: 3

    Child Thread: 2

    Main Thread: 3

    Child Thread: 1

    Exiting child thread.

    Main Thread: 2

    Main Thread: 1

    Main Thread Exiting.