C++primer第七章

(1)class与
  1. /* 
  2. C++ 中保留了C语言的 struct 关键字,并且加以扩充。在C语言中,struct 只能包含成员变量,不能包含成员函数。 
  3. 而在C++中,struct 类似于 class,既可以包含成员变量,又可以包含成员函数。 
  4.  
  5. C++中的 struct 和 class 基本是通用的,唯有几个细节不同: 
  6. 1:使用 class 时,类中的成员默认都是 private 属性的;而使用 struct 时,结构体中的成员默认都是 public 属性的。(最本质的区别) 
  7. 2:class 继承默认是 private 继承,而 struct 继承默认是 public 继承(《C++继承与派生》一章会讲解继承)。 
  8. 3:class 可以使用模板,而 struct 不能。 
  9. */  

(2)7_1

struct sales_data{

	string name;
	unsigned cnt;
	double price;
};
int main ()
{
	sales_data total;
	cout<<"请输入total"<<endl;
	if (cin>>total.name>>total.cnt>>total.price)
	{
		cout<<total.name<<"   "<<total.cnt<<"   "<<total.price<<"   "<<endl;
		sales_data trans;
		cout<<"请输入trans"<<endl;
		while (cin>>trans.name>>trans.cnt>>trans.price)
		{
			cout<<trans.name<<"   "<<trans.cnt<<"   "<<trans.price<<"   "<<endl;
			if (total.name==trans.name)
			{
				total.cnt+=trans.cnt;
			}
			else
			{
				cout<<"total.cnt*total.price"<<total.cnt*total.price<<endl;
				total=trans;
			}
		}

	}
	else
	{
		cerr<<"No data?"<<endl;
		return -1;
	}

	system("pause");
	return 0;
}

(3)7_2&7_3答案

struct sales_data{
	public:
		
			string name;
			unsigned cnt;
			double totalprice;
			string isbn() const{return name;}
			sales_data & combine(const sales_data &rhs);
	private:
	protected:

};
sales_data & sales_data::combine(const sales_data &rhs)
{
	cnt+=rhs.cnt;
	totalprice+=rhs.totalprice;
	return *this;

}
int main ()
{
	sales_data total;
	cout<<"请输入total"<<endl;
	if (cin>>total.name>>total.cnt>>total.totalprice)
	{
		cout<<total.name<<"   "<<total.cnt<<"   "<<total.totalprice<<"   "<<endl;
		sales_data trans;
		cout<<"请输入trans"<<endl;
		sales_data sum;
		sum=total;
		unsigned CNT=total.cnt;
		double TOTALPRICE=total.totalprice;
		while (cin>>trans.name>>trans.cnt>>trans.totalprice)
		{
			cout<<trans.name<<"   "<<trans.cnt<<"   "<<trans.totalprice<<"   "<<endl;
			if (total.isbn()==trans.isbn())
			{
				sum=total.combine(trans);//需要定义一个sales_data对象来引用cnt和totalprice,用total.combine(trans)直接引用不对
				CNT=sum.cnt;
				TOTALPRICE=sum.totalprice;
				
			}
			else
			{
				cout<<"CNT"<<CNT<<"TOTALPRICE"<<TOTALPRICE<<endl;
				total=trans;
			}
		}

	}
	else
	{
		cerr<<"No data?"<<endl;
		return -1;
	}

	system("pause");
	return 0;
}

(4) 7_9答案

//7_9 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
class person
{
public:
	string name;
	string addr;
	string isname() const{return name;}
	string isaddr() const{return addr;}
};
istream & read(istream &is,person &per)
{
	is>>per.name>>per.addr;//s>>per.name>>"">>per.addr;中间不能加""空格。

	return is;
}
ostream & print(ostream &os,const person &per)
{
	os<<per.name<<per.addr;//os中间可以加空格。为什么?

	return os;
}
int main ()
{
	person per1,per2;
	//read(cin,per2);
	if(read(read(cin,per2),per1));
	print(cout,per1);
	print(cout,per2);

	system("pause");
	return 0;
}

