0% found this document useful (0 votes)
26 views

Functions of C++

1. The document discusses various formatting functions in C++ like setiosflags, setprecision, and setw that can format numbers. 2. It also discusses using arrays as class member data and defines an array size enum or constant within a class. 3. The standard C++ string class is introduced which handles memory management and allows operator overloading for concatenation.

Uploaded by

sayadkhan0555
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Functions of C++

1. The document discusses various formatting functions in C++ like setiosflags, setprecision, and setw that can format numbers. 2. It also discusses using arrays as class member data and defines an array size enum or constant within a class. 3. The standard C++ string class is introduced which handles memory management and allows operator overloading for concatenation.

Uploaded by

sayadkhan0555
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

CSE 4301

Assigntment:1(Newly introduced functions of C++ )


Name: Sayad Ibna Azad
ID:210041251
Formatting Functions
Number Formatting:
A number of one-bit formatting indicators in a long int in the ios class fixates how
formatting will be carried out.
Example:
cout << setiosflags(ios::fixed) //fixed (not exponential)
<< setiosflags(ios::showpoint) //always showing the decimal point
<< setprecision(2) //two decimal places that is setting the precesion
<< setw(10) //field width 10
<< fpn; //finally, the number

Some Other formatting functions:

setw ,setprecision ,fixed ,scientific


Array as class member data
 Arrays can be utilized in classes as data elements.
Will refer to book example stakaray.cpp
Keeping the concept of object oriented programming, we cannot use a macro Max
outside the class. Defining the max array size inside class can be done using
 Enum
 #define inside the class (May not work on some compilers)
The size of the array used for the stack is specified by MAX, in the statement
enum { MAX = 10 }; //this defines a macro MAX that translates to 10.
 Standard C++ mandates that we should be able to declare MAX within the class as
static const int MAX = 10;
 This means that MAX is constant and applies to all objects in the class. Unfortunately,
some compilers, including the current version of Microsoft Visual C++, do not allow
this newly approved construction.
Array of Objects in C++

 Similar to any array declaration , we create an array of the type Distance and give it a size of
100. i.e Distance dist[100].Array bound checking falls on programmer choice and can be
restricted using conditions such as :
 if( n >= MAX )
{
cout << “\nThe array is full!!!”;
break;
}
This causes a break out of the loop and prevents the array from overflowing.
Accessing objects in an array

Using the previous code example, to the left is a visualization of


the array of objects.
A member function of an object that is an array element is
accessed using the dot operator. The array name followed by the
index in brackets is joined, using the dot operator, to the member
function name followed by parentheses. This is similar to
accessing a structure
(or class) data member, except that the function name and
parentheses are used instead of the data name.

dist[j].showdist();
The standard C++ string Class

 Standard C++ includes a new class called string. This class improves on the
traditional C-string in many ways.
 The string class assumes all the responsibility for memory management. Also,
the string class allows the use of overloaded operators, so we can
concatenate string objects with the + operator:
s3 = s1 + s2
Defining and Assigning string Objects

 We can use a constructor with no arguments, creating an empty string.


 The sstrass.cpp code file in the book shows this in action.
string s3 creates an object s3 of the class string. This creates an object using default
constructor with no argument, creating an empty string.
 The string class uses a number of overloaded operators.
 The overloaded + operator concatenates one string object with another. The statement
s3 = “Neither “ + s1 + “ nor “;
 We can also use the += operator to append a string to the end of an existing string.
The statement
s3 += s2;
appends s2, which is “Beast”, to the end of s3, producing the string “Neither Man nor Beast”
and assigning it to s3.
 string class member function: swap(), which exchanges the values of two string objects.
It’s called for one object with the other as an argument.
s1.swap(s2);
 applying it to s1 (“Man”) and s2 (“Beast”), s1 is now “Beast” and s2 is now “Man”.
Input/output with string Objects
 Input and output are handled in a similar way to that of C-strings. The << and >>
operators are overloaded to handle string objects, and a function getline() handles input
that contains embedded blanks or multiple lines.
 cout << “Enter your full name: “;
getline(cin, full_name); //reads embedded blanks
 getline(cin, address, ‘$’); //reads multiple lines and terminates by $.
 cout << “Enter your nickname: “;
cin >> nickname; //input to string object called nickname.
embedded blanks can be read in using getline(). This function is similar to the get()
function used with C-strings, but is not a member function.
 There is a variation of getline(), with three arguments, that can be used to read the
user’s address, which may require multiple lines. The third argument specifies the
character to be used to terminate the input.
Modify string Objects

 There are various ways to modify string objects.


 The erase() function removes a substring from a string. Its first argument is the
position of the first character in the substring, and the second is the length of
the substring.
 The replace() function replaces part of the string with another string. The first
argument is the position where the replacement should begin, the second is the
number of characters in the original string to be replaced, and the third is the
replacement string.
 The insert() function inserts the string specified by its second argument at the
location specified by its first argument.
 In the append() function, the first argument is the number of characters
to append, and the second is the character to be appended.
Comparing string Objects
 We can use overloaded operators or the compare() function to compare string
objects. These discover whether strings are the same, or whether they precede or
follow one another alphabetically.
 Referring to sstrcom.cpp ;
 In the first part of the program the == and < operators are used to
determine whether a name typed by the user is equal to, or precedes or
follows alphabetically, the name George.
 In the second part of the program the compare() function compares only the first
two letters of “George” with the first two letters of the name typed by the user
(userName). The arguments to this version of compare() are the starting position
in userName and the number of characters to compare, the string used for
comparison (aName), and the starting position and number of characters in aName.
 the substr() member function. It returns a substring of the string for which it was
called. Its first argument is the position of the substring, and the second is the
number of characters.
Accessing Characters in string Objects
 Individual characters within a string object can be accessed in several ways.
 We can also use the overloaded [] operator, which makes the string object
look like an array. However, the [] operator doesn’t warn you if you attempt
to access a character that’s out of bounds (beyond the end of the string, for
example). The [] operator behaves this way with real arrays, and it’s more
efficient. However, it can lead to hard-to-diagnose program bugs. It’s safer
to use the at() function, which causes the program to stop if we use an out-
of-bounds index.
 The copy() member function to copy a string object into an array of type
char, effectively transforming it into a C-string. Following the copy, a null
character (‘\0’) must be inserted after the last character in the array to complete
the transformation to a C-string.
Other string Functions
 The capacity() member function returns the actual memory occupied.
 The max_size() member function returns the maximum possible size
of a string object.This amount corresponds to the size of int variables
on the system, less 3 bytes.
 Note: String objects are not terminated with a null or zero as C-
strings
 are. Instead, the length of the string is a member of the class. So if
we are stepping along the string, we can’t rely on finding a null to tell
us when we’ve reached the end.

You might also like