C++ Prime Plus 学习笔记027

书籍:C++ Primer Plus (第六版)(中文版)

工具:Dev-C++ 5.11

电脑信息:Intel® Xeon® CPU E5-2603 v3 @ 1.60GHz

系统类型:64位操作系统,基于X64的处理器 windows10 专业版

第12章 类和动态内存分配

12.1 动态内存和类

实例12.1

strngbad.h

cpp 复制代码
#include <iostream>
#ifndef STRNGBAD_H_
#define STRNGBAD_H_
class StringBad
{
	private:
		char *str;
		int len;
		static int num_strings;
	public:
		StringBad(const char * s);
		StringBad();
		~StringBad();
		friend std::ostream & operator<<(std::ostream & os, const StringBad & st);
};
#endif

strngbad.cpp

cpp 复制代码
#include <cstring>
#include "strngbad.h"
using std::cout;

int StringBad::num_strings = 0;

StringBad::StringBad(const char * s)
{
	len=std::strlen(s);
	str=new char[len+1];
	std::strcpy(str,s);
	num_strings++;
	cout<<num_strings<<": \""<<str<<"\" object created\n";
}

StringBad::StringBad()
{
	len=4;
	str=new char[4];
	std::strcpy(str,"C++");
	num_strings++;
	cout<<num_strings<<": \""<<str<<"\" default object created\n";
}

StringBad::~StringBad()
{
	cout<<"\""<<str<<"\" object deleted, \n";
	--num_strings;
	cout<<num_strings<<" left\n";
	delete [] str;
}

std::ostream & operator<<(std::ostream & os,const StringBad & st)
{
	os<<st.str;
	return os;
}

vegnews.cpp

cpp 复制代码
#include <iostream>
using std::cout;
#include "strngbad.h"

void callme1(StringBad &);
void callme2(StringBad);

int main()
{
	using std::endl;
	{
		cout<<"String an inner block.\n";
		StringBad headline1("Celery stalks at Midnight");
		StringBad headline2("Lettuce Prey");
		StringBad sports("Spinach Leaves Bowl for dollars.");
		cout<<"headline1: "<<headline1<<endl;
		cout<<"headline2: "<<headline2<<endl;
		cout<<"sports: "<<sports<<endl;
		callme1(headline1);
		cout<<"headline1: "<<headline1<<endl;
		callme2(headline2);
		cout<<"headline2: "<<headline2<<endl;
		cout<<"Initialize one object to another:\n";
		StringBad sailor = sports;
		cout<<"sailor: "<<sailor<<endl;
		cout<<"Assign one object to another:\n";
		StringBad knot;
		knot = headline1;
		cout<<"knot: "<<knot<<endl;
		cout<<"Exiting the block.\n";
	}
	cout<<"End of main()\n";
	return 0;
}

void callme1(StringBad & rsb)
{
	cout<<"String passed by refrence:\n";
	cout<<" \""<<rsb<<"\"\n";
}

void callme2(StringBad sb)
{
	cout<<"String passed by value:\n";
	cout<<" \""<<sb<<"\"\n";	
}

编译运行结果:

cpp 复制代码
String an inner block.
1: "Celery stalks at Midnight" object created
2: "Lettuce Prey" object created
3: "Spinach Leaves Bowl for dollars." object created
headline1: Celery stalks at Midnight
headline2: Lettuce Prey
sports: Spinach Leaves Bowl for dollars.
String passed by refrence:
 "Celery stalks at Midnight"
headline1: Celery stalks at Midnight
String passed by value:
 "Lettuce Prey"
"Lettuce Prey" object deleted,
2 left
headline2:
Initialize one object to another:
sailor: Spinach Leaves Bowl for dollars.
Assign one object to another:
3: "C++" default object created
knot: Celery stalks at Midnight
Exiting the block.
"Celery stalks at Midnight" object deleted,
2 left
"Spinach Leaves Bowl for dollars." object deleted,
1 left
"Spinach Leaves Bowl for dollars." object deleted,
0 left

--------------------------------
Process exited after 0.8916 seconds with return value 3221225477
请按任意键继续. . .

12.2 改造后的新string类

实例12.2

string1.h

cpp 复制代码
#ifndef STRING1_H_
#define STRING1_H_
#include <iostream>
using std::ostream;
using std::istream;