(5)7_11答案

struct sales_data{
	
		   sales_data(){};
		   sales_data(const string &s):name(s){}
		   sales_data(const string &s,unsigned n,double m):name(s),cnt(n),totalprice(m){}
		   sales_data::sales_data(istream &is)
				{
					read(is,*this);
				}
			string name;
			unsigned cnt;
			double totalprice;
			string isbn() const{return name;}
			sales_data & combine(const sales_data &rhs);
			istream &read(istream &is,sales_data &item);
			ostream &print(ostream &os,const sales_data &item);
	public:
	private:
	protected:

};

sales_data & sales_data::combine(const sales_data &rhs)
{
	cnt+=rhs.cnt;
	totalprice+=rhs.totalprice;
	return *this;

}
istream &sales_data::read(istream &is,sales_data &item)
{
	is>>item.name>>item.cnt>>item.totalprice;
	return is;
}
ostream &sales_data::print(ostream &os,const sales_data &item)
{
	os<<item.name<<"   "<<item.cnt<<"   "<<item.totalprice;
	return os;
}
sales_data add(const sales_data &lhs,const sales_data &rhs)
{
	sales_data sum=lhs;
	sum.combine(rhs);
	return sum;

}

int main ()
{
	sales_data total;
	sales_data total1("wo");
	sales_data total2("wo",2,3);
	sales_data total3(cin);
	total.print(cout,total);
	cout<<endl;
	total1.print(cout,total1);
	cout<<endl;
	total2.print(cout,total2);
	cout<<endl;
	total3.print(cout,total3);
	cout<<endl;

	system("pause");
	return 0;
}
(6) 7_13答案

struct sales_data{
	
		   sales_data(){};
		   sales_data(const string &s):name(s){}
		   sales_data(const string &s,unsigned n,double m):name(s),cnt(n),totalprice(m){}
		   sales_data::sales_data(istream &is);
				
			string name;
			unsigned cnt;
			double totalprice;
			string isbn() const{return name;}
			sales_data & combine(const sales_data &rhs);
			
	public:
	private:
	protected:

};
sales_data add(const sales_data &lhs,const sales_data &rhs);
istream &read(istream &is,sales_data &item);
ostream &print(ostream &os,const sales_data &item);
sales_data & sales_data::combine(const sales_data &rhs)
{
	cnt+=rhs.cnt;
	totalprice+=rhs.totalprice;
	return *this;

}
istream &read(istream &is,sales_data &item)
{
	is>>item.name>>item.cnt>>item.totalprice;
	return is;
}
ostream &print(ostream &os,const sales_data &item)
{
	os<<item.name<<"   "<<item.cnt<<"   "<<item.totalprice;
	return os;
}
sales_data add(const sales_data &lhs,const sales_data &rhs)
{
	sales_data sum=lhs;
	sum.combine(rhs);
	return sum;

}
sales_data::sales_data(istream &is)
				{
					read(is,*this);
				}
int main ()
{
	sales_data total(cin);
	cout<<"请输入total"<<endl;
	if (cin)
	{
		print(cout,total);
		sales_data trans;
		cout<<"请输入trans"<<endl;
		sales_data sum;
		sum=total;
		unsigned CNT=total.cnt;
		double TOTALPRICE=total.totalprice;
		while (read(cin,trans))//第二个读取操作不能用istream初始化构造函数,why?
		{
			print(cout,trans);;
			if (total.isbn()==trans.isbn())
			{

				sum=total.combine(trans);//需要定义一个sales_data对象来引用cnt和totalprice,用total.combine(trans)直接引用不对
				CNT=sum.cnt;
				TOTALPRICE=sum.totalprice;
				
			}
			else
			{
				cout<<"CNT"<<CNT<<"TOTALPRICE"<<TOTALPRICE<<endl;
				total=trans;
			}
		}

	}
	else
	{
		cerr<<"No data?"<<endl;
		return -1;
	}

	system("pause");
	return 0;
}




