`

c++ - a sample class to show common aspects of object initialization/uninit

    博客分类:
  • c++
c++ 
阅读更多

The following classes show many a apsect of object life-cycle management, including the single argument constructor, copy constructor, default constructor, expclit constructor,  destructor and the non-static member and static member, and how to define/impl those classes

 

We might use the definition on this chapter later for other illustration. 

 

/**
* file
*  acct.h
* description: 
*   the class definition of the Account class
*
*/
class Account {

public:

/* =================
* ctor and dtors
========================*/
	Account();
	// since a constructor that takes only a single parameter can serves as a conversion operator
	// we don't want tripwire people into the trap of 
	//  Account acct = "new user";
	// to help curb/reign this kind of unwantted/unwelcome compiler aid, the explicit keyword is introduced 
	//  
	explicit
	Account(const char *, double = 0.0);
	
	//// for the purpose of demo the use of the array initalizer, we decide to use the implicit constructor approach
	//Account(const char *, double = 0.0);

	// the copy constructor 
	// the most important thing about the copy constructor is to decide whether or not to provide the copy constructor
	// the how as to implements the copy constructor comes seconds
	Account(const Account & );
	~Account();
	const char * name();

private:
	char * _name;
	unsigned int _acct_nmbr;
	double _balance;
	
	static unsigned int _unique_acct_nmbr;
	static unsigned int get_unique_acct_nmbr();
	static void return_acct_nmbr(unsigned int acct_nmbr);
protected:
};

 

 

and below is the definition/implementatio of the classes..

 

/**
* file 
*   acct.cpp
* description: 
*   the implementation source file of the acct.h
*/


#include "stdafx.h"
#include "acct.h"
#include <iostream>
#include <typeinfo>
#include <exception>
#include <fstream>
#include <functional>
#include <algorithm>
#include <vector>
#include <list>
#include <array>
#include <iterator>
#include <cstring>
#include <memory>

using std::cout;
using std::endl;
using std::end;
using std::cerr;
using std::fstream;
using std::vector;
using std::list;

using std::strcpy;
using std::strcat;
using std::strlen;

using std::auto_ptr;


/*=================================
* ctor(s) and dtor(s)
==================================*/

// for non-class members, it does not matter if you initialize the non-class member in the initializer list of you initialize the 
// the non-class member inside the constructor's body
// the convention is to initialize the non-class members inside the ctor's body, while initialize the class members in the initializer's initializer list
Account::Account() {
	_balance = 0.0;
	_name = 0;
	_acct_nmbr = 0;
	_acct_nmbr = get_unique_acct_nmbr();

	cout << "Account::Account" << endl;
}

// As said before,you can initialize the non-class member in the initliazer list. 
//Account::Account() :
//   _balance (0.0), _name(0), _acct_nmber(0)
//{
//}

// though the declaration of the Account is as such
// Account(const char * naem_, double balance_ = 0.0);
// but in our discussion, we can NOT redefine the default value of the balance_
Account::Account(const char * name_, double balance_) {
	cout << "Account::Account(const char * name_, double balance_)" << endl;

	if (name_ != NULL) {
		// thereis no need to call the delete operator because 
		// in the intializer, the name_ is guaranteed to be filled with som randome bitness
		// while the static members will be filled with zero or its default values (normally it is zeros)
		//delete name_;
		_name = new char[strlen(name_) + 1];
		strcpy(_name, name_);
	}
	_balance = balance_;

	_acct_nmbr = get_unique_acct_nmbr();
}

Account::Account(const Account& rhs) 
	: _balance(rhs._balance)
{
	cout << "Account::Account(const Account & rhs)" << endl;
	_name = new char[strlen(rhs._name) + 1];
	strcpy(_name, rhs._name);

	// must copy rhs._acct_nmbr
	_acct_nmbr = get_unique_acct_nmbr();
}

unsigned int 
	Account::_unique_acct_nmbr;

unsigned int
Account::get_unique_acct_nmbr() { 

	return ++_unique_acct_nmbr;
}

void 
	Account::return_acct_nmbr(unsigned int acct_nmbr) { 

}

inline
const char *
	Account::name() {
		return _name;
}

// Destructor server primarily to relinquish resources acquired either within the constructor or urnig the lifetime 
// thelifetime of the class object, again such as freeing a mutal exclsion lock or deleting memory allocated through operator new.
Account::~Account()  
{
	delete _name;
	return_acct_nmbr(_acct_nmbr);

	// there is no need to do the check 
	// because the compiler will ensure that. 
	//if (_name != NULL ) delete _name;
	// and since the object is destructed, there is no need to rest to some uninitialized value.
	//_name = 0;
	//_balance = 0;
	//_acct_nmbr = 0;
}

Account global;

void destructor_life_cycle_test()
{
	Account local("Anna live Plurablle", 1000);
	Account &loc_ref = global;
	auto_ptr<Account> pact(new Account("stephen Dedalus"));

	{
		Account local_too("Stephen Hero");
		// the local_too will destroy before exit the current local space. 
	}
	// the auto_ptr will destruct here

}
 
分享到:
评论

相关推荐

    Large-Scale C++ Software Design

    It is the first C++ book that actually demonstrates how to design large systems, and one of the few books on object-oriented design specifically geared to practical aspects of the C++ programming ...

    开发工具 spring-aspects-4.3.6.RELEASE

    开发工具 spring-aspects-4.3.6.RELEASE开发工具 spring-aspects-4.3.6.RELEASE开发工具 spring-aspects-4.3.6.RELEASE开发工具 spring-aspects-4.3.6.RELEASE开发工具 spring-aspects-4.3.6.RELEASE开发工具 spring...

    hibernate in action

    Both an introduction to the theoretical aspects of automated object/relational mapping and a practical guide to the use of Hibernate, this book provides extensive example code to implement an online ...

    django-master-class

    Scaling a Django application requires careful consideration of various aspects such as caching, load balancing, and database management. **Key Concepts:** - **Caching Strategies:** Implementing ...

    java与c++、c语言的比较文献翻译

    Although it is based on C++, Java is more of a “pure” object-oriented language. Both C++ and Java are hybrid languages, but in Java the designers felt that the hybridization was not as important as ...

    C++CLI:The Visual C++ Language for .NET

    "C++/CLI: The Visual C++ Language for .NET introduces Microsoft's extensions to the C++ syntax that allow you to target the common language runtime the key to the heart of the .NET 3.0 platform....

    Modern C++ Programming Cookbook

    Modern C++ Programming Cookbook ...This book follows a recipe-based approach, with examples that will empower you to implement the core programming language features and explore the newer aspects of C++.

    Object- Oriented Programming with Ansi-C

    automatic initialization to prevent another class of bugs. Chapter ten introduces delegates and shows how classes and callback functions cooperate to simplify, for example, the constant chore of ...

    JPEG2000 C语言源代码全集

    interpolation of components to a common resolution; application of registration offsets in the CRG marker segment; and colour conversion to an appropriate rendering space (sRGB here). The "kdu_...

    ssh+mysql55jar包集合

    /xscjManager/WebContent/WEB-INF/lib/spring-aspects.jar /xscjManager/WebContent/WEB-INF/lib/spring-beans.jar /xscjManager/WebContent/WEB-INF/lib/spring-context.jar /xscjManager/WebContent/WEB-INF/lib/...

    mybatis与spring整合全部jar包(包括springmvc).rar

    3.2.7/mybatis-spring-1.2.2/mysql-connector-java-5.1.7-bin/slf4j-api-1.7.5/slf4j-log4j12-1.7.5/spring-aop-4.1.3.RELEASE/spring-aspects-4.1.3.RELEASE/spring-beans-4.1.3.RELEASE/spring-context-4.1.3....

    Python Package to Visualize different aspects of a Neural Networ

    Python Package to Visualize different aspects of a Neural Network

    Addison.Wesley.C++.by.Dissection.2002.pdf

    The book is structured into chapters that cover various aspects of C++, from the basics to more advanced topics. Here, we will summarize the key points and concepts from each chapter: ### Chapter 1:...

    C++/CLI Primer: For .NET Development

    This book presents some of the important aspects of the C++/CLI language that often become a barrier preventing programmers from exploring further. The C++/CLI Primer is a powerful but compact book ...

    spring-aspects

    《深入剖析Spring Aspects——基于源码的探索》 Spring Aspects是Spring框架的一个重要组成部分,它提供了面向切面编程(AOP)的功能,使得开发者能够以声明式的方式管理横切关注点,如日志、事务管理等。本文将...

    Information-Theoretic Aspects of Neural Networks

    2.5.2 Representation of a Neuron as an Input-Dictated Cybernetic Regulator with a Quadratic Cost-function 2.5.3 Generalized Information-Theoretic Entropy Measure 2.5.4 Influence of Nonlinear ...

    C++ CLI Primer(Apress,2016).pdf

    This book presents some of the important aspects of the C++/CLI language that often become a barrier preventing programmers from exploring further. The C++/CLI Primer is a powerful but compact book ...

    Java语言规范

    is related to C and C++ but is organized rather differently, with a number of aspects of C and C++ omitted and a few ideas from other languages included. It is intended to be a production language, ...

Global site tag (gtag.js) - Google Analytics