C + + Learning notes __c++

Source: Internet
Author: User
Tags class definition comparable function prototype inheritance

L A pointer to a constant.

Inta;

Const int *p1 = &a; P1 is a pointer to a constant

INTB;

p1= &b; Correct, the value of P1 itself can be changed

P1= 1; Compilation error, cannot change the value of the object by P1

L A constant for a pointer type.

INT * Const P2 = &a;

p2&b; Error, p2 is a pointer constant, value cannot be changed

the L void type pointer can store an object address of any type.

L 0 is dedicated to representing a null pointer, which is a pointer that does not point to any valid address.

Int *p; declares an int type pointer p.

P = 0; Set p to a null pointer, without pointing to any address.

Ø details: null pointers can also be represented by NULL, which is a number of header files

Defines a macro that is defined as 0.

L Copy Constructors

A copy constructor is a special constructor that has all the characteristics of a general constructor, and its shape

The parameter is a reference to an object in this class. The effect is to initialize a new object of the same class using an already existing object (specified by the parameters of the copy constructor).

The following are general methods for declaring and implementing a copy constructor:

Class name

{

Public:

Class name (formal parameter list); Constructors

Class name (class Name & object name); Copy Constructors

}

Class Name:: Class name (class Name & object name)//replication constructor implementation

{function body

}

The copy constructor is invoked in the following three cases:

1. When you initialize another object of the class with one object of the class:

Intmain ()

{

Point A (1, 2);

Point B (a); Initializes object B with object A, and the copy constructor is called

Point C = A; Initializes object C with object A, the copy constructor is called

Cout<<b.getx () <<endl;

return 0;

}

2. If the function's formal parameter is an object of the class, when the function is invoked, the formal parameter and the argument are combined:

VOIDF (Point P)

{

Cout<<p.getx () <<endl;

}

Intmain ()

{

Point A (1, 2);

f (a); The parameter of the function is the object of the class, and when the function is called, the copy constructor calls the

return 0;

}

3. If the return value of the function is an object of the class, the function executes when it completes the return of the caller:

Point G () {

Point A (1, 2);

Returna;

}

Int Main () {

Point B;

The return value of the function is the object of the class, and when the function value is returned, the copy constructor is called

b = g ();

return 0;

}

The L- friend Relationship provides a member function of a class or object, a member function of a class, and a general function

Data sharing mechanism between the two. In layman's parlance, a friend relationship is a class that proactively declares which other classes or functions are its friends and gives them access to this class.

1. Friend function

A friend function is a non-member function that is decorated with a keyword friend in a class. A friend function can be a

A normal function, or it can be a member function of another class. Although it is not a member function of this class, the private and protected members of the class can be accessed through the object name in its function body.

2. Friend class

Like a friend function, a class can declare another class as a friend relationship. If Class A is of type B

Friend class, all member functions of a class are friend functions of Class B, and both private and protected members of Class B can be accessed. The syntax for declaring a friend class is:

Class B

{

The member declaration of the//b class.

Friend Class A//Declare a friend of B

}

L Virtual base class

The declaration of a virtual base class is performed during the definition of a derived class, in the form of the following syntax:

Class derived class Name:: Virtual Inheritance Method base class name

In multiple inheritance situations, the virtual base class keyword has the same scope and inheritance method keywords, and only works on the base class immediately following it. After a virtual base class is declared, members of the virtual base class maintain a copy of the same memory data together with the derived class.

L Access Control

1. Public inheritance

When the inheritance of a class is publicly inherited, the public members of the base class and the protected Access property are in the derived class

, while private members of the base class are not directly accessible.

2. Private inheritance

When a class inherits as a private inheritance, both the public and the protected members of the base class are given private identities

You are now in a derived class, and the private members of the base class are not directly accessible in the derived class.

3. Protection of inheritance

In protected inheritance, both public and protected members of the base class appear in the derived class as protection members

, and private members of the base class are not directly accessible.

L operator Overloading

An overload of an operator has two forms, that is, that the overload is a non-static member function of the class and that the overload is not a member

Function. Operator overloading is the general grammatical form of a member function of a class:

return type operator operator (formal parameter list)

{

function body

}

Operator overloading is a general grammatical form of a non-member function:

return type operator operator (formal parameter list)

{

function body

}

The return type specifies the return value type of the overloaded operator, that is, the operator result type; operator is the keyword that defines the operator overload; the operator is the name of the operator to overload, and must be an overloaded operator in C + +, for example, to overload the addition operator, which is written here + The parameters and types required by the overloaded operator are given in the formal parameter list.

L virtual function

The declaration syntax for general virtual function members is:

virtual function type function name (formal parameter list);

This is actually the use of the virtual keyword in the definition of the class to qualify the member function, the virtual function sound

The Ming can only appear in the function prototype declaration in the class definition, not when the member function is implemented.

In the running process, the polymorphism needs to meet 3 conditions, the first is to meet the assignment compatibility rules between classes, the second is to declare virtual functions, and the third is to call by member functions or through pointers, references to access virtual functions.

L Pure virtual functions and abstract classes

Declaration format for pure virtual functions:

virtual function type function name (parameter table) = 0;

In fact, it differs from the prototype of a general virtual function member in the writing format in that it adds "= 0". Once declared as a pure virtual function, the implementation part of the function can no longer be given in the base class. The function body of a pure virtual function is given by a derived class.

