C++ Primer Plus 第9章

文章介绍了C++编程中的最佳实践,如避免在头文件中定义函数,使用extern声明外部变量,以及*和++运算符在指针操作中的区别。通过实例演示了结构、类、模板和内存管理的使用。
摘要由CSDN通过智能技术生成

1.请不要将函数定义或变量声明放在头文件中。例如,如果在头文件中包含一个函数定义,然后在其他两个文件(属于同一个程序)中包含该头文件。则同一个程序中将包含一个函数的两个定义,除非函数是内联的。头文件中常包含的内容包括:
(1)函数原型
(2)使用#define或const定义的符号常量
(3)结构声明
(4)类声明
(5)模板声明
(6)内联函数

2.C++提供了两种变量声明。一种是定义声明(或简称为定义),它给变量分配存储空间;另一种是引用声明(或简称为声明),它不给变量分配存储空间,因为他引用已有的变量。如果要在多个文件中使用外部变量,只需在一个文件中包含该变量的定义(单定义规则),但在使用该变量的其他所有文件中,都必须用关键字extern来声明它。

3.关于*与++前缀或后缀:
举下面的例子,编译器必须决定先对str递增,还是先对str解除引用:

char * str = "Whoa" ;
char ch = *str++ ;

后缀++运算符的优先级比一元运算符高,这意味着加号运算将对str进行操作,而不是对str进行操作。也就是说,将指针加1,使之指向下一个字符,而不是修改被指针指向的字符。不过,由于++是后缀形式,因此在将*str的值赋给ch后,再将指针加1。因此,上述表达式将字符W赋给ch,然后移动指针str,使之指向字符h
下面是一个类似的例子:

char * str = "Whoa" ;
char ch = *++str ;

前缀++运算符和一元运算符的优先级相同,但它们是从右到左结合的。因此,str(不是str)将被加1,因为++运算符是前缀形式,所以首先将str加1,然后将得到的指针执行解除引用的操作。因此,str将指向字符h,并将字符h赋给ch。


第九章编程题:
(1)
golf.h:

#ifndef COORDIN_H_
#define COORDIN_H_

const int Len = 40 ;
struct golf
{
	char fullname[Len] ;
	int handicap ;
} ;

void setgolf(golf & g, const char * name, int hc) ;
int setgolf(golf & g) ;
void handicap(golf & g, int hc) ;
void showgolf(const golf & g) ;

#endif

golf.cpp:

#include <iostream>
#include <cstring>
#include "golf.h"

void handicap(golf & g, int hc)
{
        g.handicap = hc ;
}

void setgolf(golf & g, const char * name, int hc)
{
	strcpy(g.fullname, name) ;
	handicap(g, hc) ;
}

int setgolf(golf & g) 
{
	using namespace std ;
	char fullname[Len] ;
	int handicap ;
	cout << "Please enter the information:\n" ;
	cout << "Enter fullname(Empty line to quit): " ;
	if(cin.get(fullname, Len).get() == -1) return 0 ;
	cout << "Enter handicap: " ;
        cin >> handicap ;
	cin.get() ;
	setgolf(g, fullname, handicap) ;
	return 1 ;
}

void showgolf(const golf & g)
{
	std::cout << "fullname = " << g.fullname << ", " ;
	std::cout << "handicap = " << g.handicap << std::endl ;
}

main.cpp:

#include <iostream>
#include "golf.h"

const int Num = 10 ;

int main()
{
	golf player[Num] ;
	int number = 0 ;
	for (int i=0; i<Num; i++)
	{
		if(!setgolf(player[i])) break ;
		else number ++ ;
	}
	std::cout << std::endl ;
	std::cout << "Total players:\n" ;
	for (int i=0; i<number; i++)
        {
		std::cout << "#" << i << ": " ;	
                showgolf(player[i]) ;
        }
	std::cout << "Bye!\n" ;
	return 0 ;
}

(2)

#include <iostream>
#include <string>
using namespace std ;

void strcount(string str) ;

