C + + Learning

Source: Internet
Author: User
Tags class definition shallow copy

Http://blog.chinaunix.net/uid-20465760-id-1944082.html

constructor function:

constructor function: Initializes the data member of the object.

An example of a copy constructor explanation:

classcomplex{Private :    DoubleM_real; DoubleM_imag; Public:    //Copy Constructors (also known as copy constructors)//A copy constructor parameter is a reference to the class object itself that copies a new object of that class based on an existing object, typically copying the value of the data member of an existing object into the newly created object in the function//If you do not see a write-copy constructor, the system creates a copy constructor by default, but when there are pointer members in the class, there is a risk that the copy constructor is created by default, for specific reasons, please inquire about "shallow copy", "Deep copy" article discussionComplex (ConstComplex &c) {//Copy the value of the data member in Object C.M_real=C.m_real; M_img=c.m_img;       }  }; 

precedence of multiple-inheritance constructor calls in C + +:

Multiple inheritance is similar to single inheritance, which executes the base class constructor first and then executes from left to right according to the declaration of the derived class.

Http://www.cnblogs.com/hu983/p/5524682.html

http://blog.csdn.net/u012954083/article/details/23253749

#include <cstdio>#include<iostream>using namespacestd;
classa{ Public: A () {cout<<"A"<<Endl; }};classb{ Public: B () {cout<<"B"<<Endl; }};classC: PublicB Publica{ Public: A; C () {cout<<"C"<<Endl; }};intMain () {C C; return 0;}
Output: B AAC First executes the constructor of class B from left to right first when declared by a derived class, then executes the constructor of Class A, so output B, a, and then because Class C is a; This statement instantiates Class A, so the constructor for Class A executes output a, and the constructor for Class C finally executes the output C

structs in C are different from structs in C + + :

in the Structs in C can only customize data types, no functions are allowed in structs, and struct bodies in C + + may join member functions.

The structure in C involves only the data structure, not the algorithm,and the structure and class inC + + embody the combination of the architecture and the algorithm.

The similarities and differences of structs and classes in C + +:

One, the same place:

A struct can contain functions, you can also define public,private,protected data members, and after you define a struct, You can create objects with struct names.

in other words , In C + + , structs can also have member variables, can have member functions, can inherit from other classes, can also be inherited by other classes, can have virtual functions.

Second, the difference:

the members in the struct definition are by defaultPublic, and the members in the class definition by default arePrivatethe. Class that is not in theStaticmember functions have Thispointer, (whilestructis not wrong, has been misled ah, after testingstructmember functions have the same Thispointer), the class's keywordclasscan act asTemplateThe template keyword isTemplate<class t> class a{};andstructnot to.

Class type conversions:

#include <iostream>#include<string>using namespaceStd//using Namespaces//adjust the declaration locationclassteacher{ Public: FriendclassStudent;//set friends, unless you get name,sex in some way.friend Ostream&operator<< (ostream&output, Teacher t);Private:    Longnum; stringname; stringsex;};classstudent{ Public: Student (LongNumstringNamestringsex): num (num), name, sex (sex) {}//The conversion should be inside the student, defining the teacher type conversion operator function    operatorTeacher () {Teacher t; t.num = num; t.name = name; t.sex = sex;returnt;}Private:    Longnum; stringname; stringSex;};o Stream&operator<< (ostream&output, Teacher t) {Output<<"I ' m a teacher"<< t.name << t.num <<T.sex; returnoutput;}intMainintargcChar*argv[]) {Student S (123,"xiaoming","Mans"); cout<< Teacher (s);//Cast Direct Output    return 0;}

Class Template:

Like a function template, a class template is used to enable a user to define a pattern for a class so that some data members in the class, parameters of some member functions, and return values of some member functions can take any type. A class template is an abstraction of a class of classes that differ only in the number of member data types, and programmers simply create a class template for the entire class family of this batch of classes, giving a set of program code that can be used to generate a variety of concrete classes (which can be seen as instances of class templates), thus greatly improving the efficiency of programming.

The general form of defining a class template is:
Template < type name Parameter name 1, type name parameter name 2,...>
Class name
{
class declaration Body
};
For example,template <class t>
Class Smemory
{...
Public
void Mput (T x);
...
}

Represents the definition of a class template named Smemory with the type parameter T.

The general form of class member functions that are defined outside the class template is:
Template < type name Parameter name 1, type name parameter name 2,...>
function return value type class name < parameter name 1 parameter name 2,...>:: member function name (formal parameter list)
{
function body
}
Example:template <class t>
void Smemory<t>::mput (T x)
{...}

Represents a member function that defines a class template smemory, the function name is Mput, the type of parameter x is T, and the function has no return value.
A class template is an abstraction of a class family, which is simply a description of the class, and the compiler does not create program code for the class template (including member function definitions), but by instantiating the class template you can generate a concrete class and an object of that specific class.

Unlike a function template, the instantiation of a function template is done automatically by the compiler when the function call is processed, and the instantiation of the class template must be explicitly specified by the programmer in the program.
In fact, the general form of the example is:
Class name < data type 1 (or data), data type 2 (or data) ...> object name
For example,smemory<int> mol;
Represents the type parameter T of the class template smemory all replaced by the int type, thus creating a concrete class and generating an object mol for that concrete class.

#include <iostream.h>#include<conio.h>Const intSize=8; template<classT>classsmemory {//defining a class template SmemoryT Data[size];//type T, array of size data[] as data member      intcount;  Public: Smemory () {count=0; } voidMput (T x);//The type of the parameter x of the Mput () function is TT mget ();//declares a member function that returns a value of type T mget ()}; Template<classT>voidSmemory<t>::mput (T x)//defines a member function, Mput (), whose parameter type is T, which is used to assign values to individual elements of the data array{if(count==8) {cout<<"Memory is full";return; } Data[count]=x; Count++;} Template<classT>T smemory<t>::mget ()//defines the member function mget (), the return type of the function is T, which is used to remove individual elements of the data array{if(count==0) {cout<<"Memory is empty";return 0; } Count--; returnData[count];}voidMain () {smemory<int>Mo1;intICharCh='A';//instantiate the Smemory and create the object Mo1smemory<Char> Mo2;//instantiate the Smemory and create the object Mo2      for(i=0; i<8; i++) {mo1.mput (i); //Call member function mput ()Mo2.mput (CH); ch++;//Call member function mput ()} cout<<"Get mo1 =";  for(i=0;i<8; i++) cout<<mo1.mget ();//Call member function mget ()cout<<"\nget Mo2 =";  for(i=0;i<8; i++) cout<<mo2.mget ();//Call member function mget ()getch ();} The running result of the program is: Get mo1=76543210Get Mo2= HGFEDCBA

Class templates and Template classes:

The focus of the class template is the template. Represents a template that is specifically used to produce a mold of a class. Example:

1 template <typename T>2 class Vector3 {4     ... 5};

Template class is a product of the instantiation of a class template, say a specific point of the example, we compare the class template to a cookie-making mold, and the template class is made out of the cookie, and the cookie is what the taste of it is to see yourself in the instantiation of what material, you can make chocolate biscuits, can also make milk biscuits, these biscuits out of the material is not the same, the other things are the same.

In the C + + template Many places have used the TypeName and class these two keywords, sometimes these two can be replaced, then these two keywords are exactly the same?

In fact class is used to define classes, after the introduction of C + + in the template, the method of initially defining the template is: Template<class t>, here the Class keyword indicates that T is a type, and later in order to avoid the use of class in these two places may cause confusion, So the TypeName keyword is introduced, and its function is the same as class to indicate that the following symbol is a type, so that when defining the template, you can use this method:

Template<typename T> In the template definition syntax, the keyword class functions exactly like TypeName.

C + + Learning

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.