最近看了C++宝典,看时间是2005的,对于里面的程序自己也进行了编写,由于时间过久,可能有些函数的用法发生了改变,自己也对其进行了修改,用VS2017可以编译通过。
前四章学习内容
CPlusPlusStudy.h
#pragma once /*********************Function prototype************************/ ///////////////////////// Caption 1 & 2 ////////////////////////// //variable void Variable_study(); void Bool_study(); void Char_study(); void Wchar_t_study(); void Int_study(); void Float_study(); void Variable_initial(); //constant void Constant_study(); //operator void Operator_study(); void Assign_study(); void assign_common_study(); void assign_expression_study(); void MoreVariable_assign(); void Complex_assign(); void DecrementAndIncreament_study(); void Conditional_study(); void Comma_study(); //standard input and output void Cin_Cout_study(); void Common_cin_cout_study(); void Format_cin_cout_study(); ///////////////////////// Caption 3 & 4 ////////////////////////// //function overload void Overload_function(); void Overload_copy(); void string_copy(char *dest, const char *src); void string_copy(char *dest, const char *src, int len); void Overload_format(); void display_time(const struct std::tm tim); void display_time(time_t *tim); //goto jumper void Goto_study();
CPlusPlusStudy.cpp
/*********************************************************/ /******************** Author: zihan **********************/ /******************** Date: 2019/7/11 ********************/ /******************** Version: 0.0.0.1 *******************/ /******************** Destination: For C++ study *********/ /******************** FileName: CPlusPlusStudy.cpp *******/ /*********************************************************/ #include "pch.h" #include <iostream> #include "CPlusPlusStudy.h" #include <time.h> #include <Windows.h> ///////////////////////////////////////////////////////////////// // The main() function // ///////////////////////////////////////////////////////////////// int main() { std::cout << " This is main() function "; //Caption 1 & 2 //Variable_study(); //Constant_study(); //Operator_study(); //Cin_Cout_study(); //Caption 3 & 4 //Overload_function(); Goto_study(); std::cout << " ===================main() end=================== "; return 0; } ///////////////////////////////////////////////////////////////// // Caption 1 & 2 // ///////////////////////////////////////////////////////////////// /******************************* variable **************************************/ //variable void Variable_study() { std::cout << " This is Variable_study() function "; //Bool_study(); //Char_study(); //Wchar_t_study(); //Int_study(); //Float_study(); Variable_initial(); std::cout << " ===================Variable_study() end=================== "; } //bool variable void Bool_study() { std::cout << " This is Bool_study() function "; bool senior; //bool variable senior = true; //set to true //Test the senior variable if (senior) std::cout << "variable senior's value is true "; else { std::cout << "aError";//"a" make computer product voice. system("pause"); } std::cout << " ===================Bool_study() end=================== "; } //char variable void Char_study() { std::cout << " This is Char_study() function "; char c; //char variable c = 'b'; //assign 'b' std::cout << c; //display 'b' c = 'y'; //assign 'y' std::cout << c; //dispaly 'y' c = 'e'; //assign 'e' std::cout << c; //display 'e' std::cout << " ===================Char_study() end=================== "; } //wchar_t variable void Wchar_t_study() { std::cout << " This is Wchar_t_study() function "; wchar_t wc; //wide char variable wc = 'b'; //assign 'b' to wc std::wcout << wc; //display 'b' wc = 'y'; //assgin 'y' to wc std::wcout << wc; //display 'y' wc = 'e'; //assign 'e' to wc std::cout << " ===================Wchar_t_study() end=================== "; } //int variable void Int_study() { std::cout << " This is Int_study() function "; int Amount; //an int variable Amount = 123; //assign a value std::cout << Amount; //display the int std::cout << " ===================Int_study() end=================== "; } //float variable void Float_study() { std::cout << " This is Float_study() function "; float realValue; //a float variable realValue = 1.2; //assign a value //warning C4305: '=': truncation from 'double' to 'float' std::cout << realValue; //display the float std::cout << " ===================Float_study() end=================== "; } //initial variable void Variable_initial() { std::cout << " This is Variable_initial() function "; ////initialize variable support C++ & C //int Amount = 3; //initialize an int //char ch = 'A'; //initialize a char //float Value = 1.23; //initialize a float ////Display the initialized variables //std::cout << Amount; //std::cout << ' '; //std::cout << ch; //std::cout << ' '; //std::cout << Value; //initialize variable only support C++ int Amount(3); //initialize an int char ch('A'); //initialize a char float Value(1.23); //initialize a float //Display the initialized variables std::cout << Amount; std::cout << ' '; std::cout << ch; std::cout << ' '; std::cout << Value; std::cout << " ===================Variable_initial() end=================== "; } /********************************* constant **************************************/ //constant void Constant_study() { std::cout << " This is Constant_study() function "; std::cout << "This is the beginning of a very long message " "that spans severral lines of code. " "This format allows a program to build long " "string constants without going past the " "program editor's right margin. "; std::cout << " ===================Constant_study() end=================== "; } /************************************** operator *********************************/ //operator void Operator_study() { std::cout << " This is Operator_study() function "; //Assign_study(); //DecrementAndIncreament_study(); //Conditional_study(); Comma_study(); std::cout << " ===================Operator_study() end=================== "; } /////////////////assignment statement operator void Assign_study() { std::cout << " This is Assign_study() function "; //assign_common_study(); //assign_expression_study(); //MoreVariable_assign(); Complex_assign(); std::cout << " ===================Assign_study() end=================== "; } //common_assign void assign_common_study() { std::cout << " This is assign_common_study() function "; //Declare three integers int HourlyRate; int HoursWorked; int GrossPay; //Assign values to the integers HourlyRate = 15; HoursWorked = 40; GrossPay = HourlyRate * HoursWorked; //Display the variables on the screen. std::cout << HourlyRate; std::cout << ' '; std::cout << HoursWorked; std::cout << ' '; std::cout << GrossPay; std::cout << " ===================assign_common_study() end=================== "; } //expression_assign void assign_expression_study() { std::cout << " This is assign_expression_study() function "; int Celsius, Fahrenheit; //Prompt for Fahrenheit temperature std::cout << " Enter temperature as degrees Fahrenheit: "; //Read Fahrenheit tempersture from keyboard std::cin >> Fahrenheit; //Compute Celsius Celsius = 5 * (Fahrenheit - 32) / 9; //Display the result std::cout << "Temperature is "; std::cout << Celsius; std::cout << " degrees Celsius"; std::cout << " ===================assign_expression_study() end=================== "; } //continuity_assgin void MoreVariable_assign() { std::cout << " This is MoreVariable_assign() function "; unsigned int This, That, Those; //Assign the same value to three variables This = That = Those = 66000; //Dispaly three ubsigned ints std::cout << This; std::cout << ' '; std::cout << That; std::cout << ' '; std::cout << Those; std::cout << " ===================MoreVariable_assign() end=================== "; } //complex_assign void Complex_assign() { std::cout << " This is Complex_assign() function "; long Total, SubTotal, Detail; //Initial values Total = 10000; SubTotal = 90; Detail = 5; SubTotal *= Detail; //compute SubTotal Total += SubTotal; //compute Total //Dispaly all three std::cout << Total; std::cout << ' '; std::cout << SubTotal; std::cout << ' '; std::cout << Detail; std::cout << " ===================Complex_assign() end=================== "; } //////////////////decrement and increment void DecrementAndIncreament_study() { std::cout << " This is DecrementAndIncreament_study() function "; int Ctr, OldCtr, NewCtr; //Make the assignments OldCtr = 123; //OldCtr is 123 NewCtr = ++OldCtr; //NewCtr is 124, OldCtr is 124 Ctr = NewCtr--; //Ctr is 124, NewCtr is 123 //Display the results std::cout << OldCtr; std::cout << ' '; std::cout << NewCtr; std::cout << ' '; std::cout << Ctr; std::cout << " ===================DecrementAndIncreament_study() end=================== "; } //////////////////conditional operator void Conditional_study() { std::cout << " This is Conditional_study() function "; float Dues; //dues amount //Read the dues std::cout << "Enter dues amount: "; std::cin >> Dues; //Are the dues paid on time? std::cout << "On time?(y/n)"; char yn; std::cin >> yn; bool Overdue; //true if overdue, false if on time Overdue = yn != 'y'; float AmountDue; //amount to be computed //Use conditional operator to compute AmountDue = Overdue ? Dues * 1.10 : Dues; //Display the dues amount std::cout << "Amount due: "; std::cout << AmountDue; std::cout << " ===================Conditional_study() end=================== "; } //////////////////comma operator void Comma_study() { std::cout << " This is Comma_study() function "; int Val, Amt, Tot, Cnt; Amt = 30; Tot = 12; Cnt = 46; //Compute Val = rightmost expression //Val = (Amt++, --Tot, Cnt + 3); //Compute Val = leftmost expression Val = Amt++, --Tot, Cnt + 3; //Display the result std::cout << Val; std::cout << " ===================Comma_study() end=================== "; } /************************************** standard input & output *********************************/ //cin and cout void Cin_Cout_study() { std::cout << " This is Cin_Cout_study() function "; //Common_cin_cout_study(); Format_cin_cout_study(); std::cout << " ===================Cin_Cout_study() end=================== "; } //common usage void Common_cin_cout_study() { std::cout << " This is common_cin_cout_study() function "; int Amount = 3; //initialize an int char ch = 'A'; //initialize a char float Value = 1.23; //initialize a float //Display the initialized variables std::cout << Amount << ' ' << ch << ' ' << Value << std::endl; std::cout << " ===================common_cin_cout_study() end=================== "; } //format cin and cout void Format_cin_cout_study() { std::cout << " This is Format_cin_cout_study() function "; int amount = 123; std::cout << std::dec << amount << ' ' << std::oct << amount << ' ' << std::hex << amount; std::cout << " ===================Format_cin_cout_study() end=================== "; } /************************************** Function overload *********************************/ //function overload void Overload_function() { std::cout << " This is Overload_function() function "; //Overload_copy(); Overload_format(); std::cout << " ===================Overload_function() end=================== "; } //copy overload void Overload_copy() { std::cout << " This is Overload_copy() function "; char misspiggy[20], kerrnit[20]; string_copy(misspiggy, "Miss Piggy"); string_copy(kerrnit,"Kerrnit, the file transfer protocol", 30); std::cout << kerrnit << " and " << misspiggy; //std::cout << kerrnit; std::cout << " ===================Overload_copy() end=================== "; } //The first version of string_copy void string_copy(char *dest, const char *src) { std::cout << " This is string_copy2 function "; while ((*dest++ = *src++) != '