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 ...
随机推荐
- Md2All版本更新记录
Md2All版本更新记录 版本号:V2.8.2更新日期:2018-06-281:结合云图床,解决了Latex公式复制到公众号时有可能报“图片粘贴失败的问题”;2:结合云图床,解决了Latex公式复制到 ...
- 使用CMD建立指定格式的文件
一.建立空文件的几种方法1.cd.>a.txtcd.表示改变当前目录为当前目录,即等于没改变:而且此命令不会有输出.>表示把命令输出写入到文件.后面跟着a.txt,就表示写入到a.txt. ...
- 【sqli-labs】 less28 GET- Error based -All you Union&Select Belong to us -String -Single quote with parenthesis(GET型基于错误的去除了Union和Select的单引号带括号字符串型注入)
这个不是基于错误的吧,看源码可以知道错误并没有输出 那就使用;%00和order by试一下 http://192.168.136.128/sqli-labs-master/Less-28/?id=1 ...
- (转)基于MVC4+EasyUI的Web开发框架经验总结(12)--利用Jquery处理数据交互的几种方式
http://www.cnblogs.com/wuhuacong/p/4085682.html 在基于MVC4+EasyUI的Web开发框架里面,大量采用了Jquery的方法,对数据进行请求或者提交, ...
- 原生js通过最外层id获取下面指定的子元素
需求:在vue中使用v-for循环出来的元素,设置动态id,之后获取下面的所有textarea标签 template: <table cellpadding = 2 v-for="(i ...
- 布尔类型、操作符别名、C++函数、动态内存分配(new\delete)、引用(day02)
六 C++的布尔类型 bool类型是C++中基本类型,专门表示逻辑值:true/false bool在内存上占一个字节:1表示true,0表示false bool类型可以接收任意类型和表达式的结果,其 ...
- [jzoj 5775]【NOIP2008模拟】农夫约的假期 (前缀和+递推)
传送门 Description 在某国有一个叫农夫约的人,他养了很多羊,其中有两头名叫mm和hh,他们的歌声十分好听,被当地人称为"魔音"······ 农夫约也有自己的假期呀!他要 ...
- VmWare安装centos7无法上网
1.关闭防火墙 systemctl stop firewalld.service #关闭 systemctl restart firewalld.service #重启 2.虚拟机->设置-&g ...
- js中获取数组中的最大值最小值
var a=[1,2,3,5]; alert(Math.max.apply(null, a));//最大值 alert(Math.min.apply(null, a));//最小值 多维数组可以这么修 ...
- [LeetCode] 20. 有效的括号 (栈)
思路: 首先用字典将三对括号存储,遍历字符串中每个字符,遇到左括号就入栈:遇到右括号就开始判断:是否与栈弹出的顶字符相同. 如果到最后栈被清空,说明全部匹配上了,为真. class Solution( ...