通过这段时间对类和对象的学习我发现,与c语言的面向过程程序设计不同,c++注重面向对象的程序设计,而对象是由相关的数据和操作共同组成的,不会导致数据与数据处理分离,这是c++语言的优越性
类的应用:学生成绩信息管理
首先应考虑类中所包含的不同基本数据类型,比如姓名(string)、学号(int)、各科成绩(整形数组)、平均分(int)、名次(int),然后写出来学生信息的数据类
#include<bits/stdc++.h>
using namespace std;
class student
{
string name;
int no;
int score[3];
float average;
int order;
public:
student (int id,string na,int x,int y,int z):name(na),no(id){
score[0]=x;score[1]=y;score[2]=z;
order=-1;average=(score[0]+score[1]+score[2])/3;
}
student()
{
score[0]=score[1]=score[2]=0;
order=-1;average=0;
}
int getNo(){return no;}
float getAverage(){return average;};
int getOrder(){return order;};
string getName(){return name;}
void display();
};
void student::display()
{
cout<<name<<"\t"<<no<<"\t"<<score[0]<<"\t"<<score[1]<<"\t"<<score[2]<<"\t"<<average<<"\t\t"<<order<<endl;
}
int main()
{
student s1(1,"ff",66,77,88);
s1.display();
cout<<s1.getNo()<<endl;
cout<<s1.getName()<<endl;
cout<<s1.getAverage()<<endl;
cout<<s1.getOrder()<<endl;
return 0;
}
将类中所有的数据和函数都测试一遍,为下一步实现具体功能的操作类打下基础,
注意:对类的测试应该采用“滚雪球”的方法,写出一个功能,测试一个功能,否则一旦程序的某个环节出现问题,
错误将以几何的方式进行增长。
下面来分析操作类要实现的功能,例如
1、增加学生信息
2、查询学生信息(可以通过学号和名次进行查询)
3、对学生名次进行排序
#include<bits/stdc++.h>
using namespace std;
class student
{
string name;
int no;
int score[3];
float average;
int order;
public:
student (int id,string na,int x,int y,int z):name(na),no(id){
score[0]=x;score[1]=y;score[2]=z;
order=-1;average=(score[0]+score[1]+score[2])/3;
}
student()
{
score[0]=score[1]=score[2]=0;
order=-1;average=0;
}
int getNo(){return no;}
float getAverage(){return average;};
void setOrder(int x){order=x;}
void setAverage(int avg){average=avg;}
int getOrder(){return order;};
string getName(){return name;}
void setName(string name){this->name=name;}
void display();
};
void student::display()
{
cout<<name<<"\t"<<no<<"\t"<<score[0]<<"\t"<<score[1]<<"\t"<<score[2]<<"\t"<<average<<"\t\t"<<order<<endl;
}
bool cmp1(student stu1,student stu2)
{
if(stu1.getAverage()-stu2.getAverage()>=1e-9) return 1;
else return 0;
}
bool cmp2(student stu1,student stu2)
{
return stu1.getNo()<stu2.getNo();
}
class functions
{
student a[60];
int n;
public:
functions():n(0){};
void add();
void deleteStu();
void query();
int search(int no);
void change();
void sortList();
void display(int flag);
};
void functions::add()
{
int no,x,y,z;
string name;
system("cls");
cout<<"按学号,姓名,数学,英语,c++顺序输入学生信息,学号输-1表示结束"<<endl;
while ((cin>>no)&&no!=-1)
{
cin>>name>>x>>y>>z;
student s(no,name,x,y,z);
a[n++]=s;
sortList();
}
}
void functions::sortList()
{
sort (a,a+n,cmp1);
for(int i=0;i<n;++i)
a[i].setOrder(i+1);
}
void functions::display(int flag)
{
if(flag) sort(a,a+n,cmp2);
else sort(a,a+n,cmp1);
cout<<"姓名"<<"\t"<<"学号"<<"\t"<<"数学"<<"\t"<<"英语"<<"\t"<<"c++"<<"\t"<<"平均成绩"<<"\t"<<"名次"<<endl;
for(int i=0;i<n;++i)
a[i].display();
}
int functions::search(int no)
{
for(int i=0;i<n;i++)
if(a[i].getNo()==no)
return i;
return -1;
}
void functions::query()
{
int no,i;
system("cls");
cout<<"请输入要查询同学的学号,按-1结束查询:";
while(cin>>no&&no!=-1)
{
i=search(no);
if(i!=-1)
{
cout<<"姓名"<<"\t"<<"学号"<<"\t"<<"数学"<<"\t"<<"英语"<<"\t"<<"c++"<<"\t"<<"平均成绩"<<"\t"<<"名次"<<endl;
a[i].display();
cout<<"请输入要查询同学的学号,按-1结束查询:";
}
else
cout<<"学号输入有误,请重输,按-1结束查询"<<endl;
}
}
int main()
{
/*student s1(1,"ff",66,77,88);
s1.display();
cout<<s1.getNo()<<endl;
cout<<s1.getName()<<endl;
cout<<s1.getAverage()<<endl;
cout<<s1.getOrder()<<endl;
return 0;*/
functions c;
c.add();
c.query();
return 0;
}
对于删除和修改操作至今还是不知道该如何实现...
心得体会:
1.个人感觉面向对象的程序设计会比面向过程的程序设计更容易想些,在解决小问题时用算法可能会使代码更加的简洁(算法不容易想出),而用对象的话会略显繁琐,但解决比较复杂的问题时,面向对象的程序设计能够减少代码的冗余量,结构也比较清晰
2.在面向对象的思想中,任何事物都可以被看作一个对象,一个复杂的模型结构往往都是由千千万万个对象所组成、
3.在定义类时,要注意类的封装,一般都将数据成员私有化而将成员函数公有化,但友元函数可能会破坏类的封装性
4.类在定义时可以分为数据类和操作类,数据类注重于类的属性,而操作类注重于类的行为(功能)
5.若在类中定义了带有参数的构造函数,必须再定义一个不带参数的构造函数,但析构函数不能重载
6.类中包含了很多基本数据类型和对其数据的操作,不易导致数据与数据处理分离
7.在类中直接定义的函数可以当作是内联函数,可以减少子程序的调用,节省空间与时间
8、在定义类时,写出一个功能,必须对其进行测试(滚雪球)
总的来说,类与对象的学习过程需要大量接触新的陌生的知识和概念,但重要的不是知识和概念本身,而是学会怎么用学到的知识去解决一些问题,只有在实践中去体会,才能掌握门道,了解这种方式的优缺点,最终做到熟练运用,这也是学习编程语言的目的所在。