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. WPF中的触发器简单总结

    原文 http://blog.sina.com.cn/s/blog_5f2ed5cb0100p3ab.html 触发器,从某种意义上来说它也是一种Style,因为它包含有一个Setter集合,并根据一 ...

  2. XOR双向链表

    这是一个数据结构.利用计算机的的位异或操作(⊕),来降低双向链表的存储需求. ... A B C D E ... –> next –> next –> next –> < ...

  3. Cpp again

    1,

  4. Mahout源码MeanShiftCanopyDriver分析之二MeanShiftCanopyMapper仿造

    首先更正一点,昨天处理数据的时候是有问题的,直接从网页中拷贝的文件的空格是有问题的,直接拷贝然后新建的文件中的空格可能有一个两个.三个的,所以要把两个或者三个的都换为一个,在InputMapper中下 ...

  5. 君子性非异也,善假于物也 - Threejs 引入TrackballControls 查看场景

    君子性非异也,善假于物也 - Threejs 引入TrackballControls 查看场景 太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es) 本文遵循" ...

  6. C语言的复合文字

    假设需要向一个带有int型参量的函数传递一个值,这时可以传递一个int型常量,也可以传递一个int型的变量.在C99标准之前,数组参数情况于现在不一样,没有所谓的数组常量可供传递,而在C99中增加了复 ...

  7. sublime text 3解放鼠标的快捷键总结

    Sublime text 3是我最喜欢的代码编辑器,每天和代码打交道,必先利其器,掌握基本的代码编辑器的快捷键,能让你打码更有效率.刚开始可能有些生疏,只要花一两个星期坚持使用并熟悉这些常用的快捷键, ...

  8. ubuntu14.04+xfce下启用fcitx,使用中文输入法

    1. 安装fcitx  sudo apt-get install fcitx-pinyin 2.启用fcitx 打开 setting -> Language Support -> Lang ...

  9. J - 今年暑假不AC

    J - 今年暑假不AC Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit  ...

  10. 决策树ID3算法[分类算法]

    ID3分类算法的编码实现 <?php /* *决策树ID3算法(分类算法的实现) */ /* *求信息增益Grain(S1,S2) */ //-------------------------- ...