C++知识点示例4

标头.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;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 221,635评论 6 515
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 94,543评论 3 399
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 168,083评论 0 360
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 59,640评论 1 296
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,640评论 6 397
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 52,262评论 1 308
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,833评论 3 421
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,736评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 46,280评论 1 319
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,369评论 3 340
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,503评论 1 352
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 36,185评论 5 350
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,870评论 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,340评论 0 24
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,460评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,909评论 3 376
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,512评论 2 359

推荐阅读更多精彩内容