复习题
//14.6
//1
公有,北极熊是一种熊
私有,家里有厨房
公有,程序员是一种人
私有,马和驯马师的组合包含一个人
人,公有,司机是一个人
汽车,私有,司机有汽车
//2
Gloam::Gloam(int g, const char* s) : fb(s), glip(g) {}
Gloam::Gloam(int g, const Frabjous& f) : fb(f), glip(g) {}
void Gloam::tell() {
fb.tell();
std::cout << "glib: " << glib >> endl;
}
//3
Gloam::Gloam(int g, const char* s) : Frabjous(s), glip(g) {}
Gloam::Gloam(int g, const Frabjous& f) : Frabjous(f), glip(g) {}
void Gloam::tell() {
Frabjous::tell();
std::cout << "glib: " << glib >> endl;
}
//4
class Stack<Worker*> {
private:
enum { MAX = 10 };
Worker* items[MAX];
int top;
public:
Stack();
bool isempty();
bool isfull();
bool push()(const Workser*& item);
bool pop(Worker*& item);
};
//5
ArrayTP<string>arr_str;
StackTP<ArrayTP<double>>sck_arr_db;
ArrayTP<StackTP<Worker*>>arr_stk_workerp;
4个
//6
如果两条继承路线有相同的祖先,则类中将包含祖先成员的两个拷贝
将祖先类作为虚基类可以解决这种问题
编程练习
第一题
#pragma once
//wine.h
#include<valarray>
#include<string>
template<class T1,class T2>
class Pair {
private:
T1 a;
T2 b;
public:
T1& first();
T2& second();
T1 first()const { return a; }
T2 second()const { return b; }
Pair(const T1& aval, const T2& bval) :a(aval), b(bval) {}
Pair() {}
};
class Wine {
private:
typedef std::valarray<int> ArrayInt;
typedef Pair<ArrayInt, ArrayInt> PairArray;
std::string label;
PairArray pair;
int yr_num;
public:
Wine() :label("None"), yr_num(0),pair() {};
Wine(const char* l, int y, const int yr[], const int bot[]);
Wine(const char* l, int y);
void GetBottles();
const std::string& Label()const;
int sum()const;
void show()const;
};
//wine.cpp
#include<iostream>
#include"wine.h"
using namespace std;
Wine::Wine(const char* l, int y, const int yr[], const int bot[])
:label(l), yr_num(y) {
ArrayInt a(y);
ArrayInt b(y);
for (int i = 0; i < y; i++) {
a[i] = yr[i];
b[i] = bot[i];
}
pair.first() = a;
pair.second() = b;
}
Wine::Wine(const char* l, int y) :label(l), yr_num(y) {
ArrayInt a(y);
ArrayInt b(y);
for (int i = 0; i < y; i++) {
a[i] = 0;
b[i] = 0;
}
pair.first() = a;
pair.second() = b;
}
void Wine::GetBottles() {
ArrayInt a(yr_num);
ArrayInt b(yr_num);
cout << "Enter " << label << " data for " << yr_num << " year(s):" << endl;
for (int i = 0; i < yr_num; i++) {
cout << "Enter year: ";
cin >> a[i];
cout << "Enter bottles for that years: ";
cin >> b[i];
}
pair.first() = a;
pair.second() = b;
}
const string& Wine::Label()const {
return label;
}
int Wine::sum()const {
int sum = 0;
for (int i = 0; i < yr_num; i++)
sum += pair.second()[i];
return sum;
}
void Wine::show()const {
cout << "Wine: " << label << endl;
cout << "\tyear\tBottles" << endl;
for (int i = 0; i < yr_num; i++)
cout << "\t" << pair.first()[i] << "\t" << pair.second()[i] << endl;
}
template<class T1, class T2>
T1& Pair<T1, T2>::first() {
return a;
}
template<class T1, class T2>
T2& Pair<T1, T2>::second() {
return b;
}
//main.cpp
#include"wine.h"
#include<iostream>
using namespace std;
int main() {
cout << "Enter name of wine: ";
char lab[50];
cin.getline(lab, 50);
cout << "Enter number of years:";
int years;
cin >> years;
Wine holding(lab, years);
holding.GetBottles();
holding.show();
const int YEARS = 3;
int y[YEARS] = { 1993,1995,1998 };
int b[YEARS] = { 48,60,72 };
Wine more("Gushing Grape Red", YEARS, y, b);
more.show();
cout << "Total bottles for " << more.Label()
<< ": " << more.sum() << endl;
cout << "Bye!";
return 0;
}
|