《C++ primer 5》 chapter 1.5 1.6

note

In C++ we define our own data structures by defining a class. 

A class defines a type along with a collection of operations that are related to that type. 


Header file names are derived from the name of a class defined in that header. 

Header files that we write usually have a suffix of .h, but some programmers use .H, .hpp, or .hxx. 

The standard library headers typically have no suffix at all. 

This program starts with two #include directives, one of which uses a new form. Headers from the standard library are enclosed in angle brackets (< >). Those that are not part of the library are enclosed in double quotes (" "). 


File redirect 

$ addItems outfile Assuming 

$ is the system prompt and our addition program has been compiled into an executable file named addItems.exe (or addItems on UNIX systems).

This command will read transactions from a file named infile and write its output to a file named outfile in the current directory. 


Member functions are sometimes referred to as methods.

The dot operator applies only to objects of class type. The left-hand operand must be an object of class type, and the right-hand operand must name a member of that type. The result of the dot operator is the member named by the right-hand operand. 

We call a function using the call operator (the () operator).


exercise

exercise 1.20

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

int main()
{
	Sales_item s;
	while(std::cin>>s)
	{
		std::cout<<s;
	}

	system("pause");
	return 0;
}
其中用到了头文件中的

std::istream& 
operator>>(std::istream& in, Sales_item& s)
{
    double price;
    in >> s.bookNo >> s.units_sold >> price;
    // check that the inputs succeeded
    if (in)
        s.revenue = s.units_sold * price;
    else 
        s = Sales_item();  // input failed: reset object to default state
    return in;
}

std::ostream& 
operator<<(std::ostream& out, const Sales_item& s)
{
    out << s.isbn() << " " << s.units_sold << " "
        << s.revenue << " " << s.avg_price();
    return out;
}


exercise 1.21

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

int main()
{
	Sales_item a,b;
	std::cin>>a>>b;
	if(compareIsbn(a,b))
	{
		std::cout<<a+b<<std::endl;
	}
	else
	{
		std::cerr<<"not the same ISBN"<<std::endl;
	}

	system("pause");
	return 0;
}

用到头文件的输入输出重载符以及

bool compareIsbn(const Sales_item &lhs, const Sales_item &rhs) 
{ return lhs.isbn() == rhs.isbn(); }
Sales_item 
operator+(const Sales_item& lhs, const Sales_item& rhs) 
{
    Sales_item ret(lhs);  // copy (|lhs|) into a local object that we'll return
    ret += rhs;           // add in the contents of (|rhs|) 
    return ret;           // return (|ret|) by value
}

exercise 1.22

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

int main()
{
	Sales_item tran,tran_curr,sum;
	std::cin>>tran;
	while(std::cin>>tran_curr)
	{
		if(compareIsbn(tran,tran_curr))
		{
			sum = tran + tran_curr;
			tran = sum;
		}
		else
		{
			std::cerr<<"must be the same ISBN"<<std::endl;
		}		
	}
	std::cout<<sum<<std::endl;
	system("pause");
	return 0;
}


exercise 1.23

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

int main()
{
	Sales_item item1,item2,item3,newItem;
	std::cin>>item1;
	while(std::cin>>newItem)
	{
		if(newItem.isbn() == item1.isbn())
		{
			item1 += newItem;
		}
		else if(newItem.isbn() == item2.isbn())
		{
			item2 += newItem;
		}else if(newItem.isbn() == item3.isbn())
		{
			item3 += newItem;
		} else
		{
			if (item2.isbn() == "")
			{
				item2 = newItem;
			}else
			{
				item3 = newItem;
			}
		}
	}
	std::cout<<item1<<std::endl<<item2<<std::endl<<item3<<std::endl;
	system("pause");
	return 0;
}

exercise 1.24

exercise 1.25


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值