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. python--4、装饰器

    装饰器(Decorator) 使用场景:为被装饰器装饰的函数增加功能,但又不希望修改函数的定义,即在代码运行期间动态增加功能. 装饰器更多的用于后期功能升级而不是编写新的代码.装饰器不光能装饰函数,也 ...

  2. JDBC: 批量处理提高SQL处理速度

    引用:忘了 当需要成批插入或者更新记录时.可以采用Java的批量更新机制,这一机制允许多条语句一次性提交给数据库批量处理.通常情况下比单独提交处理更有效率 JDBC的批量处理语句包括下面两个方法: a ...

  3. System.DateTime.Now 24小时制。

    this.Label6.Text = "当前时间:" + System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") ...

  4. (2)dotnet开源电商系统-brnshop VS nopCommerce(dotnet两套电商来PK--第二篇:代码从哪开始-BrnMall3.0Beta)

    看大牛们的源码,对于水平一般的人,还是略微有点难度的.我从我自身读码的亲身体验,写下杂散片语,希望能和大家一同进步,也为了日后记忆上的备查. 先看的是brnMall的源码结构,从哪看起呢? 首先推荐看 ...

  5. Redis 之set集合结构及命令详解

    注:集合的元素具有唯一性,无序性 1.sadd  key   value1  value2  添加一个集合 2.smembers   key  获取一个集合的所有值 3.srem  key  valu ...

  6. SGU495Kids and Prizes 数学期望

    题意: 有n个奖品,m个人排队来选礼物,对于每个人,他打开的盒子,可能有礼物,也有可能已经被之前的人取走了,然后把盒子放回原处.为最后m个人取走礼物的期望. 题解: 本道题与之前的一些期望 DP 题目 ...

  7. eas之常用源码整理

    //查看是否有相关权限 boolean hasAllotPermission=         PermissionFactory.getRemoteInstance().hasFunctionPer ...

  8. CodeIgniter-CI之MySQL

    首先我们需要进行一下配置,这里需要修改的文件为application目录下的config目录下的database.php文件,我们修改相应的配置项,比如这里是我的配置情况: 通常我们在操作数据库之前, ...

  9. android keystore的生成和使用

    android要求所有的程序必须有签名,否则就不会安装该程序.在我们开发过程中,adt使用debug keystore,在 preference->android->buid中设置.deb ...

  10. Google Shell Style Guide

    转自:http://google.github.io/styleguide/shell.xml Shell Style Guide Revision 1.26 Paul Armstrong Too m ...