Difficulties in C ++ template programming, single-inheritance, multi-inheritance formats, value transfer of constructor, and operator overloading.

Source: Internet
Author: User

Difficulties in C ++ template programming, single-inheritance, multi-inheritance formats, value transfer of constructor, and operator overloading.

1. Single-inheritance, multi-inheritance format and constructor value transfer

2. STL common input/output streams and usage methods.

3. Operator overloading.

4. C ++ template programming.

1. Single-inheritance, multi-inheritance format and constructor value transfer

I. Single inheritance

Class derived class name: public base class name

{

// The transfer format of the base class construction value 1

Constructor (base class parameter 1, base class parameter 2, this class parameter 1, this class parameter 2 ,...): Base Class Name (base class parameter 1, base class parameter 2)

{

This type of data member 1 = this type of parameter 1;

This type of data member 2 = this type of parameter 2;

}

// Format of the base class construction value transfer 2

Constructor (base parameter 1, base parameter 2, this parameter 1, this parameter 2 ,...): Base Class Name (base parameter 1, base parameter 2), data member 1 (this parameter 1), data member 2 (this parameter 2)

{

}

Data member and member function declaration and definition;

};

Ii. Multiple inheritance formats

Class derived class name: public base class name 1, public base class name 2 ,...

{

Constructor (base class parameter 1, base class parameter 2, this class parameter 1 ,...): base class name 1 (base class parameter 1), base class name 2 (base class parameter 2)

{

This type of data member 1 = this type of parameter 1;

.........

.........

}

// Format of the base class construction value transfer 2

Constructor (base parameter 1, base parameter 2, this parameter 1, this parameter 2 ,...):

Base Class Name 1 (base parameter 1 ),

Base class name 2 (base parameter 2 ),

This type of data member 1 (this parameter 1 ),

This type of data member 2 (this parameter 2)

{

}

Data member and member function declaration;

};

2. STL common input/output streams and usage methods.

File input and output streams

C ++ reads and writes files using the ifstream, ofstream, and fstream stream classes. It also operates according to the "open --> read/write --> close" primitive. Many constants used for file read/write are defined in the base class ios. The ifstream class is only used to read file data. The ofstream class is only used to write data to files. fstream can be used to read and write file data.

Ios: in open a file as a reader

Ios: out open the file as a write

Ios: When the app writes data, it first moves the file pointer to the end of the file to append the data to the end.

Ios: ate only moves the file pointer to the end of the file at the beginning, and data can still be written at any location

Ios: Delete the original content of the file (clear the file) before trunc writes data. When the file does not exist, the file will be created.

Ios: binary open a file in binary format without any character conversion.

Std: cout <"this id is" <I <std: endl;

Std: cin> I> f;

3. Operator Overloading

CMyTime & operator + (const CMyTime & obj) const; // overload Addition

{Return * this}

CMyTime & operator-(const CMyTime & obj) const; // overload Subtraction

{Return * this}

CMyTime & operator * (double n) const; // overload Multiplication

{Return * this}

4. function templates and class templates)

// Function Template

Template

Bool equivalent (const T & a, const T & B ){

Return! (A <B )&&! (B <);

}

// Class template

Template // default parameter

Class bignumber {

T _ v;

Public:

Bignumber (T a): _ v (){}

Inline bool operator <(const bignumber & B) const; // equivalent to (const bignumber & B)

};

// Implement member functions outside the class template

Template

Bool bignumber: operator <(const bignumber & B) const {

Return _ v <B. _ v;

}

Int main ()

{

Bignumber <> a (1), B (1); // use the default parameter, "<>" cannot be omitted

Std: cout <equivalent (a, B) <'\ n'; // function template parameters are automatically derived

Std: cout <equivalent (1, 2) <'\ n ';

Std: cin. get (); return 0;

}

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.