C++_class_powerpoint_1.1
Types and Declarations
Boolean Type
bool type – boolean , logic type
bool literal – true, false
int and bool
- Nonzero->true
- Zero ->false
- true ->1
- false->0
1 <= sizeof( bool) <= sizeof(long)
void f(int a,int b)
{
bool b1{a==b};
bool b2 = a==b;
…
}
bool isGreater(int a, int b){return a > b;}
void f()
{
bool a=true,
b=true;
bool x=a+b;
bool y=a||b;
}
Knowing Ranges of Types
// Implementation-dependent
#include <limits>
int main()
{
cout << "largest integer = "
<< numeric_limits<int>::max();
cout << “smallest integer = "
<< numeric_limits<int>::min();
return ;
}
Binary Literal
int a=0b1100,b=0B0101;
::globalId can be used in a local scope, even its name is identical to local one.
int x;
void f()
{
int x=;
::x=;
x=;
…
}//::x means the x is the global one
int x=1;
void f(){
int x=2;
::x=3;
printf("%d\n",x);
}
int main()
{
f();
printf("%d",x);
return 0;
}//output 2 3
auto
If the type of a variable can be deducted from its initialization, keyword auto can be used instead of type name in the definition.
auto i{};
vector<string> v; …
for (const auto& x : v) cout << x << '\n';
for (auto i : {, , , , , , })
cout << i << '\n';
Initialization
four syntactic styles:
- T a1 {v};
- T a2 = {v};
- T a3 = v;
- T a4(v);
Pointers, Arrays and Structures
Variable-size arrays
#include <vector>
using namespace std;
void f(int i)
{
vector<int> v1(i);
vector<int> v2{,,};
vector<int> v3(i,);
…
}
Constants
Keyword const as a modifier
- A constant identifier must be initialized and cannot be assigned
- Prefer to #define macro usage
- Using symbolic constants is better than using literals in the large programs
Pointers and Constants
Prefixing a pointer with const makes an object the pointer points to, not pointer itself, a constant.
const int someInt=;
const int *p = &someInt;
//Or
int const *p = &someInt;
// *p is a constant, p is a variable
To declare a pointer itself to be a constant, use the declarator *const .
int someInt=;
int *const p = &someInt;
// p is a constant
To declare both the pointer and the object the pointer points to be constants, use two const modifiers.
const int someInt=;
const int *const p = &someInt;
//Or
int const *const p = &someInt;
// *p is a constant, p is a constant too.
void f4( )
{ int a=;
const int c=;
const int* p1=&c;
const int* p2=&a; //non const ptr const ptr
int * p3=&c; // Error!
*p3=;
}
// a non-constant pointer can assign to a constant
// pointer, but a constant pointer cannot assign to
// a non-constant pointer.
References
A reference is alias/nickname for an object or a function.
The main usages:
1. specifying arguments and return values
for functions
2. for overloaded operators.
Notation : Typename&
References must be initialized.
void f()
{
int i=;
int& r{i};
int x=r;
r=;
}//Both i and r are occupied the same memory
constant references
Constant Reference can refer to a non lvalue or other type object.
For const T&,
[1] implicit conversion to T if necessary
[2] result is placed in a temporary object
[3] reference to the temporary object
The temporary object persists until the end of its reference's lifetime.
References as Parameters of Functions
The function can change the value of an object passed to it.
This is called call-by-reference passing mechanism. It is different from call-by-value.
void increment(int& a)
{ a++; } //x++
void f()
{
int x=;
increment(x); // x==2
}//Parameter a is initialized with the argument x
The return value of the function can refer to the expression of return statement.
struct Pair{
string name;
double val;
}; vector<Pair> pairs;
double& value(const string& s)
{
for (auto& x: pairs)
if (s==x.name) return x.val;//original val+1;
pairs.push_back({s,});
return pairs.back().val;//0->1
} int main()
{
for (string buf; cin>>buf;)
// enter ^d in Linux at end of the input
value(buf)++;
for (const auto& x: pairs)
cout << x.name << ": "
<< x.val << '\n';
return ;
}
void*
Pointer to any data type(not function type)
Be able to assign, compare and cast to another pointer type
T* -->void* is type coercion
void*-->T* must use type cast
int i=;
void* pv=&i; // type coercion
int* pi1 = static_cast<int*>(pv);
int* pi2 = (int*)pv;
Structure type name
Typename of a structure is structure tag , keyword struct is unnecessary.
For example:
struct address {…};
type name is address,
but struct address in C.
Structure forward declaration
The typename of a structure is available for use as pointer to structure immediately after it has been encountered and not just after the complete declaration has been seen.
struct Link{ Link* next; … };
To allow two or more types refer to each other, use structure forward declaration.
struct List; struct Link{ List* a; … }; struct List{ Link* a; … };
C++_class_powerpoint_1.1的更多相关文章
- C++_class_powerpoint_1.2
用英文编写(复制黏贴)果然比较吃力啊,果然还是要写中文. Expressions and Statements Operator summary Scope resolution class::m ...
随机推荐
- 浏览器缓存机制(HTTP缓存机制)
其机制是根据HTTP报文的缓存标识进行的. 过程:浏览器发起HTTP请求 – 服务器响应该请求.那么浏览器第一次向服务器发起该请求后拿到请求结果,会根据响应报文中HTTP头的缓存标识,决定是否缓存结果 ...
- 在命令提示符窗口下(cmd)使用指令操作并编译java代码,运行java编译代码
使用cmd操作java代码,编译.java文件,运行.class文件. 操作步骤: 1:创建一个文件夹: 例如:在e盘根目录(\)下面创建一个名为Hello的文件夹: 使用md指令:如图 在e盘中会生 ...
- 【Oracle】重命名数据文件
1)查看当前数据文件位置 SQL> select file_id,file_name,tablespace_name from dba_data_files; FILE_ID FILE_NAME ...
- 【转载】使用JSONObject生成和解析json
1. json数据类型 类型 描述 Number 数字型 String 字符串型 Boolean 布尔型 Array 数组 Object 对象 null 空值 (1)json中不区分整数.小数等类型, ...
- 【udacity】机器学习-支持向量机
Evernote Export 支持向量机(Support Vector Machine) 不适定问题不止一个决策边界 要找一个决策边界,不仅能将训练集很好的划分,而且提升模型的泛化能力 支持向量机直 ...
- Django—链接MySQL
Djalgo基础配置方法 静态文件配置方法 1 所有的静态文件都放在 static 文件夹下,例如Bootstrap值类的第三方库,通常 static 文件下会创建 css image js 文件,用 ...
- [系统资源]port range
ip_local_port_range 端口范围 sysctl Linux中有限定端口的使用范围,如果我要为我的程序预留某些端口,那么我需要控制这个端口范围, 本文主要描述如何去修改端口范围. /pr ...
- 类型信息、异常、I/O流(day11)
二十三 运行时的类型信息 typeid运算符 #include <typeinfo> typeid(类型/对象) )返回typeinfo的对象,用于描述类型信息. )在typeinfo类中 ...
- 洛谷P1208 [USACO1.3]混合牛奶 Mixing Milk【贪心+背包】
由于乳制品产业利润很低,所以降低原材料(牛奶)价格就变得十分重要.帮助Marry乳业找到最优的牛奶采购方案. Marry乳业从一些奶农手中采购牛奶,并且每一位奶农为乳制品加工企业提供的价格是不同的.此 ...
- CSS定位相关
CSS display:inline和float:left两者区别 ①display:inline:任何不是块级元素的可见元素都是内联元素.其表现的特性是“行布局”形式!(行布局:其表现形式始终以行进 ...