C + + is too weak (in fact, not at all!) )
Dig a hole to learn c++!
Uninterrupted updates!
Code Listing 1:
#include <math.h>#include <limits.h>#include <complex>#include <string>#include <functional>#include <iterator>#include <algorithm>#include <vector>#include <stack>#include <queue>#include <set>#include <map>#include <list>#include <bitset>#include <sstream>#include <iomanip>#include <fstream>#include <iostream>#include <ctime>#include <cmath>#include <cstring>#include <cstdio>#include <time.h>#include <ctype.h>#include <string.h>#include <assert.h>using namespace STD;/* Compile the member and then compile the function body first, so the member function can directly use the other members of the class to declare internal or external definitions within the class without worrying about the order member function that appears.classSale_data { Public: Sale_data () =default;//default constructorSale_data (Const string&s,DoubleP)://ConstructorsBookno (s), Revenue (p) {}DoubleAvg_price ()Const;stringISBN ()Const{return This->bookno;//this is a constant pointer} sale_data& Combine (Constsale_data&);friend BOOLComare (Sale_data &is)//friend function to access private members{returnIS.IS_OK; }stringBookno ="You are a pig";unsignedUnits_sold;DoubleRevenuePrivate:BOOLIS_OK;};DoubleSale_data::avg_price ()Const//define the function body externally{if(Units_sold)returnRevenue/units_sold;Else return 0;} sale_data& Sale_data:: Combine (ConstSale_data &rhs) {units_sold + = Rhs.units_sold; Revenue + = Rhs.revenue;return* This;//Returns the object that called the function}////////////declaring a functionSale_data Add (sale_data&,Constsale_data&);STD:: Ostream &print (STD:: Ostream&,Constsale_data&);STD:: IStream &read (STD:: Istream&, sale_data&);////////////defining the Read print functionIStream &read (istream& is, sale_data& item) {DoublePrice =0; is >> item.bookno >> item.units_sold >> Price; Item.revenue = Price * Item.units_sold;returnis;} Ostream &print (STD::ostream& OS,Constsale_data& item) {OS << item.isbn () <<" "<< Item.units_sold <<" "<< item.revenue <<" "<< item.avg_price () << Endl;returnOS;}////////////Defining the Add functionSale_data Add (sale_data& RHS,Constsale_data& lhs) {sale_data sum = LHS;//Storage andSum.combine (RHS);returnsum;}////////////class External definition constructors/*sale_data::sale_data (Std::istream &is) {Read (is, *this);//read function reads data from is to deposit this object}*/////////////Sale_data Total; Sale_data Trans;///////////main functionintMain () {cout<< TOTAL.ISBN () << Endl; Total.combine (trans);copy of the/////////classTotal = trans;//equivalent toTotal.bookno = Trans.bookno;//bookno and other members ... return 0;}
Code Listing 2:
#include <math.h>#include <limits.h>#include <complex>#include <string>#include <functional>#include <iterator>#include <algorithm>#include <vector>#include <stack>#include <queue>#include <set>#include <map>#include <list>#include <bitset>#include <sstream>#include <iomanip>#include <fstream>#include <iostream>#include <ctime>#include <cmath>#include <cstring>#include <cstdio>#include <time.h>#include <ctype.h>#include <string.h>#include <assert.h>using namespace STD;classscreen{ Public:typedef string:: Size_type pos;the members of the//definition type must first be defined and then used //using pos = string::size_type;Screen () =default; Screen (POS ht): Hight (HT) {}stringGet ()Const{returnContents } screen &move (POS R, pos c); Screen &Set(Char);Private: POS cursor =0; POS hight =0, width =0;stringContents;};inlinescreen& Screen::move (POS r,pos c)//inline for defining inline functions{pos row = hight * width; cursor = hight +1;return* This;}//Function overloading: Function name shape different from the number of parameter typesinlineScreen &screen::Set(Charc) {Contents[cursor] = C;return* This;} Screen Myscreen;intMain () {Myscreen.Set(' # ');return 0;}
Code Listing 3:
class Screen{public: class Window_mgr;// 友元类 友元关系不传递 void// 其他类的函数的友元private:};class Window_mgr{public: void clear();private:};Screen myscreen;int main(){ return0;}
Object-Oriented learning "C + +"