C++知识点示例4

2018-10-11  本文已影响0人  MagicalGuy

标头.h

#pragma once
namespace XXX {
    int b;
}

#include "stdafx.h"
#include "标头.h"
#include <iostream>
#include <iomanip>
using namespace std;
using std::cin;
namespace XXX {
    int a;
}
class A {
public:
    int m_nNum;
};
class B :protected A{
public:
    A::m_nNum;// 保护变公有
};
class C {
public:
    C() { printf("C\n"); }
    B m_b;
    A m_a;
};

class D {
public:
    explicit D(int a) {
        m_nNum = a;
    }
    int m_nNum;
};
int main() {
    D obj(10);
    D objA = D(20);
    objA = D(100);
    return 0;
}

===========================

// 01.静态联编.cpp: 定义控制台应用程序的入口点。
//

#include "stdafx.h"
class A {
public:
    virtual void  Fun1() {
        printf("A::Fun1\n");
    }
    void Fun2() {
        printf("A::Fun2\n");
    }
};
class B :public A {
public:
    virtual void Fun2() {
        printf("B::Fun2\n");
    }
    virtual void Fun1() {
        printf("B::Fun1\n");
    }
};
int main() {
    A objA, *pA;

    B objB, objB2,*pB;
    pA = &objB;
    pA->Fun1();
    pA->Fun2();
    pB = (B*)&objA;
    pB->Fun1();
    pB->Fun2();
    return 0;
}

=========================

// 02.多继承的动态联编.cpp: 定义控制台应用程序的入口点。
//

#include "stdafx.h"

class A1 {
public:
    virtual void Fun1() {
        printf("A1::Fun1\n");
    }
    virtual void Fun2() {
        printf("A1::Fun2\n");
    }
};
class A2 {
public:
    virtual void Fun1() {
        printf("A2::Fun1\n");
    }
    virtual void Fun2() {
        printf("A2::Fun2\n");
    }
};
class A :public A1, public A2 {
public:
    virtual void Fun1() {// 两个表里的Fun1函数地址都变成A::Fun1
        printf("A::Fun1\n");
    }
    virtual void Fun3() {// Fun3的地址放在拷贝自A1的虚函数表数组后面
        printf("A::Fun3\n");
    }
};
int main()
{
    A1 objA1;
    A2 objA2;
    A obj;
    return 0;
}

================================

// 03.纯虚函数和抽象类.cpp: 定义控制台应用程序的入口点。
//

#include "stdafx.h"

class A {
public:
    virtual void Fun1() = 0;
};
class A1 :public A {
public:
    // 派生类可以不实现纯虚函数
    // 如果不实现,该类也是抽象类
    // 不能实例化对象
    void Fun2() {
        printf("A1\n");
    }
};
int main()
{
    A obj;// 抽象类不能实例化对象
    A1 obj1;
    return 0;
}

=========================

#include "stdafx.h"

class CBase1 {
public:
    CBase1() { m_nNum1 = 10; }
    int m_nNum1;
    virtual ~CBase1() {}
};
class CBase2 {
public:
    CBase2() { m_nNum2 = 20; }
    int m_nNum2;
    virtual ~CBase2() {}
};
class CDerived :public CBase1, public CBase2 {
public:
    virtual ~CDerived() {}
};

int main()
{
    CDerived obj;
    CBase1 *objBase1 = &obj;
    CBase2 *objBase2 =dynamic_cast<CBase2*>(objBase1);
    return 0;
}

===============================

// 00.函数模板.cpp: 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <string.h>

template<typename T>
int Max(T a, T b) {
    return a > b ? a : b;
}

template<>
int Max<char*>(char* a, char* b) {
    return strcmp(a, b);
}

int Max(char* a, char* b) {
    return strcmp(a, b);
}

double Max(double a, double b) {
    return a > b ? a : b;
}

