c++沉思录第十章的例子

2023-12-06 10:19
文章标签 c++ 第十章 例子 沉思

本文主要是介绍c++沉思录第十章的例子,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

关于虚函数和纯虚函数的知识需要我们好好注意。
#ifndef NEWPICTURE_H
#define NEWPICTURE_H
#include<iostream>
using namespace std;
class P_Node
{friend class Picture;
protected:P_Node();virtual ~P_Node(){};virtual int width()const=0;virtual int height()const=0;	virtual void display(ostream &, int, int)const=0;int max(int x, int y)const { return x > y ? x : y; }void pad(ostream & os, int x, int y)const ;
private:int use;
};
class Picture
{friend class String_Pic;friend class Frame_Pic;friend class Hcat_Pic;friend class Vcat_Pic;
public:Picture();Picture(const char * const *, int);Picture(const Picture &);~Picture();Picture & operator =(const Picture &);P_Node * p;Picture(P_Node * m);int height()const;int width()const;void display(ostream &, int, int)const;friend ostream & operator <<(ostream & os, const Picture & p);friend Picture frame(const Picture & p);friend Picture operator & (const Picture & p, const Picture & q);friend Picture operator | (const Picture & p, const Picture & q);
};
class String_Pic :public P_Node
{friend class Picture;String_Pic(const char* const * arr, int n);~String_Pic();char** data;int size;int height()const;int width()const;void display(ostream &, int, int)const;
};class Frame_Pic :public P_Node
{friend Picture frame(const Picture &);int height()const;int width()const;void display(ostream &, int, int)const;Frame_Pic(const Picture &);Picture p;
};class Vcat_Pic :public P_Node
{friend Picture operator &(const Picture & n, const Picture & m);Vcat_Pic(const Picture & n, const Picture & m);int height()const;int width()const;void display(ostream &, int, int)const;Picture top, bottom;
};class Hcat_Pic :public P_Node
{friend Picture operator |(const Picture & n, const Picture & m);Hcat_Pic(const Picture & n, const Picture & m);int height()const;int width()const;void display(ostream &, int, int)const;Picture left,right;
};Picture::Picture(const Picture & m) :p(m.p)
{m.p->use++;
}
Picture::Picture(P_Node * m) : p(m){}
Picture::~Picture()
{if (--p->use == 0)delete p;
}
Picture& Picture::operator=(const Picture & q)
{q.p->use++;if (--p->use == 0)delete p;p = q.p;return *this;
}
int Picture::height()const{ return p->height(); }
int Picture::width()const { return p->width(); }
void Picture::display(ostream & os, int n, int m)const
{p->display(os, n, m);
}
Picture::Picture(const char* const * arr, int n) :p(new String_Pic(arr, n)){}
P_Node::P_Node() :use(1){}String_Pic::String_Pic(const char * const * arr, int n)
: data(new char*[n]), size(n)
{for (int i = 0; i < n; ++i){data[i] = new char[strlen(arr[i]) + 1];strcpy_s(data[i],strlen(arr[i])+1,arr[i]);}
}
String_Pic::~String_Pic()
{for (int i = 0; i < size; ++i)delete[]data[i];delete[]data;}
int String_Pic::height()const{ return size; }
int String_Pic::width()const
{int n = 0;for (int i = 0; i < size; ++i)n = max(n, strlen(data[i]));return n;
}void String_Pic::display(ostream & os, int row, int width)const
{int start = 0;if (row >= 0 && row < height()){os << data[row];start = strlen(data[row]);}pad(os, start, width);
}
int Frame_Pic::height()const{ return p.height() + 2; }
int Frame_Pic::width()const{ return p.width() + 2; }
void Frame_Pic::display(ostream& os, int row, int wd)const
{if (row < 0 || row >= height()){pad(os, 0, wd);}else{if ( 0 == row  || row == height() - 1){os << "+";int i = p.width();while (--i >= 0)os << "-";os << "+";}else{os << "|";p.display(os, row - 1, p.width());os << "|";}pad(os, width(), wd);}
}
Frame_Pic::Frame_Pic(const Picture & m) :p(m){}
Picture frame(const Picture & m)
{return new Frame_Pic(m);
}
Vcat_Pic::Vcat_Pic(const Picture & t, const Picture & b) : top(t), bottom(b){}
Hcat_Pic::Hcat_Pic(const Picture & l, const Picture & r) : left(l),right(r){}
Picture operator &(const Picture & n, const Picture & m)
{return new Vcat_Pic(n, m);
}
Picture operator |(const Picture & n, const Picture & m)
{return new Hcat_Pic(n, m);
}
int Vcat_Pic::height()const{ return top.height() + bottom.height(); }
int Vcat_Pic::width()const{ return max(top.width(), bottom.width()); }
int Hcat_Pic::height()const{ return max(left.height(), right.height()); }
int Hcat_Pic::width()const{ return left.width()+ right.width(); }
void Vcat_Pic::display(ostream& os, int row, int wd)const
{if (row >= 0 && row < top.height())top.display(os, row, wd);else{if (row < top.height() + bottom.height())bottom.display(os, row - top.height(), wd);elsepad(os, 0, wd);}
}
void Hcat_Pic::display(ostream & os, int row, int wd)const
{left.display(os, row, left.width());right.display(os, row, right.width());pad(os, width(), wd);
}
void P_Node::pad(ostream & os, int x, int y)const
{for (int i = x; i < y; ++i)os << " ";
}ostream & operator <<(ostream & os, const Picture & p)
{int ht = p.height();for (int i = 0; i < ht; ++i){	p.display(os, i, 0); os << endl;}return os;
}
#endif
#include"NewPicture.h"
#include<iostream>
using namespace std;
int main()
{char * init[] = { "Summer", "love", "Xieweizhong" };Picture p(init, 3);cout << p << endl;cout << frame((p | frame(p)) | (p | frame(p))) << endl;cout << frame((p & frame(p)) | (p | frame(p))) << endl;cout << frame((p | frame(p)) | (p & frame(p))) << endl;cout << frame((p & frame(p)) | (p & frame(p))) << endl;cout << frame((p | frame(p)) & (p | frame(p))) << endl;cout << frame((p & frame(p)) & (p | frame(p))) << endl;cout << frame((p | frame(p)) & (p & frame(p))) << endl;cout << frame((p & frame(p)) & (p & frame(p))) << endl;return 0;
}


