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的更多相关文章

  1. C++_class_powerpoint_1.2

    用英文编写(复制黏贴)果然比较吃力啊,果然还是要写中文. Expressions and Statements Operator summary Scope resolution   class::m ...

随机推荐

  1. CentOS 安装dotNetCore

    如果要在CentOS上运行.net Core程序,必须安装.net Core Sdk 具体安装 方法,可以参考微软官方站点说明,非常详细: 1)百度搜索 .Net Core 2)先择CentOS版本: ...

  2. ArrayList<HashMap<String,Object>>集锦

    1.   Android中如何从一个Activity中ArrayList<HashMap<String,Object>>传递到另一个activity?      eg:     ...

  3. mysql数据库之存储过程入门

    引用:百度百科 存储过程 存储过程(Stored Procedure)是在大型数据库系统中,一组为了完成特定功能的SQL 语句集,存储在数据库中,经过第一次编译后再次调用不需要再次编译,用户通过指定存 ...

  4. 对学Oracle数据库初学者的开场篇

    前言:因为项目原因,近期开始学习Oracle数据库.Oracle是目前最流行的数据库之一,功能强大,性能卓越,相对的学习的难度还是不小.我打算将自己的学习过程记录下来,做个积累,方便自己和其他的学习者 ...

  5. Webpack 打包学习

    前段时间项目主管让测试组长研究webpack打包方式,闲暇时自己想学习一下,留着备用,本周日学习一下. https://www.jianshu.com/p/42e11515c10f

  6. 团体程序设计天梯赛-练习集-L1-040. 最佳情侣身高差

    L1-040. 最佳情侣身高差 专家通过多组情侣研究数据发现,最佳的情侣身高差遵循着一个公式:(女方的身高)×1.09=(男方的身高).如果符合,你俩的身高差不管是牵手.拥抱.接吻,都是最和谐的差度. ...

  7. Linux下文件查找命令find笔记

    在Linux命令下如果需要快速自己系统所需要处理的文件,可以通过find命令快速进行检索. 如果想在某个路径下查找相应的文件可以执行如下命令: find path -name filename # p ...

  8. 原生js通过最外层id获取下面指定的子元素

    需求:在vue中使用v-for循环出来的元素,设置动态id,之后获取下面的所有textarea标签 template: <table cellpadding = 2 v-for="(i ...

  9. 【剑指Offer】59、按之字形顺序打印二叉树

      题目描述:   请实现一个函数按照之字形打印二叉树,即第一行按照从左到右的顺序打印,第二层按照从右至左的顺序打印,第三行按照从左到右的顺序打印,其他行以此类推.   解题思路:   这道题仍然是二 ...

  10. [luogu4026 SHOI2008]循环的债务 (DP)

    传送门 吐槽洛谷难度标签qwq Solution 显然是一道神奇的DP,由于总钱数不变,我们只需要枚举前两个人的钱数就可知第三个人的钱数 DP的时候先枚举只用前k个币种,然后枚举前两个人的钱数,然后枚 ...