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 第五章:语句 ...
 
随机推荐
- mysql查询语句理解
			
看一个查询语句 ,)) as passcount FROM (SELECT b.user,b.full_name,b.user_group From login_log a LEFT JOIN vic ...
 - JS 之 offsetWidth\offsetleft
 - 在OC项目工程中混编Swift
			
1.创建一个OC项目工程,然后在Build Settings中找到如下字段,修改. 2.然后在项目中创建swift文件,如果系统提示是否需要创建桥接文件的时候,点击确定. 然后在Build Setti ...
 - JAVA基础 (二)反射 深入解析反射机制
			
在谈论到反射这个问题时,你是否有例如以下疑问? 不管是在.NET还是Java中反射的原理和机制是一样的,理解了一种还有一种就能够迎刃而解,想要理解反射首先须要了解底层的一些概念和执行.理解了反射有助于 ...
 - 赵雅智_BroadcastReceiver
			
BroadcastReceiver 用于接收程序(包含用户开放的程序和系统内建程序)所发出的Broadcast intent 耗电量 开机启动 窃取别人短信 窃取别人电话 开发: 创建须要启动的Br ...
 - 假设说这个世界不是真实存在的,仅仅是一段代码,迄今为止你发现了哪些bug?
			
给这个世界写代码的不是一个人,而是一个团队(这么大的项目,一个人开发不了).并且严重怀疑这个一个开源项目.开发人员被我们觉得是神,所以一神论是不正确的,众神论才是真理,且凡人是有机会成为神的(參悟神道 ...
 - Pat(Advanced Level)Practice--1043(Is It a Binary Search Tree)
			
Pat1043代码 题目描写叙述: A Binary Search Tree (BST) is recursively defined as a binary tree which has the f ...
 - google base库之simplethread
			
// This is the base SimpleThread. You can derive from it and implement the // virtual Run method, or ...
 - (4)事件处理——(3)代码执行的顺序(Timing of code execution)
			
In Chapter 1, Getting Started, we noted that $(document).ready()was jQuery's primary way to perform ...
 - php小数点后取两位的三种实现方法
			
php小数点后取两位的方法. 方法一.经常用到小数点后取几位,但不能进位的情况. 比如3.149569取小数点后两位,最后两位不能四舍五入.结果:3.14. 可以使用函数floor. 该函数是舍去取整 ...