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. unicode、UTF-8、UTF-16的历史

    1:中国人民通过对 ASCII 编码的中文扩充改造,产生了 GB2312 编码,可以表示6000多个常用汉字. 2:汉字实在是太多了,包括繁体和各种字符,于是产生了 GBK 编码,它包括了 GB231 ...

  2. Android第三方登陆之新浪微博Weibo篇(原生登陆授权)

    前言 Android第三方登录可以说是非常的常见,今天主要先说一下新浪微博第三方登陆授权. SDK版本支持 SDK v3.0已经发布了支持iPhone和Android的版本. 须将你的应用的包名签名信 ...

  3. (转载) ORA-12537:TNS连接已关闭

    今天在远程客户端配置EBS数据库连接的时候发生“ORA-12537:TNS连接已关闭”的错误.进入服务器运行如下命令:$tnsping VIS 这里VIS如果定义服务名,可以写成 $ tnsping ...

  4. Gartner2017年数据科学领域最酷供应商出炉,实至名归

    文 | 帆软数据应用研究院 水手哥 更多大数据资讯和企业案例可关注 :知乎专栏<帆软数据应用研究院> 近日,Gartner公布了2017年度数据科学和机器学习领域的最酷供应商,清一色的美国 ...

  5. Centos6.6 安装Redis

    一.介绍 redis在做数据库缓存,session存储,消息队列上用的比较多 二.安装 $ yum install -y wget gcc make tcl $ wget http://downloa ...

  6. 数据迁移 Migration

  7. 题解 P2605 【[ZJOI2010]基站选址】(From luoguBlog)

    线段树优化dp 数组f[i][j]表示在前i个村庄内,第j个基站建在i处的最小费用 根据交线牛逼法和王鹤松式可得方程 f[i][j]=min(f[k][j−1]+cost(k,i)) cost(k,i ...

  8. Django MVC与MTV概念 Ajax、分页实现

     MVC与MTV概念 MTV与MVC(了解)        MTV模型(django):            M:模型层(models.py)            T:templates      ...

  9. C#第三节课(2)

    运算符 using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.T ...

  10. Hexo系列(五) 撰写文章

    在利用 Hexo 框架搭建一个属于我们自己的博客网站后,下面我们就来谈谈怎样在网站上书写我们的第一篇博客吧 一.创建文章 在站点文件夹中打开 git bash,输入如下命令创建文章,其中 title ...