C + + Express, suitable for C-base friends (3)

Source: Internet
Author: User

7. Strings and vectors
A. String is an array type
%1.c string values and C string variables
Assuming that the string is "Hello", you need a character array of 6 cable variables, and the last position is used to hold '/'. ' s ' is a null character that is used to characterize the end of a string.
Note: The following two declaration methods are not consistent:
Char short_string[] = ' abc ';
Char short_string[] = {' A ', ' B ', ' C '};
The former will have a ' after ABC ', but the latter is not.
Note: Char a[3];
A= "ER"; This statement is illegal. For a string, you cannot assign a value in this way except when you can assign a value in the declaration.
To invoke the string function provided by C + +, you need # include <cstring>
The Atoi function can convert a string to a number.  Eg:atoi ("1234"); An integer of 1234 is returned. #include <cstdlib> required for use
B. Standard String class
%1. Introduction to the Standard string class
To use the string class, you must include:
#include <string>
using namespace Std;
How to declare:
string noun = "name";
phrase = "I love" + noun + "!";
String s1, s2 ("Hello");
I/O for the%2.string class
Consider the following code
string C1, C2;
CIN >> C1;
CIN >> C2;
If user input: May the hair on your toes grow long and curly!
So S1 get May,s2 get the;
If you want the program to read the entire line input into a variable of type string, you can use the Getline function. Under #include<string>: getline usage is different and cannot be used Cin.getline
Instead: string line;
Getline (Cin,line);
There may be a problem with the mix of CIN and getline.
%3. String processing can be done with the strings class
The string class contains member functions that can be used to simplify the processing of strings. String, which can be accessed in the form of an array.
%4. Convert the string class to a C string:
Eg:char a_c_string[];
String string_variable = "CString";
strcpy (A_c_string,cstring.c_str ());
C. Vectors:
%1. Basic knowledge of vectors
In order to declare the variable V as a vector of the base type int, the following:
#include <vector>
Vector<int> v;
The index number of the vector starts at zero, although you can use V[i] to access the value of the corresponding position in the vector, but it cannot be initialized in this way. To add a value for the first time for the corresponding index position, use the Member
function Push_back. To add elements in the vector is to add, first add position 0, position 1, position 2;
Eg:vector<double> sample;
Sample.push_back (0.0);
Sample.push_back (1.1);
Sample.push_back (2.2);
The number of elements in a vector is called the length of the vector (size), and you can use the member function size to determine how many elements are in a vector.

8. pointers and dynamic arrays:
A. Pointer: pointer is the address of a memory variable
%1. Pointer variable
Double *p; A pointer to a variable of type double is declared.
int *p1,*p2,v1,v2; You can declare a pointer along with a normal variable.
The following statement lets the pointer point to a variable:
p1=&v1;
After this, *P1 and v1 have exactly the same properties. Vary
Note the use of * and & for the pointer.                        Always pay attention to the difference between P1=P2 and *P1=*P2. operator new?
%2. Basic memory management
The operator delete can destroy a variable and return the space he occupies to free storage.
Eg:delete p;
You can define pointer types when you are programming:
Eg:typedef int* IntPtr;
INTPTR p; This is equivalent to the int *p;
B. Dynamic array dynamic array is a writing program when the length of an array is not specified, and his length is determined when the program is run.
%1. Array variables and pointer variables
int a[10];
int *p;
P=a;
P[0],P[1],... With A[0],a[1],... Have the same effect.
%2. Creating and using dynamic arrays
Double *a;
A=new Double[array_size]; Array_size is a variable of type int, and its value is given in the process of running the program.
delete [] A; Destroys a dynamic array.

