om institute of technology & management juglan, … · lab manual object oriented programming using...

47
1 OM INSTITUTE OF TECHNOLOGY & MANAGEMENT JUGLAN, HISAR-125001 LAB MANUAL Object Oriented Programming Using C++BCA-231 DEPARTMENT OF COMPUTER APPLICATIONS

Upload: others

Post on 19-Oct-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

  • 1

    OM INSTITUTE OF TECHNOLOGY & MANAGEMENT

    JUGLAN, HISAR-125001

    LAB MANUAL

    Object Oriented Programming Using ‘C++’

    BCA-231

    DEPARTMENT OF COMPUTER APPLICATIONS

  • 2

    Hardware & Software requirment

    Hardware:-Any operating system like UNIX,LINUX and Windows. Machine must be

    supported 0-127 ASCII char format.

    Software:-

    Like c programs, c++ programs can be created using any text editor. But theremust be a compiler which convert high level language into machine language. Such as Turboc++ and Borland c++. These also provide an integrated environment for developing and editingprograms.

  • 3

    INDEX

    Object Oriented Programming Using ‘C++’

    BCA-231

    Sr.No Program Name Page No.1. Introduction to C++. 4

    2. Write a program in C++ to print average of two numbers. 5

    3. Write a program in C++ of using scope resolution operator. 6-7

    4. Write a program in C++ of using inline function. 8-9

    5. Write a program in C++ of using class. 10-11

    6. Write a program in C++ to perform nesting of memberfunctions.

    12-14

    7. Write a program in C++ of using friend function. 15-16

    8. Write a program in C++ of using constructors in class. 17-18

    9. Write a program in C++ of using destructors. 19-2010. Write a program in C++ of using overloading unary

    operator.21-23

    11. Write a program in C++ of using overloading binaryoperators.

    24-26

    12. Write a program in C++ of using single inheritance. 27-29

    13. Write a program in C++ of using multilevel inheritance. 30-33

    14. Write a program in C++ of using multiple inheritance. 34-37

    15. Write a program in C++ of using hybrid inheritance. 38-41

    16. Write a program in C++ of using virtual function. 42-43

    17. Write a program in C++ to perform random access. 44-45

    18. Write a program in C++ of using operations on binary files. 46-47

  • 4

    Introduction to c++

    C++ is an object-oriented programming language. It was developed by Bjarne Stroustrup atAT&T Bell laboratories.

    C++ is an extension of C with a major addition of the class construct feature of Simula67.Stroustrup initially called the new language “C with class”.The idea of C++ comes from the c increment operator ++.

    Basic concepts of object-oriented programming

    1. Objects.2. Classes.3. Data abstraction and encapsulation.4. Inheritance.5. Polymorphism.6. Dynamic binding.7. Message passing.

    Applications of c++

    1. Since c++ allows us to create hierarchy-related objects.2. It is expected that c++ will replace C as a genral-purpose language in the near

    future.

  • 5

    /*wap to print the average of two number.*/

    #include

    #include

    void main()

    {

    int num1,num2;float ave;

    clrscr();

    coutnum1>>num2;

    ave=float(num1+num2)/2;

    cout

  • 6

    /*wap in c++ of using scope resolution operator.*/

    #include

    #include

    int b=100;

    void main()

    {

    clrscr();

    int b=50;

    {

    int bh=b;

    int b=25;

    cout

  • 7

    Output

  • 8

    /*wap in c++ of using inline function*/

    #include

    #include

    class New

    {

    public:

    inline long mul(int num1,int num2)

    {

    return num1*num2;

    }

    long add(int num1,int num2);

    };

    inline long new::add(int num1,int num2)

    {

    return num1+num2;

    }

    void main()

    {

    int a,b;

    clrscr();

    couta>>b;

    New obj;

  • 9

    cout

  • 10

    /*wap in c++ of using class.*/

    #include

    #include

    class Demo

    {

    int num1,num2;

    public:

    void get()

    {

    coutnum1>>num2;

    }

    void set()

    {

    cout

  • 11

    getch();

    }

    Output

  • 12

    /*wap to perform nesting of member functions.*/

    #include

    #include

    class Demo

    {

    int a, b;

    public:

    void getdata();

    int largest();

    void display();

    };

    void Demo :: getdata()

    {

    couta>>b;

    }

    int Demo :: largest()

    {

    if(a>=b)

    {

    return a;

    }

    else

    {

  • 13

    return b;

    }

    }

    void Demo :: display()

    {

    cout

  • 14

    Output

  • 15

    /*wap in c++ of using friend function*/

    #include

    #include

    class Sample

    {

    int a,b;

    public:

    void Setvalue()

    {

    a=25;

    b=50;

    }

    friend float mean(Sample S)

    };

    float mean(Sample S)

    {

    return float(S.a+S.b)/2.0;

    }

    int main()

    {

    clrscr();

    Sample X;

  • 16

    X.Setvalue();

    cout

  • 17

    /*wap in c++ of using constructor*/

    #include

    #include

    class Demo

    {

    public:

    Demo()

    {

    cout

  • 18

    Output

  • 19

    /*wap in c++ of using destructors*/

    #include

    #include

    int count=0;

    class Alpha

    {

    public:

    Alpha()

    {

    count++;

    cout

  • 20

    cout

  • 21

    /*wap in c++ of using overloading unary operator*/

    #include

    #include

    class Space

    {

    int x,y,z;

    public:

    void Getdata(int a, int b, int c);

    void Display(void);

    void operator -();

    };

    void Space :: Getdata(int a, int b, int c)

    {

    x=a;

    y=b;

    z=c;

    }

    void Space :: Display(void)

    {

    cout

  • 22

    {

    x=-x;

    y=-y;

    z=-z;

    }

    void main()

    {

    clrscr();

    Space S;

    S.Getdata(10,-20,30);

    cout

  • 23

    Output

  • 24

    /*wap in c++ of using overloading binary operator*/

    #include

    #include

    class Demo

    {

    float x,y;

    public:

    Demo()

    {}

    Demo(float real,float img)

    {

    x=real;

    y=img;

    }

    Demo operator +(Demo);

    void show();

    };

    Demo Demo::operator +(Demo ob)

    {

    Demo temp;

    temp.x=x+ob.x;

    temp.y=y+ob.y;

    return (temp);

  • 25

    }

    void Demo::show()

    {

    cout

  • 26

    Output

  • 27

    /*wap in c++ of using single inheritance.*/

    #include

    #include

    class B

    {

    int a;

    public:

    int b;

    void Get_ab();

    int Get_a();

    void show_a();

    };

    class D : public B

    {

    int c;

    public:

    void Mul();

    void Display();

    };

    void B :: Get_ab()

    {

    a=5;

    b=10;

    }

  • 28

    int B :: Get_a()

    {

    return a;

    }

    void B :: show_a()

    {

    cout

  • 29

    d.show_a();

    d.Display();

    d.b=20;

    d.Mul();

    d.Display();

    getch();

    return 0;

    }

    Output

  • 30

    /*wap in c++ of using multilevel inheritance.*/

    #include

    #include

    class Student

    {

    protected:

    int roll;

    public:

    void get_num(int);

    void put_num();

    };

    void Student::get_num(int a)

    {

    roll=a;

    }

    void Student::put_num()

    {

    cout

  • 31

    public:

    void get_mks(float,float);

    void put_mks();

    };

    void Test::get_mks(float a,float b)

    {

    sub1=a;sub2=b;

    }

    void Test::put_mks()

    {

    cout

  • 32

    cout

  • 33

    Output

  • 34

    /*wap in c++ of using multiple inheritance.*/

    #include

    #include

    class Student

    {

    protected:

    int roll;

    public:

    void get_num(int);

    void put_num();

    };

    void Student::get_num(int a)

    {

    roll=a;

    }

    void Student::put_num()

    {

    cout

  • 35

    public:

    void get_mks(float,float);

    void put_mks();

    };

    void Test::get_mks(float a,float b)

    {

    sub1=a;sub2=b;

    }

    void Test::put_mks()

    {

    cout

  • 36

    cout

  • 37

    Output

  • 38

    /*wap in c++ of using Hybrid inheritance.*/

    #include

    #include

    class Student

    {

    protected:

    int roll;

    public:

    void get_num(int);

    void put_num();

    };

    void Student::get_num(int a)

    {

    roll=a;

    }

    void Student::put_num()

    {

    cout

  • 39

    public:

    void get_mks(float,float);

    void put_mks();

    };

    void Test::get_mks(float a,float b)

    {

    sub1=a;sub2=b;

    }

    void Test::put_mks()

    {

    cout

  • 40

    {

    cout

  • 41

    coutr;

    couts1>>s2;

    obj.get_score();

    obj.get_num(r);

    obj.get_mks(s1,s2);

    obj.display();

    getch();

    }

    Output

  • 42

    /*wap in c++ of using virtual function.*/

    #include

    #include

    class Base

    {

    public:

    virtual void display()

    {

    cout

  • 43

    ptr=&obj;

    ptr->display();

    getch();

    }

    Output

  • 44

    /*wap in c++ of using I\O operation on characters.*/

    #include

    #include

    void main()

    {

    int count=0;

    char c;

    clrscr();

    cout

  • 45

    Output

  • 46

    /*wap in c++ of using I/O operation on binary files.*/

    #include

    #include

    #include

    #include

    void main()

    {

    clrscr();

    float height[4]={55.55,66.6,77.7,88.8};

    ofstream outfile;

    outfile.open("c:\\mca.dat");

    outfile.write((char *)&height,sizeof(height));

    outfile.close();

    for(int i=0;i

  • 47

    getch();

    }

    Output