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();
}

c++ primer plus 习题答案(7)的更多相关文章

  1. c++ primer plus 习题答案(1)

    c++ primer plus 习题答案用的是第五版,IDE仍然是vs2013.我只标注了题号,具体的题目找下书上对应内容吧. p110.8 #include<iostream> #inc ...

  2. c++ primer plus 习题答案(8)

    p475.2 //头文件: class Cd{ private: char *performers; char *label; int selections; double playtime; pub ...

  3. c++ primer plus 习题答案(6)

    p425.1 #include<iostream> #include<cstring> #include<cstdlib> using namespace std; ...

  4. c++ primer plus 习题答案(5)

    p333.7 #include<iostream> #include<cstring> #include<cstdlib> using namespace std; ...

  5. c++ primer plus 习题答案(4)

    p333.3 #include<iostream> #include<cstdlib> #include<cstring> #include<string&g ...

  6. c++ primer plus 习题答案(3)

    p296.3 #include<iostream> #include<cstdlib> #include<string> #include<cstring&g ...

  7. c++ primer plus 习题答案(2)

    p221.8 #include<iostream> #include<cstdlib> #include<cstring> using namespace std; ...

  8. C++Primer第五版——习题答案目录

    目前正在刷<C++Primer>这本书,会在博客上记录课后习题答案,答案仅供参考. 因为水平有限,如有有误之处,希望大家不吝指教,谢谢! 目录地址 使用的系统为:win 10,编译器:VS ...

  9. 《C++Primer》第五版习题答案--第五章【学习笔记】

    <C++Primer>第五版习题答案--第五章[学习笔记] ps:答案是个人在学习过程中书写,可能存在错漏之处,仅作参考. 作者:cosefy Date: 2020/1/15 第五章:语句 ...

随机推荐

  1. 如何让多个不同版本的jquery库共存

    问题描述:公司的登录接口使用的是jquery1.4.2,因为我要使用一个jquery.pagination的分页控件(jquery1.7.2).如果我使用了1.7.2,登录接口会有问题. <sc ...

  2. 织梦DEDECMS 首页列表页内容也时间日期调用标签

    DEDECMS利用strftime()函数格式化时间的所有参数详解,包括年份日期进制.小时格式等,大家收藏吧,呵. 日期时间格式 (利用strftime()函数格式化时间)0 dedecms首页时间标 ...

  3. 7.15 css与js 选择奇偶子元素的区别

    js: 选取偶数位置的 <tr> 元素 $("tr:even") 选取奇数位置的 <tr> 元素 $("tr:odd") css 选取偶 ...

  4. Minimum Inversion Number(归并排序)

    Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java ...

  5. 【Android进阶学习】shape和selector的结合使用

    shape和selector是Android UI设计中经常用到的,比如我们要自定义一个圆角Button,点击Button有些效果的变化,就要用到shape和selector.可以这样说,shape和 ...

  6. 自己写的操作sql的公共类

    /* /' `\/ `. . .' : `. `. \\.' , `.` `. `. ,___/|\. `. : . \, .'./ ' '\ , ' .\ . \_.~ _; ; \/'. `\ . ...

  7. ContentType 属性 MIME

    ".asf" = "video/x-ms-asf" ".avi" = "video/avi" ".doc&qu ...

  8. HTML5的绘图的支持

    一.简单介绍canvas元素 <canvas.../>是HTML5新增的一个元素,该元素用于绘制图形.实际上<canvas../>只是相当于一张画布. 它除了可以指定通用属性外 ...

  9. matlab GUI之自定义菜单小结

    自定义菜单 1.uimenu对象 h=uimenu('PropertyName','ProperValue') h=uimenu(parent,'PropertyName','ProperValue' ...

  10. Linux学习之Makefile文件的编写

    转自:http://goodcandle.cnblogs.com/archive/2006/03/30/278702.html 目的:       基本掌握了 make 的用法,能在Linux系统上编 ...