void WriteChar( int score) {
    printf("%d", score);
}
void WriteChar(char* score) {
    printf("%s", score);
}

int main()
{
    // 函数参数是int,int
    Max(10, 20);
    Max(100, 200);
    const char *p1 = "123";
    const char* p2 = "abc";
    Max(p1, p2);
    return 0;
}

==============================

// 01.函数匹配顺序.cpp: 定义控制台应用程序的入口点。
//

#include "stdafx.h"

template<typename T>
void Max(T a, T b) {
    printf("1\n");
}

template<typename T>
void Max(T* a, T* b) {
    printf("2\n");
}

// int* 输出2
// int 输出3
template<>
void Max<int*>(int* a, int* b) {
    printf("3\n");
}


int main()
{
    int a, b;
    Max(&a, &b);
    return 0;
}

============================

// 02.类模板.cpp: 定义控制台应用程序的入口点。
//

#include "stdafx.h"

template<class T>
class CVector {
public:
    CVector() {
        m_nCount = 0;
        m_p = new T[10]{};
    }
    void Push(T nEle);
    void Pop() {
        m_nCount--;
    }
    int m_nCount;
    T *m_p;
};
// 模板类成员函数的类外实现
template<class T>
void CVector<T>::Push(T nEle) {
    m_p[m_nCount++] = nEle;
}

int main() {
    CVector<int> obj;
    return 0;
}

==============================

// 03.STL.cpp: 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <string>
using std::string;
#include <vector>
using std::vector;
#include <map>
using std::map;
void VecTest() {
    vector<int> obj;
    // 从后端添加新元素
    obj.push_back(10);
    // 指定位置插入1个元素
    obj.insert(obj.begin(), 20);
    // 指定位置插入10个相同元素
    obj.insert(obj.begin(), 10, 100);
    // 指定位置插入另一个vector中的元素
    vector<int> obj2;
    obj2.insert(obj2.begin(), obj.begin(), obj.begin() + 10);
    // 删
    // 删除最后一个元素
    obj.pop_back();
    // 删除下标为1的元素
    obj.erase(obj.begin() + 1);
    // 删除下标2~4 3个元素
    obj.erase(obj.begin() + 2, obj.begin() + 5);
    // 改
    obj[3] = 200;
    // 查
    // 数组元素个数
    int nCount = obj.size();
    for (int i = 0; i < nCount; i++) {
        if (obj[i] == 100) {
            obj[i] += 100;
        }
    }
}

void StringTest() {
    string obj;
    // 增
    obj += "12345";
    obj.insert(obj.begin(), 'a');
    // 在0这个位置插入字符串1111
    obj.insert(0, "1111");
    // 删
    // 删除下标从0开始的两个元素
    obj.erase(0, 2);
    // 只删除1个
    obj.erase(obj.begin());
    // 全部删除
    obj.erase();
    // 改
    obj[1] = '0';
    // 查
    // 查找字符
    obj.find('0');
    // 查找字符串
    obj.find("1111");
    // 字符串比较
    obj == "1111";
    obj.compare("1111");
}

void MapTest() {
    map<const char*, int> score;
    // 增
    score["班长"] = 100;
    score["学习委员"] = 0;
    // 查
    if (score.find("班长") != score.end())
        printf("班长的成绩=%d", score["班长"]);
    // 删
    score.erase("班长");
    // 改
    score["学习委员"] = 60;
}

int main() {
    MapTest();
    return 0;
}

============================

// 04.异常处理.cpp: 定义控制台应用程序的入口点。
//

#include "stdafx.h"

void Fun()throw(int,char) {
    throw 12;
}

int main()
{
    //int a = 10, b = 0;
    //try
    //{
    //  // 把有可能发生异常的地方用try包起来
    //  if (b == 0)
    //  {
    //      throw "除数不能为0";
    //  }
    //  int c = a / b;
    //  throw 1.2;
    //}
    //catch (const char* e)
    //{
    //  printf("异常:%s\n",e);
    //}
    //catch (int) {
    //  printf("正常\n");
    //}
    //catch (...) {
    //  printf("未知类型\n");
    //}
    try {
        Fun();
    }
    catch (...) {
        printf("111\n");
    }
    return 0;
}

