第十四章编程练习(2)

本文介绍了一个名为WINE的C++编程练习,包括三个文件:WINE.h, WINE.cpp 和 main.cpp。这些文件可能涉及类定义、实现及主程序逻辑,旨在提升编程技能。" 132727131,19673332,Open3D点云随机颜色渲染编程指南,"['点云处理', '三维视觉', '几何处理', 'Open3D库', '编程']

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

WINE.h

#pragma once
#ifndef WINE_H_
#define WINE_H_
#include<valarray>
#include<iostream>
template <class T1,class T2>
class Pair {
private:
	T1 a;
	T2 b;
public:
	Pair();
	void setfist(const T1 & y) { a = y; };
	void setscond(const T2 & bo) { b = bo; };
	T1 & first();
	T2 & second();
	T1 fist()const{return a; };
	T2 second()const{return b; };
	Pair(const T1 & y, const T2 & b) :year(y), bottle(b) {};
};
class String {
private:
	char * str;
public:
	String();
	virtual ~String();
	String(char * ch);
	String(const String & st);
	String & operator=(const String & st);
	char * display()const { return str; };
	friend std::ostream & operator<<(std::ostream & os, const String & st);
};
typedef std::valarray<int> ArrayInt;
typedef Pair<ArrayInt, ArrayInt>PairArray;
class Wine :private PairArray ,String//普通类继承类模板需要将模板实例化
{
private:
	int years;
public:
	Wine(const String & l, int y, const int yr[], const int bot[]);
	Wine(const String & l, int y);
	void GetBottles();//提示用户输入年份
	void Label() { std::cout << (const String &)(*this); };//显示葡萄酒的名称,这里使用向上强制转换访问基类数据
	int sum() {return Pair::second().sum();};//返回瓶数的总和
	void Show()const;
	Wine & operator=(const Wine & w);
};
template<class T1, class T2>
inline Pair<T1, T2>::Pair()
{
	a = 0;
	b = 0;
}
template<class T1,class T2>
T1 & Pair<T1,T2>::first()
{
	return a;
}
template<class T1,class T2>
T2 & Pair<T1, T2>::second()
{
	return b;
}
#endif

WINE.cpp

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

Wine::Wine(const String & l, int y, const int yr[], const int bot[]):String(l)
{
	years = y;
	ArrayInt f(yr, y);//创建valarray数组
	ArrayInt b(bot, y);
	Pair::setfist(f);
	Pair::setscond(b);
}

Wine::Wine(const String & l, int y):String(l)
{
	Pair::setfist(ArrayInt(y));
	Pair::setscond(ArrayInt(y));
	years = y;
}

void Wine::GetBottles()
{
	using std::cin;
	using std::cout;
	for (int i = 0; i < years; i++)
	{
		cout << "Enter years: ";
		cin >> Pair::first()[i];//pa.first()返回一个valarray<int> a,所以这里等价于valarray<int>a[i]
		cout << "Enter bottle for the year: ";
		cin >> Pair::second()[i];
	}
}

void Wine::Show() const
{
	using std::cout;
	using std::endl;
	cout << "Wine:" << String::display() << endl;
	cout << "\tYear\tBottle" << endl;
	for (int i = 0; i < years; i++)
	{
		cout<<"\t"<< Pair::fist()[i] << "\t" << Pair::second()[i] << endl;
	}
}

Wine & Wine::operator=(const Wine & w)
{
	PairArray::operator=(w);//调用基类的赋值运算符
	String::operator=(w);
	years = w.years;
	return *this;
}

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

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

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

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

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

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

main.cpp

#include <iostream>
#include "WINE.h"
int main()
{
	using std::cout;
	using std::endl;
	using std::cin;
	cout << "Enter name of wine: ";
	char lab[50];
	cin.getline(lab, 50);
	cout << "Enter number of years: ";
	int yrs;
	cin >> yrs;
	Wine holding(lab, yrs);
	holding.GetBottles();
	holding.Show();
	const int YRS = 3;
	int y[YRS] = { 1993,1995,1998 };
	int b[YRS] = { 48,60,72 };
	Wine more("Gushing Grape Red", YRS, y, b);
	more.Show();
	cout << "Total bottles for ";
	more.Label();
	cout << ": " << more.sum() << endl;
	cout << "Bye\n";
	cin.get();
	cin.get();
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值