p427.4

 //头文件:
#include<iostream>
#ifndef STACK_H_
#define STACK_H_
typedef unsigned long Item; class Stack{
private:
enum{MAX=};
Item *pitems;
int size;
int top;
public:
Stack(int n = );
Stack(const Stack &st);
~Stack();
bool isempty()const;
bool isfull()const;
bool push(const Item & item);
bool pop();
Stack & operator=(const Stack & st);
friend std::ostream & operator<<(std::ostream & os, const Stack &st);
}; #endif //方法:
#include<iostream>
#include<cstring>
#include"Stack.h"
using std::cout;
using std::endl; Stack::Stack(int n){
size = n;
top = n;
pitems = new Item[n];
for (int i = ; i < size; i++)
pitems[i] = ;
} Stack::Stack(const Stack &st){
size = st.size;
top = st.top;
pitems = new Item[size];
for (int i = ; i < size; i++)
pitems[i] = st.pitems[i];
} Stack::~Stack(){
delete[]pitems;
} bool Stack::isempty()const{
if (top == )
return true;
else return false;
} bool Stack::isfull()const{
if (top == MAX)
return true;
else return false;
} bool Stack::push(const Item & item){
if (isfull()){
cout << "the stack is already full" << endl;
return false;
}
else{
Item *temp;
temp = new Item[size + ];
for (int i = ; i < size; i++)
*(temp + i) = *(pitems + i);
*(temp + size) = item;
delete[]pitems;
pitems = temp;
size++;
top++;
return true;
}
} bool Stack::pop(){
if (isempty()){
cout << "the stack is already empty" << endl;
return false;
}
else{
Item *temp;
temp = new Item[size - ];
for (int i = ; i < (size - ); i++)
temp[i] = pitems[i];
delete[]pitems;
pitems = temp;
size--;
top--;
return true;
}
} Stack & Stack::operator=(const Stack & st){
if (this == &st)
return *this;
size = st.size;
top = st.top;
delete[]pitems;
pitems = new Item[st.size];
for (int i = ; i < size; i++)
pitems[i] = st.pitems[i];
return *this;
} std::ostream & operator<<(std::ostream & os, const Stack &st){
os << "size: " << st.size << endl
<< "top: " << st.top << endl;
for (int i = ; i < st.size; i++)
os << st.pitems[i] << " ";
return os;
} //驱动:
#include<iostream>
#include<cstdlib>
#include"Stack.h"
using namespace std; int main(){
Stack ct1;
Stack ct2();
cout << "ct1 "<< ct1 <<endl;
cout << "ct2 "<< ct2 <<endl;
Stack ct3 = ct2;
cout << "ct3 "<< ct3 << endl;
ct1 = ct2;
cout << "ct1 " << ct1 << endl;
ct2.push();
cout << "ct2 " << ct2 << endl;
ct1.pop();
cout << "ct1 "<< ct1 << endl; system("pause");
return ;
}

p474.1

 //头文件:
class Cd{
private:
char performers[];
char label[];
int selections;
double playtime;
public:
Cd(char *s1, char *s2, int n, double x);
Cd(const Cd & st);
Cd();
virtual ~Cd();
virtual void Report()const;
Cd & operator = (const Cd & st);
}; class Classic : public Cd
{
private:
char production[];
public:
Classic(char *s1 = "nullbody1", char *s2 = "nullbody2", char *s3 = "nullbody3", int n = , double x = );
Classic(const Classic & st);
virtual void Report()const;
Classic & operator=(const Classic & st);
}; //方法:
#include<iostream>
#include<cstring>
#include"classic.h" using std::cout;
using std::endl; Cd::Cd(char *s1, char *s2, int n, double x){
strncpy(performers, s1, );
performers[] = '\0';
strncpy(label, s2, );
label[] = '\0';
selections = n;
playtime = x;
} Cd::Cd(const Cd & st){
strcpy(performers, st.performers);
strcpy(label, st.label);
selections = st.selections;
playtime = st.playtime;
} Cd::Cd(){
selections = ;
playtime = ;
} void Cd::Report()const{
cout << "performers: " << performers << endl
<< "label: " << label << endl
<< "selections: " << selections << endl
<< "playtime: " << playtime << endl;
} Cd & Cd::operator = (const Cd & st){
if (this == &st)
return *this;
strcpy(performers, st.performers);
strcpy(label, st.label);
selections = st.selections;
playtime = st.playtime;
return *this;
} Classic::Classic(char *s1, char *s2, char *s3, int n, double x): Cd(s2, s3, n, x){
strncpy(production, s1, );
production[] = '\0';
} Classic::Classic(const Classic & st): Cd(st){
strcpy(production, st.production);
} Cd::~Cd(){ } void Classic::Report()const{
cout << "production: " << production << endl;
Cd::Report();
} Classic & Classic::operator=(const Classic & st){
if (this == &st)
return *this;
Cd::operator=(st);
strcpy(production, st.production);
return *this;
} //驱动:
#include<iostream>
#include<cstdlib>
using namespace std;
#include"classic.h"
void Bravo(const Cd & disk); int main(){
Cd c1("Beatles", "Capitol", , 35.5);
Classic c2 = Classic("piano sonata in B flat", "Alfred Brendel", "Philips", , 57.17);
Cd *pcd = &c1; cout << "using object directly\n";
c1.Report();
c2.Report(); cout << "using type cd *pointer to objects:\n";
pcd->Report();
pcd = &c2;
pcd->Report(); cout << "calling a function with a Cd reference argument:\n";
Bravo(c1);
Bravo(c2);
cout << "testing assignment: ";
Classic copy;
copy = c2;
copy.Report(); system("pause");
return ;
} void Bravo(const Cd & disk){
disk.Report();
}

最新文章

  1. 学号20145332 《信息安全系统设计基础》实验五 简单嵌入式WEB服务器实验
  2. 进击的Python【第五章】:Python的高级应用(二)常用模块
  3. How to create/restore a slave using GTID replication in MySQL 5.6
  4. spring aop advice
  5. codeforce #339(div2)C Peter and Snow Blower
  6. Android 拖动条(SeekBar)实例 附完整demo项目代码
  7. POJ1125 Stockbroker Grapevine(最短路)
  8. 《Pointers On C》读书笔记(第四章 语句)
  9. MySQL的SELECT ...for update
  10. 【Socket编程】通过Socket实现TCP编程
  11. 【BZOJ1095】捉迷藏(动态点分治)
  12. vue中watch检测到不到对象属性的变化的解决方法
  13. [翻译] Visual Studio 2019: 极速编码. 智能工作. 创造未来.
  14. Systick时钟定时
  15. 微信小程序插件 - 开发教程
  16. Python实现的复杂的计算器的代码
  17. 让UI设计师崩溃的瞬间,你经历过哪些?
  18. install virtualenv
  19. iOS笔记UI--使用storyboard加入约束
  20. Spark(十二)SparkSQL简单使用

热门文章

  1. chromedriver bug
  2. iOS 更改导航栏返回button文字
  3. Android 匿名共享内存C++接口分析
  4. SP2-0618: Cannot find the Session Identifier. Check PLUSTRACE role is enabled
  5. Rabbit and Grass(杭电1849)(尼姆博弈)
  6. text选中后displa出label内容
  7. AdventureWorks2008 数据库安装
  8. C++_基础_类和对象3
  9. html+css基础
  10. java编辑器