这篇关于c++沉思录第十章的例子的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/461472

相关文章

C++如何通过Qt反射机制实现数据类序列化

《C++如何通过Qt反射机制实现数据类序列化》在C++工程中经常需要使用数据类,并对数据类进行存储、打印、调试等操作,所以本文就来聊聊C++如何通过Qt反射机制实现数据类序列化吧... 目录设计预期设计思路代码实现使用方法在 C++ 工程中经常需要使用数据类,并对数据类进行存储、打印、调试等操作。由于数据类

Linux下如何使用C++获取硬件信息

《Linux下如何使用C++获取硬件信息》这篇文章主要为大家详细介绍了如何使用C++实现获取CPU,主板,磁盘,BIOS信息等硬件信息,文中的示例代码讲解详细,感兴趣的小伙伴可以了解下... 目录方法获取CPU信息:读取"/proc/cpuinfo"文件获取磁盘信息:读取"/proc/diskstats"文

C++使用printf语句实现进制转换的示例代码

《C++使用printf语句实现进制转换的示例代码》在C语言中,printf函数可以直接实现部分进制转换功能,通过格式说明符(formatspecifier)快速输出不同进制的数值,下面给大家分享C+... 目录一、printf 原生支持的进制转换1. 十进制、八进制、十六进制转换2. 显示进制前缀3. 指

C++中初始化二维数组的几种常见方法

《C++中初始化二维数组的几种常见方法》本文详细介绍了在C++中初始化二维数组的不同方式,包括静态初始化、循环、全部为零、部分初始化、std::array和std::vector,以及std::vec... 目录1. 静态初始化2. 使用循环初始化3. 全部初始化为零4. 部分初始化5. 使用 std::a

C++ vector的常见用法超详细讲解

《C++vector的常见用法超详细讲解》:本文主要介绍C++vector的常见用法,包括C++中vector容器的定义、初始化方法、访问元素、常用函数及其时间复杂度,通过代码介绍的非常详细,... 目录1、vector的定义2、vector常用初始化方法1、使编程用花括号直接赋值2、使用圆括号赋值3、ve

如何高效移除C++关联容器中的元素

《如何高效移除C++关联容器中的元素》关联容器和顺序容器有着很大不同,关联容器中的元素是按照关键字来保存和访问的,而顺序容器中的元素是按它们在容器中的位置来顺序保存和访问的,本文介绍了如何高效移除C+... 目录一、简介二、移除给定位置的元素三、移除与特定键值等价的元素四、移除满足特android定条件的元

Python获取C++中返回的char*字段的两种思路

《Python获取C++中返回的char*字段的两种思路》有时候需要获取C++函数中返回来的不定长的char*字符串,本文小编为大家找到了两种解决问题的思路,感兴趣的小伙伴可以跟随小编一起学习一下... 有时候需要获取C++函数中返回来的不定长的char*字符串,目前我找到两种解决问题的思路,具体实现如下:

C++ Sort函数使用场景分析

《C++Sort函数使用场景分析》sort函数是algorithm库下的一个函数,sort函数是不稳定的,即大小相同的元素在排序后相对顺序可能发生改变,如果某些场景需要保持相同元素间的相对顺序,可使... 目录C++ Sort函数详解一、sort函数调用的两种方式二、sort函数使用场景三、sort函数排序

Java调用C++动态库超详细步骤讲解(附源码)

《Java调用C++动态库超详细步骤讲解(附源码)》C语言因其高效和接近硬件的特性,时常会被用在性能要求较高或者需要直接操作硬件的场合,:本文主要介绍Java调用C++动态库的相关资料,文中通过代... 目录一、直接调用C++库第一步:动态库生成(vs2017+qt5.12.10)第二步:Java调用C++

C/C++错误信息处理的常见方法及函数

《C/C++错误信息处理的常见方法及函数》C/C++是两种广泛使用的编程语言,特别是在系统编程、嵌入式开发以及高性能计算领域,:本文主要介绍C/C++错误信息处理的常见方法及函数,文中通过代码介绍... 目录前言1. errno 和 perror()示例:2. strerror()示例:3. perror(