`
_jia_shun
  • 浏览: 24902 次
  • 性别: Icon_minigender_1
  • 来自: 安徽
社区版块
存档分类
最新评论

C++大学基础教程_10_67_new和delete和static

 
阅读更多
//Employee.h
#ifndef EMPLOYEE_H_
#define EMPLOYEE_H_

class Employee
{
public:
	Employee(const char * const,const char * const);
	~Employee();
	const char *getFirstName() const;
	const char *getLastName() const;
	static int getCount();
private:
	char *firstName;
	char *lastName;

	static int count;
	
};

#endif

 

//Employee.cpp

#include "Employee.h"
#include <iostream>
#include <cstring>
using namespace std;

int Employee::count = 0;

int Employee::getCount()
{
	return count;
}

Employee::Employee(const char * const first,const char * const last)
{
	firstName = new char[strlen(first) + 1];
	strcpy(firstName,first);
	lastName = new char[strlen(last) + 1];
	strcpy(lastName,last);

	count++;
	cout << "Employee constructor for " << firstName << " " << lastName 
		   << " called." << endl;
}

Employee::~Employee()
{
	cout << "~Employee() called for " << firstName << " " << lastName
		    << endl;
	delete [] firstName;
	delete [] lastName;

	count--;
}

const char * Employee::getFirstName() const
{
	return firstName;
}

const char * Employee::getLastName() const
{
	return lastName;
}

 

//main.cpp

#include "Employee.h"
#include <iostream>
using namespace std;

//只要测试new 和delete的用法,以及static 类成员
int main()
{
	cout << "Number of employee before instantiation of any object is "
		   << Employee::getCount() << endl;
	
	Employee *ePtr1 = new Employee("Susan","Banke");
	cout << "Number of employees after objects are instantiated is " 
		   << ePtr1->getCount() << endl;
	Employee *ePtr2 = new Employee("Jax","Lanester");
	cout << "Number of employees after objects are instantiated is " 
		   << ePtr2->getCount() << endl;
	Employee *ePtr3 = new Employee("Bob","Javin");
	cout << "Number of employees after objects are instantiated is " 
		   << ePtr3->getCount() << endl;
	cout << "Number of  all employees after objects are instantiated is "
		   << Employee::getCount() 
	//    << ePtr1->getCount()
	//    << (*ePtr2).getCount()
	//	   << ePtr2->getCount()
	//	   << ePtr3->getCount()       //显示的结果上面全部是一样的,这就是static的用法
	       << endl;
	cout << "Employee 1:" 
		   << ePtr1->getFirstName() << " " 
		   << ePtr1->getLastName() << endl;
	cout << "Employee 2:"
		   << ePtr2->getFirstName() << " " 
		   << ePtr2->getLastName() << endl;
	cout << "Employee 3:" 
		   << ePtr3->getFirstName() << " " 
		   << ePtr3->getLastName() << endl;

	delete ePtr1;
	ePtr1 = 0;
	delete ePtr2;
	ePtr2 = 0;
	delete ePtr3;
	ePtr3 = 0;

	cout << "Number of employees after objects are deleted is "
		   << Employee::getCount() << endl;;
	system("pause >> cout");
	return 0;
}

 

 

  • 大小: 165 KB
分享到:
评论

相关推荐

    c++基础教程,简洁扼要

    适合新手的教程,我自己也在学,以下是部分学习笔记 69 函数指针 70 动态内存分配 Dynamic memory pointer = new type pointer = new type [elements] 第一个表达式用来给一个单元素的数据类型分配内存。第二个...

    C++大学教程,一本适合初学者的入门教材(part2)

    7.6 动态内存分配与new和delete运算符 7.7 static类成员 7.8 数据抽象与信息隐藏 7.8.1 范例:数组抽象数据类型 7.8.2 范例:字符串抽象数据类型 7.8.3 范例:队列抽象数据类型 7.9 容器类与迭代 7.10 ...

    C++大学教程,一本适合初学者的入门教材(part1)

    7.6 动态内存分配与new和delete运算符 7.7 static类成员 7.8 数据抽象与信息隐藏 7.8.1 范例:数组抽象数据类型 7.8.2 范例:字符串抽象数据类型 7.8.3 范例:队列抽象数据类型 7.9 容器类与迭代 7.10 ...

    传智播客扫地僧视频讲义源码

    本教程共分为5个部分,第一部分是C语言提高部分,第二部分为C++基础部分,第三部分为C++进阶部分,第四部分为C、C++及数据结构基础部分,第五部分为C_C++与设计模式基础,内容非常详细. 第一部分 C语言提高部分目录...

    C++大学教程

    C++大学教程(目录) 第1章 计算机与C++编程简介-------------------------------------------------1 1.1 简介--------------------------------------------------------------1 1.2 什么是计算机--------...

    C++ Primer中文版(第5版)李普曼 等著 pdf 1/3

     第Ⅰ部分 C++基础 27  第2章 变量和基本类型 29  2.1 基本内置类型 30  2.1.1 算术类型 30  2.1.2 类型转换 32  2.1.3 字面值常量 35  2.2 变量 38  2.2.1 变量定义 38  2.2.2 变量声明和定义的关系 41  ...

    C++Primer(第5版 )中文版(美)李普曼等著.part2.rar

     第Ⅰ部分 C++基础 27  第2章 变量和基本类型 29  2.1 基本内置类型 30  2.1.1 算术类型 30  2.1.2 类型转换 32  2.1.3 字面值常量 35  2.2 变量 38  2.2.1 变量定义 38  2.2.2 变量声明和定义的关系 41  ...

    java基础入门教程

    最 近 一 年 多 来 ,在 Internet上 出 现 的 特 别 吸 引 人 的 事 件 就是 Ja va语 言 和 用 Java编 写 的 浏 览 器 HotJava。 1991年 ,SUN MicroSystem 公 司 的 Jame Gosling、 Bill Joe等 人 , 为 在电视 、 ...

    Qt Creator 的安装和hello world 程序+其他程序的编写--不是一般的好

    一、Qt Creator 的安装和hello world 程序的编写(原创) 1.首先到Qt 的官方网站上下载Qt Creator,这里我们下载windows 版的。 下载地址:http://qt.nokia.com/downloads 如下图我们下载:Download Qt SDK for ...

    新概念C语言.李一波(带详细书签).pdf

    19.9 动态分配/撤销内存的运算符new和delete 223 19.10 小 结 224 第20章 C++的面向对象基础 226 20.1 概述 226 20.2 类和对象 227 20.3 构造函数和析构函数 230 20.3.1 构造函数 230 20.3.2 析构函数 232 ...

Global site tag (gtag.js) - Google Analytics