繼承:inheritance 多型:polymorphism 建構子:constructor 複製建構子:copy constructor 解構子:destructor 函子/函式物件:functor 抽象類別:abstract class 父類:base class 子類:derived class
boolshellCmd(conststring &cmd, string &result){ char buffer[512]; result = "";
// Open pipe to file FILE* pipe = popen(cmd.c_str(), "r"); if (!pipe) { returnfalse; }
// read till end of process: while (!feof(pipe)) { // use buffer to read and add to result if (fgets(buffer, sizeof(buffer), pipe) != NULL) result += buffer; }
// Open pipe to file FILE* pipe = popen(cmd.c_str(), "r"); if (!pipe) { returnfalse; }
// read till end of process: while (!feof(pipe) || stop) { // use buffer to read and add to result if (fgets(buffer, sizeof(buffer), pipe) != NULL) cout << buffer; }
intmain(){ //Base b; // error: cannot declare variable ‘b’ to be of abstract type ‘Base’ Derived d; d.func1(); Base *d2 = new Derived(); d2->func1(); return0; }
intmain(){ Animal* animal; Dog dog; Cat cat; //Unknown u; // error: cannot declare variable ‘u’ to be of abstract type ‘Unknown’ animal = &dog; animal->eat();
animal = &cat; animal->eat(); return0; }
以上就是 C++ virtual 的兩種用法介紹, 如果你覺得我的文章寫得不錯、對你有幫助的話記得 Facebook 按讚支持一下!