=============================

// 05.自定义异常类.cpp: 定义控制台应用程序的入口点。
//

#include "stdafx.h"

class CMyError {
public:
    CMyError() {}
    void Fun() {
        printf("pop错误,CVector已空\n");
    }

};

template<class T>
class CVector {
public:
    CVector() {
        m_nCount = 0;
        m_p = new T[10]{};
    }
    void Push(T nEle);
    void Pop() {
        if (m_nCount == 0)
        {
            throw CMyError();
        }
        m_nCount--;
    }
    int m_nCount;
    T *m_p;
};
// 模板类成员函数的类外实现
template<class T>
void CVector<T>::Push(T nEle) {
    m_p[m_nCount++] = nEle;
}

int main()
{
    CVector<int> obj;
    for (int i = 0; i < 10; i++) {
        obj.Push(i);
    }
    for (int i=0;i<100;i++)
    {
        try
        {
            obj.Pop();// 触发异常后面的代码不会被执行
            printf("%d\n", i);
        }
        catch (CMyError& e)
        {
            e.Fun();
            break;
        }
    }
    return 0;
}

================================

// 06.系统异常类的使用.cpp: 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <exception>
using std::exception;

class CMyError :public exception {
public:
    CMyError(int nPos) :exception("下标越界\n"), m_nPos(nPos) {
    }
    virtual const char *what() const {
        printf("数组下标:%d\n", m_nPos);
        return exception::what();
    }
    int m_nPos;
};

template<class T>
class CVector {
public:
    CVector() {
        m_nCount = 0;
        m_p = new T[10]{};
    }
    void Push(T nEle);
    void Pop() {
        m_nCount--;
    }
    T& operator[](int nPos) {
        if (nPos >= m_nCount) {
            throw CMyError(nPos);
        }
        return m_p[nPos];
    }
    int m_nCount;
    T *m_p;
};
// 模板类成员函数的类外实现
template<class T>
void CVector<T>::Push(T nEle) {
    m_p[m_nCount++] = nEle;
}

int main() {
    CVector<int> obj;
    for (int i = 0; i < 10; i++) {
        obj.Push(i);
    }
    for (int i = 0; i < 100; i++) {
        try {
            //obj.operator[](i) = i+10;
            obj[i] = i + 10;
            //printf("%d\n", i);
        }
        catch (const std::exception& e) {
            printf("%s\n", e.what());
            break;
        }
    }
    return 0;
}

===============================

// 07. 文件的输入输出.cpp: 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <fstream>
using namespace  std;
int main()
{
    // 向文件内写
    ofstream outFile("aaa.txt",ios_base::out|ios_base::binary,_SH_DENYRW);
    int a = 12;
    outFile.write((const char*)&a, 4);
    outFile.close();
    // 从文件中读
    ifstream inFile("aaa.txt", ios_base::in | ios_base::binary, _SH_DENYRW);
    char buf[100] = {};
    // 获取文件大小
    //把文件指针移动到最后位置
    inFile.seekg(0,// 移动到距离下一个参数的偏移
        ios_base::end);// 移动基准
    // 告诉我文件指针偏移值
    int nSize = inFile.tellg();
    // 把文件指针移动到开始位置,因为要读数据
    inFile.seekg(0,ios_base::beg);
    //inFile.read(buf, nSize);
    int b;
    inFile.read((char*)&b, 4);
    printf("%d\n", b);
    // 既读又写
    //fstream ioFile;
    //ioFile.open("aaa.txt", ios_base::app | ios_base::in | ios_base::out | ios_base::binary);
    return 0;
}
上一篇 下一篇

猜你喜欢

热点阅读