C++primer第七章

  1. / * 
  2. C ++中保留了C语言的结构关键字,并且加以扩展。在C语言中,struct只能包含成员变量,不能包含成员函数。 
  3. 而在C ++中,struct类似于class,既可以包含成员变量,又可以包含成员函数。 
  4.  
  5. C ++中的结构和类基本是通用的,唯有几个细节不同: 
  6. 1:使用class时,类中的成员默认都是private属性的;而使用struct时,结构体中的成员默认都是public属性的。(最本质的区别) 
  7. 2:class继承默认是private继承,而结构继承默认是公共继承(“C ++继承与派生”一章会讲解继承)。 
  8. 3:class可以使用模板,而struct不能。 
  9. * /  
  1. / * 
  2. C ++中保留了C语言的结构关键字,并且加以扩展。在C语言中,struct只能包含成员变量,不能包含成员函数。 
  3. 而在C ++中,struct类似于class,既可以包含成员变量,又可以包含成员函数。 
  4.  
  5. C ++
    struct sales_data {
    
    	字符串名称;
    	无符号的cnt;
    	双重价格;
    };
    int main()
    {
    	sales_data total;
    	COUT << “请输入总” << ENDL;
    	如果(cin >> total.name >> total.cnt >> total.price)
    	{
    		cout << total.name <<“”<< total.cnt <<“”<< total.price <<“”<< endl;
    		sales_data trans;
    		COUT << “请输入反式” << 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;
    			}
    			其他
    			{
    				COUT << “total.cnt * total.price” << total.cnt * total.price << ENDL;
    				总=反;
    			}
    		}
    
    	}
    	其他
    	{
    		cerr <<“没有数据?”<< endl;
    		返回-1;
    	}
    
    	系统( “暂停”);
    	返回0;
    }

    中的结构和类基本是通用的,唯有几个细节不同: 
  6. 1:使用class时,类中的成员默认都是private属性的;而使用struct时,结构体中的成员默认都是public属性的。(最本质的区别) 
  7. 2:class继承默认是private继承,而结构继承默认是公共继承(“C ++继承与派生”一章会讲解继承)。 
  8. 3:class可以使用模板,而struct不能。 
  9. * /  

struct sales_data {

	字符串名称;
	无符号的cnt;
	双重价格;
};
int main()
{
	sales_data total;
	COUT << “请输入总” << ENDL;
	如果(cin >> total.name >> total.cnt >> total.price)
	{
		cout << total.name <<“”<< total.cnt <<“”<< total.price <<“”<< endl;
		sales_data trans;
		COUT << “请输入反式” << 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;
			}
			其他
			{
				COUT << “total.cnt * total.price” << total.cnt * total.price << ENDL;
				总=反;
			}
		}

	}
	其他
	{
		cerr <<“没有数据?”<< endl;
		返回-1;
	}

	系统( “暂停”);
	返回0;
}
struct sales_data {
	上市:
		
			字符串名称;
			无符号的cnt;
			双倍总价;
			string isbn()const {return name;}
			sales_data&combine(const sales_data&rhs);
	私人的:
	保护:

};
sales_data&sales_data :: combine(const sales_data&rhs)
{
	CNT + = rhs.cnt;
	totalprice + = rhs.totalprice;
	返回*这个;

}
int main()
{
	sales_data total;
	COUT << “请输入总” << ENDL;
	如果(cin >> total.name >> total.cnt >> total.totalprice)
	{
		cout << total.name <<“”<< total.cnt <<“”<< total.totalprice <<“”<< endl;
		sales_data trans;
		COUT << “请输入反式” << ENDL;
		sales_data总和;
		总和=总;
		无符号的CNT = total.cnt;
		双TOTALPRICE = total.totalprice;
		while(cin >> trans.name >> trans.cnt >> trans.totalprice)
		{
			cout << trans.name <<“”<< trans.cnt <<“”<< trans.totalprice <<“”<< endl;
			if(total.isbn()== trans.isbn())
			{
				总和= total.combine(反式); //需要定义一个SALES_DATA对象来引用CNT和totalprice,用total.combine(反式)直接引用不对
				CNT = sum.cnt;
				TOTALPRICE = sum.totalprice;
				
			}
			其他
			{
				COUT << “CNT” << CNT << “TOTALPRICE” << TOTALPRICE << ENDL;
				总=反;
			}
		}

	}
	其他
	{
		cerr <<“没有数据?”<< endl;
		返回-1;
	}

	系统( “暂停”);
	返回0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值