Object Oriented Programming - 23


Syntax:

cout<<parameter_1<<parameter_2<<...<<parameter_n;

In which: parameters can be of type integer, real, character, character string

Function: Output data to the screen. Before using cout and the output operator, you can use some formatting methods such as: width, precision, fill.

Similar to input operators, we can also define output operator overloads on self-defined data, for example, object type data. We add to example 5.1

Define overloading of the export operator (<<) for the multiclass object ts and complete the program as follows:

Example 5.2:

#include<iostream.h>

#include<conio.h>

#include<string.h> struct ts

{

char sbd[5]; char name[30];

unsigned int score; public:

friend ostream &operator<<(ostream &os, const ts &t)

{

cout<<"nName:"<<sbd; cout<<"Name and surname:"<<s.name; cout<<"Total score:"<<s.score; return os;

}

friend istream &operator>>(istream &is, ts &t)

{

cout<<"Name:";

is.getline(t.sbd,5);//method used to enter character string cout<<"Full name:";

is.getline(name,30); cout<<"Total score:";


is>>t.diem; is.ignore(); return is;

}

};

void main()

{

ts ds[100]; int n,i;

cout<<"Enter candidate number:"; cin>>n;

cin.ignore(1);//clear keyboard cache for(i=1;i<=n;i++)

{

cout<<"Enter information about the candidate "<<i<<":n"; cin>>ds[i];

}

cout<<"nList of candidatesn"; for(i=1;i<=n;i++) cout<<ds[i]<<"n"; getch();

}


5.4. Formatting methods


5.4.1. Output value format content

The format content defines the parameters:

- Specified width

- Accuracy

- Filler characters

- And other parameters

Actual width of output value:


C++ will convert the output value into a character string and then display this string on the screen. We will call the number of characters in this string the actual width of the output value. For example, with the following statements:

int n = 4567, m = -23 ; float x = -3.1416 ;

char ht[] = Tran Van Thong ;

then the actual width is 4, the height is 3, the width is 7, and the height is 14.

Specified width:

It is the minimum number of positions on the screen reserved for printing a value. By default, the width is set to 0. We can use the cout.width() method to set this width. Example statement:

cout.width(8);

will set the specified width to 8.

Relationship between actual width and specified width:

- If the actual width is greater than or equal to the specified width, the number of positions on the screen containing the output value will be equal to the actual width.

- If the actual width is smaller than the specified width, the number of positions on the screen containing the output value will be equal to the specified width. There will be some extra positions. The extra positions will be padded (filled) with padding characters.

Define padding characters:

The default filler character is a space (blank). However, it is possible to use the cout.fill() method to select a different filler character. For example, with the following statements:

int n=123; // Actual width is 3 cout.fill( * ); // Filler character is * cout.width(5); // Specified width is 5 cout << n ;

then the printed result is:

**123

Accuracy:


The number of decimal places (when printing real numbers). The default precision is 6. However, the cout.precision() method can be used to select the precision. For example, with the following statements:

float x = 34.455 ; // Actual width 6 cout.precision(2) ; // Precision 2 cout.width(8); // Conventional width 8 cout.fill( 0 ) ; // Fill character is 0 cout << x ;

then the printed result is:

0034.46


5.4.2. Formatting methods

Syntax: int cout.width()

Function: Indicates the current specified width.

Syntax: int cout.width(int n);

Function : sets the new specified width to n and returns the previous specified width

there.

Note : The specified width n is only effective for one output value. After that, C++ applies the specified width of 0.

For example with the statements: int m =1234, n = 56; cout << nAB cout.width(6);

cout << m ; cout << n ;

then the printed result is:

AB 123456

(There are 2 spaces between B and number 1).


Syntax: int cout.precision();


Function : Indicates the current precision (applied to output formula values).

Syntax: int cout.precision(int n);

Function : sets the precision to n decimal places and specifies the previous precision. The set precision will remain in effect until a new precision setting statement is encountered.

Syntax: char cout.fill();

Function : Indicates the current padding character being applied.


Syntax: char cout.fill(char ch);

Function : Specifies that the new padding character will be ch and specifies the padding character.

previously used. The set padding character will remain in effect until a new padding character selection statement is encountered.

Example 5.3:

#include <iostream.h>

#include <conio.h> void main()

{


clrscr();

float x=-3.1551, y=-23.45421;

cout.precision(2); cout.fill('*');

cout << "n" ; cout.width(8); cout << x; cout << "n" ; cout.width(8); cout << y; getch();

}


After execution, the program prints the following two lines to the screen:

***-3.16

**-23.45


5.5. Format flags


5.5.1. General concept of flags

Each flag contains one bit. Flags have two states: On - has value 1

Off - has value 0

Flags can be stored in a variable of type long. The following flags are defined in the <iostream.h> file:


ios::left

ios::right

ios::internal

ios::dec

ios::oct

ios::hex

ios::fixed

ios::scientific

ios::showpos

ios::uppercase

ios::showpoint

ios::showbase

Maybe you are interested!

Object Oriented Programming - 23


5.5.2. Uses of the flags

Flags can be divided into groups:

Group 1: includes positioning flags (alignment)

ios::left, ios::right, ios::internal

ios::left flag : When the ios:left flag is enabled, the printed value is on the left side of the specified area, with padding characters behind, for example:

35***

-89**

ios::right flag : When the ios:right flag is enabled, the printed value is on the right side of the specified area.

default, the padding characters come first, for example:

***35

**-89

Note : The ios::right flag is enabled by default.


ios::internal flag : The ios:internal flag has the same effect as the ios::right flag except that the sign (if any) is printed first, for example:

***35

-**89

Example 5.4:

#include <iostream.h>

#include <conio.h> void main()

{


clrscr();

float x=-87.1551, y=23.45421;

cout.precision(2); cout.fill('*');

cout.setf(ios::left); // Enable flag ios::left cout << "n" ;

cout.width(8); cout << x; cout << "n" ; cout.width(8); cout << y;

cout.setf(ios::right); // Enable flag ios::right cout << "n" ;

cout.width(8); cout << x; cout << "n" ; cout.width(8); cout << y;

cout.setf(ios::internal); // // Set the ios::internal flag cout << "n" ;


cout.width(8); cout << x; cout << "n" ; cout.width(8); cout << y; getch();

}


After executing the program, it prints out 6 lines as follows:

-87.16**

23.45***

**-87.16

***23.45

-**87.16

***23.45


Group 2: includes integer format flags

ios::dec, ios::oct, ios::hex

+ When ios::dec is on (default): Integers are printed in base 10

+ When ios::oct is on: Integers are printed in octal form

+ When ios::hex is on: Integers are printed in hexadecimal form

Group 3: includes real number format flags

ios::fixed, ios::scientific, ios::showpoint

Default: The ios::fixed flag is on and the ios::showpoint flag is off.

+ When ios::fixed is on and the ios::showpoint flag is off, the real number is printed in decimal form, the number of fractional digits (after the decimal point) is calculated with a precision of n but when printing, the trailing zeros are omitted.

For example, if the precision n = 4 then:

The real number -87.1500 is printed: -87.15

The real number 23.45425 is printed: 23.4543

Comment


Agree Privacy Policy *