class String 
{
	private:
		char * str;
		int len;
		static int num_strings;
		static const int CINLIM = 80;
	public:
		String(const char * s);
		String();
		String(const String &);
		~String();
		int length() const {return len;}
		
		String & operator=(const String &);
		String & operator=(const char *);
		char & operator[] (int i);
		const char & operator[] (int i) const ;
		
		friend bool operator<(const String &st,const String &st2);
		friend bool operator>(const String &st1,const String &st2);
		friend bool operator==(const String &st,const String &st2);
		friend ostream & operator<<(ostream & os,const String & st);
		friend istream & operator>>(istream & is,String & st);
		
		static int Howmany();
};
#endif

string1.cpp

cpp 复制代码
#include <cstring>
#include "string1.h"
using std::cin;
using std::cout;

int String::num_strings = 0;

int String::Howmany()
{
	return num_strings;
}

String::String(const char * s)
{
	len=std::strlen(s);
	str=new char[len+1];
	std::strcpy(str,s);
	num_strings++;
}

String::String()
{
	len=4;
	str=new char[1];
	str[0]='\0';
	num_strings++;
}

String::String(const String & st)
{
	num_strings++;
	len=st.len;
	str=new char [len+1];
	std::strcpy(str,st.str);
}

String::~String()
{
	--num_strings;
	delete [] str;
}

String & String::operator=(const String & st)
{
	if(this == &st)
	{
		return *this;
	}
	delete [] str;
	len=st.len;
	str=new char[len+1];
	std::strcpy(str,st.str);
	return *this;
}

String & String::operator=(const char * s)
{
	delete [] str;
	len=std::strlen(s);
	str=new char[len+1];
	std::strcpy(str,s);
	return *this;
}

char & String::operator[](int i)
{
	return str[i];
}

const char & String::operator[](int i) const
{
	return str[i];
}

bool operator<(const String &st1,const String &st2)
{
	return (std::strcmp(st1.str,st2.str)<0);
}

bool operator>(const String &st1,const String &st2)
{
	return st2<st1;
}

bool operator==(const String &st1,const String &st2)
{
	return (std::strcmp(st1.str,st2.str)==0);
}

ostream & operator<<(ostream &os,const String & st)
{
	os<<st.str;
	return os;
}

istream & operator>>(istream & is,String &st)
{
	char temp[String::CINLIM];
	is.get(temp,String::CINLIM);
	if(is)
		st=temp;
	while(is && is.get() != '\n')
		continue;
	return is;
}

sayings1.cpp

cpp 复制代码
#include <iostream>
#include "string1.h"
const int ArSize =10;
const int MaxLen=81;

int main()
{
	using std::cout;
	using std::cin;
	using std::endl;
	String name;
	cout<<"Hi,what's your name?\n>> ";
	cin>>name;
	
	cout<<name<<",please enter upto "<<ArSize
		<<" short sayings <empty line to quit>:\n";
	String sayings[ArSize];
	char temp[MaxLen];
	int i;
	for(i=0;i<ArSize;i++)
	{
		cout<<i+1<<": ";
		cin.get(temp,MaxLen);
		while(cin && cin.get() != '\n')
			continue;
		if(!cin || temp[0] == '\0')
			break;
		else
			sayings[i]=temp;
	}
	int total = i;
	if(total>0)
	{
		cout<<"Here are your sayings:\n";
		for(i=0;i<total;i++)
			cout<<sayings[i][0]<<": "<<sayings[i]<<endl;
		int shortest=0;
		int first=0;
		for(i=1;i<total;i++)
		{
			if(sayings[i].length()<sayings[shortest].length())
			{
				shortest =i;
			}
			if(sayings[i]<sayings[first])
				first=i;
		}
		cout<<"shortest saying:\n"<<sayings[shortest]<<endl;
		cout<<"First alphabetically:\n"<<sayings[first]<<endl;
		cout<<"This program used: "<<String::Howmany()
			<<"String objects.Bye!\n";										
	}	
	else
	{
		cout<<"No input! Bye.\n";
	}

	return 0;
}

编译运行结果:

cpp 复制代码
Hi,what's your name?
>> Misty guts
Misty guts,please enter upto 10 short sayings <empty line to quit>:
1: a fool and his money are soon parted
2: pnney wise,pound foolish
3: the love of money is the root of much evil
4: cut of sight,out of mind
5: absence makes the heart fonder
6: absinthe makes the hart grow fonder
7:
Here are your sayings:
a fool and his money are soon parted: a fool and his money are soon parted
pnney wise,pound foolish: pnney wise,pound foolish
the love of money is the root of much evil: the love of money is the root of much evil
cut of sight,out of mind: cut of sight,out of mind
absence makes the heart fonder: absence makes the heart fonder
absinthe makes the hart grow fonder: absinthe makes the hart grow fonder
shortest saying:
pnney wise,pound foolish
First alphabetically:
a fool and his money are soon parted
My favorite saying:
the love of money is the root of much evil
Bye.

--------------------------------
Process exited after 175.1 seconds with return value 0
请按任意键继续. . .

12.3 在构造函数中使用new时应注意的事项

12.4 有关返回对象的说明

12.5 使用指向对象的指针

实例12.3

将实例12.2 的主文件修改为sayings2.cpp

cpp 复制代码
#include <iostream>
#include <cstdlib>
#include <ctime>
#include "string1.h"
const int ArSize =10;
const int MaxLen=81;

int main()
{
	using namespace std;
	String name;
	cout<<"Hi,what's your name?\n>> ";
	cin>>name;
	
	cout<<name<<",please enter upto "<<ArSize
		<<" short sayings <empty line to quit>:\n";
	String sayings[ArSize];
	char temp[MaxLen];
	int i;
	for(i=0;i<ArSize;i++)
	{
		cout<<i+1<<": ";
		cin.get(temp,MaxLen);
		while(cin && cin.get() != '\n')
			continue;
		if(!cin || temp[0] == '\0')
			break;
		else
			sayings[i]=temp;
	}
	int total = i;
	if(total>0)
	{
		cout<<"Here are your sayings:\n";
		for(i=0;i<total;i++)
			cout<<sayings[i]<<": "<<sayings[i]<<endl;
		
		String *shortest = &sayings[0];
		String * first = &sayings[0];
		for(i=1;i<total;i++)
		{
			if(sayings[i].length()<shortest->length())
				shortest = &sayings[i];
			if(sayings[i]<*first)
				first=&sayings[i];
		}

		cout<<"shortest saying:\n"<<*shortest<<endl;
		cout<<"First alphabetically:\n"<<*first<<endl;
		srand(time(0));
		int choice = rand()%total;
		String *favorite = new String(sayings[choice]);
		cout<<"My favorite saying:\n"<<*favorite<<endl;
		delete favorite;									
	}	
	else
	{
		cout<<"Not much to say,eh?\n";
	}
	
	cout<<"Bye.\n";

	return 0;
}

编译运行结果:

cpp 复制代码
Hi,what's your name?
>> Kirt Rood
Kirt Rood,please enter upto 10 short sayings <empty line to quit>:
1: a friend in need is a friend indeed
2: neither  a borrower nor a lender be
3: a stitch in time saves nine
4: a niche in time saves stine
5: it takes a crook to catch a crook
6: cold hands,warm heart
7:
Here are your sayings:
a friend in need is a friend indeed: a friend in need is a friend indeed
neither  a borrower nor a lender be: neither  a borrower nor a lender be
a stitch in time saves nine: a stitch in time saves nine
a niche in time saves stine: a niche in time saves stine
it takes a crook to catch a crook: it takes a crook to catch a crook
cold hands,warm heart: cold hands,warm heart
shortest saying:
cold hands,warm heart
First alphabetically:
a friend in need is a friend indeed
My favorite saying:
a friend in need is a friend indeed
Bye.

--------------------------------
Process exited after 124.1 seconds with return value 0
请按任意键继续. . .

实例12.4

placenew1.cpp

cpp 复制代码
#include <iostream>
#include <string>
#include <new>
using namespace std;
const int BUF = 512;
class JustTesting
{
	private:
		string words;
		int number;
	public:
		JustTesting(const string & s ="Just Testing",int n=0)
		{
			words =s;
			number=n;
			cout<<words<<" constructed\n";
		}
		~JustTesting()
		{
				cout<<words<<" destroyed\n";
		}
		void show() const
		{
			cout<<words<<", "<<number<<endl;
		}
};

