C++基礎總結

  • 2020 年 12 月 28 日
  • 筆記

1.頭文件 : #include<iostream> //包含頭文件

2.命名空間: using namespace std;

3.對象定義: int z(0); //等同於 z = 0

4.函數聲明: int result(int,int);

5.#define 為宏定義,定義常量。

6.函數重載: int max(int,int); //2個整形參數的函數原型
int max(int,int,int); //3個整形參數的函數原型
例如: #include<iostream>
    using namespace std;
    int max(int,int);
            int max(int,int,int);
            void main(){
            cout<<max(35,25)<<“,”<<max(25,35,39)<<endl;
            }
            int max(int m1.int m2){
            return(m1>m2?m1:m2);
           }
            int max(int m1,int m2,int m3)
          {
            int t = max(m1,m2);
            return max(t,m3);
          }

7.動態分配記憶體 : new 類型名[size] //申請可以存儲size 個該數據類型的對象

8.指針名 = new 結構名;
delete 指針名;

9.delete x;為釋放空間

10.引用 :數據類型&別名= 對象名: int&a = c; //聲明a 是c 的引用,a 和c的地址相同

11.使用常量的指針是在非常量指針聲明前面使用const
const int *p;

12.常量指針
int x = 5;
int * const p = &x;

13.指向常量的常量指針
int x = 2;
const int* const p = &x;

14.包含頭文件<alogrithm> 可以對數組進行升冪排序,反轉數組的內容 ,複製數組的內容

15.包含頭文件<functional>可以對數組進行降冪排序和檢索

16.數組內容反轉:reverse(a,a+Len);

17.copy(a,a+Len,b)//將數組a 的內容複製到數組b

18.數組升冪排序 : sort(a,a+Len);

19.數組降冪排序: sort(a,a+Len,greater<Type>());

20查找數組內容:find(a,a+Len,value);

21.輸出數組內容:copy(a,a+Len,ostream_iterator<Type>(cout,”字元串”));

例子:
  #include<iostream>
  #include<alogrithm>
  #include<functionaa>
  using namespace std;
  void main(){
  double a[] = {1.1,4.4,3.3,5.5},b[4];
  copy(a,a+Len,ostream_iterator<double>(cout,””));
  cout<<endl;
  reverse_copy(a,a+4,ostream_iterator<double>(cout,””));
  cout<<endl;
  copy(a,a+4,b);
  copy(b,b+4,ostream_iterator<double>(cout,””));
  cout<<endl;
  sort(a,a+4);
  copy(a,a+4,ostream_iterator<double>(cout,””));
  cout<<endl;
  reverse_copy(a,a+4,b);
  copy(a,a+4,ostream_iterator<double>(cout,””));
  cout<<endl;
  }

22.setiosflags(long flag) 設置flag指定的標誌位;

23.setprecision(int n) 設置浮點數輸出精度n;

 —————————————————————————————–

——————————————————————————————————
1.結構
struct 結構體{
數據成員
成員函數
}
例子:
struct Point{
void Sety(double a,double b){
x = a; y =b;
}
};

2.構造函數: 函數名與結構名同名。 構造函數名 對象名 (初始化參數);

3.對象名稱.成員函數(參數(可供選擇的消息內容)) string str1 = “Where are you ” string newstr =st1.substr(3,3) 這裡的第一個參數是截取字元串中的字元位置,第2個參數是截取長度。

4.對象名稱.find(要查找的字元串,開始查找的位置);
int i = stt1.find(“are”,0);

5.使用complex對象
#include<complex> //定義複數對象

complex <數據類型> 對象名(實部值,虛部值);

例子: complex <int> num1(1.3); //複數 1+3i;
complex <float> num2(2.2,4); // 複數2.2 + 4i;

6.ostram_iterator<char>(cout) : 輸出語句

7.void swap(string ,string); //使用string 類的對象作為函數對象
swap(str1,str2); //使用傳值方式傳遞str1和str2的數據成員值

8. void swap(string*,string*); //使用string 類的指針作為函數參數
swap(&str1,&str2); //使用傳值地址方式傳遞str1和str2的地址值

