C++: Essay 1

Object-oriented (oo): Objects are ultimately divided into two elements: attributes (that indicate the characteristics of your class, variables in the class) and behavior (that is, an action of the object you generate, and a method in the class).

1 Encapsulation: It means to combine the properties and methods of the object into an independent system unit, and hide the internal details of the object as much as possible.

2 Abstraction: The process of abstraction is the process of generalizing specific issues and the process of uniformly describing a class of public issues

3 Inheritance: The subclass object has all the same properties and methods as its base class.

4 Polymorphism: refers to the attributes and behaviors defined in the base class being inherited by subclasses, they can have different data types or performance behaviors. (You can develop your characteristics at the same time)

The animal is a basic category, its subcategories will be tigers, rabbits, etc. As an animal, there is a behavior defined as Move(), then these subclasses will adopt different personalized Move() methods (walking, flying, etc.) according to their own characteristics after inheriting.

Array summation C language program:

Since the array name is the address of its first element, there are some correlations between the array and the pointer, but they are not completely equivalent.

#include<stdio.h>
#include<iostream>
int addArray(int arr[],int n);

using namespace std;
int main()
{
	int data[] = { 0,1,2,3,4,5,6,7,8,9 };
	int size = sizeof(data) / sizeof(data[0]);
	printf("实参数组data大小:%d\n", sizeof(data));//1输出40(是整个数组的长度)
	printf("和为:%d\n", addArray(data,size));//4为什么没有当作一个数组?
	//因为这里传过来他是把它当作一个地址,她不可能说把整个数组元素都传过来,这样效率太慢了。
	//(因为数组是连续存放的,他不如把第一个元素的地址当作指针传过去,接着你要索引依次递推索引到你需要的元素)
	//所以在3处实参传递的时候它是当指针传过来的
	return 0;
}
int addArray(int arr[], int n)//3所以在整个整个函数里面所有的srr其实被当成一个指针
{
	int i , sum = 0;
	printf("形参数组arr大小:%d\n", sizeof(arr));//2输出8(8个字节)(是一个指针变量的长度)(是一个地址长度)(因为在64为系统里面就是8个字节存放一个地址)
	for (i = 0; i < n; i++)
	{
		sum += arr[i];
		cout << sum << endl;
	} 
	return sum;
}

Change it:

#include<stdio.h>
#include<iostream>
int addArray(int *arr,int n);//不定义为一个数组了直接定义为一个指针

using namespace std;//名称空间。C++标准库所使用的所有标识符(类,函数,对象等的名称都是在一个同一个特殊的名称空间(std中来定义))
int main()
{
	int data[] = { 0,1,2,3,4,5,6,7,8,9 };
	int size = sizeof(data) / sizeof(data[0]);
	printf("和为:%d\n", addArray(data,size));//传过来传一个地址过来(因为数组名就相当于数组的首地址)
        //cout<<"结果是:"<<addArray(data,size)<<endl;//endl是回车加清空缓冲区的意思
	return 0;
}
int addArray(int *arr, int n)//地址传过来用一个指针接收
{
	int i , sum = 0;
	for (i = 0; i < n; i++)
	{
		sum += *arr++;//用指针来索引(arr++是地址递增,前边加*号是取值)
	} 
	return sum;
}

Conclusion: Declaring the parameters of a function as an array is the same as declaring as a pointer.

C language uses printf to achieve output, but printf is a function,

The above C++ uses cout output, and cout is an object. Cout is an output stream object, it is the abbreviation of console out (console output), is an object belonging to the basic_ostream class, the ostream class is defined in the iostream header file. ( Class is the abstract collection of a class of things together to form a class. For example, people are a class, Asians are the inheritance of a class, and Chinese are a small class.)

Stream object cin, the type of this object is istream, he knows how to read data from the user terminal.

cin>>i;//cin, the operator is also called the extraction operator, which in turn extracts an integer from the input stream object cin. (A space or Enter key is said to be an integer.)

When the user enters the keyboard, the corresponding character will be entered into the keyboard buffer of the operating system, so that when the user clicks the Enter key on the keyboard, the operating system transmits the contents of the keyboard buffer to the internal buffer of the cin stream (that is stdin stream buffer), the ">>" operator then extracts the required information from this buffer.

using namespace std;//Name space. All identifiers used by the C++ standard library (the names of classes, functions, objects, etc. are all in the same special namespace (defined in std)) (all variables are contained in this namespace, and this space must be referenced Variables in, this space must be set out) (If there is no such instruction, we can use std::cout syntax to call the output stream object)

