1. A small example
Main.cpp
1# include <iostream>2# include <vector>3 4 using namespacestd;5 6# include"Object.h"7 8 intMain ()9 {Tencout <<"Hello"<<Endl; One AObject obj ( A); -cout << obj.getdata () <<Endl; - thevector<int> arr = {1,2,3}; - //Range for - for(auto Ele:arr) - { +cout << ele <<Endl; - } + return 0; A } at
Object.h
1 #pragmaOnce2 classObject3 {4 Public:5 //Object ();//Constructor Declaration6Object (intdata);7~Object ();8 voidSetData (intdata);9 //int object::getdata ();Ten intGetData (); One Private: A intdata; -};
Object.cpp
1#include"Object.h"2Object::object (intdata)3 {4 This->data =data; 5 }6 7object::~Object ()8 {9 Ten } One A voidObject::setdata (intdata) - { - This->data =data; the } - - intObject::getdata () - { + return This-data; -}
&
1 voidFuncstringstr)2 {3str =" World";4cout << str <<Endl; 5 }6 intMain ()7 {8 ......9 stringstr ="Hello";Ten func (str); Onecout << str <<Endl; A } - - Print: the World - Hello - - //another Kind + voidFuncstring&str) - { +str =" World"; Acout << str <<Endl; at } - intMain () - { - ...... - stringstr ="Hello"; - func (str); incout << str <<Endl; - } to + Print: - World theWorld
*
1 voidFunc2 (Charfilename[])2 {3cout << filename <<Endl; 4 }5 6 intMain ()7 {8 CharFilename[] ="Hello.txt"9 func2 (filename); Ten } One A //equivalence - voidFunc2 (Char*filename) - { thecout << filename <<Endl; - } - - intMain () + { - CharFilename[] ="Hello.txt" + func2 (filename); A}
Pointer
1 voidFunc2 (Char*filename)2 {3*filename ='a';4cout << filename <<Endl; 5 }6 7 intMain ()8 {9 CharFilename[] ="Hello.txt"Ten func2 (filename); One } A - Print: -Aello.txt
// * (P+offset) = P[offset] void Func2 (char* filename) { 1'a'; << filename << endl; } int Main () { char"hello.txt" func2 (filename); }
2. Comparison of C + + with Python
(1)
Vector<int>
Array
(2)
[All in all] list
List<int>
(3)
(+/-) set
Unordered_set<int>
(4)
{1: "Hello", 2: "World"} dict
Unordered_map<int, string>
C + + Object-oriented Foundation