Header Ads Widget

Ticker

6/recent/ticker-posts

Practical 6 OOP

 PRACTICAL – 6.1

Write  a  C++  program  to  implement  function  overloading  in  order  to  compute  power(m,n)where

(1)m is double and n is int

(2)m and n are int.

#include <iostream>
using namespace std;
class Power
{
public:
    void Pow(double a, int b)
    {
        double ans = 1;
        for (int i = 1; i <= b; i++)
        {
            ans = ans * a;
        }
        cout << a << " Power " << b << " is " << ans << endl;
    }
    void Pow(int a, int b)
    {
        double ans = 1;
        for (int i = 1; i <= b; i++)
        {
            ans = ans * a;
        }
        cout << a << " Power " << b << " is " << ans << endl;
    }
};
int main()
{
    Power r;
    r.Pow(2.5, 3);
    r.Pow(2, 3);
    return 0;
}

PRACTICAL –  6.2

Create a function called reverse () that takes two parameters. The first parameter, called str is  a  pointer  to  a  string  that  will  be  reversed  upon  return  from  the  function. The  second parameter is called count, and it specifies how many characters of str to reverse. Give count a default value that, when present, tells reverse () to reverse the entire string.

#include <iostream>
#include <string>
using namespace std;
class Str
{
public:
    void string_ret(string &s, int a)
    {
        a -= 1;
        for (int i = 0; i < a; i++)
        {
            char c;
            c = s[a];
            s[a] = s[i];
            s[i] = c;
            a--;
        }
    }
};
int main()
{
    Str r;
    string j;
    int num;
    cout << "Enter String : ";
    cin >> j;
    cout << "Enter Term Which You want to reverse : ";
    cin >> num;
    if (num <= j.length())
    {
        r.string_ret(j, num);
    }
    else
    {
        cout << "Enter Valid Input" << endl;
    }
    cout << j << endl;
    return 0;
}

PRACTICAL – 6.3

Write  a  program  to  demonstrate  the  use  of  arrays  within  a  class.Create  and  manage  an inventory system

#include <iostream>
#include <string>
using namespace std;
class Inventory
{
    string item[100];
    int cost[100];

public:
    void setdata(int product_num)
    {
        cout << "Enter Product name : ";
        cin >> item[product_num];
        cout << "Enter Cost : ";
        cin >> cost[product_num];
    }
    void getdata(int product_num)
    {
        cout << "Enter Product name : " << item[product_num] << endl;
        cout << "Enter Cost : " << cost[product_num] << endl;
    }
};
int main()
{
    Inventory r;
    r.setdata(1);
    r.getdata(1);
    return 0;
}

Post a Comment

0 Comments