int main()
{
	char * buffer = new char[BUF];
	JustTesting *pc1,*pc2;
	pc1=new (buffer) JustTesting;
	pc2=new JustTesting("Heap1",20);
	
	cout<<"Memory block addresses:\n"<<"buffer: "
		<<(void *)buffer<<" heap: "<<pc2<<endl;
	cout<<"Memory contents:\n";
	cout<<pc1<<": ";
	pc1->show();
	cout<<pc2<<": ";
	pc2->show();
	
	JustTesting *pc3,*pc4;
	pc3=new (buffer) JustTesting("Bad Idea",6);
	pc4=new JustTesting("Heap2",10);
	cout<<"Memory contents:\n";
	cout<<pc3<<": ";
	pc3->show();
	cout<<pc4<<": ";
	pc4->show();
	
	delete pc2;
	delete pc4;
	
	delete [] buffer;
	cout<<"Done\n";
	
	return 0; 
	
}

编译运行结果:

cpp 复制代码
Just Testing constructed
Heap1 constructed
Memory block addresses:
buffer: 0xb25940 heap: 0xb21900
Memory contents:
0xb25940: Just Testing, 0
0xb21900: Heap1, 20
Bad Idea constructed
Heap2 constructed
Memory contents:
0xb25940: Bad Idea, 6
0xb25bb0: Heap2, 10
Heap1 destroyed
Heap2 destroyed
Done

--------------------------------
Process exited after 0.2065 seconds with return value 0
请按任意键继续. . .

实例12.5

placenew2.cpp

cpp 复制代码
#include <iostream>
#include <string>
#include <new>
using namespace std;
const int BUF = 512;
class JustTesting
{
	private:
		string words;
		int number;
	public:
		JustTesting(const string & s ="Just Testing",int n=0)
		{
			words =s;
			number=n;
			cout<<words<<" constructed\n";
		}
		~JustTesting()
		{
				cout<<words<<" destroyed\n";
		}
		void show() const
		{
			cout<<words<<", "<<number<<endl;
		}
};

int main()
{
	char * buffer = new char[BUF];
	JustTesting *pc1,*pc2;
	pc1=new (buffer) JustTesting;
	pc2=new JustTesting("Heap1",20);
	
	cout<<"Memory block addresses:\n"<<"buffer: "
		<<(void *)buffer<<" heap: "<<pc2<<endl;
	cout<<"Memory contents:\n";
	cout<<pc1<<": ";
	pc1->show();
	cout<<pc2<<": ";
	pc2->show();
	
	JustTesting *pc3,*pc4;
	pc3=new (buffer + sizeof(JustTesting)) JustTesting("Better Idea",6);
	pc4=new JustTesting("Heap2",10);
	cout<<"Memory contents:\n";
	cout<<pc3<<": ";
	pc3->show();
	cout<<pc4<<": ";
	pc4->show();
	
	delete pc2;
	delete pc4;
	pc3->~JustTesting();
	pc1->~JustTesting();
	
	delete [] buffer;
	cout<<"Done\n";
	
	return 0; 
	
}

编译运行结果:

cpp 复制代码
Just Testing constructed
Heap1 constructed
Memory block addresses:
buffer: 0x1d5940 heap: 0x1d1900
Memory contents:
0x1d5940: Just Testing, 0
0x1d1900: Heap1, 20
Better Idea constructed
Heap2 constructed
Memory contents:
0x1d5950: Better Idea, 6
0x1d5bb0: Heap2, 10
Heap1 destroyed
Heap2 destroyed
Better Idea destroyed
Just Testing destroyed
Done

--------------------------------
Process exited after 0.2174 seconds with return value 0
请按任意键继续. . .
相关推荐
qq_571099351 小时前
学习周报二十五
学习
赖small强1 小时前
【Linux C/C++开发】Linux C/C++ 堆栈溢出:原理、利用与防护深度指南
linux·c语言·c++·stack·堆栈溢出
爱学习的梵高先生1 小时前
C++:基础知识
开发语言·c++·算法
oioihoii1 小时前
C++对象生命周期与析构顺序深度解析
java·开发语言·c++
IMPYLH1 小时前
Lua 的 tonumber 函数
开发语言·笔记·后端·junit·游戏引擎·lua
xlq223221 小时前
24.map set(下)
数据结构·c++·算法
Xudde.1 小时前
BabyPass靶机渗透
笔记·学习·安全·web安全
kblj55552 小时前
学习Linux——学习工具——DNS--BIND工具
linux·运维·学习
晚风吹长发2 小时前
初步了解Linux中文件描述符-fd
linux·运维·服务器·c++·开发·文件