Note: the coding tool is CLion+Cygwin64
catalogue
Reference and constant reference
Exchange two variable values by reference
Special writing method of invisible parameter variable name
Difference from C
C language is process oriented and C + + is object-oriented.
C language environment cannot run C + + code, and C + + environment can run C code.
C language constants are pseudo constants and C + + constants are true constants.
C language does not allow function overloading, and C + + allows function overloading.
constant
C language
You can modify the value of a constant indirectly through the pointer.
#include <stdio.h> int main(){ const int num = 100; printf("num = %d\n", num); // num = 10000; // Compilation failed int * num_p = # *num_p = 10000; printf("num = %d\n", num); return 0; }
Output:
num = 100 num = 10000
C++
#include<stdio.h> int main() { const int num = 100; printf("num = %d\n", num); // num = 10000; // Compilation failed // int *num_ p = #// Compilation failed return 0; }
Output:
num = 100
Reference and constant reference
Reference: is an alias for a memory address. Defined by the format of the data type &.
Exchange two variable values by reference
#Include < iostream > / / C + + standard IO using namespace std;// Namespace void swap(int &a, int &b){ int tmp = a; a = b; b = tmp; } int main(){ int num1 = 99; int num2 = 100; cout << "num1 = " << num1 << ", num2 = " << num2 << endl; /** * cout: C++output * << Operator overloading * endl Equivalent to \ n */ swap(num1, num2); cout << "num1 = " << num1 << ", num2 = " << num2 << endl; return 0; }
Output:
num1 = 99, num2 = 100 num1 = 100, num2 = 99
const reference
Constant references are used when you only need to display data and are not allowed to modify data.
#include <iostream> using namespace std; void show(const int & a){// Using object can better reflect the effect of constant reference // a = 10000;// Compilation failed cout << "a = " << a << endl; } int main(){ show(100); return 0; }
Output:
a = 100
function overloading
C language
#include <stdio.h> int add(int a, int b){ return a + b; } // Compilation error //int add(int a, int b, int c){ // return a + b + c; //} int main(){ return 0; }
C++
#include <iostream> using namespace std; int add(int a, int b){ return a + b; } int add(int a, int b, int c){ return a + b + c; } int main(){ int num = 99, num2 = 100, num3 = 10000; cout << num << " + " << num2 << " = " << add(num, num2) << endl; cout << num << " + " << num2 << " + " << num3 << " = " << add(num, num2, num3) << endl; return 0; }
Output:
99 + 100 = 199 99 + 100 + 10000 = 10199
Default parameter
#include <iostream> using namespace std; int add(int a = 3, int b = 99){ return a + b; } int main(){ int num = 99, num2 = 100; cout << add() << endl; cout << add(num) << endl; cout << num << " + " << num2 << " = " << add(num, num2) << endl; return 0; }
Output:
102 198 99 + 100 = 199
Special writing method of invisible parameter variable name
Part of the parameters declared by the method only have data types and no variable names. This method is written in the C + + source code, which may be occupied in advance for future expansion.
#include <iostream> using namespace std; void log(char * text, int, int){ cout << "text = " << text << endl; } int main(){ log("Log content", 1, 1); return 0; }
Output:
text = Log content
Preliminary study on class
Standard definition method of class:
Both header and implementation files are defined.
Only variable and function declarations are defined in the header file.
Implement the function implementation of the class stored in the file.
Header file:
class Student{ private: char * name; int age; public: void setName(char* name); void setAge(int age); char * getName(); int getAge(); };
Implementation file:
#include "Student.h" void Student::setName(char *name) { this->name = name; } void Student::setAge(int age) { this->age = age; } char * Student::getName() { return this->name; } int Student::getAge() { return this->age; }
objects creating
There are two ways to create memory, static and dynamic.
The keyword new is required for dynamic creation, and delete needs to be called manually for release. New and delete are paired. Similar to malloc and free.
#include <iostream> #include "Student.h" using namespace std; int main(){ Student stu;// Created in a static memory mode, the memory will be released automatically after the function is executed stu.setName("zhang wuji"); stu.setAge(18); cout << stu.getName() << ", " << stu.getAge() << endl; // With or without (), it can be created by dynamically opening up memory. You need to manually call delete to release memory Student * stu2 = new Student; stu2->setName("ss"); stu2->setAge(14); cout << stu.getName() << ", " << stu2->getAge() << endl; if(stu2) { delete stu2;// Manually free dynamic memory stu2 = NULL; } return 0; }
Output:
zhang wuji, 18 zhang wuji, 14
Boolean type of C + +
The non-zero or true of C language is still applicable in C + +.
At the same time, C + + has its own boolean type, which is declared with bool.
Literal: true prints as 1, false prints as 0.
#include <iostream> using namespace std; int main(){ bool flag = true; cout << "flag = " << flag << endl; cout << false << endl; if(239847){ cout << "True" << endl; } if(!0){ cout << "True" << endl; } if(-384.23) { cout << "True" << endl; } return 0; }
Output:
flag = 1 0 True True True