4.1 静态成员
4.1.1静态数据成员
#include<iostream>
using namespace std;
class Student
{
private:
int num;
char name[20];
public:
static int total;
Student(){total++;}
~Student(){total--;}
Student( int n, char *p="Wang");
void GetName();
int GetNum();
};
int Student::total=0; //静态数据成员初始化
Student::Student(int n,char *p)
{
num=n;
strcpy(name,p);
total++;
}
void Student::GetName()
{
cout<<name<<endl;
}
int Student::GetNum()
{
return num;
}
int main()
{
cout<<"The number of all students:"<<Student::total<<endl;
Student *p=new Student(13);
cout<<"The number of all students:"<<Student::total<<endl;
cout<<"The number of all students:"<<p->total<<endl;
delete p;
cout<<"The number of all students:"<<Student::total<<endl;
Student s[2];
cout<<"The number of all students:"<<s[0].total<<endl;
cout<<"The number of all students:"<<s[1].total<<endl;
return 0;
}
4.1.2 静态成员函数
#include<iostream>
using namespace std;
class Student
{
private:
int num;
char name[20];
static int total;//私有静态数据成员
public:
Student(){total++;}
~Student(){total--;}
Student( int n, char *p="Wang");
void GetName();
int GetNum();
static void Print();//声明一个静态成员函数
};
int Student::total=0; //静态数据成员初始化
Student::Student(int n,char *p)
{
num=n;
strcpy(name,p);
total++;
}
void Student::GetName()
{
cout<<name<<endl;
}
int Student::GetNum()
{
return num;
}
void Student::Print()
{
cout<<"The number of all students:"<<total<<endl;
}
int main()
{
Student::Print();
Student *p=new Student(13);
Student::Print();
p->Print();
delete p;
Student::Print();
Student s[2];
s[0].Print();
s[1].Print();
return 0;
}
4.2 共享数据的保护
4.2.1 常数据成员
如普通的常量一样,在类中有时需要用到常量。而这些常量如果按以往的方法定位为全局变量,显然不利于代码的移植。在类中,允许定义常数据成员,仅在本类中起作用,方便了类的移植。
1.h
#ifndef _Circle
#define _Circle
class Circle
{
private:
double Radius;
const double PI;
public:
Circle(double r=0):PI(3.1415926) //只能通过初始化列表来初始化常数数据成员
{
Radius=r;
}
double Area();
double Circumference();
};
#endif
2.h
#include "1.h"
double Circle::Area()
{
return PI*Radius*Radius;
}
double Circle::Circumference()
{
return 2*PI*Radius;
}
#include<iostream>
using namespace std;
#include "2.h"
int main()
{
Circle c1(3.5),c2;
cout<<"area of c1="<<c1.Area()<<", circumference of c1="<<c1.Circumference()<<endl;
cout<<"area of c1="<<c2.Area()<<", circumference of c1="<<c2.Circumference()<<endl;
return 0;
}
4.2.2 常成员函数
如果一个成员函数对类中数据成员只做访问而不做修改,则最好将此成员函数说明为常成员函数,以明确表示它对数据成员的保护性。
1.h
#ifndef _Circle
#define _Circle
class Circle
{
private:
double Radius;
const double PI;
public:
Circle(double r=0):PI(3.1415926) //只能通过初始化列表来初始化常数数据成员
{
Radius=r;
}
double Area();
double Circumference();
double GetRadius()const;
};
#endif
2.h
#include "example32.h"
double Circle::Area()
{
return PI*Radius*Radius;
}
double Circle::Circumference()
{
return 2*PI*Radius;
}
double Circle::GetRadius()const
{
return Radius;
}
main.cpp
#include<iostream>
using namespace std;
#include "2.h"
int main()
{
Circle c1(3.5),c2;
cout<<"radius of c1="<<c1.GetRadius();
cout<<", area of c1="<<c1.Area()<<", circumference of c1="<<c1.Circumference()<<endl;
cout<<"radius of c2="<<c2.GetRadius();
cout<<", area of c2="<<c2.Area()<<", circumference of c2="<<c2.Circumference()<<endl;
return 0;
}
4.2.3 常对象
常对象时必须初始化,而其对象的成员值不能被修改。
常对象只能调用它的常成员函数而不能调用普通的成员函数。
#include<iostream>
#include<string>
using namespace std;
class Person
{
private:
int age;
char *name;
public:
Person(int n=1, char *na="Zhuli");
~Person();
void Print();
void Print()const;
void ModifyAge();
};
Person::Person(int n,char *na)
{
age=n;
name=new char[strlen(na)+1];
strcpy(name,na);
}
Person::~Person()
{
delete []name;
}
void Person::Print()
{
cout<<"age:"<<age<<"name:"<<name<<endl;
cout<<"This is const Print()."<<endl;
}
void Person::Print()const
{
cout<<"age:"<<age<<" name:"<<name<<endl;
cout<<"This is const Print()."<<endl;
}
void Person::ModifyAge()
{
age++;
}
int main()
{
const Person p1(17,"Wu");
cout<<"Output const object p1"<<endl;
p1.Print();
Person p2(18,"Zhang");
cout<<"Output general object p2"<<endl;
p2.ModifyAge();
p2.Print();
return 0;
}
4.3 程序实例——学生信息管理系统
//example4_06_student.h:定义学生类
#ifndef _STUDENT
#define _STUDENT
#include<iostream>
#include<string>
using namespace std;
#define SIZE 80
class Student
{
char *name;
char ID[19];
char number[10];
char speciality[20];
int age;
static int count; //实际有意义的学生个数,小于等于对象的个数
public:
Student();
Student( char *na , char *id , char *num , char * spec ,int ag);
Student(const Student &per);
~Student();
char* GetName()const; //可以定义为常成员函数
char* GetID(); //不可以定义为常成员函数
char* GetNumber(); //不可以定义为常成员函数
char* GetSpec(); //不可以定义为常成员函数
int GetAge()const;
void Display()const;
void Input();
void Insert();
void Delete();
static int GetCount( ); //新增加的静态成员函数
};
#endif
//example4_06_student.cpp:实现学生类
#include "example4_06_student.h"
int Student::count=0; //静态数据成员的初始化
Student::Student()
{
name=NULL;
age=0;
}
Student::Student( char *na , char *id , char *num , char * spec ,int ag)
{ if(na)
{
name=new char[strlen(na)+1];
strcpy(name,na);
}
strcpy(ID, id);
strcpy(number,num);
strcpy(speciality, spec);
age=ag;
count++;
}
Student::Student(const Student &per)
{
if(per.name)
{
name=new char[strlen(per.name)+1];
strcpy(name,per.name);
}
strcpy(ID, per.ID);
strcpy(number,per.number);
strcpy(speciality, per.speciality);
age=per.age;
count++;
}
Student::~Student()
{
cout<<"disCon"<<endl;
if (name)
delete []name;
count--;
}
char* Student:: GetName()const
{
return name;
}
char* Student::GetID()
{
return ID;
}
int Student::GetAge()const
{
return age;
}
char* Student::GetSpec()
{
return speciality;
}
char* Student::GetNumber()
{
return number;
}
void Student::Display()const
{
cout<<"姓 名:"<<name<<endl;
cout<<"身份证:"<<ID<<endl;
cout<<"学 号:"<<number<<endl;
cout<<"专 业:"<<speciality<<endl;
cout<<"年 龄:"<<age<<endl<<endl;
}
void Student::Input()
{
char na[10];
cout<<"输入姓 名:";
cin>>na ;
if(name)
delete []name;
name=new char[ strlen(na)+1];
strcpy( name, na );
cout<<"输入身份证:";
cin>>ID ;
cout<<"输入年 龄:";
cin>>age;
cout<<"输入专 业:";
cin>>speciality ;
cout<<"输入学 号:";
cin>>number;
count++; //此句增加,每输入一个,学生总数加1
}
void Student::Insert() //新增
{
if (!age) //当对象的age成员值为0时,就可以在此对象处重新输入以覆盖
Input();
}
void Student::Delete() //新增
{
age=0; //只简单地将age置0而不移动数组元素
count--;
}
int Student::GetCount( ) //新增静态成员函数,专门用来访问静态数据成员
{
return count;
}
//example4_06.cpp: 第三个文件,定义学生类的对象以及一些函数,完成程序功能
#include<iostream>
#include "example4_06_student.h"
using namespace std;
const int N=10;
void menu();
void OutputStu(const Student *array ); //指针形式参数前加const保护
void InputStu(Student *array);
int SearchStu(const Student *array, char *na); //指针形式参数前加const保护
bool InsertStu(Student *array);
bool DeleteStu(Student *array, char *na);
int main()
{
Student array[N];
int choice;
char na[20];
do
{
menu();
cout<<"Please input your choice:";
cin>>choice;
if( choice>=0 && choice <= 5 )
switch(choice)
{
case 1:InputStu(array);break;
case 2:
cout<<"Input the name searched:"<<endl;
cin>>na;
int i;
i=SearchStu(array, na);
if (i==N)
cout<<"查无此人!\n";
else
array[i].Display();
break;
case 3:OutputStu(array); break;
case 4: if (InsertStu(array))
cout<<"成功插入一条记录\n";
else
cout<<"插入失败!\n";
break;
case 5:
cout<<"Input the name deleted:"<<endl;
cin>>na;
if ( DeleteStu(array,na) )
cout<<"成功删除一条记录\n";
else
cout<<"删除失败!\n";
break;
default:break;
}
}while(choice);
return 0;
}
void menu()
{
cout<<"**********1.录入信息**********"<<endl;
cout<<"**********2.查询信息**********"<<endl;
cout<<"**********3.浏览信息**********"<<endl;
cout<<"**********4.插入信息**********"<<endl; //新增菜单
cout<<"**********5.删除信息**********"<<endl; //新增菜单
cout<<"**********0.退 出**********"<<endl;
}
void OutputStu(const Student *array )
{
cout<<"学生总人数="<<Student::GetCount()<<endl; //此句有修改
for(int i=0 ; i<N ; i++) //此句有修改,循环控制条件及输出条件
if (array[i].GetAge())
array[i].Display();
}
int SearchStu(const Student *array, char *na)
{
int i,j=N;
for(i=0 ; i<N ; i++) //此句有修改,循环控制条件
if (array[i].GetAge()) //保证是有效记录
if( strcmp( array[i].GetName() , na) == 0 )
{
j=i;
break;
}
return j;
}
void InputStu(Student *array ) //此函数与第三章中有较大修改,请注意
{
char ch;
int i=0;
do
{ if (Student::GetCount()==N)
cout<<"人数已满,无法继续录入!"<<endl;
if (!array[i].GetAge())
array[i++].Input();
cout<<"继续输入吗?(Y or N )"<<endl;
cin>>ch;
}while(ch=='Y');
}
bool InsertStu(Student *array)
{
if (Student::GetCount()==N) //判断是否有位置插入记录
{
cout<<"人数已满,无法插入记录!"<<endl;
return false;
}
for (int i=0; array[i].GetAge() ; i++); //找第一个年龄为0的空位置
array[i].Insert();
return true;
}
bool DeleteStu(Student *array, char *na)
{
if (Student::GetCount()==0)
{
cout<<"没有记录,无法删除!"<<endl;
return false;
}
int i=SearchStu(array, na); //调用查找函数,判断此人是否存在
if (i==N)
{
cout<<"查无此人,无法删除!\n";
return false;
}
array[i].Delete(); //如果存在,直接删除
return true;
}