C++学习笔记(一)

    科技2023-10-28  104

    cout<<"input number:"<<endl;

    1.cout 输出打印,相当于C语言的printf。cin 控制台输入,相当于C语言的scanf。endl是打印换行。 cout<<"input number:"中“<<”的意义是将字符串input number:插入到输出流cout中返回,后面也是将endl插入到字符串中,表示换行。

    2.cin的使用 cin是接受控制台输入的数据,必须先定义一个指定类型的变量接收。

    #include <iostream> using namespace std; int main() { int a; cin>>a; cout<<"输入的数字是"<<a<<endl; return 0; }

    也可以连续输入多组数据

    cin>>a>>b>>c;

    3.函数模板 C++中的函数模板类似于Java中的泛型形式如下

    template<class 类型名1class 类型名2> 返回值 函数名(形参表列) { 函数体 }

    举例:

    #include <iostream> using namespace std; template <class T1,class T2> T1 add(T1 t1,T2 t2){ T1 result = t1+t2; cout<<"结果是"<<result<<endl; return result; } int main() { add(12,13); add(23,25); return 0; }

    4.内联函数 内联函数的申明用inline关键字。 每一个函数,在执行过程中系统都会开辟一块栈空间,存放函数的各种数据,如果一个函数执行代码少,调用次数多,就会需要很多栈空间,造成资源浪费,这里C++就创造了内联函数的概念来解决这个问题。 内联函数的申明和定义实现必须写在一起,不能先申明后定义,如果是是先申明后定义,则编译器会取消内联。 内联函数必须定义在调用函数的前面。 内联函数,就是在编译期,编译器会将内联函数代码直接插入到调用处,从而免去函数的调用过程。 申明定义的内联函数,C++编译器不一定会允许内联,当if else、switch、for循环,条件判断的语句过多时,编译器会取消内联。 总结内联函数的限制:

    不能存在任何形式的循环语句不能存在过多的条件判断语句函数体不能过于庞大不能对函数进行取地址操作函数内联申明必须在调用语句之前 当用户没有用inline申明内联函数,但函数内逻辑代码超级简单,比如就一个打印语句,编译器也有可能将函数内联。

    举例:

    #include<iostream> using namespace std; inline int Max(int a,int b) { return a>b?a:b; } int main() { cout<<Max(3,5)<<endl; cout<<Max(7,9)<<endl; return 0; }

    5.类的定义 C++ 中类的定义跟Java类似可以在类中定义属性和方法。如果类中定义的方法比较多,读起来就感觉很乱,所以C++就添加了另外一种机制,可以在类里面申明,在类外实现。 举例:

    #include <iostream> using namespace std; class Student{ char name[100]; int age; void goHome(); }; void Student::goHome(){ cout<<"我是开车回家的"<<endl; }

    6.类的使用 类的初始化直接用.进行赋值即可,访问可以用指针

    #include <iostream> #include <cstring> using namespace std; class Student{ public: char name[100]; int age; void goHome(); }; void Student::goHome(){ cout<<"我是开车回家的"<<endl; } int main() { Student student; student.age = 16; strcpy(student.name,"小明"); cout<<"我是"<<student.name<<endl; cout<<"我的年龄是"<<student.age<<endl; student.goHome(); cout<<"-----------------------------------------------------"<<endl; Student *P_student = &student; cout<<"我是"<<P_student->name<<endl; cout<<"我的年龄是"<<P_student->age<<endl; P_student->goHome(); return 0; }

    7.类的构造函数 如果不定义构造函数,系统会默认添加无参的构造函数,如果已经定义了构造函数,则系统不会给我们添加。

    class Student{ public: Student(char *name,int age); char name[100]; int age; void goHome(); }; void Student::goHome(){ cout<<"我是开车回家的"<<endl; } Student::Student(char *str ,int a){ strcpy(name,str); age = a; }

    8.析构函数(Destructor) 析构函数跟构造函数并列,构造函数是在创建对象是调用,而析构函数是在对象销毁时调用,一般用于对象资源的释放。同构造函数一样,如果用户自己不定义析构函数,则系统会默认给我们加上一个默认的析构函数。如果我们自定义了,系统就不会给我们加,析构函数只能有一个。

    class Student{ public: Student(char *name,int age); ~Student(); char name[100]; int age; void goHome(); }; void Student::goHome(){ cout<<"我是开车回家的"<<endl; } Student::Student(char *str ,int a){ strcpy(name,str); age = a; cout<<"我是构造函数"<<endl; } Student::~Student(){ cout<<"我是析构函数"<<endl; }

    9.友元函数 如果有些私有属性,希望在特定 的场景或者是被特定的人访问,就可以用友元函数。如下所示,友元函数用friend 申明

    class Point{ private: double x; double y; public: Point(double a,double b){ x = a; y = b; } int GetPoint(){ cout<<"("<<x<<","<<y<<")"; return 0; } friend double Distance(Point &a,Point &b); }; //求两点之间的距离 double Distance(Point &a,Point &b){ double xx; double yy; xx = a.x-b.x; yy = a.y-b.y; return sqrt(xx*xx+yy*yy); } 友元函数中没有this指针,不能用this。如果访问的是类的非静态成员,则需要对象作为参数,不然外面还是没法范围私有属性和函数如果要访问的对象是全局对象,则函数这里就不需要参数。 10.友元类的申明与使用 #include<iostream> #include<math.h> using namespace std; class Point{ private: double x;//私有变量,外部无法访问 double y;//私有变量,外部无法访问 public: Point(double a,double b){ x = a; y = b; } int GetPoint(){ cout<<"("<<x<<","<<y<<")"; return 0; } int distancetoLine(){ } friend class Tool;//申明友元类,意义是在Tool类中可以访问Point类中的私有属性和私有函数 }; class Tool{//实现友元类 public: double GetX(Point &A){ cout<<A.x<<endl; return A.x;//通过友元类访问 } double GetY(Point &A){ cout<<A.y<<endl; return A.y; } double dis(Point &A){ cout<<sqrt(A.x*A.x+A.y*A.y)<<endl; return sqrt(A.x*A.x+A.y*A.y); } };

    11.继承类的写法

    class AlarmClock:public Clock{}

    12.函数的默认参数

    void defaultFun(int a = 10){ cout<<"defaultFun a ="<<a<<endl; } int main(){ defaultFun(); defaultFun(16); }

    输出结果为

    defaultFun a =10 defaultFun a =16

    有打印结果可以看出,如果调用有默认参数的函数,也可以不穿参数,系统会自动取默认参数执行相关代码。 默认参数规则:

    若调用函数是填写的参数,则使用你填写的参数。多参数的函数,如果有填写参数,则参数是从左边开始填充。

    13.函数的占位参数

    void add(int a,int b,int){ int c = a+b; } int main(){ add(10,23,12); }

    占位参数的意义是兼容C语言中可能出现的不规范写法。

    结语:C++跟Java同属于面向对象的编程语言,多态,继承等特性都有,使用跟java类似。

    Processed: 0.013, SQL: 8