9.void swap(int[]); //使用數組形參”類型[]”的形式
swap(a); //傳遞數組名a,也就是指針名

10.把實參對象的地址傳給形參對象,使形參對象的地址取實參對象的地址,從而使形參對象和實參對象共享同一個單元,這就是地址傳遞方式。

11.例子:
  #include<iostream>
  using namespace std;
  typedef double array[12]; //自定義數組標識符
  void avecount(array& b,int n) // 一個參數使用引用,一個參數使用對象
  {
  double ave(0);
  int count(0);
  for(int j=0;j<n-2;j++)
  {
  ave = ave + b[j];
  if(b[j] < 60)
  count++;
  }
  b[n-2] = ave /(n-2);
  b[n-1] =count;
  }
  void main()
  {
  array b = {12,15,18,20,98,49,80,62,54,98,100};
  array &a = b;
  avecount(a,12);
  cout<<“平均成績為”<<a[10]<<“分,不及格的人數為:”<<int (a[11])<<“人”<<end;
  }

12.默認參數
void Display(string s1,string st2 = “”, string st3 = “”); // 默認從右到左賦值

13. 使用const 保護數據
void change(const string&s)
{
string s2 = s +”NO”;
cout<< s2<<endl;
}

14.返回引用函數
數據類型&函數名(參數列表); int& index(int i);
例子:
  #include<iostream>
  using namespace std;
  int a[] = {2,4,6,8,9,18};
  int& index(int i); //原型聲明
  void main()
  {
  index (3) = 16;
  cout<<index(3)<<endl; //輸出16
  }
  int& index(int i)
  {
  return a[i]; //返回指定下標的整數數組內容
  }

15.返回指定函數
類型標識符 *函數名(參數列表) float *input(int& n);

16.返回對象的函數
string input(const int);

17. 內聯函數 : 其必須在程式中第一次調用此函數的語句出現之前定義
#include<iostream>
using namespace std;
inline int isnumber(char c)
{
return(c >=’0′ && c <=’9′) ? 1 :0;
}
void main()
{}

18.函數模板
Type max(Type m1,Type m2)
{return(m1 > m2) ? m1 : m2;}

19.#include<complex>
void printer(complex<int>);
void printer(complex<double>);
複數模板

20.函數模板的參數

函數模板名<模板參數>(參數列表)
max<int>(2,5)
函數模板名(參數列表)

21.使用顯式規則和關鍵字typename
作用是代替template 參數列表中的關鍵字class
template<typename T>
T max(T m1,T m2)
{}

————————————————————————————————————- 

—————————————————————————————————————————–
1.定義類
class 類名{
private:
私有數據和函數
public :
公有數據和函數
protected:
保護數據和函數
}

2.定義成員函數
返回類型 類名 ::成員函數名(參數列表)
{
成員函數的函數體
}
void Point : : Setxy(int a,int b)
{
x=a;y=b;
}

3.不能在類體內給數據成員賦值

4.類的對象

Point A,B; //定義Point類型的對象A和B
Point *p = &A; //定義指向對象A的Point類型的指針
Point &R = B; //定義R為Point 類型對象B的引用

“.” 訪問對象的成員,指針使用「->」
A.Setxy(11);
P-> Display();

5.print(A) //等價於A.Display();

6 類名 *對象指針名
類名* 對象指針名 = 對象的地址
對象指針名 -> 對象成員名 //訪問對象的成員

實例: 使用內聯函數Point 和使用Point類指針和引用

  #include<iostream>

  using namespace std;

  class Point {
  private:
  int x,y;
  public:
  void Setxy(int a, int b)
  {x = a; y = b; }
  void Move(int a,int b)
  {x = x + a; y = y + b;}
  void Display()
  {cout<<x<<“,”<<y<<endl;}
  int Getx()
  {return x;}
  int Gety()
  {return y;}
  };
  void print(Point *a)
  {
  a->Display();
  }
  void print(Point&a)
  {a.Display();}
  void main()
  {
  Point A ,B,*p;
  Point &RA = A;
  A.Setxy(34,54);
  B = A;
  p = &B;
  p ->Setxy(112,115);
  print(p);
  p->Display();
  RA.Move(-80,23);
  print(A);
  print(&A);
  }

