oops program file

Upload: naman-garg

Post on 05-Apr-2018

238 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/31/2019 OOPs Program File

    1/84

    OOPS

    PROGRAM

    FILE

    Submitted by-Naman Garg

    3rd SEM

    ECE B1

    Group-3

    UE105050

  • 7/31/2019 OOPs Program File

    2/84

    2

    Naman Garg 3rd

    SEM ECE UE105050

  • 7/31/2019 OOPs Program File

    3/84

    3

    Naman Garg 3rd

    SEM ECE UE105050

    PROGRAM-1

    Q- Program to input a value and display WELL DONE that many times.

    #include

    #include

    void main()

    {clrscr();

    int n; //no. of times to print

    coutn;

    for(int i=1;i

  • 7/31/2019 OOPs Program File

    4/84

    4

    Naman Garg 3rd

    SEM ECE UE105050

    OUTPUT

  • 7/31/2019 OOPs Program File

    5/84

    5

    Naman Garg 3rd

    SEM ECE UE105050

    PROGRAM-2

    Q- Program to find larger of 2 nos.

    #include

    #include

    void main()

    {clrscr();

    int a,b;

    couta>>b;

    if(a>b)

    cout

  • 7/31/2019 OOPs Program File

    6/84

    6

    Naman Garg 3rd

    SEM ECE UE105050

    OUTPUT

  • 7/31/2019 OOPs Program File

    7/84

    7

    Naman Garg 3rd

    SEM ECE UE105050

    PROGRAM-3

    Q- Program to print the pattern

    1

    22

    333

    4444

    ............

    #include

    #include

    void main()

    {clrscr();

    int n; //Stores no. of rows to print

    coutn;

    for(int i=0;i

  • 7/31/2019 OOPs Program File

    8/84

    8

    Naman Garg 3rd

    SEM ECE UE105050

    OUTPUT

  • 7/31/2019 OOPs Program File

    9/84

    9

    Naman Garg 3rd

    SEM ECE UE105050

    PROGRAM-4

    Q- The rates for electricity consumption are

    For the first 100 units - 60p per unit

    For next 200 units - 80p per unit

    Beyond 300 units - 90p per unit

    All users are charged min of Rs50. If the total amt is more than Rs300 then an

    additional surcharge of 15% is added. WAP a program to read names and units

    consumed and print out the total charges.

    #include

    #include

    #include

    void main()

    {clrscr();

    char name[30];

    float units,charge=50.0;

    cout

  • 7/31/2019 OOPs Program File

    10/84

    10

    Naman Garg 3rd

    SEM ECE UE105050

    if(charge>300.0)

    charge=charge+.15*charge;

    cout

  • 7/31/2019 OOPs Program File

    11/84

    11

    Naman Garg 3rd

    SEM ECE UE105050

    OUTPUT

  • 7/31/2019 OOPs Program File

    12/84

    12

    Naman Garg 3rd

    SEM ECE UE105050

    PROGRAM-5

    Q- WAP to find sum, transpose and product of 2 matrix.

    #include

    #include

    #include

    class matrix

    { int a[10][10], b[10][10], c[10][10];

    int m,n,p,q; // size of 2 matrix

    int ch; // choice

    public:

    inline void initialise(); //Initialise sizes to zero

    void menu(); //Prints main menu

    void choose(); //Chooses option from main menu

    void size1(); //Inputs and checks size of 1st matrix

    void size2(); //Inputs and checks size of 2nd matrix

    void input1(); //Inputs matrix 1

    void input2(); //Inputs matrix 2

    void print_c(); //Prints output matrix

    void sum(); //Calc sum of 2 matrix

    void transpose(); //Calc transpose of matrix

    void product(); //Calc prod of matrices

    };

    void matrix::initialise()

    { m=n=p=q=0;

    }

  • 7/31/2019 OOPs Program File

    13/84

    13

    Naman Garg 3rd

    SEM ECE UE105050

    void matrix::menu()

    { clrscr();

    initialise();

    cout

  • 7/31/2019 OOPs Program File

    14/84

    14

    Naman Garg 3rd

    SEM ECE UE105050

    }

    }

    }

    void matrix::size1()

    {

    cout10)

    { cout

  • 7/31/2019 OOPs Program File

    15/84

    15

    Naman Garg 3rd

    SEM ECE UE105050

    size2();

    }

    }

    void matrix::input1()

    {

    cout

  • 7/31/2019 OOPs Program File

    16/84

    16

    Naman Garg 3rd

    SEM ECE UE105050

    getch();

    menu();

    }

    void matrix::sum()

    {

    size1();

    size2();

    if(m!=p||n!=q)

    { cout

  • 7/31/2019 OOPs Program File

    17/84

    17

    Naman Garg 3rd

    SEM ECE UE105050

    size1();

    input1();

    for(int i=0;i

  • 7/31/2019 OOPs Program File

    18/84

    18

    Naman Garg 3rd

    SEM ECE UE105050

    { c[i][j]=0;

    for(int k=0;k

  • 7/31/2019 OOPs Program File

    19/84

    19

    Naman Garg 3rd

    SEM ECE UE105050

    OUTPUT

  • 7/31/2019 OOPs Program File

    20/84

    20

    Naman Garg 3rd

    SEM ECE UE105050

  • 7/31/2019 OOPs Program File

    21/84

    21

    Naman Garg 3rd

    SEM ECE UE105050

    PROGRAM-6

    Q- WAP power() to raise a number m to a power n. The function takes double

    value for m and int value for n. Use default value of 2 for n.

    #include

    #include

    double power(double m, int n=2)

    {

    double p=1;

    for(int i=1;i

  • 7/31/2019 OOPs Program File

    22/84

    22

    Naman Garg 3rd

    SEM ECE UE105050

    OUTPUT

  • 7/31/2019 OOPs Program File

    23/84

    23

    Naman Garg 3rd

    SEM ECE UE105050

    PROGRAM-7

    Q- WAP power() to raise a number m to a power n. The function takes double

    value for m and int value for n. Use default value of 2 for n. Also take int value

    of m and use function overloading.

    #include

    #include

    double power(double m, int n=2)

    {

    double p=1;

    for(int i=1;i

  • 7/31/2019 OOPs Program File

    24/84

    24

    Naman Garg 3rd

    SEM ECE UE105050

    coutm;

    coutn;

    p=power(m,n);

    cout

  • 7/31/2019 OOPs Program File

    25/84

    25

    Naman Garg 3rd

    SEM ECE UE105050

    OUTPUT

  • 7/31/2019 OOPs Program File

    26/84

    26

    Naman Garg 3rd

    SEM ECE UE105050

    PROGRAM-8

    Q- Create two classes DM and DB storing distances. DM stores dist in metre

    and cm while DB in feet and inches. WAP to read the values of class objectsand add one obj of DM with other of DB. Use friend function to carry out this

    task. Take object storing result as DM object.

    #include

    #include

    class DB;

    class DM

    { float metre,cm;

    public:

    void input()

    { coutmetre>>cm;

    }

    friend DM convert(DM,DB);

    void disp()

    { cout

  • 7/31/2019 OOPs Program File

    27/84

    27

    Naman Garg 3rd

    SEM ECE UE105050

    void input()

    { coutfeet>>inch;

    }

    friend DM convert(DM m,DB f);

    };

    DM convert(DM m,DB f)

    { DM conv;

    float total_inch=12*f.feet+f.inch;

    float cm=m.metre*100+m.cm;

    float total_cm=cm+(2.54*total_inch);

    conv.metre= (int) total_cm/100;

    conv.cm=(total_cm/100-conv.metre)*100;

    return conv;

    }

    void main()

    {

    clrscr();

    DM m,conv;

    DB f;

    m.input();

    f.input();

  • 7/31/2019 OOPs Program File

    28/84

    28

    Naman Garg 3rd

    SEM ECE UE105050

    conv=convert(m,f);

    conv.disp();

    getch();

    }

  • 7/31/2019 OOPs Program File

    29/84

    29

    Naman Garg 3rd

    SEM ECE UE105050

    OUTPUT

  • 7/31/2019 OOPs Program File

    30/84

    30

    Naman Garg 3rd

    SEM ECE UE105050

    PROGRAM-9

    Q- Define and implement a class to store bank accounts. Include following

    data members- name of depositor

    account no.

    type of account

    balance

    member functions-

    assign initial values

    deposit an amount

    withdraw amount

    display name and balance

    #include

    #include

    #include

    #include

    class account

    { char name[30];

    long acc_no;

    char type; //s-saving, c-current, d-dormant

    float balance;

    public:

    void menu();

    void choice();

    void assign();

    void deposit();

    void withdraw();

  • 7/31/2019 OOPs Program File

    31/84

    31

    Naman Garg 3rd

    SEM ECE UE105050

    void disp();

    };

    void account::menu()

    { clrscr();

    cout

  • 7/31/2019 OOPs Program File

    32/84

    32

    Naman Garg 3rd

    SEM ECE UE105050

    else

    { coutch;

    goto A;

    }

    }

    void account::assign()

    { cout

  • 7/31/2019 OOPs Program File

    33/84

    33

    Naman Garg 3rd

    SEM ECE UE105050

    coutamt;

    balance=balance+amt;

    cout

  • 7/31/2019 OOPs Program File

    34/84

    34

    Naman Garg 3rd

    SEM ECE UE105050

    puts(name);

    cout

  • 7/31/2019 OOPs Program File

    35/84

    35

    Naman Garg 3rd

    SEM ECE UE105050

    OUTPUT

  • 7/31/2019 OOPs Program File

    36/84

    36

    Naman Garg 3rd

    SEM ECE UE105050

  • 7/31/2019 OOPs Program File

    37/84

    37

    Naman Garg 3rd

    SEM ECE UE105050

  • 7/31/2019 OOPs Program File

    38/84

    38

    Naman Garg 3rd

    SEM ECE UE105050

    PROGRAM-10

    Q- Create a class string. Use overloaded operator == to compare two strings.

    #include

    #include

    #include

    #include

    class string

    {

    char str[30];

    public:

    string operator==(string);

    void getstr();

    void disp();

    };

    void string::getstr()

    { cout

  • 7/31/2019 OOPs Program File

    39/84

    39

    Naman Garg 3rd

    SEM ECE UE105050

    if(strcmp(str,s1.str)==0)

    cout

  • 7/31/2019 OOPs Program File

    40/84

    40

    Naman Garg 3rd

    SEM ECE UE105050

    OUTPUT

  • 7/31/2019 OOPs Program File

    41/84

    41

    Naman Garg 3rd

    SEM ECE UE105050

    PROGRAM-11

    Q- Create a class FLOAT containing one data member. Overload all four

    arithematic operators so that they operate on objects of FLOAT.

    #include

    #include

    class FLOAT

    {

    float f;

    public:

    void get()

    { coutf;

    }

    void disp()

    { cout

  • 7/31/2019 OOPs Program File

    42/84

    42

    Naman Garg 3rd

    SEM ECE UE105050

    return f3;

    }

    FLOAT FLOAT::operator-(FLOAT f1)

    { FLOAT f3;

    f3.f=f-f1.f;

    return f3;

    }

    FLOAT FLOAT::operator*(FLOAT f1)

    { FLOAT f3;

    f3.f=f*f1.f;

    return f3;

    }

    FLOAT FLOAT::operator/(FLOAT f1)

    { FLOAT f3;

    f3.f=f/f1.f;

    return f3;

    }

    void main()

    {

    clrscr();

    FLOAT f1,f2,f3;

    f1.get();

  • 7/31/2019 OOPs Program File

    43/84

    43

    Naman Garg 3rd

    SEM ECE UE105050

    f2.get();

    f3=f1+f2;

    cout

  • 7/31/2019 OOPs Program File

    44/84

    44

    Naman Garg 3rd

    SEM ECE UE105050

    OUTPUT

  • 7/31/2019 OOPs Program File

    45/84

    45

    Naman Garg 3rd

    SEM ECE UE105050

    PROGRAM-12

    Q- Create a base class 'shape'. Use this class to store two double type values

    that could be used to compute the area of figures. Derive two specific classesrectangle and triangle from base class shape. Add to the base class, a member

    function get_data() to initialise base class data member and another function

    disp_area() to display and compute area of figures. Make disp_area() as virtual

    function and redefine in derived class to suit their requirements. WAP to

    accept dimensions interactively and display the area. Treat two inputs as

    length and breadth for rectangle and base and height for triangle.

    #include

    #include

    #include

    void choose();

    class shape

    {

    protected:

    double a,b; //two sides

    double ar;

    public:

    void get_data()

    { couta>>b;

    }

    virtual void disp_area()

    { cout

  • 7/31/2019 OOPs Program File

    46/84

    46

    Naman Garg 3rd

    SEM ECE UE105050

    };

    class triangle : public shape

    { public:

    void disp_area()

    { get_data();

    ar=a*b/2;

    cout

  • 7/31/2019 OOPs Program File

    47/84

    47

    Naman Garg 3rd

    SEM ECE UE105050

    cout

  • 7/31/2019 OOPs Program File

    48/84

    48

    Naman Garg 3rd

    SEM ECE UE105050

    void main()

    {

    clrscr();

    choose();

    getch();

    }

  • 7/31/2019 OOPs Program File

    49/84

    49

    Naman Garg 3rd

    SEM ECE UE105050

    OUTPUT

  • 7/31/2019 OOPs Program File

    50/84

    50

    Naman Garg 3rd

    SEM ECE UE105050

  • 7/31/2019 OOPs Program File

    51/84

    51

    Naman Garg 3rd

    SEM ECE UE105050

    PROGRAM-13

    Q- Create a base class 'shape'. Use this class to store two double type values

    that could be used to compute the area of figures. Derive two specific classesrectangle and triangle from base class shape. Add to the base class, a member

    function get_data() to initialise base class data member and another function

    disp_area() to display and compute area of figures. Make disp_area() as virtual

    function and redefine in derived class to suit their requirements. WAP to

    accept dimensions interactively and display the area. Treat two inputs as

    length and breadth for rectangle and base and height for triangle

    Extend the program to disp area of circle. Create another derived class named

    circle computing area of circle.

    #include

    #include

    #include

    void choose();

    class shape

    {

    protected:

    double a,b; //two sides

    double ar;

    public:

    void get_data(double d1,double d2=0.0)

    { a=d1;

    b=d2;

    }

    virtual void disp_area(){ cout

  • 7/31/2019 OOPs Program File

    52/84

    52

    Naman Garg 3rd

    SEM ECE UE105050

    };

    class triangle : public shape

    { public:

    void disp_area()

    { ar=a*b/2;

    cout

  • 7/31/2019 OOPs Program File

    53/84

    53

    Naman Garg 3rd

    SEM ECE UE105050

    void menu()

    {

    clrscr();

    cout

  • 7/31/2019 OOPs Program File

    54/84

    54

    Naman Garg 3rd

    SEM ECE UE105050

    t.get_data(d1,d2);

    t.disp_area();

    }

    case 3:

    { double d1;

    circle c;

    coutd1;

    c.get_data(d1);

    c.disp_area();

    }

    case 9:

    exit(0);

    default:

    { cout

  • 7/31/2019 OOPs Program File

    55/84

    55

    Naman Garg 3rd

    SEM ECE UE105050

    OUTPUT

  • 7/31/2019 OOPs Program File

    56/84

    56

    Naman Garg 3rd

    SEM ECE UE105050

    PROGRAM-14

    Q- WAP to read list containing item name, code, and cost and produce a

    column wise output. The name is left justified, code and cost are right justifiedcost having precision of two digits.

    #include

    #include

    #include

    #include

    class item

    { int code;

    char name[15];

    float cost;

    public:

    void get_data();void display();

    };

    void item::get_data()

    { coutcode;

    cout

  • 7/31/2019 OOPs Program File

    57/84

    57

    Naman Garg 3rd

    SEM ECE UE105050

    cout

  • 7/31/2019 OOPs Program File

    58/84

    58

    Naman Garg 3rd

    SEM ECE UE105050

    OUTPUT

  • 7/31/2019 OOPs Program File

    59/84

    59

    Naman Garg 3rd

    SEM ECE UE105050

    PROGRAM-15

    Q- WAP to read list containing item name, code, and cost and produce a

    column wise output. The name is left justified, code and cost are right justifiedcost having precision of two digits. Fill unused spaces with hyphen.

    #include

    #include

    #include

    #include

    class item

    { int code;

    char name[15];

    float cost;

    public:

    void get_data();void display();

    };

    void item::get_data()

    { coutcode;

    cout

  • 7/31/2019 OOPs Program File

    60/84

    60

    Naman Garg 3rd

    SEM ECE UE105050

    cout

  • 7/31/2019 OOPs Program File

    61/84

    61

    Naman Garg 3rd

    SEM ECE UE105050

    OUTPUT

  • 7/31/2019 OOPs Program File

    62/84

    62

    Naman Garg 3rd

    SEM ECE UE105050

    PROGRAM-16

    Q- A file phone.txt contains list of names and phone nos. WAP to read file and

    output the list in two columns. The names should be left justified and phonenos write justified.

    #include

    #include

    #include

    #include

    class list

    { char name[20];

    long phone;

    public:

    void get();

    void put();void read_file();

    void write_file();

    };

    void list::get()

    { coutname;

    coutphone;

    write_file();

    }

    void list::put()

    { read_file();

  • 7/31/2019 OOPs Program File

    63/84

    63

    Naman Garg 3rd

    SEM ECE UE105050

    cout

  • 7/31/2019 OOPs Program File

    64/84

    64

    Naman Garg 3rd

    SEM ECE UE105050

    cout

  • 7/31/2019 OOPs Program File

    65/84

    65

    Naman Garg 3rd

    SEM ECE UE105050

    OUTPUT

  • 7/31/2019 OOPs Program File

    66/84

    66

    Naman Garg 3rd

    SEM ECE UE105050

    PROGRAM-17

    Q- WAP to count no. of words in a string.

    #include

    #include

    #include

    int words(char str[100])

    {

    int word=0;

    for(int i=0;str[i]!='\0';i++)

    if(str[i]==' '&&str[i-1]!=' '&&str[i+1]!='\0')

    word++;

    return (word+1);

    }

    void main()

    {clrscr();

    char str[100];

    int word;

    cout

  • 7/31/2019 OOPs Program File

    67/84

    67

    Naman Garg 3rd

    SEM ECE UE105050

    OUTPUT

  • 7/31/2019 OOPs Program File

    68/84

    68

    Naman Garg 3rd

    SEM ECE UE105050

    PROGRAM-18

    Q- WAP to check if string is palindrome or not.

    #include

    #include

    #include

    int palindrome(char str[30])

    {

    int len,i,j;

    for(len=0;str[len]!='\0';len++);

    for(i=0,j=len-1;i

  • 7/31/2019 OOPs Program File

    69/84

    69

    Naman Garg 3rd

    SEM ECE UE105050

    if(res==1)

    cout

  • 7/31/2019 OOPs Program File

    70/84

    70

    Naman Garg 3rd

    SEM ECE UE105050

    OUTPUT

  • 7/31/2019 OOPs Program File

    71/84

    71

    Naman Garg 3rd

    SEM ECE UE105050

    PROGRAM-19

    Q- Write a program to demonstrate use of constructors.

    #include

    #include

    #include

    class String

    {

    int length;

    public:

    char *name;

    String()

    {

    name=new char[1];

    }

    String(char *a)

    {

    length=strlen(a);

    name=new char[length+1];

    strcpy(name,a);

    }

    void concatenate(char *a, char*b)

    {

    length=strlen(a)+strlen(b);

    name=new char[length+1];

    strcpy(name,a);

    strcat(name,b);

    }

  • 7/31/2019 OOPs Program File

    72/84

    72

    Naman Garg 3rd

    SEM ECE UE105050

    void display()

    {

    cout

  • 7/31/2019 OOPs Program File

    73/84

    73

    Naman Garg 3rd

    SEM ECE UE105050

    OUTPUT

  • 7/31/2019 OOPs Program File

    74/84

    74

    Naman Garg 3rd

    SEM ECE UE105050

    PROGRAM-20

    Q- WAP to find max and min value in array.

    #include

    #include

    void main()

    {clrscr();

    int i,n,max,min;

    int list[100];

    max=min=list[0];

    coutn;

    for(i=0;i

  • 7/31/2019 OOPs Program File

    75/84

    75

    Naman Garg 3rd

    SEM ECE UE105050

    OUTPUT

  • 7/31/2019 OOPs Program File

    76/84

    76

    Naman Garg 3rd

    SEM ECE UE105050

    PROGRAM-21

    Q- Write a program to print Fibonacci series

    #include

    #include

    void main()

    {

    clrscr();

    unsigned long ctr,c,a,b,n;

    coutn;

    a=0;

    b=1;

    cout

  • 7/31/2019 OOPs Program File

    77/84

    77

    Naman Garg 3rd

    SEM ECE UE105050

    OUTPUT

  • 7/31/2019 OOPs Program File

    78/84

    78

    Naman Garg 3rd

    SEM ECE UE105050

    PROGRAM-22

    Q- WAP to toggle case of characters in a string.

    #include

    #include

    #include

    void strtoggle(char str[])

    { int i;

    for(i=0;str[i]!='\0';i++)

    {

    if(str[i]>='a' && str[i]='A' && str[i]

  • 7/31/2019 OOPs Program File

    79/84

    79

    Naman Garg 3rd

    SEM ECE UE105050

    OUTPUT

  • 7/31/2019 OOPs Program File

    80/84

    80

    Naman Garg 3rd

    SEM ECE UE105050

    PROGRAM-23

    Q- WAP to sort the cities entered by the user.

    #include

    #include

    #include

    #include

    void main()

    {

    clrscr();

    char arr[15][30], temp[30];

    int no;

    coutno;

    for(int i=0;i

  • 7/31/2019 OOPs Program File

    81/84

    81

    Naman Garg 3rd

    SEM ECE UE105050

    }

    }

    cout

  • 7/31/2019 OOPs Program File

    82/84

    82

    Naman Garg 3rd

    SEM ECE UE105050

    OUTPUT

  • 7/31/2019 OOPs Program File

    83/84

    83

    Naman Garg 3rd

    SEM ECE UE105050

    PROGRAM-24

    Q- Program to check for password entered by user.

    #include

    #include

    void main()

    {

    clrscr();

    cout

  • 7/31/2019 OOPs Program File

    84/84

    84

    OUTPUT