(7)7_21答案

struct sales_data{
	friend sales_data add(const sales_data &,const sales_data &);
	friend istream & read(istream &is,sales_data &item);
	friend ostream &print(ostream &os,const sales_data &item);
	public:
		sales_data(){};
		sales_data(const string &s):name(s){}
		sales_data(const string &s,unsigned n,double m):name(s),cnt(n),totalprice(m){}
		sales_data::sales_data(istream &is);
		string isbn() const{return name;}
		sales_data & combine(const sales_data &rhs);
	private:
		string name;
		unsigned cnt;
		double totalprice;
	protected:

};
sales_data add(const sales_data &lhs,const sales_data &rhs);
istream &read(istream &is,sales_data &item);
ostream &print(ostream &os,const sales_data &item);
sales_data & sales_data::combine(const sales_data &rhs)
{
	cnt+=rhs.cnt;
	totalprice+=rhs.totalprice;
	return *this;

}
istream &read(istream &is,sales_data &item)
{
	is>>item.name>>item.cnt>>item.totalprice;
	return is;
}
ostream &print(ostream &os,const sales_data &item)
{
	os<<item.name<<"   "<<item.cnt<<"   "<<item.totalprice;
	return os;
}
sales_data add(const sales_data &lhs,const sales_data &rhs)
{
	sales_data sum=lhs;
	sum.combine(rhs);
	return sum;

}
sales_data::sales_data(istream &is)
				{
					read(is,*this);
				}
int main ()
{
	cout<<"请输入total"<<endl;
	sales_data total(cin);
	if (cin)
	{
		print(cout,total);
		sales_data trans;
		cout<<"请输入trans"<<endl;
		while (read(cin,trans))//第二个读取操作不能用istream初始化构造函数,why?
		{
			print(cout,trans);;
			if (total.isbn()==trans.isbn())
			{
				total.combine(trans);//需要定义一个sales_data对象来引用cnt和totalprice,用total.combine(trans)直接引用不对	
			}
			else
			{
				print(cout,total);
				total=trans;
			}
		}

	}
	else
	{
		cerr<<"No data?"<<endl;
		return -1;
	}

	system("pause");
	return 0;
}

(8)7_27答案

class screen {
public:
	typedef string::size_type pos;
	screen(){}
	//screen (char c='   ', pos ht=0,pos wh=0,pos cur=0):height(ht),width(wh),cursor(cur),contents(ht*wh,c){}
	screen(char c,pos ht=0,pos wh=0,pos cur=0):height(ht),width(wh),cursor(cur),contents(ht*wh,c){}

	char get ()const;
	char get (pos ht,pos wd)const;
	screen &move(pos r ,pos c);

	void some_member ()const;

	screen &set (char);
	screen &set (pos,pos,char);

	screen &display(ostream &os){do_display(os);return *this;}
	const screen &display(ostream &os) const {do_display(os);return *this;}//第二个const有待进一步理解

private:
	pos cursor;
	pos height,width;
	string contents;
	mutable size_t access_ctr;

	void do_display (ostream &os )const {os<<contents;}
};
inline screen & screen::move(pos r ,pos c)
{
	pos row=r*width;
	cursor=row+c;
	return *this;
}
inline char screen::get(pos r ,pos c)const
{
	pos row=r*width;
	return contents[row+c];

}
inline char screen::get()const
{
	return contents[cursor];
}
void screen::some_member ()const
{
	++access_ctr;
}
inline screen& screen::set(char c)
{
	contents[cursor]=c;
	return *this;
}
inline screen& screen::set(pos r,pos col ,char c)
{
	contents[r*width+col]=c;
	return *this;
}        
int main()
{
	screen myscreen('X',5,5);
	myscreen.move(4,0).set('#').display(cout);
	cout<<"\n";
	myscreen.display(cout);
	cout<<"\n";

	system("pause");
	return 0;
}



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值