7.Point : : point() :x(0),y(0); //定義不帶參數的構造函數
Point : : point(int a ,int b) :x(a),y(b);//定義帶2個參數的構造函數

8. Point C[2];
先對C[0]兩個數據成員初始化為0
再對C[1]兩個數據成員初始化為0
Point C[2] ={Point(5,6),Point(6,8)};

9.Point *ptr2 =new Point(2,8);
使用new建立的動態對象只能用delete刪除,釋放空間。

10.類型兼容規則賦值語句:

A&r = b; //派生類對象初始化基類引用
A*pa=&b;//派生類對象地址賦值給基類指針
B*pb = &b;
pa = pb; //派生類指針賦值給基類指針

11.私有繼承
class B :private A{};
當類的繼承方式為私有繼承時,基類中的公有成員和保護成員都以私有成員身份出現在派生類中,而基類的私有成員在派生類中不可以直接訪問。
12.定義派生類構造函數格式:
派生類名:派生類名(參數表):基類名1(基類1初始化參數表),….,基類名m(基類m初始化參數表),
成員對象名1(成員對象1初始化參數表),…,成員對象名n(成員對象n初始化參數表)
{
派生類構造函數函數體

}

————————————————————————————————————–
————————————————————————————————————–

1.頭文件 :<iostream>
2. 使用命名空間: using namespace(關鍵字) std
3:函數的原型聲明: result
4,定義變數 const
5,定義結構:struct
6, int z(x) x-賦值
7,定義結構對象 Point 相似 Point a = new Point;
8,輸出: count<<“,,,,,,,,”;
9, 輸入: cin>>”,,,,,,”;
10.換行 :endl C: \n Jave : println
11,函數重載:
int max(int,int);//2個整形參數的函數原型
int max(int,int,int);//3個整形參數的函數原型
12, 指針
new 類型名[size] //申請可以存儲size個該數據類型的對象
不使用的時候要用: delete p; 清理記憶體,釋放空間。

指針名 = new 結構名;//分配
delete 指針名; //釋放

13, 引用
數據類型&別名 = 對象名;
14,指向常量的指針
const int *p;

15., 數組升降冪用包含頭文件<algorithm>
降冪排序和檢索用包含頭文件<functional>

16,數組內容反轉 reverse(a,a + Len);
複製數組內容 copy(a,a +Len,b);複製到數組b
逆向複製 reverse_copy(a,a+Len,b);複製到數組b

升冪排序 sort(a,a+Len);
降冪排序 sort(b,b + Len, great<Type>());
查找 find (a,a+Len,value);//查找數組是否存在value 的元素

—————————————————————————————-
—————————————————————————————–

#include<iostream>

using namespace std;
int result(int , int);
const int k = 2;
struct Point{
int x,y;
};

int main()
{
int z(0),b(50);
Point a;
cout<<“輸入兩個整數(以空格區分):”;
cin >> a.x >> a.y ;
z = (a.x + a.y ) * k;
z = result(z,b);
cout<< “計算結果如下:” <<endl;
cout<<“((” <<a.x<< “+” <<a.y<< “)*” <<k<< “)+” <<b<< “=” <<z<<endl;
return 0;
}

int result (int a,int b)
{
return a + b;
}

———————————————————————————————-
———————————————————————————————-

C++ 的類

Class student
{
private :
char snane[20];
int ago;
public:
student(char na[],int ag);
student();
}
student::student(char na[] ,int ag): age(ag)//參數列表
{
Strepy(sname,na);
}
}

 

參數列表: myDate:: myDate(int y,int x,int d):year(y),month(x),day (d){

}
賦值只能在參數列表內的參數里賦值,不能在函數體里直接賦值

虛函數:
1.只有非靜態成員函數才能定義虛函數
2.構造函數不能定義虛函數
3.不要在構造函數和析構函數中調用虛函數
4.全局函數不能聲明為虛函數
5.靜態成員不能聲明為虛函數
6.內聯成員不能聲明為虛函數
7.友緣函數不能聲明為虛函

——————————————————————

——————————————————————-