C、C++: 引用、指针、实例、内存模型、namespace
// HelloWorld.cpp : Defines the entry point for the console application.
// #include "stdafx.h"
#include "string.h"
#include "iostream.h" /**
* 在C、C++语言中
* 声名语句中: 声明指针变量用*,声明变量或者常量都不加*。
* 譬如函数声明中的参数,返回值类型等。例如类中的字段的声明。
* 在赋值语句中: * 表示取值。 &表示取变量的地址,也就是取指针。
*
* class,struct,unit: 也遵循上述原则。另外,
* 在使用指针进行变量的访问、方法的调用时,要使用 ->
* 在使用实例进行变量的方式、方法的调用时,要使用.
* 例如personTest()方法中:
* p是指针,*p就是p所指向的实例。
*
* personTest()测试中的内存模型:
* 指针p Value *p ,也可以称为实例
* ___________ __________ ___________
* | | | name |-------> |Fang JiNuo | *name
* | | —————> | age : 23 | |___________|
* |___________| | address | ______________________
* |__________|-------> | Bei Jing, Haid Dian | *address
* |_____________________|
*/
class Person
{
private:
char* name;
int age;
char* address;
public :
char* toString()
{
char* ret=name;
return ret;
}
void setAge(int age){
this->age=age;
}
void setAddress(char* address){
this->address=address;
}
void setName(char* name){
this->name=name;
}
}; void personTest(){
Person * p=new Person();
p->setAddress("Bei Jing, Hai Dian"); // 采用指针的方式赋值
(*p).setName("Fang JiNuo"); // 采用对象的方式赋值
(*p).setAge();
printf("show info:\n%s\n", (*p).toString());
} void switchTest(){
int a=;
int b=;
int c=;
char oper='+';
switch(oper){
case '+':
c=a+b;
case '-':
c=a-b;
break;
case '*':
c=a*b;
break;
}
printf("c = %d %c %d = %d", a, oper, b, c);
} /**
* C 语言的输入输出
*/
void input_Output_test_c(){
printf("Hello World!\n");
printf("zhang san, wo cao \n");
int a,b;
printf("input tow int number, pattern is d, d:\n");
scanf("%d, %d", &a, &b);
printf("a is %d\n",a);
printf("b is %d\n",b);
printf("a+b=%d\n",a+b);
}
/**
* C++ 的输入输出
* 使用前需要include iostream.h
*/
void input_Output_test_cpp()
{
// << out1 << out2 << out3 << out4 << endl;
// endl 代表endline,也就是换行
char * content=new char;
cout << "plese input:" << endl;
cin>> content;
cout << content << endl;
}
/**
* namespace
* C 语言不支持。
* C++ 支持。
*
*/
namespace MyNS
{
void namespaceTest(){
cout << "MyNS namespace invoked" << endl;
}
}
void namespaceTest(){
cout << "current namespace invoked" << endl;
}
int main(int argc, char* args[])
{
// input_Output_test_c();
// personTest();
// switchTest();
// input_Output_test_cpp();
namespaceTest();
MyNS::namespaceTest();
return ;
}
C、C++: 引用、指针、实例、内存模型、namespace的更多相关文章
- 栈 堆 stack heap 堆内存 栈内存 内存分配中的堆和栈 掌握堆内存的权柄就是返回的指针 栈是面向线程的而堆是面向进程的。 new/delete and malloc/ free 指针与内存模型
小结: 1.栈内存 为什么快? Due to this nature, the process of storing and retrieving data from the stack is ver ...
- C++二级指针第二种内存模型(二维数组)
C++二级指针第二种内存模型(二维数组) 二维数组 二维数组本质上是以数组作为数组元素的数组,即“数组的数组”. 定义 类型说明符 数组名[常量表达式][常量表达式] 例如: float a[3][4 ...
- C语言 二级指针内存模型混合实战
//二级指针内存模型混合实战 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> #i ...
- C语言 二级指针内存模型③
//二级指针内存模型③ #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> #incl ...
- C语言 二级指针内存模型②
//二级指针第二种内存模型 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> #incl ...
- C语言 二级指针内存模型①
//二级指针第一种内存模型 #include<stdio.h> #include<stdlib.h> //说明:①:类似于int a[5]={0},数组名a是一维数组a中首元素 ...
- C++对象内存模型2 (虚函数,虚指针,虚函数表)
从例子入手,考察如下带有虚函数的类的对象内存模型: class A { public: virtual void vfunc1(); virtual void vfunc2(); void func1 ...
- c语言二级指针内存模型
第一种: 指针数组作为输入参数 char *myArray[] = {"aaaaaa", "ccccc", "bbbbbb", " ...
- C++二级指针第一种内存模型(指针数组)
二级指针第一种内存模型(指针数组) 指针的输入特性:在主调函数里面分配内存,在被调用函数里面使用指针的输出特性:在被调用函数里面分配内存,主要是把运算结果甩出来 指针数组 在C语言和C++语言中,数组 ...
随机推荐
- git diff ^M的消除
这是由于换行符在不同的操作系统上定义的区别造成的. Windows用CR LF来定义换行,Linux用LF. CR全称是Carriage Return ,或者表示为\r, 意思是回车. LF全称是Li ...
- js实现css3的过渡,需要注意的一点(浏览器优化)
大部分浏览器对元素几何改变时候的重排做了优化.据说是这样子,一定时间内本应多次重排的改变,浏览器会hold住,仅一次重排.其中如果使用分离的一步处理过程,例如计时器,依然多次重排.例如,当我们应用tr ...
- Cesium应用篇:3控件(1)Clock
创建 跟Clock相关的主要有Animation控件和Timeline控件,通常两者会放在一起使用. 在Cesium中,Viewer默认开启这两个控件,如果你想要不显示控件,可以在Viewer初始化中 ...
- 求一个数组的最大子数组(C/C++实现)
最大子数组:要求相连,加起来的和最大的子数组就是一个数组的最大子数组.编译环境:VS2012,顺便说句其实我是C#程序员,我只是喜欢学C++. 其实这是个半成品,还有些BUG在里面,不过总体的思路是这 ...
- 数据库基础及T-SQL语句
字符类型: int 整型float 小数double 小数varchar(20) 字符串bit 布尔型数据datetime 日期时间类型text 长文本 (以下两种不经常使用) money 存货币im ...
- C# 将内容写入txt文档
<1> FileStream fs = new FileStream(@"D:\text.txt", FileMode.Append); StreamWriter s ...
- SQL Server 存储(1/8):理解数据页结构
我们都很清楚SQL Server用8KB 的页来存储数据,并且在SQL Server里磁盘 I/O 操作在页级执行.也就是说,SQL Server 读取或写入所有数据页.页有不同的类型,像数据页,GA ...
- 微服务(Microservices)——Martin Flower【翻译】
原文是 Martin Flower 于 2014 年 3 月 25 日写的<Microservices>. 本文内容 微服务 微服务风格的特性 组件化(Componentization ) ...
- web.config配置
<appSettings> <add key="DepartmentRootID" value="A90AEAEC-E3D4-43DE-BB67-854 ...
- php中的登陆login
Login <?php require "../include/DBClass.php"; $username=$_POST['UserName']; $password=$ ...