Header Ads Widget

Ticker

6/recent/ticker-posts

Practical 2 OOP

 PRACTICAL – 2.1

Write  a  program  to  find  the  largest  of  three  integers  using  a  swap function.  The  function accepts integer arguments by reference

#include <iostream>

using namespace std;
void swap(int *num1, int *num2)
{
    int temp = *num1;
    *num1 = *num2;
    *num2 = temp;
}
void max(int x, int y, int z)
{
    int max_num;
    if (x > y && x > z)
    {
        swap(&y, &z);
        swap(&x, &y);
    }
    else if (y > z)
    {
        swap(&x, &y);
        swap(&y, &z);
    }
    else
    {
        swap(&z, &x);
    }
    cout << "Number in decending order : " << x << y << z;
}
int main()
{
    max(1, 5, 3);
    return 0;
}

PRACTICAL – 2.2

Design classes named Triangle, Square, and Circle. Make the different function in each class to find areas of particular shape

#include <iostream>
using namespace std;
class Triangle
{
    int bse, hig;

public:
    Triangle(int num1, int num2) : bse(num1), hig(num2)
    {
        cout << "Base and Height saved sucessfully : " << bse << " " << hig << endl;
    }
    double area()
    {
        double area = (bse * hig) / 2;
        return area;
    }
};
class Circle
{
    int rad;

public:
    Circle(int a) : rad(a)
    {
        cout << "Radius saved sucessfully : " << rad << endl;
    }
    double area()
    {
        double area_circle = (3.14) * rad * rad;
        return area_circle;
    }
};
class Squre
{
    int len;

public:
    Squre(int a) : len(a)
    {
        cout << "Length saved sucessfully : " << length << endl;
    }
    int area()
    {
        int are_squ = len * len;
        return are_squ;
    }
};
int main()
{
    Triangle x(2, 4);
    cout << "Area of Triangle " << x.area() << endl;
    Circle y(4);
    cout << "Area of Circle " << y.area() << endl;
    Squre z(9);
    cout << "Area of Squre " << z.area() << endl;
    return 0;
}
PRACTICAL – 2.3 Create a class with string pointer as data member and member functions:
#include <iostream>
using namespace std;
class abc
{
    char *ptr, b[20];

public:
    void getdata()
    {
        cout << "enter any string:";
        cin >> b;
    }
    void display()
    {
        ptr = b;
        cout << "you entered:";
        while (*ptr != '\0')
        {
            cout << *ptr;
            ++ptr;
        }
    }
} obj;
int main()
{
    obj.getdata();
    obj.display();
    return 0;
}

Post a Comment

0 Comments