9. Defining Classes
A. Structure
%1. Structure for heterogeneous data:
The structure is defined as follows:
struct Cdaccount
{
Double balance;
Doubel interest_rate;
int term;
};//Notice the semicolon in this place.
Where the keyword struct declares this is a struct type definition. The identifier Cdaccount is the name of the struct type. Two or more struct types can use the same structure name.
%2. struct as function parameter
For a function, it can have a value invocation parameter of a struct type, or a reference parameter of a struct type.
The return value of a function can also be a struct.
Eg:cdaccount Shrink_warp (double the_balance,double the_rate,int the_term)
{
Cdaccount temp;
...
return temp;
}
%3. Initialize the struct
struct Date
{
int mouth;
int day;
int year;
};
Date due_date = {09,06,2015};
B. class
%1. Defining class and member functions
A class is a data type whose variables are objects.
Eg:class DayOfYear
{
Public
void output ();
int day;
int month;
}; Note the semicolon here
The void function can be defined in a C + + file and is defined in the same way
void Dayofyear::output ()
{
cout << "month=" << Month
<< "day=" << day << Endl;
}
How to declare a class: DayOfYear today;
The way to call a function is Today.output ();
%2 Public members and private members
Private members are not able to access them directly anywhere in the program unless they are within the definition of a member function. Private members are defined in the following way:
Class DayOfYear
{
Public
void output ();
int getday ();
int getmonth ();
Private
void Check_date (); Private member functions
int day; Private member variable
int month; Private member variable
};
The following are some tips for programming classes:
First. Set all the member variables to be private;
Second. Write accessor functions for each class get and assign function set;
%3. Use an operator with an object:
DayOfYear Due_date,tomorrow;
Based on this definition, the following statements are valid:
Due_date=tomorrow; The private variables will also be assigned values.
%4. Constructors for initialization
When declaring an object, it is often necessary to initialize some or all of its variables. To do this, when you define a class, you define a special function that becomes a constructor. A constructor is a member function that declares a function
is automatically invoked. The constructor is used to initialize the value of the variable and to perform any initialization operations that may be required. Here are a few things to keep in mind when using constructors:
First: The constructor must have the same name as the class.
Second: the definition of a constructor cannot return a value. Also, in the beginning of a function declaration or in a function header, you are not allowed to specify a return type (even void).
Third: The constructor should be placed in the public section.
The initialization constructor can be declared in the following way:
Bankaccount::bankaccount (): Balance (0), interest_rate (0.0)
{
The subject intentionally left blank;
}
For such a definition: The method of initialization should be BankAccount account; This is the right thing to do.
Or:
Bankaccount::bankaccount (int dollar,int cents,double rate): Balance (dollars+0.01*cent), interest_rate (rate)
{
if ((dollar<0) | | | (cents<0) | | (rate<0))
{
Exit (0);
}
}
C. Abstract data types
%1. class for generating abstract data types
Data types consist of a collection of values and a series of basic operations defined by those values. If a programmer using a data type cannot access the details of values and operations, this data type is called abstract data class
Type (ADT);
The type of programmer's definition does not automatically become ADT, and it is best to implement all of the programmer's defined data types as ADT. In order for a class to become ADT, a class must be defined in a specific way.
In order to define a class as an abstract data type, you need to strictly differentiate between the way the programmer uses the type and the implementation details of that class.
In order to achieve this, it is necessary to observe the following principles:
First: Set all member variables to private variables.
Second: Set each basic action required by the user programmer as a public member function and fully specify how each public member function is used.
Third: Set any auxiliary functions as private member functions.
D. Inheritance
Inheritance relationships between stream classes:
One of the most powerful features of C + + is the use of derived classes.
When we say that Class A is a derived class of Class B, it means that class A has all the attributes of Class B, but it may also add some new features. Ifstream is a derived class of IStream.
void Say_hello (ostream& any_out_stream)
{
Any_out_stream << "Hello";
}
This can be used in the following ways:
Ofstream Fout;
Fout.open ("afiled.at");
Say_hello (cout); This is used because Ofstream is a derived class of ostream.
Say_hello (Fout);

C + + Express, suitable for C-base friends (3)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.