(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. 使用ajaxfileupload插件进行Ajax Post 异步提交多个文件

    前台代码: <div> <div> <img src="images/pro_upload.png" onclick="javascript ...

  2. swift_Dictionary 字典

    // //  main.Swift //  字典 // //  Created by zhangbiao on 14-6-15. //  Copyright (c) 2014年 理想. All rig ...

  3. JAVA语法02之课程问题解决

    (一)示例程序+运行结果: ①EnumTest.java public class EnumTest { public static void main(String[] args) { Size s ...

  4. hihocoder-平衡树·SBT

    http://hihocoder.com/problemset/problem/1337 #1337 : 平衡树·SBT 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 ...

  5. Latex 建立带有竖线和编号的算法环境

    Latex 建立带有竖线和编号的算法环境 Latex源码: \documentclass{article} \usepackage{amssymb} \usepackage{amsmath} \use ...

  6. JS/jquery获取iframe内部元素和ifame中获取外部元素精华

    1.从外部获取iframe内部元素方法: js : window.frames['frame'].document.getElementById("imglist");   //f ...

  7. EF6 CodeFirst 实践系列文章列表

    2015 Jul.16 EF6 CodeFirst+Repository+Ninject+MVC4+EasyUI实践(一) 来自:wangweimutou 本系列源自对EF6 CodeFirst的探索 ...

  8. AndroidTouch事件总结

    1.自定义的控件几乎都要用到触摸事件,不交互怎么响应,相关的事件处理函数由dispatchTouchEvent.onInterceptTouchEvent.onTouchEvent,处理这些事件的由v ...

  9. IOS Core Animation Advanced Techniques的学习笔记(四)

    第五章:Transforms   Affine Transforms   CGAffineTransform是二维的     Creating a CGAffineTransform   主要有三种变 ...

  10. Linq to entities 学习笔记

    Linq to  entities ---提供语言集成查询支持用于在概念模型中定义的实体类型. 首先可以根据http://msdn.microsoft.com/en-us/data/jj206878该 ...