PS: Overloading is to give new meaning to an old thing. ">>" was originally defined as a right shift operator, which is overloaded in C++, and when it is used in the manner shown here, it is used to extract information from the input stream object.

The C language is the same as C++, the string ends with 0.

cin.ignore(n);//忽略前n个字符;
cin.getline(buf,n);//获取输入的n个字符(字符串要包含自动后边添加的0所以是一共获取了实际的n-1个),放在buf缓冲区中。
cin.peek();//从字符里面挑取字符
cin.get();//获取字符
cin,gcount();
cin.read();
cout.write();
cout.precision();
cout.width();

--------File I/O:

Write a file copy program to copy one file to another. * (For example: fileCopy, sourceFile, destFile) (meaning there is a fileCopy.exe program to copy source.c to destFile.c)

int main(int argc,char*argv[]);
//在程序中,main有两个参数,整型变量argc和字符指针数组argv[];
//argc的含义是程序的参数数量,包含本身(就是程序自身生成的exe)。
//argv[]的每一个指针指向命令行的一个字符串,所以argv[0]指向字符串“copyFile.exe”,argv[1]指向字符串...,argv[2]指向字符串...。
//C语言版本
...
int  main(int argc,char* argv)
{
   FILE *in,*out;//in和out是我们声明的两个文件指针,它们的类型都是FILE*,分别u走位两个I/O流对象使用(如何理解文件指针,好端端的文件他是存放在磁盘上的,当我们要用到她的时候,必须要暂时把他们拿到内存中,拿到内存中地址是随机分配的,我们不知道地址怎么访问它,我们就必须定义两个指针(因为指针变量里边存放的就是地址),那知道了他的地址才能对他进行操作,所以我们打开之后,就是把地址传给我们定义好的文件指针in和out通过in和out这两个我们就能知道文件的地址,间接能够对文件进行操作)
   int ch;
   if(argc!=3)//为了确保程序参数个数的正确性
   {
       fprintf(stderr,"输入形式:copyFile 源文件名 目标文件名\n")//意思就是输入的参数不是三个的话,就要提示输入参数形式为....
   }
   if((in=fopen(argv[1],"rb"))==NULL)//以二进制可读的方式打开文件并返回文件指针给in
   {
       fprintf(stderr,"打不开文件:%s\n",argv[1]);//后边两个参数分别是即将被复制的文件和复制后生成的新文件名,先判断这两个文件是否可以打开或者写入
       exit(EXIT_FAILURE);//终止
   }
   if((out=fopen(argv[2],"wb"))=NULL)//以二进制可写的方式打开文件并返回文件指针给out
   {
       fprintf(stderr,"打不开文件:%s\n",argv[2]);//第三个元素打不开,把这提示信息写入了错误输出流stderr,然后他会自动显示在我们的显示器上,(意思就是未能成功打开我们会像标准错误流stderr发送一条信息)
       fclose(in);//因为只有上边打开了你才能执行到这一步,所以打开后要关闭
       exit(EXIT_FILURE);
   }
//getc()函数从一个输入流(stdin)读取一个字符,putc()函数把这个字符写入到输出流(stdout)
   while((ch=getc(in))!=EOF)//EOF其实是个宏定义等于-1(文件的结尾一般都是EOF以-1标志结束)//在这里输入流是这个文件流in,获取一个字符,然后给了字符ch(上边定义的是int型为什么没有定义char呢?因为getc()函数返回值是int类型)
   {//到结尾的话就不应进行循环了,因为循环体内的内容就是把这个ch写进out这个文件里面去
       if(putc(ch,out)==EOF)//同时进行检查如果他是一个写入文件结尾标志的话他就进行下面语句内容break跳出循环
       { 
          break;
       }
   }
   //(因为上边EOF有两个意思:第一读取错误,第二是文件结束)所以下边两个语句是对文件进行检查,是哪个意思
   if(ferror(in))
   {
       printf("读取文件 %s 失败!\n",argv[1]);
   }
   if(ferror(in))
   {
       printf("写入文件 %s 失败!\n",argv[2]);
   }
   printf("成功复制一个文件!\n");
   fclose(in);
   fclose(out);//把两个关掉
   return 0;

}

C++ version