A class with pure virtual functions is an abstract class. The main function of an abstract class is to create a common interface for a class family that enables it to perform polymorphic properties more efficiently.

An abstract class cannot be instantiated, that is, an object of an abstract class cannot be defined, but a pointer and reference to an abstract class can be defined.

L function Templates and class templates

The so-called parametric polymorphism is to parameterize the types of objects processed by the program so that a certain process

The order can be used to handle many different types of objects.

Function templates are defined in the form of:

template function name (parameter table)

{

Definition of function body

}

Class templates enable users to define a pattern for a class that allows certain data members in a class, parameters of certain member functions, return values, or local variables to be arbitrary types, including system-predefined and user-defined.

The syntax form for a class template declaration is:

Template < template parameter table >

Class name

{

Class member declaration

}

If you need to define its member functions outside of the class template, use the following form:

template< template Parameter Table >

Type name Class name < template parameter Identifier list:: Function name (parameter table)

A class template declares itself to be not a class, it describes a family of classes. The template generates a specific class based on the needs of the reference only if it is referenced by another code. Use a template class to create objects that should be declared in the following format:

Template name < template parameter Table > object name 1,..., object name N;

Basic concepts of generic programming

We can use the concept (concept) to describe the data required by the generic programming as Parameters

the function. The "concept" here is a term for generic programming, which is the content of these functions, and its extension is all the data types that have these features. For example: "All data types that can be used in larger than size, have public copy constructors, and can be assigned with ' = ' are a concept that can be written as sortable." a data type that has the function required by a concept is called the model of this concept. for example, the int data type is a model of the sortable concept. For two different concepts A and B. If all the functionality required by concept A is also the function required by concept B (i.e. the model of concept B must be the model of concept a), then concept B is the sub model of concept a. The concept of "all data types that can be larger than the size" is comparable, so sortable is the comparable concept.

L Adapter

An adapter (adapter) is an object that provides a new interface for an existing object, and the adapter itself is generally

Does not provide new functionality, but exists only to change the interface of an object.

L typeid

typeID is used to return the actual type of the pointer or reference to the object. Note: typeID is an operator,

is not a function. Run-time to know the variable type name, you can use the typeid (variable). Name (), you need to be aware that not all compilers output "int", "float," and so on, for this type of compiler can be used: float f = 1.1f; if (typeID (f) = = typeID (0.0f)).

Overview of the L- STL

The Standard Template Library (Standard Template Library,stl) was originally made by HP company Alexander

Stepanov and Menglee developed a template library to support C + + generic programming, which was incorporated into the C + + standard in 1994 as part of the C + + standard library.

Four basic components of STL:

1. Containers (container)

A container is an object that holds and contains a set of elements. The Container class Library includes 7 basic containers: vectors (vector), two-terminal queues (DEPUE), lists (list), set (set), multiple sets (Multiset), mappings (map), and multiple mappings (Multimap). These 7 containers can be divided into two basic types: sequential containers (Sequence container) and associated containers (associative container). A sequential container organizes a set of elements of the same type in a strict linear fashion, which is the vector, the two-terminal queue, and the list container. Associative containers have the ability to quickly extract elements based on a set of indexes, and the collection and mapping containers belong to this one.

2. Iterator (iterator)

Iterators provide a way to sequentially access each element in a container. For iterators, you can use the "+ +" operator to get an iterator that points to the next element, and you can use the "*" operator to access the element that an iterator points to.

3. Function Object (functionobject)

A function object is an object that behaves like a function, and it can be called like a function call. Any ordinary function and any object that overloads a class of the "()" operator can be used as a function object, and the function object is a generic function.

4. Algorithm (algorithm)

STL contains more than 70 algorithms, these algorithms cover a considerable range of applications, including search algorithm, sorting algorithm, elimination algorithm, counting algorithm, comparison algorithm, transform algorithm, permutation algorithm and container management. One of the most important features of these algorithms is their uniformity, and can be widely used in different objects and built-in data types.

Characteristics comparison of 3 kinds of sequential containers in STL

Operation

vectors (vector)

two-terminal queue (deque)

lists (list)

Random Access

Fast

More slowly

No

Head Insert

(Push_front)

No Push_front.

Can only be done with insert

Fast

Existing iterator fails, pointer, reference not invalidated

Fast

Existing iterators, pointers, references are not invalidated

Head deletion

(push_back)

No push_back.

Can only be done with erase.

Fast

Only the elements that are deleted are

Iterator, pointer, reference invalidation

Tail Insert

(push_back)

Fast

When the container extension occurs, all existing iterators, pointers, references are invalidated;

Otherwise, no existing iterators, pointers, references are affected.

Fast

Existing iterator fails, pointer, reference not invalidated

Fast

Existing iterators, pointers, references are not invalidated

Tail Delete

(Pop_back)

Fast

Only causes the iterator, pointer, and reference of the deleted element to fail

Any location

Inserting (insert)

The nearer the insertion position, the slower the head. When a container extension occurs, all iterators, pointers, references are invalidated, otherwise the iterator, pointer, reference after the insertion position is invalidated

The closer the position is, the slower the middle.

Causes all iterators, pointers, references to fail

Fast

Existing iterators, pointers, references are not invalidated

Any location

Delete (erase)

The closer the deletion position, the slower the head. Invalidates only the iterator, pointer, reference after the location of the deletion

Fast

Only causes the iterator, pointer, and reference of the deleted element to fail

Related Article

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.