您的位置:首页 > 编程语言 > C语言/C++

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

#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++ 课程