(1)用static修饰类成员变量(属性),表明该变量是静态的,无论创建多少对象,都只创建一个一个静态属性副本,也就是对象们共享同一个静态属性,这个方法常用的一个用途就是用来计算程序调用了多少次这个类来创建对象也就是创建过多少个对象。

#ifndef TIME_H_
#define TIME_H_

#include <iostream>
using namespace std;

class Time
{
private:
	int hours;
	int minutes;
	int seconds;
	static int count;
public:
	Time();
	Time(int h = 0, int m = 0, int s = 0);

……

#include "Time.h"
#include <cstdlib>//支持abort()函数

int Time::count = 0;//初始化时不用加static

Time::Time()
{
	hours = minutes = seconds = 0;
	count++;
}

Time::Time(int h, int m, int s)
{
	if (h < 0 || h > 24 || m > 60 || m < 0 || s > 60 || s < 0)
	{
		cout << "初始化参数输入有误,程序终止!" << endl;
		abort();
	}
	hours = h;
	minutes = m;
	seconds = s;
	count++;
}

同时,若非整形/枚举型const的静态属性,都必须在实现文件(.cpp)中进行初始化,且初始化时独立于其他成员函数的,也就是不能在构造函数中初始化静态属性:

int Time::count = 0;//初始化时不用加static

若是整形/枚举型const的静态属性,才可以并且必须在声明文件(.h)中初始化,且在声明中初始化的整形/枚举型static属性,必须声明为const类型。

(2)同时这里说一下“返回对象的引用”这个方法,

Time Time::max(const Time &t1, const Time &t2)
{
	return t1;
}

这种返回方法需要创建一份t1对象的副本(调用复制构造函数),效率比较低。而下面这个方法:

const Time& Time::max(const Time &t1, const Time &t2)
{
	return t1;
}

则返回的是对象t1的引用(别名),效率更高,同时因为t1在参数中是声明为const类型的,所以返回值也必须声明为const(这里的const表明我返回给你的东西你不能进行修改),否则报错。

(3)最后再讲一个小知识点,就是用new关键字来创建类的对象,

Time *a = new TIme();

这句话之后必须同时搭配

delete a;

进行使用,而非系够函数自动delete。

这个知识点援引自《C++ primer plus》(第六版中文版)12.5.1节:

Time.h:

#pragma once
/*
* Time.h
*
*  Created on: 2016-4-16
*      Author: lvlang
*/

#ifndef TIME_H_
#define TIME_H_

#include <iostream>
using namespace std;

class Time
{
private:
	int hours;
	int minutes;
	int seconds;
	static int count;
public:
	Time();
	Time(int h = 0, int m = 0, int s = 0);//如果不传入值则自动初始化为0
	void AddHr(int h);
	void AddMin(int m);
	void reset(int h = 0, int m = 0, int s = 0);
	void show()const;//const在这里表明本函数不会也不能去修改属性
	Time sum(const Time &t)const; //传引用比传值效率高
	Time operator+(const Time &t)const;
	const Time &max(const Time &t1, const Time &t2);
	~Time();
};

#endif /* TIME_H_ */

Time.cpp:

/*
* Time.cpp
*
*  Created on: 2016-4-16
*      Author: lvlang
*/

#include "Time.h"
#include <cstdlib>//支持abort()函数

int Time::count = 0;

Time::Time()
{
	hours = minutes = seconds = 0;
	count++;
}

Time::Time(int h, int m, int s)
{
	if (h < 0 || h > 24 || m > 60 || m < 0 || s > 60 || s < 0)
	{
		cout << "初始化参数输入有误,程序终止!" << endl;
		abort();
	}
	hours = h;
	minutes = m;
	seconds = s;
	count++;
}

void Time::AddHr(int h)
{
	hours = (hours + h) % 24;
}

void Time::AddMin(int m)
{
	int temp = this->minutes + m;
	this->hours += temp / 60;
	this->minutes = temp % 60;
}

void Time::reset(int h, int m, int s)
{
	if (h < 0 || h > 24 || m > 60 || m < 0 || s > 60 || s < 0)
	{
		cout << "参数输入有误,程序终止!" << endl;
		abort();
	}
	this->hours = h;
	this->minutes = m;
	this->seconds = s;
}

void Time::show()const
{
	cout << "Hours: " << this->hours << " Minutes: " << this->minutes
		<< " Seconds: " << this->seconds <<" Count: "<<count<< endl;
}
Time Time::sum(const Time &t)const
{
	Time temp(0,0,0);
	int ts = (this->seconds + t.seconds) / 60;
	temp.seconds = (this->seconds + t.seconds) % 60;
	temp.minutes = (this->minutes + t.minutes + ts) % 60;
	int tm = (this->minutes + t.minutes + ts) / 60;
	temp.hours = (this->hours + t.hours + tm) % 24;
	return temp;
	//return *this;//返回当前对象(this为指向当前对象的指针)
}
Time Time::operator+(const Time &t)const
{
	Time temp(0,0,0);
	int ts = (this->seconds + t.seconds) / 60;
	temp.seconds = (this->seconds + t.seconds) % 60;
	temp.minutes = (this->minutes + t.minutes + ts) % 60;
	int tm = (this->minutes + t.minutes + ts) / 60;
	temp.hours = (this->hours + t.hours + tm) % 24;
	return temp;//return之后会自动调用一次析构函数把temp的空间回收
}

const Time& Time::max(const Time &t1, const Time &t2)
{
	return t1;
}

Time::~Time()
{
	cout << "析构函数被调用" << endl;
}

main.cpp

#include "Time.h"

int main()
{
	Time time(10,10,10);
	time.show();
	time.AddHr(2);
	time.show();
	time.AddMin(20);
	time.show();
	Time t(1, 1, 1);
	t.show();
	t.reset(9, 9, 9);
	t.sum(time);
	t.show();

	/*t = t + time;
	t.show();*/

	return 0;
}

static成员变量与返回对象的引用的更多相关文章

