1. 程式人生 > >CPP_Basic_Code_P12.1-PP12.10.4

CPP_Basic_Code_P12.1-PP12.10.4

CPP_Basic_Code_P12.1-PP12.10.4

//  The Notes Created by Z-Tech on 2017/2/17.
//  All Codes Boot on 《C++ Primer Plus》V6.0
//  OS:MacOS 10.12.4
//  Translater:clang/llvm8.0.0 &g++4.2.1
//  Editer:iTerm 2&Sublime text 3
//  IDE: Xcode8.2.1&Clion2017.1

//P12.1-P12.3
Z_Head.h
#ifndef XXX_H
#define XXX_H

#include
<iostream>
class StringBad { private: char * str; int len; static int num_strings; public: StringBad(const char * s); StringBad(); ~StringBad(); friend std::ostream &operator<<(std::ostream & os,const StringBad & st); }; #endif SubFunctions.
cpp #include "Z_Head.h" #include <cstring> using std::cout; int StringBad::num_strings=0; StringBad::StringBad(const char * s) { len = std::strlen(s); str = new char[len+1]; std::strcpy(str,s); num_strings++; cout<<num_strings<<": \""<<str<<"\" object created\n"
; } StringBad::StringBad() { len = 4; str = new char[len]; std::strcpy(str,"C++"); num_strings++; cout<<num_strings<<": \""<<str<<"\" default object created\n"; } StringBad::~StringBad() { cout<<"\""<<str<<"\" object deleted, "; --num_strings; cout<<num_strings<<" left\n"; delete [] str; } std::ostream &operator<<(std::ostream & os,const StringBad & st) { os<<st.str; return os; } Main.cpp #include "Z_Head.h" using std::cout; void callme1(StringBad & ); void callme2(StringBad); int main() { using std::endl; { cout<<"String an inner block.\n"; StringBad headline1("Celery Stalks at Midnight"); StringBad headline2("Lettuce Prey"); StringBad sports("Spinach Leaves Bowl for Dollars"); cout<<"headline1: "<<headline1<<endl; cout<<"headline2: "<<headline2<<endl; cout<<"sports: "<<sports<<endl; callme1(headline1); cout<<"headline1: "<<headline1<<endl; callme2(headline2);//Bug出處 cout<<"headline2: "<<headline2<<endl; cout<<"Initialize one object to another:\n"; StringBad sailor = sports; cout<<"sailor: "<<sailor<<endl; cout<<"Assign one object to another:\n"; StringBad knot; knot = headline1; cout<<"knot: "<<knot<<endl; cout<<"Exiting the block.\n"; } cout<<"End of main()\n"; return 0; } void callme1(StringBad & rsb) { cout<<"String passed by reference:\n"; cout<<" \""<<rsb<<"\"\n"; } void callme2(StringBad sb) { cout<<"String passed by value:\n"; cout<<" \""<<sb<<"\"\n"; } //P12.4-P12.6 Z_Head.h #ifndef XXX_H #define XXX_H #include <iostream> using std::ostream; using std::istream; class String { private: char * str; int len; static int num_strings;//宣告靜態資料成員,但此處無法定義 static const int CINLIM = 80;//靜態常量可以初始化 public: //建構函式和方法 String(const char * s); String(); String(const String &); ~String(); int length() const {return len;}//行內函數 //操作符過載成員函式 String & operator=(const String &); String & operator=(const char *); char & operator[](int i); const char & operator[](int i) const;//const版本 //操作符過載友元函式 friend bool operator<(const String &st1,const String &st2); friend bool operator>(const String &st1,const String &st2); friend bool operator==(const String &st1,const String & st2); friend ostream & operator<<(ostream & os,const String & st); friend istream & operator>>(istream & is,String & st); //靜態類成員函式 static int HowMany(); }; #endif SubFunctions.cpp #include "Z_Head.h" #include <cstring> using std::cout; using std::cin; int String::num_strings=0; //靜態類成員函式 int String::HowMany() { return num_strings;//統計物件建立的次數 } //建構函式和方法 String::String(const char * s)//指標版本建立物件 { len = std::strlen(s); str = new char[len + 1]; std::strcpy(str,s); num_strings++; } String::String()//預設引數建立 { len = 4; str = new char[1]; str[0] = '\0'; num_strings++; } String::String(const String & st)//引用物件建立物件 { num_strings++; len = st.len; str = new char[len + 1]; std::strcpy(str,st.str); } String::~String()//解構函式 { --num_strings; delete [] str; } //操作符過載成員函式 String & String::operator=(const String & st)//過載使用引用物件的賦值 { if (this == &st) return *this; delete [] str;//釋放指標指向記憶體 len = st.len; str = new char[len + 1]; std::strcpy(str,st.str); return *this; } String & String::operator=(const char * s)//過載使用字串指標的賦值 { delete [] str; len = std::strlen(s); str = new char[len + 1]; std::strcpy(str,s); return *this; } char & String::operator[](int i)//過載物件陣列的引用 { return str[i]; } const char & String::operator[](int i) const//過載常量版本 { return str[i]; } //操作符過載友元函式 bool operator<(const String &st1,const String &st2)//過載物件字串排序< { return (std::strcmp(st1.str,st2.str)<0);//st1在st2前則返回負數 } bool operator>(const String &st1,const String &st2)//過載物件字串排序> { return (st2.str<st1.str);//巧妙利用前者 } bool operator==(const String &st1,const String & st2)//過載物件字串排序== { return (std::strcmp(st1.str,st2.str)==0); } ostream & operator<<(ostream & os,const String & st)//過載物件的輸出流 { os<<st.str; return os; } istream & operator>>(istream & is,String & st)//過載物件的輸入流 { char temp[String::CINLIM]; is.get(temp,String::CINLIM); if (is) st=temp; while (is && is.get()!='\n') continue; return is; } Main.cpp #include "Z_Head.h" const int ArSize = 10; const int MaxLen = 81; int main() { using std::cout; using std::cin; using std::endl; String name; cout<<"Hi,what's your name?\n>> "; cin>>name;//讀取姓名到name cout<<name<<",please enter up to "<<ArSize <<" short saying <empty line to quit>:\n"; String saying[ArSize];//建立物件陣列 char temp[MaxLen];//臨時字串陣列 int i; for (i = 0;i < ArSize;i++) { cout<<i+1<<": "; cin.get(temp,MaxLen);//讀取諺語到temp while (cin && cin.get()!='\n')//讀取成功且不到結尾則繼續 continue; if (!cin||temp[0]=='\0')//讀取失敗或者碰到字串結尾\0則彈出 break; else saying[i]=temp;//將讀取的temp存入saying物件陣列 } int total = i; if (total > 0)//如果確實讀取成功了 { cout<<"Here are your saying:\n"; for (i = 0;i < total;i++) cout<<saying[i][0]<<": "<<saying[i]<<endl; int shortest = 0; int first = 0; for (i = 1;i < total;i++) { if (saying[i].length() < saying[shortest].length())//字串長度 shortest = i;//i已被設定為第一個諺語 if (saying[i] < saying[first])//誰小就在前,並被賦值到first first = i; } cout<<"Shortest saying :\n"<<saying[shortest]<<endl; cout<<"First alphabetically:\n"<<saying[first]<<endl; cout<<"This program used "<<String::HowMany()//統計被建立的物件數 <<" String objects.Bye.\n"; } else cout<<"No input!Bye.\n"; return 0; } //P12.7 //配合P12.4-P12.5 const int ArSize = 10; const int MaxLen = 81; int main() { using namespace std; String name; cout<<"Hi,What's your name?\n>>"; cin>>name; cout<<name<<",Please enter up to "<<ArSize <<" short saying <empty line to quit>:\n"; String saying[ArSize]; char temp[MaxLen]; int i; for (i = 0;i < ArSize;i++) { cout<<i+1<<": "; cin.get(temp,MaxLen); while (cin&&cin.get()!='\n') continue; if (!cin||temp[0]=='\0') break; else saying[i]=temp; } int total = i; if (total > 0) { cout<<"Here are your saying:\n"; for (i = 0;i < total;i++) cout<<saying[i]<<'\n'; String * shortest = &saying[0]; String * first = &saying[0]; for (i = 1;i < total;i++) { if (saying[i].length() < shortest->length())//注意長度比較 shortest = &saying[i]; if (saying[i] < *first) first = &saying[i]; } cout<<"Shortest saying:\n"<< * shortest<<endl; cout<<"First saying:\n"<< * first<<endl; srand(time(0)); int choice = rand() % total; String * favorite = new String(saying[choice]); cout<<"My favorite saying:\n"<< *favorite<<endl; delete favorite; } else cout<<"Not much to say, eh?\n"; cout<<"Bye.\n"; return 0; } //P12.8 #include <iostream> using namespace std; const int BUF = 512; class JustTesting { private: string words; int number; public: JustTesting(const string &s = "Just Testing", int n = 0) { words = s; number = n; cout << words << " constructed\n"; } ~JustTesting() { cout << words << " destroyed"; } void Show() const { cout << words << ", " << number << endl; } }; int main() { char *buffer = new char[BUF]; JustTesting *pc1,*pc2; pc1=new (buffer) JustTesting; pc2=new JustTesting("Heap1",20); cout<<"Memory block addresses:\n"<<"buffer:" <<(void *)buffer<<" heap: "<<pc2<<endl; cout