#include<Fstream>//是文件流File stream
#include<iostream>//事实Fstream已经包含了iostream
using namespace std;
int main(int agc,char* argv[])
{
	ifstream in("test1.txt",ios::in);
	if (!in)//如果他返回0的话(就是文件打开失败),再非0就为真
	{
		cerr << "打开文件1失败" << endl;
		return 0;
	}
	ifstream out("test2.txt",ios::out);
	if (!out)//如果他返回0的话(就是文件打开失败),再非0就为真
	{
		cerr << "打开文件2失败" << endl;
		return 0;
	}
	char x;
	while (in >> x)//1">>"这里这个操作符被重载过了,它又变成流了,这种文件里面in流到了这个字符x里边去,每次流一个过去
	{
		out>> x;//2然后再从x流到cout这个输出终端,最后循环嘛
	}
	cout << endl;
	in.close();//关闭
	out.close();
}
#include<Fstream>//是文件流File stream
#incluede<iostream>//事实Fstream已经包含了iostream
using namespace std;
int main()
{
   ifstream in;//输入文件流ifstream(input file stream)是Fstream这个头文件报刊的一个类,一个文件输入流的一个类,这个类定义了一个对象in(那么这个in就拥有了这个类的所有功能(属性和函数))。
   in.open("test.txt");//in有一个函数就是open
//上边可以将两句合成一句写为:ifstream in("test.txt");//不用open直接带参数的对象
   if(!in)//如果他返回0的话(就是文件打开失败),再非0就为真
   {
      cerr<<"打开文件失败"<<endl;
      return0;
   }
   char x;
   while(in>>x)//1">>"这里这个操作符被重载过了,它又变成流了,这种文件里面in流到了这个字符x里边去,每次流一个过去
   {
      cout<<x;//2然后再从x流到cout这个输出终端,最后循环嘛
   }
   cout<<endl;
   in.close();//关闭
   return 0;
}

The C language uses a pointer FILF* type pointer to directly point to an address of a file in our memory. In C++, an object is passed through a class. This object is specially used for input, and then it is accessed through this object. Member function access to operate on this file.

File reading class: ifstream.

File writing class: ofstream.

#include<Fstream>//是文件流File stream
#incluede<iostream>//事实Fstream已经包含了iostream
using namespace std;
int main()
{
   ofstream out;//类的对象和声明变量是一样的,把out改成其他也行的
   out.open("test.txt");//out是指向了test.txt这个文件//打开就是把他从磁盘加载到内存
//上边可以将两句合成一句写为:ofstream out("test.txt");
   if(!out)
   {
      cerr<<"打开文件失败"<<endl;
      return0;
   }
   for(int i=0;i<10;i++)
   {
      out<<i;//利用循环把0到9,从变量i流到了out指针里面去,流到了out文件里面去(因为上边out是指向了test.txt这个文件)(怎么流呢,看他往哪指就流到哪)(如果源文件里边有东西就会被覆盖掉)
   }
   cout<<endl;//endl就是回车
   out.close();//关闭
   return 0;
}

When creating an object of the ifstream and ofstream classes, the above code passes the name of the file to their constructor (for the time being understood as the function (method) used by the object by default).

The above can also accept two parameters:

ifstream in(char* filename,int open_mode);
//第一个是指针指向了文件名(他是一个字符串)
//open_mode表示打开模式,气质用来定义怎么的反射光hi打开文件(跟open的参数一样)
模式见下图
(什么是文本形式,什么是二进制形式?没有翻译为一堆数据那他就是文本形式)

Picture:

example:

#include<Fstream>
#incluede<iostream>
using namespace std;
int main()
{
   ofstream out("test.txt",ios:app);//就是以添加的形式打开
   if(!out)
   {
      cerr<<"打开文件失败"<<endl;
      return0;
   }
   for(int i=0;i<10;i++)
   {
      out<<i;
   }
   cout<<endl;//endl就是回车
   out.close();//关闭
   return 0;
}

Open with multiple modes: (using OR operator: "|")

#include<fstream>
#include<iostream>
using namespace std;
int main()
{ 
   fatream fp("test.txt",ios::in|ios::out);//对象命名为fp//同时以可读和可写入形式打开
   if(!fp)
   {
      cerr<<"打开文件失败!"<<endl;
   }
   fp<<"I LOVE";//将这些字符"I LOVE"流进去fp
//上边的内容是把字符串流入了我们的文件中
//下边是通过str显示
   static char str[10];//静态的数组,为了定义他不能够被改变。
   fp.seekg(ios::beg);//ios::beg使得文件指针指向文件头;ios::end就是文件尾。
   fp>>str;//把fp的内容给了数组
   cout<<str<<endl;//把数组显示
   fp.close();
   return 0;
}

0x1 |0x2=11 mainly depends on whether there is a number in this bit, if there is one, it will be the corresponding mode.

 

Guess you like

Origin blog.csdn.net/m0_37957160/article/details/108779846