#include //for sqrt
#include //for strlen
#include//用到setw()和setprecision()
过程控制语句
break; 退出循环,不再执行该循环
continue;退出此次循环,执行下次循环
构造函数
//构造函数是成员函数,符合类中成员函数设计规则
#include<iostream>
using namespace std;
class Desk{ //桌子
public:
Desk(); //构造函数声明
private:
int high, width, length;
}; //----------------------------------
class Stool{ //凳子
public:
Stool(){ //类内定义构造函数
high=3; width=3; length=3;
cout<<“ ”<<high<<“ ”<<width<<“ ”<<length<<endl;
}
private:
int high, width, length;
}; //----------------------------------
Desk::Desk(){ //类外定义构造函数,要加类名::
high=5; width=5; length=5;
cout<<“ ”<<high<<“ ”<<width<<“ ”<<length<<endl;
}//------------------------------------
int main(){
Desk da; //自动调用构造函数Desk()
Stool sa; //自动调用构造函数Stool()
}
//析构函数名为波浪号加类名,无参数,无返回类型,作用一般是释放对象建立之初所占用的资源,改写上述代码如下
#include<iostream>
#include<algorithm>
using namespace std;
class A{
int* aa, num;
public:
A(int n){ num = n; aa = new int[n]; } //构造函数
~A(){ delete[] aa; } //析构函数无参数,在对象结束生命期之际由系统自动调用
void input(){ for(int i=0; i<num; i++) cin>>aa[i]; }
void proc(){ sort(aa, aa+num); }
void print(){ for(int i=0; i<num; i++) cout<<‘ ‘<<aa[i]; }
};
void f(){
A a(5); //创建对象
a.input();
a.proc();
a.print();
} //对象a在此处消灭,自动调用析构函数~A(),达到资源释放的目的
int main(){
f();
}
//输入 8 5 3 7 2
//输出 2 3 5 7 8
//C++成员函数可重载,构造函数同样也可重载
#include<iostream>
using namespace std;
class Tdate{
int month, day, year; // 默认private
public: // 以下四个重载函数,每个对应一种构建对象方式
Tdate();
Tdate(int d);
Tdate(int m,int d);
Tdate(int m,int d,int y);
//其他公共成员
};
Tdate::Tdate(){ month=4; day=15; year=1995; }
Tdate::Tdate(int d){ month=4; day=d; year=1996; }
Tdate::Tdate(int m,int d){ month=m; day=d; year=1997; }
Tdate::Tdate(int m,int d,int y){ month=m; day=d; year=y; }
int main(){
Tdate aday;
Tdate bday(10);
Tdate cday(2,12);
Tdate dday(1,2,1998);
}
//程序中,类内是一个世界,类外是一个世界
//类内数据由成员函数来设置和修改,不允许类外直接修改数据成员(private),保护类内世界的清静,数据错误由成员函数担责
//类外是调用该类成员函数的应用程序,无须也不允许关心类的细节
class Student{
int semesHour;
float gpa;
public:
float addCourse(int hours, float grade){
gpa = semesHour*gpa+grade*hours; //总分
semesHour += hours; //调整学时
gpa /= semesHour; //调整平均成绩
}
float grade(){ return gpa; } //获取平均成绩
int hours(){ return semesHour; } //获取学时
};
int main(){
Student s;
s.addCourse(3, 4.5); //通过该成员函数修改数据成员
cout<<s.gpa; //错:不该直接取用
cout<<s.grade();
s.semesHour += 1; //错:类外不允许访问类内私有数据
}
结构与数组
//批量访问结构变量用到结构数组
//若结构体大,则结构数组排序,最好用结构指针数组
#include<iostream>
using namespace std;
struct Person{ //结构定义与结构数组初始化可合成
char name[20];
unsigned long id;
float salary;
}a[6]={{"jone", 12345,339.0},{"david",13916,449.0},
{"marit",27519,311.0},{"jasen",42876,623.0},
{"peter",23987,400.0},{"yoke", 12335,511.0}};
int main(){
Person* pA[6]={&a[0],&a[1],&a[2],&a[3],&a[4],&a[5] };//指针数组
for(int i=1; i<6; i++)
for(int j=0; j<=5-i; j++)
if(pA[j]->salary > pA[j+1]->salary){ //比较工资
Person* tmp = pA[j]; //交换结构指针
pA[j] = pA[j+1];
pA[j+1] = tmp;
}
for(int k=0; k<6; k++) //通过结构指针,输出结构数组
cout<<pA[k]->name<<" "<<pA[k]->id<<" "<<pA[k]->salary<<endl;
}
//结构体属于自定义数据类型
//凡类型实体都可以作为函数参数传递
//为提高效率,最好用结构的引用传递函数参数
#include<iostream>
using namespace std;
struct Person {
char name[20];
unsigned long id;
float salary;
};
void print(Person& pr){ //输出结构实体,传递引用参数
cout<<pr.name<<" " //不影响结构实体分量的访问
<<pr.id<<" "<<pr.salary<<endl;
}
Person allone[4] = { {"jone", 12345, 339.0},
{"david", 13916, 449.0},
{"marit", 27519, 311.0},
{"yoke", 12335, 511.0} };
int main(){
for(int i=0; i<4; i++)
print(allone[i]);
}
//可将函数计算结果存于结构实体中,实现多值返回
#include <iostream>
using namespace std;
struct Person{
char name[20];
unsigned long id;
float salary;
};
Person getPerson(){ //返回结构实体
Person temp;
cout<<"Please enter a name,id,salary for one person:\n";
cin>>temp.name>>temp.id>>temp.salary;
return temp;
}
void print(Person& p){ //传递结构引用
cout<<p.name<<" "<<p.id<<" "<<p.salary<<endl;
}
int main(){
for(int i=0; i<3; i++)
print( getPerson() ); //获得结构实体予以输出
}
//传递结构引用参数同样可以实现计算结果的多值返回
//创建链表与遍历链表
//创建链表方式多样(此举一例),遍历链表总是从链首开始
#include<iostream>
using namespace std;
struct Student{ //结构体与链首指针
int number;
float score;
Student* next;
} *head = NULL; //全局链首指针指向空
void create(){ //创建链表函数,使全局链首指针不为空
Student *pEnd; //链尾结点指针,用于在其后面链入结点
for(int num; cin>>num && num; ){ //结点编号为0则结束
Student* pS = new Student; //新建一个结点,准备插入链表
pS->number = num;
cin>>pS->score; //结点赋值
(head ? Head : pEnd->next)=pS; //若空链,则接链首,否则挂链尾
pEnd = pS; //调整链尾指针
}
pEnd->next = NULL; //链尾结点指向空,表示链尾
}
void showList(){ //遍历输出链表
cout<<"now the items of list are \n";
for(Student* h = head; h; h=h->next) //临时结点指针h用以遍历
cout<<h->number<<","<<h->score<<endl;
}
int main(){
create();
showList();
}
//删除链表结点
//给出要删除的结点值,先要定位结点,故删除伴随查找
//假定全局链首指针Student* head;
void remove(int num){
if(head==NULL){ //排除空链情况,以免操作出错
cout<<“List NULL!\n”;
return;
}
Student* h = head;
if(head->number==num){ //删除结点在链首情形
head = head->next;
cout<<num<<“ the head of list have been deleted\n”;
delete h;
return;
}
for(Student* ph=h->next; !ph; h=ph, ph=ph->next)
if(ph->number==num){ //h与ph一前一后,若确认ph,则h好操作
cout<<num<<“ have been deleted\n”;
h->next = ph->next; //h跨过ph指向下一个结点
delete ph; //删除ph指向的结点
return;
}
}
//插入链表结点
//若给出插入位置(用结点指针表示)则插入表示为
void insert(int num, Student* pos){ //要插入在pos位置结点之后
Student* p = new Student; //创建新结点
p->number = num; //新结点赋值
p->next = pos->next; //新结点接入pos位置之后结点
pos->next = p; //pos位置结点链接上新结点
}
创建链表的过程可看作不断在链首插入结点的过程,也可看作不断在链尾插入结点的过程
简单程序回顾