这是作者在2022年3月7日学习C++语言时做的笔记,如果对您也产生了帮助,作者将不胜荣幸!如果您发现了作者的错误,我也非常乐意与您交流!
课堂开始,LDM老师复习了C++中的引用概念,并且使用了一小写转大写程序作为例子。
void turn (char& a) //a是一个被引用的变量,可以直接改变下面的c变量的值
{
a=a-32; //大小写ASCII码差32
}
int main()
{
char c;
cout<<"input a char:";
cin>>c;
turn(c);
cout<<c<<endl;
return 0;
}
接下来是一个关于动态数组的使用的例子,以下代码段功能是接收六个裁判的打分,并且输出去掉最高分和最低分的总分。需要注意的是动态生成的变量一定要记得释放,否则会产生严重的影响(特别是在服务器端)。
class test
{
private:
int n;
public:
test (int nn)
{
n=nn;
}
void output() //实际上将所有的功能都集成在了output中
{
int i;
int sum=0;
int* a=new int[n]; //新建动态数组
for(i=0;i<n;++i) //输入原始数据即裁判打分
{
cout<<"input "<<i+1<<":";
cin>>a[i];
}
for(i=0;i<n;++i) //分数求和
{
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; //去掉一个最高分和一个最低分
cout<<"sum="<<sum<<endl;
delete []a; //释放空间
}
};
int main()
{
test t(6);
t.output();
return 0;
}
对于上述代码的取最值部分我们还可以进行如下优化,但是下面这种优化却未必会提高代码的运行速度,但却能减小一点点代码量。sort函数默认将会以升序排列。
sort(a,a+n); //sort函数需要<algorithm>的支持
cout<<"sum="<<sum-a[0]-a[n-1]<<endl; //a[0],a[n-1]为两个最值
但是这时我们会发现随着delete的运行,裁判的原始数据会丢失,如果我们想要保留裁判的数据的话,C++为我们提供了一个工具:容器。下面是最简单的数组容器:
/* 可变数组容器的调用需要<vector>库的支持 */
class test
{
private:
int n;
vector <int> a; //定义容器a,该占用空间 可以保存数据
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); //使用push_back函数以向容器中压入数据
}
}
void output()
{
int i;
int sum=0;
for(i=0;i<a.size();++i) //使用a.size()函数可以动态地适应不同的情况
{
sum=sum+a[i];
}
sort(a.begin(),a.end()); //返回头尾指针
cout<<"sum="<<sum-a[0]-a[n-1]<<endl;
}
};
类的继承与派生在软件重用时起到良好的作用,它将使我们迭代软件的速度大为加快。
但是他也有很大的缺点:在软件迭代了很多次之后,后来者将对原始代码一无所知,使得我们无法对基类进项有效的优化,间接导致了“屎山”的产生。
一般情况下我们采取“父类写功能,子类做应用”的原则进行开发,这一思想和主函数中不做运算,只调用函数的思想大同小异。下面直接用一个例题记录我学的知识点:
class test1 //父类
{
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 //继承的方式 子类
{
protected:
int b;
public:
test2() //通常子类的构造函数要大于等于父类的
{
b=2000;
}
test2(int bb):test1() //子类需要写多个函数以对应父类的重载
{
b=bb;
}
test2(int aa,int bb):test1(aa) //直接调用父类构造函数初始化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;
}
//对等差数列1,4,7,10...
//求前n项的和
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;
}
注:下面是替代C语言中宏定义的方法:
//C语言:#define PI 3.1415926
//C++ : const double PI=3.1415926;
当我们想要在类中创建新的对象或者动态控件也是可以的;
class test2:public test1
{
protected:
int a;
public:
test2 t1,t2; //新的对象
test2* t3=new test2; //动态创建新的对象
delete t3;
};