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 ...
随机推荐
- java编程基础篇-------> 从键盘输入一位整数,代表月份,编程判断指定月份属于一年中的哪个季度。如果是 12 月、1 月、2 月,就属于冬季。
从键盘输入一位整数,代表月份,编程判断指定月份属于一年中的哪个季度.如果是 12月.1 月.2 月,就属于冬季:如果是 3 月.4 月.5 月,就属于春季:如果是 6 月.7 月.8 月,就属于夏季: ...
- Android环境的搭建遇到的问题和解决方案
安卓环境的搭建,我花了将近一天的时间,在最后终于找到了一个比较好的方案. 第一个问题是安卓的官网(http://developer.android.com)很难登录.SDK和ADT都是需要在官网上下载 ...
- Android开发笔记(10)——使用Fragment传递
转载请注明:http://www.cnblogs.com/igoslly/p/6911165.html 由于最近废寝忘食地在开发App,没来得及及时做总结,没有用很高级的部件,勉强也使用一些功能完成了 ...
- react基础篇三
事件处理 React事件绑定属性的命名采用驼峰式写法,而不是小写. 如果采用 JSX 的语法你需要传入一个函数作为事件处理函数,而不是一个字符串(DOM元素的写法) 例如,传统的 HTML: < ...
- MVC POST请求后执行javascript代码
[HttpPost] public ActionResult PostTest() { //你的业务代码 //...... //要执行的js string js = "window.loca ...
- (转)Arcgis for Js之GeometryService实现测量距离和面积
http://blog.csdn.net/gisshixisheng/article/details/40540601 距离和面积的测量时GIS常见的功能,在本节,讲述的是通过GeometryServ ...
- nginx_安装测试
首先安装环境: [root@local nginx-1.9.14]# yum install gcc-c++ pcre pcre-devel zlib zlib-devel openssl op ...
- 测试模式 windows2008 内部版本7601
win server 2008 r2 enterprise 64位系统. 最近手贱,对服务器进行了一下更新,结果傻叉了,这是什么鬼,明明显示已经激活的,但就是有这么一串碍眼的字幕. 电脑右下角居然出现 ...
- luogu P2852 [USACO06DEC]牛奶模式Milk Patterns 后缀数组 + Height数组 + 二分答案 + 扫描
后缀数组有一个十分有趣的性质: $height[rk[i]] >= height[rk[i-1]] - 1$ Code: #include <bits/stdc++.h> #d ...
- php第七节课
多态,重载,克隆 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://ww ...