int main()
{
	string input ;
	
	cout << "Enter a line:\n" ;
	getline(cin, input) ;
	while(input.size())
	{
		strcount(input) ;
		cout << "Enter next line (empty line to quit):\n" ;
		getline(cin, input) ;
	}
	cout << "Bye\n" ;
	return 0 ;
} 

void strcount(string str)
{
	static int total = 0 ;
	
	cout << "\"" << str << "\" contains " ;
	total += str.size() ;
	cout << str.size() << " characters\n" ;
	cout << total << " characters total\n" ;
}

(3)

#include <iostream>
#include <cstring>
#include <new>
using namespace std ;

struct chaff
{
	char dross[20] ;
	int slag ;
} ;

int main()
{
	cout << "The first way:\n" ;
	int array[100] ;
	char dross[20] ;
	chaff * t1 = new (array) chaff [2] ;
	
	for (int i=0; i<2; i++)
	{
		cout << "Dross: " ;
		cin.get(dross, 20).get() ;
		strcpy(t1[i].dross, dross) ;
		cout << "Slag: " ;
		cin >> t1[i].slag ;
		cin.get() ;
	}
	cout << endl ;
	for (int i=0; i<2; i++)
	{
		cout << "#" << i+1 << ": " ;
		cout << "dross = " << t1[i].dross << ", " ;
		cout << "slag = " << t1[i].slag << endl ;
	}
	
	cout << "The Second way:\n" ;
	int * Array = new int [100] ;
	chaff * t2 = new (Array) chaff [2] ;
	for (int i=0; i<2; i++)
    {
        cout << "Dross: " ;
        cin.get(dross, 20).get() ;
        strcpy(t2[i].dross, dross) ;
        cout << "Slag: " ;
        cin >> t2[i].slag ;
        cin.get() ;
    }
	cout << endl ;
    for (int i=0; i<2; i++)
    {
         cout << "#" << i+1 << ": " ;
         cout << "dross = " << t2[i].dross << ", " ;
         cout << "slag = " << t2[i].slag << endl ;
    }
	delete [] Array ;
}

(4)
Sale.h

#ifndef COORDIN_H_
#define COORDIN_H_

namespace SALES
{
	const int QUARTERS = 4 ;
	struct Sales
	{
		double sales[QUARTERS] ;
		double average ;
		double max ;
		double min ;
	} ;
	void setSales(Sales & s, const double ar[], int n) ;
	void setSales(Sales & s) ;
	void showSales(const Sales & s) ;
}

#endif

Sales.cpp

#include <iostream>
#include "Sale.h"

namespace SALES
{
	using namespace std ;

	void setSales(Sales & s, const double ar[], int n)
	{
		int i ;
		for (i=0; i<4&&i<n; i++)
			s.sales[i] = ar[i] ;
		while (i<4)
		{
			s.sales[i] = 0 ;
			i++ ;
		}
		
		s.min = s.max = s.sales[0] ;
		double total = 0 ;
		for (int i=0; i<4; i++)
		{
			if (s.sales[i] < s.min) s.min = s.sales[i] ;
			if (s.sales[i] > s.max) s.max = s.sales[i] ;
			total += s.sales[i] ;
		}
		s.average = total / 4 ;
	}
	
	void setSales(Sales & s)
	{
		cout << "Enter severl numbers:\n" ;
		double ar[4] = {0} ;
		for (int i=0; i<4; i++)
			cin >> ar[i] ;
		setSales(s, ar, 4) ;
	}
	
	void showSales(const Sales & s)
	{
		cout << "sales =" ;
		for (int i=0; i<QUARTERS; i++)
			cout << " " << s.sales[i] ;
		cout << " , " ;
		cout << "average = " << s.average << ", " ;
		cout << "max = " << s.max << ", " ;
		cout << "min = " << s.min << endl ;
	}
}

main.cpp

#include "Sale.h"
using namespace SALES ;

int main()
{
	Sales s1, s2 ;
	double sales[6] = {0.1, 0.5, 0.7, 0.3, 0.9, 1.1} ;
	setSales(s1) ;
	setSales(s2, sales, 6) ;
	showSales(s1) ;
	showSales(s2) ;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值