C++语言基础二

85 阅读2分钟

类的构造

Student.h //避免多次引入相同头文件,编译不通过。

#ifndef STUDENT_H
#define STUDENT_H
class Student{
    int i;
    //声明友元函数
    friend void printI(Student*);
    //声明友元类
    friend  class Teacher;
    
    public:
      Student();
      Student(int age);
      ~Student(); //析构函数
      void setAge(int age);
       void setI(int i);
    
    private:
        int i;
    protected:
        int j;
        
}
#endif //STUDENT_H


Student.cpp

#include"Student.h"
#include<iostream>
using namespace std;

Student::Student(){
    cout << "构造方法" << endl;
}

Student::~Student(){
    cout << "析构方法" << endl;
}

void Student::setAge(int age) : age(age){
    
}

/**
* 该函数为常量函数,常量函数不能修改
* 成员的属性值。
*/
void Student::setI(int i) const{
    
}

//main调用
void main(){
    Student s;
    cout<< "hello world" << endl;
}

单例模式

class Student{
    private:
        static Student* instance;
        Student();
    public:
        Student* getInstance();
}

//cpp

Student* Student::instance = 0;

Student* Student::getInstance(){
    if(!instance){
        instance = new Student();
    }
    return instance;
}

函数重载

操作符重载

class Test {
    public: 
        int i;
        
        Test operator+(const Test& t){
            Test t3;
            t3->i = this->i + t->i;
            return t3;
        }
}


void main(){
    Test t1;
    t1.i = 10;
    Test t2;
    t2.2 = 20;
    Test t3 = t1 + t2;
}

继承与多态

class Parent{
    public:
        void test();
        
        //纯虚函数,需要让子类必须实现的函数
        virtual void test1()=0;
}

class Child1:Parent{ //默认私有继承
    //注意: 私有继承只能继承父类的私有属性
    public:
        void test();
}

  
class Child1:public Parent{ //共有继承
    
}

//多态分为静态多态和动态多态
//java中的多态为动态多态,C++中需要配合vitural关键子使用。

virtual关键字使用时的注意事项:

  1. 构造方法不要声明为虚方法;
  2. 析构方法需要声明为虚方法。

模板编程

函数模板 (java 泛型方法 )

template
T compare(T t1,T t2){
return t1>t2?t1:t2;
}

类模板 (java 泛型类)

template<class T, class E>
class Q{
public:
T test(T t,E e){
return t;
}
}

类型转换和异常处理

类型转换

强制类型转换
a. const_cast //字面上理解是去除const
b. static_cast //静态类型转换,如int 转char
c. dynamic_cast //父类和子类之间的类型转转换
d. reinterpret_cast //重解释类型


//const_cast转换
const char* a;
char* b = const_cast<char*>a;

char* c;
const char* d = const_cast<char*>c;

//static_cast转换
//1.基本数据类型的转换
//2.void* 与其他类型指针的转换
//3.父类与子类的指针或者引用的转换(发生在编译期,针对静态多态)
dynamic_cast转换
//父类与子类的指针或者引用的转换(发生在运行期,针对动态多态)

异常处理

try{
    
}catch(const char* m){
    
}

try{
    
}catch(Exception &e){
    
}

文件与流操作

//C语言
#include<stdio.h>

fputc
fputs
fgetc

//C++语言
ofstream ifstream

容器

  • 序列性容器

    vector list dequeue queue stack priority queue

      vector<int> vec_1;
      vector<int> vec_2(1); //初始化长度为1
      vector<int> vect_3(6,1); //初始化长度为6,元素初始值为1;
      vector<int> vect_4(vect_3);
      //增加元素
      vec_1.push_back(11);
      //访问元素
      vec_1[0];
      //分别获取队首和队尾的元素
      vec_1.font();
      vec_2.back();
      vec_1.clear();
      vec_1.erase(1,2);
    
  • 关联性容器

    set map

      set<int> set1 = {1,2,3,4};
      set.size(); //获取长度
      set1.insert(6);
      
      pair<set<int>::interator,boolean> pair = set1.insert(77);
      
      set<int>::interator itt = set1.begin();
      
      for(;itt!=set1.end();itt++){
          cout << *itt << endl;
      }
    
      map<int,string>map1;
      map<int,string>map2 = {{1,'ss'},{2,'aa'}};
      map2.insert({3,'dfs'});
    

命名空间

为了避免方法名冲突,而采用命名空间

namespace space1 {
    void fun(){
        cout<< "space1 fun" << endl;
    }
}

namespace space2{
    void fun(){
        cout<< "space2 fun" << endl;
    }
}

int main(){
    space1::fun();
    space2::fun();
}

引用

c++中引用和指针的区别:
    a. 不存在空引用,引用必须关联到内存空间
    b. 一旦引用被初始化为某个对象,就不能被指向到其他对象;但是指针可以。
    c. 引用必须在创建时,进行初始化。
    
int a = 11;
int * c = &a;
int & d = a;