//Student.h
/*
私有继承:使用私有继承,基类的共有成员和保护成员都将成为派生类的私有成员
多重继承:表示可以有多个基类。
*/
#ifndef STUDENT
#define STUDENT
#include<iostream>
#include<string>
#include<array>
using std::string;
using std::cout;
using std::endl;
using std::array;
//名称类
class Name
{
protected:
string name;
public:
Name();
Name(string namep);
string getName();
};
//分数类
class Score
{
protected:
array<double,3> scores;
public:
Score();
Score(double score0, double score1, double score2);
array<double, 3> getScores();
};
class Student : private Name,private Score //私有多重继承
{
public:
Student();
Student(string namep,double score0, double score1, double score2 );
~Student();
friend std::ostream & operator<<(std::ostream &os, const Student &stu);
};
#endif // !STUDENT
//student.cpp
#include "Student.h"
Name::Name() {
name = "none";
}
Name::Name(string namep) {
name = namep;
}
string Name::getName() {
return name;
}
Score::Score() {
scores[0] = 0.0;
scores[1] = 0.0;
scores[2] = 0.0;
}
Score::Score(double score0, double score1, double score2) {
scores[0] = score0;
scores[1] = score1;
scores[2] = score2;
}
array<double, 3> Score::getScores() {
return scores;
}
Student::Student():Name(),Score()
{
}
Student::Student(string namep, double score0, double score1, double score2):Name(namep),Score(score0,score1,score2) {
}
Student::~Student()
{
}
std::ostream & operator<<(std::ostream &os, const Student &stu) {
cout << "name:" << stu.name << " scores:" << stu.scores[0] << "," << stu.scores[1] << "," << stu.scores[2] << endl;
return os;
}
int mainstudent() {
Student stu = Student("yueer",90.1,95.5,100);
cout << stu << endl;
//stu.getName(); 私有继承后外部不可访问基类中的成员函数
return 0;
}
//Stack.h
/*
使用template 定义模版
*/
#ifndef STACKN
#define STACKN
template <class Type>
class Stackn
{
private:
enum {MAX=5};
Type items[MAX];
int top;
public:
Stackn();
bool isEmpty();
bool isFull();
bool push(const Type &item);
bool pop(Type &item);
};
#endif // !STACKN
//Stackn.cpp
#include "Stackn.h"
#include "Student.h"
#include<iostream>
using std::count;
using std::endl;
template <class Type>
Stackn<Type>::Stackn()
{
top = 0;
}
template <class Type>
bool Stackn<Type>::isEmpty()
{
return top == 0;
}
template <class Type>
bool Stackn<Type>::isFull()
{
return top == MAX;
}
template <class Type>
bool Stackn<Type>::push(const Type &item) {
if (isFull()) {
cout << "stack is full" << endl;
return false;
}
items[top++] = item;
cout << "push stack[" << top - 1 << "] = " << item ;
return true;
}
template <class Type>
bool Stackn<Type>::pop(Type &item) {
if (isEmpty()) {
cout << "stack is empty" << endl;
return false;
}
item = items[--top];
cout << "pop stack[" << top << "] = " << item ;
return true;
}
int main() {
Student stu10("aaa", 1.1, 1.1, 1.1);
Student stu11("bbb", 2.1, 2.1, 2.1);
Student stu12("ccc", 1.1, 1.1, 1.1);
Student stu13("ddd", 1.1, 1.1, 1.1);
Student stu14("eee", 1.1, 1.1, 1.1);
Student stu15("fff", 1.1, 1.1, 1.1);
Stackn<Student> stackn;
stackn.push(stu10);
stackn.push(stu11);
stackn.push(stu12);
stackn.push(stu13);
stackn.push(stu14);
stackn.push(stu15);
Student stu;
stackn.pop(stu);
stackn.pop(stu);
stackn.pop(stu);
stackn.pop(stu);
stackn.pop(stu);
stackn.pop(stu);
cout << stu << endl;
return 0;
}
输出结果:
push stack[0] = name:aaa scores:1.1,1.1,1.1
push stack[1] = name:bbb scores:2.1,2.1,2.1
push stack[2] = name:ccc scores:1.1,1.1,1.1
push stack[3] = name:ddd scores:1.1,1.1,1.1
push stack[4] = name:eee scores:1.1,1.1,1.1
stack is full
pop stack[4] = name:eee scores:1.1,1.1,1.1
pop stack[3] = name:ddd scores:1.1,1.1,1.1
pop stack[2] = name:ccc scores:1.1,1.1,1.1
pop stack[1] = name:bbb scores:2.1,2.1,2.1
pop stack[0] = name:aaa scores:1.1,1.1,1.1
stack is empty
name:aaa scores:1.1,1.1,1.1