  1. C++中的static 成员变量的一些注意点

    C++中的static成员变量主要用来为多个对象共享数据 例: #include <iostream> using namespace std; class Student{ public ...

  2. static 成员变量、static 成员函数、类/对象的大小

    一.static 成员变量 对于特定类型的全体对象而言,有时候可能需要访问一个全局的变量.比如说统计某种类型对象已创建的数量. 如果我们用全局变量会破坏数据的封装,一般的用户代码都可以修改这个全局变量 ...

  3. 牛客网Java刷题知识点之关键字static、static成员变量、static成员方法、static代码块和static内部类

    不多说,直接上干货! 牛客网Java刷题知识点之关键字static static代表着什么 在Java中并不存在全局变量的概念,但是我们可以通过static来实现一个“伪全局”的概念,在Java中st ...

  4. java static成员变量方法和非static成员变量方法的区别

    这里的普通方法和成员变量是指,非静态方法和非静态成员变量首先static是静态的意思,是修饰符,可以被用来修饰变量或者方法. static成员变量有全局变量的作用       非static成员变量则 ...

  5. C++11类内static成员变量声明与定义

    众所周知,将一个类内的某个成员变量声明为static型,可以使得该类实例化得到的对象实现对象间数据共享. 在C++中,通常将一个类的声明写在头文件中,将这个类的具体定义(实现)写在cpp源文件中. 因 ...

  6. Runtime之成员变量&属性&关联对象

    上篇介绍了Runtime类和对象的相关知识点,在4.5和4.6小节,也介绍了成员变量和属性的一些方法应用.本篇将讨论实现细节的相关内容. 在讨论之前,我们先来介绍一个很冷僻但又很有用的一个关键字:@e ...

  7. 类的static成员变量和成员函数能被继承吗

    1.   父类的static变量和函数在派生类中依然可用,但是受访问性控制(比如,父类的private域中的就不可访问),而且对static变量来说,派生类和父类中的static变量是共用空间的,这点 ...

  8. static成员变量

    可以创建一个由同一个类的所有对象共享的成员变量.要创建这样的成员,只需将关键字 static 放在变量声明的前面,如下面的类所示: class StatDemo { private: static i ...

  9. C++ 虚指针、成员变量与类对象的偏移地址

    先给出一段代码实现 #include <iostream> using namespace std; class animal { protected: int age; public: ...

随机推荐

  1. 去掉文件夹的.svn文件

    加注册表 代码为: Windows Registry Editor Version 5.00 [HKEY_LOCAL_MACHINE"SOFTWARE"Classes"F ...

  2. android JNI开发

    1.NDK简介 NDK(Native Development Kit)NDK提供了一系列的工具,帮助开发者快速开发C(或C++)的动态库,并能自动将so和java应用一起打包成apk.NDK集成了交叉 ...

  3. centos 6.7安装与配置vncserver

    vnc是一款使用广泛的服务器管理软件,可以实现图形化管理,下面简单介绍一下如何在centos6.7下安装vnc. 安装vncserver  yum install tigervnc tigervnc- ...

  4. ExtJS Grid导出excel文件

    ExtJS Grid导出excel文件, 需下载POI:链接:http://pan.baidu.com/s/1i3lkPhF 密码:rqbg 1.将Grid表格数据连同表格列名传到后台 2.后台导出e ...

  5. CSS3--transform

    transform:向元素应用2D或3D转换,该属性允许我们对属性旋转,缩放,移动,或倾斜. transfrom:none:定义不进行转换 transfrom:matrix(n,n,n,n,n,n); ...

  6. VMware8.0虚拟机中安装Ubuntu12.04使用NAT设置连接网络

    之前一直尝试使用“桥接”的方法,但是一打开虚拟机,本机windows就断网.最后不得不换种方法,还好尝试了很多遍终于使用NAT设置成功的联网了. 说明:本机windows连接的是无线网. 1.检查自己 ...

  7. ES5——函数,对象,方法,this

    JS由表达式和语句组成 表达式:计算出一个值,但并不进行任何操作,不改变计算机运行状态 语句:包括 声明语句,赋值语句,控制结构 函数,对象,方法,this 数组和对象:是两个非常重要的数据类型 函数 ...

  8. js中转移符

    "<a href='javascript:;' onclick='javascript:changeChannelRuleStatus(\"" + options. ...

  9. 工作需求----表单多选框checkbox交互

    关于多选框,反选及选取几个: 1.html内容 <!--begin checkbox--> <div class="c_n_manage_tablexx"> ...

  10. C# 中的 Static

    今天测试了一下C#中 static 的初始化顺序: 1.调用时才初始化, 2.按照调用顺序初始化 3.先执行类的静态方法,然后初始化静态变量及方法 4.继承时,先执行子类的静态方法,然后执行父类的静态 ...