C++第四节:一个例子、静态成员
2015-07-29 19:32
405 查看
一个例子、静态成员
1、静态变量:
1.1 静态局部变量:存放在静态存储区,在两次函数调用期间保留原值仅在函数内部可见,可以被自动初始化为0
1.2 静态全局变量:作用域:仅在变量所在的文件内
2、类的静态成员:
2.1 静态成员的值和某一个具体对象无关
2.2 静态成员整个类只有一份拷贝,不需要每个对象都保留一份copy(而非静态成员,每个对象都有一个特定的值,每个对象都为自己的非静态成员,在内存中保留一个位置)
2.3 访问方式 类名::静态成员名
2.4 静态成员不应该让对象来调用(不推荐)
2.5 必须在类声明之后,用::初始化(不在头文件初始化)
3、静态成员函数:
3.1 被类的所有对象共享,不是和某个特定对象而是和整个类相关联
3.2 不可以使用this指针,因为this指针是指向每一个具体对象的,所以this指针本身是非静态成员变量
3.3 静态成员函数只可以访问静态成员,而不能访问非静态成员,反之,非静态成员可以访问静态成员
3.4 同样有public/private,私有静态成员函数不能在外部用类名或对象名访问,只能从类内部被其他静态成员函数访问
3.5 导入类所在头文件,就可以让多个文件调用(用类名调用),大大减少代码书写量
~~~一个例子~~~:三层复合类
Computer.h
静态成员
#include <iostream>
using namespace std;
class Employee
{
public:
Employee(char * name_str);
static void acc_total (float f); //静态成员函数
void print ();
int getId();
private:
char name[50]; //非静态成员变量,每个对象都有一个自己独立的拷贝,在内存中有单独的空间
int id;
static int next_id; //静态成员变量
static float total_pay;
};
//在类体之外,对静态成员变量进行初始化;静态成员变量的值也将在初始化后一直保持
int Employee::next_id = 0;
float Employee::total_pay = 0; //数据类型 类名::静态成员变量名 = 初始值
//非静态成员函数,可以访问静态成员(变量和函数)
Employee::Employee(char * name_str)
{
next_id++; //静态成员变量,值一直保持,属于整个类
id = next_id; //非静态成员变量,每个员工的id都不一样
//用静态成员变量next_id 的自增和值的保持,确保非静态成员变量id的值各不相同
strcpy(name, name_str);
cout << name << "id is " << id << endl;
}
int Employee::getId()
{
return id;
} //非静态成员函数
void Employee::acc_total(float f) //静态成员函数
{
total_pay += f; //累加
cout << "Total pay is " << total_pay << endl;
//静态成员函数,不能访问非静态成员变量,和非静态成员函数,反之可以
//静态成员函数是属于整个类的,和某个特定的对象无关,那就不应该访问属于某个特定对象的非静态成员
int getId();
//cout << name << "id is " <<id << endl;
}
void Employee::print()
{
cout << name << "id is " << id << endl;
}
int main ()
{
Employee Jack("Jack"); //1
Employee Tom("Tom"); //2
Employee Jacky("Jacky");
Employee Lina("Lina");
int i = Jack.getId(); //1
Jack.acc_total(100); //非静态成员变量,调用静态成员函数
Tom.acc_total(100);
Jacky.acc_total(100);
Lina.acc_total(100);
Employee::acc_total(100); //类名::调用静态成员函数
Employee::acc_total(100);
Employee::acc_total(100);
Employee::acc_total(100);
return 0;
}
1、静态变量:
1.1 静态局部变量:存放在静态存储区,在两次函数调用期间保留原值仅在函数内部可见,可以被自动初始化为0
1.2 静态全局变量:作用域:仅在变量所在的文件内
2、类的静态成员:
2.1 静态成员的值和某一个具体对象无关
2.2 静态成员整个类只有一份拷贝,不需要每个对象都保留一份copy(而非静态成员,每个对象都有一个特定的值,每个对象都为自己的非静态成员,在内存中保留一个位置)
2.3 访问方式 类名::静态成员名
2.4 静态成员不应该让对象来调用(不推荐)
2.5 必须在类声明之后,用::初始化(不在头文件初始化)
3、静态成员函数:
3.1 被类的所有对象共享,不是和某个特定对象而是和整个类相关联
3.2 不可以使用this指针,因为this指针是指向每一个具体对象的,所以this指针本身是非静态成员变量
3.3 静态成员函数只可以访问静态成员,而不能访问非静态成员,反之,非静态成员可以访问静态成员
3.4 同样有public/private,私有静态成员函数不能在外部用类名或对象名访问,只能从类内部被其他静态成员函数访问
3.5 导入类所在头文件,就可以让多个文件调用(用类名调用),大大减少代码书写量
~~~一个例子~~~:三层复合类
Computer.h
#ifndef __C__No729Class__Computer__ #define __C__No729Class__Computer__ #include <iostream> using namespace std; class Computer { public: Computer(int *l, double *w, double h); ~Computer(); void print(); private: int *lines; double *width; double height; }; #endif /* defined(__C__No729Class__Computer__) */Computer.cpp
#include "Computer.h" Computer::Computer(int *l, double *w, double h) { lines = new int(*l); width = w; height = h; } Computer::~Computer() { if(lines) { delete lines; lines = NULL; } cout << "析构Computer" << endl; } void Computer::print() { cout << "lines is " << *lines << ",width is " << *width << ",height is " << height << endl; }Student.h
#ifndef __C__No729Class__Student__ #define __C__No729Class__Student__ #include <iostream> #include "Computer.h" class Student { private: double weight; Computer *c; public: Student(int *l, double *w, double h, double e); ~Student(); void print(); }; #endif /* defined(__C__No729Class__Student__) */Student.cpp
#include "Student.h" Student::Student(int *l, double *w, double h, double e) { weight = e; c = new Computer(l, w, h); //指向堆空间,里面是一个Computer的对象 } Student::~Student() { if(c) { delete c; c = NULL; } cout << "析构Student" << endl; } void Student::print() { (*c).print(); cout << ",weight is " << weight << endl; }ClassRoom.h
#ifndef __C__No729Class__ClassRoom__ #define __C__No729Class__ClassRoom__ #include <iostream> #include "Student.h" class ClassRoom { public: ClassRoom(int *l, double *w, double h, double e, char *n); ~ClassRoom(); void print(); private: Student s1; Student *s2; char *name; }; #endif /* defined(__C__No729Class__ClassRoom__) */ClassRoom.cpp
#include "ClassRoom.h" ClassRoom::ClassRoom(int *l, double *w, double h, double e, char *n):s1(l, w, h, e) { s2 = new Student(l, w, h, e); //name = new char(*n); name = new char(strlen(n) + 1); strcpy(name ,n); } ClassRoom::~ClassRoom() { if(s2) { delete s2; s2 = NULL; } if(name) { delete name; name = NULL; } } void ClassRoom::print() { s1.print(); (*s2).print(); cout << "name is " << name << endl; }main.cpp
#include "ClassRoom.h" int main() { int a = 10; double c = 30, d = 40, e = 50; char *n = "小明"; Computer c1(&a, &c, d); c1.print(); Computer *c2 = new Computer(&a, &c, d); c2 -> print(); delete c2; c2 = NULL; Student st1(&a, &c, d, e); st1.print(); Student *st2 = new Student(&a, &c, d, e); (*st2).print(); delete st2; st2 = NULL; ClassRoom cr1(&a, &c, d, e, n); cr1.print(); ClassRoom *cr2 = new ClassRoom(&a, &c, d, e, n); (*cr2).print(); delete cr2; cr2 = NULL; return 0; }
静态成员
#include <iostream>
using namespace std;
class Employee
{
public:
Employee(char * name_str);
static void acc_total (float f); //静态成员函数
void print ();
int getId();
private:
char name[50]; //非静态成员变量,每个对象都有一个自己独立的拷贝,在内存中有单独的空间
int id;
static int next_id; //静态成员变量
static float total_pay;
};
//在类体之外,对静态成员变量进行初始化;静态成员变量的值也将在初始化后一直保持
int Employee::next_id = 0;
float Employee::total_pay = 0; //数据类型 类名::静态成员变量名 = 初始值
//非静态成员函数,可以访问静态成员(变量和函数)
Employee::Employee(char * name_str)
{
next_id++; //静态成员变量,值一直保持,属于整个类
id = next_id; //非静态成员变量,每个员工的id都不一样
//用静态成员变量next_id 的自增和值的保持,确保非静态成员变量id的值各不相同
strcpy(name, name_str);
cout << name << "id is " << id << endl;
}
int Employee::getId()
{
return id;
} //非静态成员函数
void Employee::acc_total(float f) //静态成员函数
{
total_pay += f; //累加
cout << "Total pay is " << total_pay << endl;
//静态成员函数,不能访问非静态成员变量,和非静态成员函数,反之可以
//静态成员函数是属于整个类的,和某个特定的对象无关,那就不应该访问属于某个特定对象的非静态成员
int getId();
//cout << name << "id is " <<id << endl;
}
void Employee::print()
{
cout << name << "id is " << id << endl;
}
int main ()
{
Employee Jack("Jack"); //1
Employee Tom("Tom"); //2
Employee Jacky("Jacky");
Employee Lina("Lina");
int i = Jack.getId(); //1
Jack.acc_total(100); //非静态成员变量,调用静态成员函数
Tom.acc_total(100);
Jacky.acc_total(100);
Lina.acc_total(100);
Employee::acc_total(100); //类名::调用静态成员函数
Employee::acc_total(100);
Employee::acc_total(100);
Employee::acc_total(100);
return 0;
}
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- 关于指针的一些事情
- c++ primer 第五版 笔记前言
- share_ptr的几个注意点
- Lua中调用C++函数示例
- Lua教程(一):在C++中嵌入Lua脚本
- Lua教程(二):C++和Lua相互传递数据示例
- C++联合体转换成C#结构的实现方法
- C++编写简单的打靶游戏
- C++ 自定义控件的移植问题
- C++变位词问题分析
- C/C++数据对齐详细解析
- C++基于栈实现铁轨问题
- C++中引用的使用总结
- 使用Lua来扩展C++程序的方法
- C++中调用Lua函数实例
- Lua和C++的通信流程代码实例
- C与C++之间相互调用实例方法讲解
- C++ Custom Control控件向父窗体发送对应的消息
- C++中拷贝构造函数的应用详解