c++ primer plus 习题答案(7)
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)的更多相关文章
- c++ primer plus 习题答案(1)
c++ primer plus 习题答案用的是第五版,IDE仍然是vs2013.我只标注了题号,具体的题目找下书上对应内容吧. p110.8 #include<iostream> #inc ...
- c++ primer plus 习题答案(8)
p475.2 //头文件: class Cd{ private: char *performers; char *label; int selections; double playtime; pub ...
- c++ primer plus 习题答案(6)
p425.1 #include<iostream> #include<cstring> #include<cstdlib> using namespace std; ...
- c++ primer plus 习题答案(5)
p333.7 #include<iostream> #include<cstring> #include<cstdlib> using namespace std; ...
- c++ primer plus 习题答案(4)
p333.3 #include<iostream> #include<cstdlib> #include<cstring> #include<string&g ...
- c++ primer plus 习题答案(3)
p296.3 #include<iostream> #include<cstdlib> #include<string> #include<cstring&g ...
- c++ primer plus 习题答案(2)
p221.8 #include<iostream> #include<cstdlib> #include<cstring> using namespace std; ...
- C++Primer第五版——习题答案目录
目前正在刷<C++Primer>这本书,会在博客上记录课后习题答案,答案仅供参考. 因为水平有限,如有有误之处,希望大家不吝指教,谢谢! 目录地址 使用的系统为:win 10,编译器:VS ...
- 《C++Primer》第五版习题答案--第五章【学习笔记】
<C++Primer>第五版习题答案--第五章[学习笔记] ps:答案是个人在学习过程中书写,可能存在错漏之处,仅作参考. 作者:cosefy Date: 2020/1/15 第五章:语句 ...
随机推荐
- (9)Xamarin测试账号申请与续用
原文 Xamarin测试账号申请与续用 在Xamarin网站上可以申请30天试用的测试账号.试用期内,Xamarin会提供完整的功能试用. 30天试用时间到期后,在Visual Studio里面你加载 ...
- 面向对象程序设计-C++_课时11new & delete
Dynamic memory allocation new new int; new Stash; new int[10]; new返回这个对象的指针 delete delete p; delete[ ...
- 【我所認知的BIOS】—>ADU.exe
[我所認知的BIOS]—>ADU.exe By LightSeed 2009-5-12 1.概要 在學習的過程中,肯定會要用不少的工具,作為底層的engineer那麼用的工具大多是DOS下.在D ...
- STL 源代码剖析 算法 stl_algo.h -- merge sort
本文为senlie原创.转载请保留此地址:http://blog.csdn.net/zhengsenlie merge sort ----------------------------------- ...
- 【Winform开发2048小游戏】
先来看一下界面: 游戏帮助类 class GameCore { //游戏地图 private int[,] map = new int[4, 4]; //合并时用到的临时数组 private int[ ...
- 设计模式之---模板方法template method的使用
在面向对象系统的分析与设计过程中经常会遇到这样一种情况:对于某一个业务逻辑(算法实现)在不同的对象中有不同的细节实现,但是逻辑(算法)的框架(或通用的应用算法)是相同的.Template Method ...
- AlarmManager类的应用(实现闹钟功能)
1.AlarmManager,顾名思义,就是“提醒”,是Android中常用的一种系统级别的提示服务,可以实现从指定时间开始,以一个固定的间隔时间执行某项操作,所以常常与广播(Broadcast)连用 ...
- php用百度地图API进行IP定位和GPS定位
<?php /** * 根据地理坐标获取国家.省份.城市,及周边数据类(利用百度Geocoding API实现) * 百度密钥获取方法:http://lbsyun.baidu.com/apico ...
- ASP.NET MVC View向Controller传值方式总结
1:QueryString传值1)也可以使用new{}来为form的action增加querystring2)在controler里使用Request.QueryString["word&q ...
- textContent和innerHtml
textContent,innerText, 查询或者设置元素的文本内容. textContent如,html: <p>test gogo</p> javascript中: v ...