C++Primer Plus练习
文章平均质量分 78
☆☆★☆☆
这个作者很懒,什么都没留下…
展开
-
C++的四种类型转换
/*C++中有4个类型转换符:static_castdynamic_castreinterpret_castconst_cast使用格式:xx_cast<type>(expression)*/#include <iostream>using namespace std;class Person { virtual void run() {};};class Student:public Person {};class Teacher {};..原创 2020-12-31 19:13:55 · 323 阅读 · 0 评论 -
C++中static的一种应用:单例模式
单例模式:设计模式的一种,保证某个类永远只创建一个对象1.构造函数私有化2.定义一个私有的static成员变量指向唯一的那个单例对象3.提供一个公共的访问单例对象的接口#include <iostream>using namespace std;class Person {private: static Person* teacher; //定义一个唯一的对象 Person() {} //构造函数私有化 Person(const Person&am原创 2020-12-31 16:18:02 · 517 阅读 · 0 评论 -
C++静态成员(static)
静态成员:被static修饰的成员变量\函数可以通过对象(对象.静态成员)、对象指针(对象指针->静态成员)、类访问(类名::静态成员)静态成员变量存储在数据段(全局区,类似于全局变量),整个程序运行过程中只有一份内存对比全局变量,它可以设定访问权限(public、protected、private),达到局部共享的目的必须初始化,必须在类外面初始化,初始化时不能带static,如果类的声明和实现分离(在实现.cpp中初始化)静态成员函数内部不能使用this指针(this指针只原创 2020-12-30 23:00:47 · 147 阅读 · 1 评论 -
C++几种运算符重载: +,-,+=,==,!=,++ ,负号,<<,>>
#include <iostream>using namespace std;class Point { int m_x; int m_y;public: int getX() { return m_x; }; int getY() { return m_y; }; Point(int x, int y) :m_x(x), m_y(y) {} void display() { cout << "(" << m_x << "," <.原创 2020-12-29 22:42:50 · 1297 阅读 · 0 评论 -
C++简单写入文本文件和读取文本文件
写入文本文件 使用文件输出的主要步骤如下: 1.包含头文件fstream 。 2.创建一个ofstream 对象。 3.将该ofstream 对象同一个文件关联起来。 4.就像使用cout 那样使用该ofstrcam 对象。#include <iostream>#include <fstream>using namespace std;int main() { //调用ofstrea...原创 2020-12-10 11:37:15 · 6594 阅读 · 0 评论 -
setw(),setfill() 和左右对齐 控制输出流的格式
头文件:#include <iomanip>setw():用来控制输出宽度的,括号里填写字符宽度。例:setw(7) 设置了7个空格,但并不是说后面紧跟的输出内容不能超过7个字符。后面输出的内容不受setw()宽度的影响。setfill():填充setw()中默认的空格。括号中可以自定义用什么去填充空格。例:setfill ( '+' ) 这里是用 + 号填充。如:#include <iostream>#include <iomanip...原创 2020-12-08 17:23:55 · 4251 阅读 · 0 评论 -
C++怎么判断输入string类字符是不是一个数字,然后将string类型转换成int型
判断方法来源于:https://zhidao.baidu.com/question/362958084.html#include<iostream>#include<string> //提供string类,说白了相当于一个动态数组,并且重载了[]运算符,所以可以访问下标#include <cctype> //调用isdigit函数要用它,isdigit函数是判断一个字符是否是数字,是返回true,否则返回falseusing namespace std;..原创 2020-12-02 11:20:13 · 2330 阅读 · 0 评论