Inheritance, derivation and container of C + + language

This is the author's note when learning C + + language on March 7, 2022. If it helps you, the author will be honored! If you find the author's mistake, I am also very happy to communicate with you!

At the beginning of the class, LDM teacher reviewed the concept of reference in C + + and used a lowercase to uppercase program as an example.

void turn (char& a)	    //A is a referenced variable. You can directly change the value of the following c variable
{
	a=a-32;             //Case ASCII code difference 32
}

int main()
{
	char c;
	cout<<"input a char:";
	cin>>c;
	turn(c);
	cout<<c<<endl;
	return 0;
}

The following is an example of the use of dynamic arrays. The function of the following code segment is to receive the scores of six referees and output the total score excluding the highest score and the lowest score.

class test
{
private:
	int n;
public:
	test (int nn)
	{
		n=nn;
	}
	void output()                //All functions will be integrated in the actual output
	{
		int i;
		int sum=0;
		int* a=new int[n];		 //New dynamic array 
		for(i=0;i<n;++i)         //Enter the original data and the referee will score
		{
			cout<<"input "<<i+1<<":";
			cin>>a[i];
		}
		for(i=0;i<n;++i)         //Fraction summation
		{
			sum=sum+a[i];
		}	
		int max=a[0],min=a[0];
		for(i=0;i<n;++i)
		{
			if(max<a[i])	max=a[i];
			if(min>a[i])	min=a[i];
		}
		sum=sum-max-min;        //Remove a highest score and a lowest score
		cout<<"sum="<<sum<<endl;
		delete []a;				//Free up space 
	}
};

int main()
{
	test t(6);
	t.output();
	return 0;
}

For the most valued part of the above code, we can also carry out the following optimization, but the following optimization may not improve the running speed of the code, but it can reduce the amount of code.

sort(a,a+n);                            //The sort function needs the support of < algorithm >
cout<<"sum="<<sum-a[0]-a[n-1]<<endl;    //a[0],a[n-1] are the two most important values

But at this time, we will find that with the operation of delete, the original data of the referee will be lost. If we want to retain the referee's data, C + + provides us with a tool: container. Here are the simplest containers:

/*  The call of variable array container needs the support of < vector > library  */	
class test
{
private:
	int n;
	vector <int> a;				        //Define container a, which can hold data 
public:
	test (int nn)
	{
		n=nn;
	}
	void input()
	{
		int i,temp;
		for(i=0;i<n;++i)
		{
			cout<<"input "<<i+1<<":";
			cin>>temp;
			a.push_back(temp);			//Using push_ The back function to push data into the container
		}
		
	}
	void output()
	{
		int i;
		int sum=0;
		for(i=0;i<a.size();++i)			//Use the a.size() function to dynamically adjust the situation when the number of referees is different 
		{
			sum=sum+a[i];
		}
		sort(a.begin(),a.end());		//Return head and tail pointer 
		cout<<"sum="<<sum-a[0]-a[n-1]<<endl;
	}
};

Class inheritance and derivation play a good role in software reuse, which will greatly accelerate the speed of software iteration. However, it also has great disadvantages: after many iterations of our software, the latecomers will know nothing about the original code, which makes us unable to optimize it, and also leads to the emergence of "Shishan". In general, we adopt the principle of "the parent class writes the function and the child class does the application" for development. This idea is similar to the idea of calling the function instead of operation in the main function. Here is an example to record my knowledge points:

class test1								//Parent class 
{
protected:								//Family, protected
	int a; 
public:
	test1()
	{
		a=1000;
	}
	test1(int aa)
	{
		a=aa;
	}
	void input1()
	{
		cout<<"input a:";	cin>>a;
	}
	void output1()
	{
		cout<<"a="<<a<<endl;;
	}
};

class test2:public test1				//Inheritance method subclass 
{
protected:
	int b; 
public:
	test2()                             //Usually, the constructor of the subclass should be greater than or equal to the constructor of the parent class
	{
		b=2000;
	}
	test2(int bb):test1()				//The subclass needs to write multiple functions to correspond to the overload of the parent class 
	{
		b=bb;
	}
	test2(int aa,int bb):test1(aa)		//Directly call the parent constructor to initialize a 
	{
		b=bb;
	}
	void input2()
	{
		cout<<"input b:";	cin>>b;
	}
	void output2()
	{
		cout<<"b="<<b<<endl;;
	}
};
int main()
{
	test2 t(200);
	t.input2();
	t.output1();
	t.output2();
	return 0;
}
//For the arithmetic sequence 1,4,7,10
//Sum the first n items
class test1
{
protected:
	int m;
public:
	test1()
	{
		m=3;
	}
	test1(int mm)
	{
		m=mm;
	}
	int output1(int n)
	{
		int i,temp=1;
		for(i=1;i<n; ++i)
		{
			temp=temp+m;
		}
		return temp;
	}
};

class test2:public test1
{
protected:
	int n;
public:
	test2()
	{
		n=2;
	}
	test2(int mm,int nn):test1(mm)
	{
		n=nn;
	}
	void output2()
	{
		int sum=0,flag=0;
		test2* t=new test2(m,n);
		for(int i=1;i<=n;++i)
		{
			sum=sum+t->output1(i);
			if(flag==0)
			{
				cout<<"sum="<<output1(i);
				flag=1;
			}
			else
				cout<<"+"<<output1(i);
		}
		cout<<"="<<sum<<endl;
		delete t;
	}
};

int main()
{
	test2 t(3,3);
	t.output2();
	return 0;
}

Note: the following is a method to replace the macro definition in C language:

//C language: #define PI 3.1415926
//C++ :  const double PI=3.1415926;

Keywords: C++ linq p2p

Added by wes007 on Mon, 07 Mar 2022 17:57:53 +0200