
C++学习笔记
文章平均质量分 75
C++笔记
理心炼丹
只争朝夕
展开
-
【翁恺】37-STL简述
其他参考https://blog.youkuaiyun.com/qq_40155090/category_10014675.html?spm=1001.2014.3001.5482原创 2022-04-27 15:41:24 · 931 阅读 · 0 评论 -
【翁恺】36-流的运算符
标准流:原创 2022-04-27 15:25:43 · 548 阅读 · 0 评论 -
【翁恺】35-流的概念
C++标准输入输出流:cout、cin(C中printf、scanf不安全,不对字符串里面进行检查)。以流的方式对文本进行操作、对外部设备的输入输出。inserter:把一个对象插入到输出流里面的运算符;<<extractor:把一个对象从流里面解析出来的运算符;>>流,一维(一个数字就能确定位置)单方向 (流过去了就没有了,不能在任意的地方读写),左移右移针对整数来讲的,看<<、>>运算符的。流的形式:文本形...原创 2022-04-27 11:29:19 · 883 阅读 · 0 评论 -
【翁恺】34-异常语句
try blocks try blocks try {...} catch ... catch ... establishes any number of handers not needed if you don't useanyhandlers shows where you expert to handle exceptions const cycles exception handlers select excepti..原创 2022-04-27 11:10:58 · 158 阅读 · 0 评论 -
【翁恺】33-异常的抛出和捕捉
example:Vectortemplate <class T> class Vector{private: T* m_elements; int m_size;public: Vector (int size = 0):m_size(size)... ~Void(){delete [] m_elements;} void length(int); int length(){return m_size;} T& opera原创 2022-04-27 10:35:55 · 869 阅读 · 0 评论 -
【翁恺】32-异常基本概念
run-time errorthe basic philosophy of c++ is that "badly formed code will not be run" there's always something happen in run-time it is very important to deal with all possible situation in the future runningread a fileopen the file determine its s原创 2022-04-26 17:34:09 · 463 阅读 · 0 评论 -
【翁恺】31-模板
why templates? suppose you need a list of X and a list of Y the lists would use similar code they differ by the type shored in the list choices require common base class 方案1 may not be desirable clone code 方案2 preserves type-safe....原创 2022-04-26 16:44:13 · 341 阅读 · 0 评论 -
【翁恺】30-运算符重载-类型转换
value classesappear to be primitive data types passed to and returned from functions have overloaded operators(often) can be converted to and from other types example: Complex, Data, Stringuser-defined type conversionsa conversion operator can be原创 2022-04-26 11:48:43 · 239 阅读 · 0 评论 -
【翁恺】29-运算符重载-赋值
copying vs. initializationMyType b;MyType a = b;//拷贝构造:做了一个新的对象出来a = b;//赋值------------------------class Fi{public: Fi(){}};class Fee{public: Fee(int){} Fee(const Fi&){}};int main(){ Fee fee = 1;//Fee(int) Fi f..原创 2022-04-26 10:52:48 · 173 阅读 · 0 评论 -
【翁恺】28-运算符重载-原型
argument passing 参数传递if it is read-only pass it in as a const reference(except built-ins) make member functions const that don't change the class (boolean operators,+,-,etc) 不修改算子的const,修改的不加const for global functions,if the left-hand side changes pa..原创 2022-04-26 10:25:20 · 414 阅读 · 3 评论 -
【翁恺】27-运算符重载-基本规则
overloaded operators 重载几乎所有的运算符 allows user-defined types to act like built in types another way to make a function call unary and binary operators can be overloaded: + - * / % ^(异或) & | ~= < > += -= *= /= %=^= &= |= <..原创 2022-04-25 18:35:20 · 391 阅读 · 0 评论 -
【翁恺】26-静态成员
can we apply static to members? static means hidden persistent hidden:A static member is a member obeys usual access rules persistent:independent of instances static members are class-wide variables or functions static member.原创 2022-04-21 12:55:04 · 661 阅读 · 0 评论 -
【翁恺】25-静态对象
static 在C语言中有两层含义:1. 存储是持久存储的 2. 访问是受限的static in c++C++ 中更复杂,因为有成员变量和成员函数。 C 中 全局变量static 那么该变量就只在.c 文件内有效,本地变量static 那么该变量就是持久存储,statictwo basic meaningsstatic storage allocated one at fixed address visibility of a name internal linkage ..原创 2022-04-21 10:12:11 · 257 阅读 · 0 评论 -
【翁恺】24-拷贝构造2
如果类包含指针?Copy pointer:#ifndef PERSON_H_#define PERSON_H_class Person{public: Person(const char* s);//构造函数 ~Person();//析构函数 void print();//函数原型,没有实际body,有一些什么样的函数 //...//private: char* name ;//数据成员 char* instead of string //...};#endif // !原创 2022-04-20 13:07:29 · 836 阅读 · 1 评论 -
【翁恺】23-拷贝构造1
copying create a new object from an existing one for example,when calling a function //currency as pass-by-value argumentvoid function(Currency p){ cout<<"X = "<<p.dollars;}...Currency bucks(100,0);func(bucks);//bucks is co原创 2022-04-20 00:07:46 · 878 阅读 · 0 评论 -
【翁恺】22-引用再研究
references as class members declared without initial value如果一个类的成员是引用,那么声明的时候没有办法给一个初始值。 must be initialized using constructor initializer list class X{public: int& m_y; X(int& a);};X::X(int a):m_y(a){} returning references.原创 2022-04-18 23:38:09 · 200 阅读 · 0 评论 -
【翁恺】21-多态的实现
How virtuals work in c++任何一个类如果有虚函数,这个类的对象就会比正常的大一点。#include <iostream>using namespace std;class A{public: A():i(10){}; virtual void f(){ cout<<"A::f()---->i="<<i<<endl; } int i;};int main(){ A a; a.f(); c.原创 2022-04-18 23:06:16 · 325 阅读 · 0 评论 -
【翁恺】20-多态性
a drawing programdata center operatons render move resize Inheritance in C++can define one class in term of another can capture the notion that an ellipse is a shape a circle is a special kind of ellipse a rectangle is a different sh原创 2022-03-15 23:12:23 · 1175 阅读 · 0 评论 -
【翁恺】19-向上造型
conversionspublic inheritance should imply(意味着) substitution(代替) if B is a A,you can use a B anywhere an A can be used if B is a A, then everything that is true for A is also true of B be careful if the substitution if not valid D is derive原创 2022-03-15 22:46:10 · 1249 阅读 · 0 评论 -
【翁恺】18-引用
C++ 复杂在于:太多的能够放对象的地方:对象可以放在 堆栈、堆、全局数据区 太多可以访问对象的方式:直接掌握对象(变量内放对象)、通过指针访问对象、引用去访问对象3*3 共 9种组合。Declaring references references are a new data type in C++ char c; //a characterchar* p = &c; //a pointer to a characterchar&am原创 2022-03-14 23:13:10 · 1042 阅读 · 4 评论 -
【翁恺】17-const
constdeclares avariableto have a constant value.constantsconstants are variables observe scoping rules declared with "const" type modifier(修饰) compile time constantsconst int bufsize = 1024;value must be initialized unless you mak...原创 2022-02-07 20:00:00 · 458 阅读 · 0 评论 -
【翁恺】16-内联函数
overhead(额外开销) for a function callthe processing time required by a device prior to the execution of a command push a parameters push return address prepare return values pop all pushed 调取一个函数,利用堆栈实现,额外开销如上inline functions an inline funct原创 2022-02-07 14:46:00 · 400 阅读 · 0 评论 -
【翁恺】15-函数缺省参数值
function overloading 函数重载 same function with different arguments lists void print(char *str,int width);//#1void print(double d,int width);//#2void print(long l,int width);//#3void print(int i,int width);//#4void print(char *str);//#5print("panc原创 2022-02-07 11:29:02 · 341 阅读 · 0 评论 -
【翁恺】14-子类父类关系
declare an employee classclass Employee {public: Employee(const std::string &name, const std::string &ssn); const std::sting &get_name() const; void print(std::ostrwam &out) const; void pint(std::ostrwam &out,const s原创 2022-02-07 11:13:11 · 409 阅读 · 0 评论 -
【翁恺】13-继承
软件重用:组合:对象拼出新的对象。 C++:组合出来的也是类继承:类得到新的类reusing the interfaceinheritance is to take the existing class,clone it,and then make additions and modifications to the clone.inheritancelanguage implementation technique also an important component of th原创 2022-02-06 16:15:54 · 329 阅读 · 0 评论 -
【翁恺】12-对象组合
软件重用的一种方式:对象的组合。需要列表初始化。原创 2022-02-06 13:42:48 · 226 阅读 · 0 评论 -
【翁恺】11-初始化列表
cladd Point{private: const float x,y; Point(float xa =0.0,float ya=0.0):y(ya),x(xa){}}; can initialize any type of data pseudo-constructor calls for built-ins no need to perform assignment within body of constructor order of initial原创 2022-01-18 15:02:53 · 577 阅读 · 0 评论 -
【翁恺】10-访问限制
自己指的是这个类的成员函数。private是对类来说的,不是对对象的。同一个类的对象之间是可以互相访问私有的成员变量的。private的限制仅仅在编译时刻,意味着,运行时刻可以访问私有的东西。C++的OOP 仅在源代码时候存在,编译后的 .o 和其他语言是一样的,不会在运行时刻检查访问限制。别的类,别的函数,别的类内的某个函数friend的授权也是在编译时刻检查的。运算符重载时候用的较多。...原创 2022-01-18 14:51:49 · 311 阅读 · 0 评论 -
【翁恺】09-new and delete
dynamic memory allocation 在堆里面申请空间new new int; // 分配一块 int 空间 new Stash; // 分配Stash对象的空间 然后调用构造函数初始化 new int[10] // 分配10个int 空间 delete // 给它一个地址 delete p; // delete[] p; 相当于c中malloce和free任何运算都有结果,new操作返回的是地址动态制造对象。 new 带着[ ], ......原创 2022-01-18 14:12:27 · 262 阅读 · 0 评论 -
【翁恺】08-对象初始化
编译器会在 { 开始的地方分配在大括号内所有变量的空间。构造器的调用要到对象定义的地方。storage allocationthe compiler allocates all the storage for a scope at the opening brace of scope the constructor call does't happen until the sequence point where the object is defined对象没有构造,如果进行析构会编译不通过。原创 2022-01-18 11:04:55 · 314 阅读 · 0 评论 -
【翁恺】07-构造和析构
point::init()c++是一个注重效率的语言,不会自己做初始化,区别于java,自己搬进来自己打扫;vs debug模式中未初始化的内存是十六进制的“0xcd,两个构成中文“烫”;依赖于的程序员的自觉性,素养;定义了一个对象,对象里面的数据没有初始化。guaranteed initialization with the constructorif a class has a constructor,the compiler(编译器) automatically cal原创 2022-01-18 10:16:47 · 594 阅读 · 0 评论 -
【翁恺】06-成员变量
local variable local variables are defined inside a method,have a scope limited to the method to which they belong a local variable of the same name as a field will prevent the filed being accessed form within a method 和c类似,重名时本地变量会屏蔽成员变量fiel原创 2022-01-18 01:13:37 · 237 阅读 · 0 评论 -
【翁恺】05-时钟例子
面向对象的思想去思考abstract 抽象,在某个层次看,屏蔽细节abstraction is the ability to ignore details of parts to focus attention on a higher level of a problem modularization(模块化) is the process of dividing a whole into well-defined(容易辨认的) parts.which can be built and exami原创 2022-01-18 00:06:52 · 262 阅读 · 0 评论 -
【翁恺】04-头文件
definition of a class in c++,separated .h and .cpp files are used to define one class class declaration and prototypes in that class are in the header file(.h) all the bodies of those functions are in the source file(.cpp) (.h)--declaration-原创 2022-01-17 18:02:51 · 247 阅读 · 0 评论 -
【翁恺】03-自动售货机例子
ticket machineticket machines prints a ticket when a customer inserts the correct money for their fare our ticket machines work by customers inserting money into them ,and then requesting a ticket to be printed.a machine keeps a running total of the amo原创 2022-01-17 16:34:29 · 367 阅读 · 0 评论 -
【翁恺】02-面向对象编程Object-Oriented
2-1 什么是对象?导向:Oriented从对象出发考虑、解决、分析问题。对象:object = entity(东西,实体) 。什么都是东西。可能是可见也可能是不可见。在程序设计语言中,对象就是变量。int i变量用来保存数据,变量的类型决定变量能保存什么样的数据。对象 = 属性 + 服务 data:the properties(性质) or status(身份),被保护起来 operations(操作):函数,提供服务 不要直接去操作内部的数据。原创 2022-01-17 15:51:17 · 441 阅读 · 0 评论 -
【翁恺】01-第一个C++程序
// IDE 需要建立一个project //文件不一定有后缀名(Windows习惯)//Linux中多以小写命名,特别强调的地方用大写,没有文件名后缀#include<iostream>//使用命名空间,防止命名冲突using namespace std;int main() { //“<<”把后面的东西向cout中输出;左移(C语言) //endl----end of line cout << "hellom,world!i.原创 2022-01-17 14:37:26 · 242 阅读 · 0 评论