C++ 笔记

namespace

cpp
namespace SomeName {
void someFunction();
}
...
SomeName::someFunction();
  • using
    • using namespace std;
  • cmath vs math.h
    • cxx headers are in std namespace; .h ones are in global
    • better using cxx headers if writing c++

cin, cout

cpp
#include <iostream>
...
cout << "Hello, world!";

<< for cout is overloaded.

string

cpp
#include <string>
using namespace std;
  • concat +
  • append .append(): append to the original variable and return a reference
  • length .length(), .size()
  • index
    cpp
    string str = "Hello";
    char c = str[0];
    cout << c << endl;   // H
    str[1] = 'a';
    cout << str << endl; // Hallo

array

  • declare string boys[2];
  • initialization string boys[2] = {"Bob", "John"};string boys[] = {"Bob", "John"};
  • foreach loop
    • for (string boy: boys)
  • sizeof(): size in bytes

structure

  • no need for type def (compared to c)
    cpp
    struct Cat {
        string name;
    };
    ...
    Cat c; // "struct Cat" seems working too
  • not named struct
    cpp
    struct {
        string name;
    } cat;
    ...
    cat.name = "Mew";

reference

  • declare & initialize string& s = str;
  • reference cannot be reassigned once initialized
    cpp
    string str = "Hello", str2 = "world";
    string& s = str;
    s = str2;            // changes value of str to str2, not refering s to str2
    cout << str << endl; // world
  • const reference the value pointed to by the reference cannot be modified via the reference

function

  • default parameter
    cpp
    void foo(string param="default value") {
        ...
    }
  • pass by reference
    • avoid object copy
  • pass arrays no mem copy occured. items can be changed.
    cpp
    void foo(int nums[5]) {
        nums[1] = 1;
    }
    ...
    int nums[5] = {0};
    foo(nums);
    cout << nums[1]; // 1
  • overloading

lambda function

cpp
[capture] (param) {
  //body
}
  • capture
    • default capture &, =

class

definition

cpp
class MyClass {       // The class
  public:             // Access specifier
    int myNum;        // Attribute (int variable)
    string myString;  // Attribute (string variable)
    MyClass() {       // Constructor
      cout << "Hello World!";
    }
    void myMethod() {
        ...
    }
};

instance

cpp
MyClass obj1, obj2;
obj1.myString = "Hello";
obj2.myMethod();

constructor

  • with parameter
    cpp
    class MyClass {       
      public:
        int myNum;
        string myString;
        MyClass(int num) {
            myNum = num;
        }
    };
    ...
    MyClass obj(1);
  • defined outside
    cpp
    class Car {
      public:
        string brand;
        string model;
        int year;
        Car(string x, string y, int z); // Constructor declaration
    };
    
    // Constructor definition outside the class
    Car::Car(string x, string y, int z) {
      brand = x;
      model = y;
      year = z;
    }
  • initializer list
    cpp
    class Car {
      public:
        string model;
        Car(string model) : model(model) {}
    };

destructor

cpp
class MyClass {       
  public:
    ~MyClass() {  }
};

access modifier

  • public, private, protected
  • private by default

this

a pointer to current object

inheritance

  • using colon
    cpp
    class Vehicle {
    
    };
    class Car : public Vehicle {    // 1
    
    };
  • access modifier when inheriting (1 in above example)
    • public inherit
    • private inherit the inheritance is not known outside Car
  • multiple inheritance
    cpp
    class MyChildClass: public MyClass, public MyOtherClass {  };

virtual functions and override

  • Kind of run-time polymorphism
  • Ensures that correct function version is called
cpp
class Base {
public:
  virtual void foo() {
    cout << "This is Base" << endl;
  }
  void bar() {
    cout << "This is Base" << endl;
  }
};
class Sub1: public Base {
public:
  void foo() override {
    cout << "This is Sub1" << endl;
  }
  void bar() {
    cout << "This is Sub1" << endl;
  }
};
...
Sub1 sub1;
Base* ptr = &sub1;
Base& ref = sub1;
ptr->foo(); // This is Sub1
ptr->bar(); // This is Base
ref.foo();  // This is Sub1
ref.bar();  // This is Base

lvalue & rvalue

See https://www.internalpointers.com/post/c-rvalue-references-and-move-semantics-beginners

Polymorphism

General concept

  • Ad hoc overloading in oop
  • parametric generic in C# and Java
  • subtyping subclass implements method differently