­

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.友缘函数不能声明为虚函

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

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