Header Ads Widget

Ticker

6/recent/ticker-posts

Practical 14 OOP

PRACTICAL – 14.1

Use various flags and bit fields to produce formatted output.

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
    cout.fill('*');
    cout.setf(ios::right, ios::adjustfield);
    cout.width(10);
    cout << "VALUE" << endl;

    cout.fill('_');
    cout.setf(ios::right, ios::adjustfield);
    cout.width(10);
    for (int i = 0; i < 100; i++)
    {
        cout.fill('_');
        cout.width(3);
        cout << i;
        cout.width(10);
        cout.precision(3);
        cout << sqrt(i) << endl;
    }
    return 0;
}

PRACTICAL –  14.2

Write a program to use manipulators setw, setiosflags and setprecision for formatted output.

 

#include <iostream>

#include <iomanip>
using namespace std;

ostream &number(ostream &output)
{
    output << setfill('-')
           << setw(15)
           << setiosflags(ios::right)
           << setiosflags(ios::fixed);
    output << "Number = ";
    return output;
}
ostream &squre(ostream &output)
{
    output << setfill(' ')
           << setw(15)
           << setiosflags(ios::right)
           << setiosflags(ios::fixed);
    output << "Squre = ";
    return output;
}
int main()
{

    for (int i = 0; i < 10; i++)
    {
        cout << number << i << squre << i << endl;
    }
}

PRACTICAL – 14.3

Write  a  program  to  read  a  list  containing  item  name,  item  code,  and  cost  interactively  and produce a three column output as shown below.

NAME            CODE            COST

TURBOC++   1001               250.95

C Primer       905                 95.70

….                                      

                                       


#include <iostream>
#include <iomanip>
using namespace std;

class manage
{
    char *p;
    int code;

    float cost;

public:
    manage()
    {
        p = new char[20];

        cout << "Enter Item name:";
        cin.ignore();
        cin.getline(p, 19);
        cout << "Enter Item Code:";
        cin.ignore();
        cin >> code;
        cout << "Enter Item Cost:";
        cin >> cost;
        cout << endl;
    }

    ~manage()
    {
        delete[] p;
    }

    void Display()
    {
        cout << setw(15);
        cout.setf(ios::left, ios::adjustfield);
        cout << this->p;
        cout << setw(5);
        cout.setf(ios::right, ios::adjustfield);
        cout << this->code;
        cout << setw(15) << setprecision(2);
        cout.setf(ios::showpoint);
        cout.setf(ios::fixed, ios::floatfield);
        cout << this->cost << endl;
    }
};
void indexDisplay()
{
    cout << setw(15) << setiosflags(ios::left) << "NAME";
    cout << setw(5);
    cout.setf(ios::right, ios::adjustfield);
    cout << "CODE";
    cout << setw(15) << "COST" << endl;
}
void horizontalLine()
{
    cout << setw(40) << setfill('-') << "\n";
    cout.fill(' ');
    cout << endl;
}
int main()
{
    int size;
    cout << "How many products do you wants to enter:";
    cin >> size;
    manage qty[size];
    horizontalLine();
    indexDisplay();
    horizontalLine();
    for (int i = 0; i < size; i++)
        qty[i].Display();
    horizontalLine();
    return 0;
}

Post a Comment

0 Comments