相關推薦

CPP_Basic_Code_P12.1-PP12.10.4

CPP_Basic_Code_P12.1-PP12.10.4 // The Notes Created by Z-Tech on 2017/2/17. // All Codes Boot on 《C++ Primer Plus》V6.0 // OS:MacOS 10.12.4

java DefaultMutableTreeNode 樹形結構 目錄 1. Tree的概念 1 1.1. treeNode介面,mutabletreenode介面 1 1.2. 10-4:以T

java DefaultMutableTreeNode   樹形結構   目錄 1. Tree的概念 1 1.1. treeNode介面,mutabletreenode介面 1 1.2. 10-4:以TreeModel構造JTree. 1

C語言代碼編程題匯總:顯示表達式1*2+3*4+...+9*10的表示形式

clas ron urn ++ class align int c語言代碼 程序 顯示表達式1*2+3*4+...+9*10的表示形式 源程序代碼如下: 1 /* 2 2017年6月7日22:54:51 3 功能:實現1*2+3*4+...+9*10

0.1 使用w查看系統負載 - 10.2 vmstat命令 - 10.3 top命令 - 10.4 sar命令 - 10.5 nload命令

使用 查看 系統負載 - 10.1 使用w查看系統負載 - 10.2 vmstat命令 - 10.3 top命令 - 10.4 sar命令 - 10.5 nload命令 # 10.1 使用w查看系統負載 ![mark](http://oqxf7c508.bkt.clouddn.com/blo

【探路者】10月16日立會報告(團隊第1周-第4次)

投票 主題 logs 報告 ati images 團隊 其他 照片 【探路者】組成員及各位博客地址。 1藺依銘:http://www.cnblogs.com/linym762/ 2張恩聚:http://www.cnblogs.com/zej87/ 3米赫:http://w

10.1 使用w查看系統負載 10.2 vmstat命令 10.3 top命令 10.4 sar命令

分享圖片 mark tle bold 被攻擊 margin src 攻擊 top 10.1-使用w查看系統負載10.2 vmstat命令1、vmstat命令Vmstat 1 表示每1秒,顯示一次Vmstat 1 5 表示每1秒顯示,共顯示5次10.3 top命令92個進程,

10.1 使用w查看系統負載 10.2 vmstat命令 10.3 top命令 10.4 sar命令 10.5 nload命令

僵屍進程 line 一行 uptime rtp ica blog lis png [root@lizhipeng01 ~]# w 17:43:46 up 6 min, 1 user, load average: 0.00, 0.04, 0.05 負載,1

01 使用while 循環輸入1 2 3 4 5 6 8 9 10

輸入 while == pos bre blog body while 循環 break start = 1while True: if start == 7: start += 1 continue print(start)

如何使用迴圈計算1 + 2 +3 + 4 + 5 + 6 + 8 + 9 + 10的值

1、先嚐試輸出不包含7 第一種方式(在等於7時加1然後繼續下次迴圈) count = 1 while count <= 10: if count == 7: count += 1 # 如果等於7 在count=7的基礎上加1 然後繼續執行迴圈 co

ACMNO.21 C語言-逆序輸出 輸入10個數字,然後逆序輸出。 輸入 十個整數 輸出 逆序輸出,空格分開 樣例輸入 1 2 3 4 5 6 7 8 9 0

題目描述 輸入10個數字,然後逆序輸出。 輸入 十個整數 輸出 逆序輸出,空格分開 樣例輸入 1 2 3 4 5 6 7 8 9 0 樣例輸出 0 9 8 7 6 5 4 3 2 1 提示 陣列?堆疊? 來源/分類 C語言

用while語句輸出1 2 3 4 5 6 8 9 10

The while time spa port else while語句 pre print import time count=1 while count<=10: print(count,time.time()) if count != 6:

MariaDB 10.4.1 釋出,流行的 MySQL 分支版本

   MariaDB 10.4.1 已釋出,更新內容如下: Syntax New FLUSH SSL command to reload SSL certificates without server restart (MDEV-16266)

1.zookeeper-3.4.10叢集搭建

目錄 Zookeeper簡介 Zookeeper是一個高效的分散式協調服務,可以提供配置資訊管理、命名、分散式同步、叢集管理、資料庫切換等服務。它不適合用來儲存大量資訊,可以用來儲存一些配置、釋出與訂閱等少量資訊。Hadoop、Storm、訊息中介軟體、RPC服務框架、分散式資料庫同步系統,

為列表b_list = [1, 2, 3, 4, 5, 6, 6, 7, 8, 8, 8, 10, 11]去重複

方法一、 遍歷去重複:建立一個新的列表a,然後讓新的列表裡的元素和目標列表b的元素作對比,如果a中沒有b中的元素則在a列表後追加,否則對a不做處理即可,最後輸出a b_list = [1, 2, 3, 4, 5, 6, 6, 7, 8, 8, 8, 10, 11] d_list=[] for i

c語言 對10個數組元素依次賦值為0 1 2 3 4 5 6 7 8 9,要求按逆序輸出

                對10個數組元素依次賦值為0,1,2,3,4,5,6,7,8,9,要求按逆序輸出。 解:程式:

查詢無限整數序列的第n位1,2,3,4,5,6,7,8,9,10,11,...

本題源自leetcode  400 ------------------------------------------------------------- 思路:1 1-9 有9 位數,10-99 有180 位。因此我們首先找到這個位數是幾位數。 2 在找到這個數,然後

計算1!+2!+3!+4!+5!+6!+7!+8!+9!+10!+......的值(需注意整型變數的範圍)

#include<stdio.h> //***(1)*** //計算1!+2!+3!+4!+5!+6!+7!+8!+9!+10!的值 int func(int n)//一個數的階乘 { if(n>0) return n*func(n-1); if

一個關於執行緒的經典面試題,要求用三個執行緒,按順序列印1,2,3,4,5.... 71,72,73,74, 75. 執行緒1先列印1,2,3,4,5, * 然後是執行緒2列印6,7,8,9,10, 然後是

package thread; /**  *   * 一個關於執行緒的經典面試題,要求用三個執行緒,按順序列印1,2,3,4,5.... 71,72,73,74, 75. 執行緒1先列印1,2,3,4,5,  * 然後是執行緒2列印6,7,8,9,10, 然後是執行緒3列印

基於 Ansible 快速部署 kubernetes 1.10.4 HA 高可用叢集

一、背景       菜鳥只是拿來試試,順便記錄下過程。二、部署過程2.1  準備機器      本次實驗準備了4臺虛機,192.168.5.201~204 ,主機名 node-01 ~ node-04      node-01 當作部署機,node-01~02 作為 Mas