Saturday, 12 October 2013

C++ course(ninth section)

C++(tninth section)
ninth section(user-defined data types,and the "string" type)

Enumeration Type

Data type - a set of values together with a set of operations on those values
To define a new simple data type, called enumeration type, we need three things:
−A name for the data type
−A set of values for the data type
−A set of operations on the values.
A new simple data type can be defined by specifying its name and the values, but not the operations 
The values must be identifiers.

The syntax for enumeration type is:
value1, value2, … are identifiers called enumerators
value1 < value2 < value3 <...
enum typename {value1,value2,....};
Enumeration type is an ordered set of values 
If a value has been used in one enumeration type 
−It cannot be used by another in the same block 
The same rules apply to enumeration types declared outside of any blocks.

Example:
enum sports {football,basketball,baseball};
sports s;
Assignment
The statement:
popularSport = FOOTBALL;
stores the word FOOTBALL into popularSport
The statement:
mySport = popularSport;
copies the contents of the variable popularSport into mySport.
Operations
No arithmetic operation is allowed on enumeration types
The following statements are illegal:
mySport=s+2;
s=football+basketball;
The increment and decrement operations are not allowed on enumeration types
The following statements are illegal:
s++;
s--;
The string Type
To use the data type string, the program must include the header file <string>
The statement
string name = "William Jacob";
declares name to be a string variable and also initializes name to "William Jacob"
The first character in name, 'W', is in position 0, the second character, 'i', is in position 1, and so on.
The variable name is capable of storing any size string
Binary operator + (to allow the string concatenation operation), and the array subscript operator [], have een defined for the data type string
For example, if str1 = "Sunny", the statement stores the string "Sunny Day" into str2:
str2 = str1 + " Day";
length Function
Length returns the number of characters currently in the string 
The syntax to call the length function is: 
strVar.length()
where strVar is variable of the type string
length has no arguments 
length returns an unsigned integer 
The value returned can be stored in an integer variable.

cout<<variable.length();
size Function
The function size is the same as the function length
Both functions return the same value
The syntax to call the function size is:
strVar.size()
where strVar is variable of the type string
As in the case of the function length, the function size has no arguments.
find Function
find searches a string for the first occurrence of a particular substring
Returns an unsigned integer value of type string::size_type giving the result of the search
The syntax to call the function find is:
strVar.find(strExp)
where strVar is a string variable and strExp is a string expression evaluating to a string
The string expression strExp can also be a character.
If successful, find returns the position in strVar where the match begins 
For the search to be successful, the match must be exact 
If unsuccessful, find returns the special value string::npos (“not a position within the string”).

substr Function
substr returns a particular substring of a string 
The syntax to call the function substr is: 
strVar.substr(expr1,expr2)
where expr1 and expr2 are expressions evaluating to unsigned integers.

The expression expr1 specifies a position within the string (starting position of the substring)
The expression expr2 specifies the length of the substring to be returned.
swap Function
swap interchanges the contents of two string variables
The syntax to use the function swap is
strVar1.swap(strVar2);
where strVar1 and strVar2 are string variables
Suppose you have the following statements:
string str1 = "Warm";
string str2 = "Cold";
After str1.swap(str2); executes, the value of str1 is "Cold" and the value of str2 is "Warm.

Unknown

Author & Editor

Has laoreet percipitur ad. Vide interesset in mei, no his legimus verterem. Et nostrum imperdiet appellantur usu, mnesarchum referrentur id vim.

0 comments:

Post a Comment

 
biz.