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++语言中,数组 ...
随机推荐
- VirtualBox安装Debian6的方法和步骤(详细)
下面是用VirtualBox安装Debian6的方法和步骤 l 新建一个文件夹,用于存放虚拟硬盘,如Debian l 打开VirtualBox,点击新建 l 输入虚拟机名称,Debian_6 l 给虚 ...
- Microsoft Visual Studio 插件
AnkhSVN BatchFormat CodeMaind Nuget Package Manager
- UWP开发入门(二十二)——Storyboard和Animation
微博上有同学问我MyerSplash是如何实现那个很炫的图片点亮,然后移动到屏幕中央的效果.惭愧啊,我又不是作者哪里会知道.硬着头皮去GitHub拜读了高手的代码,自愧弗如,比我不知道高到哪里去了…… ...
- Javascript函数的几种写法
最近在看某个插件的源码时,总是看到各种不同风格的js函数的写法.(怪我只是初级水平,看的一头雾水) 于是想找点资料,总结总结,心里不清不楚的总是很别扭! 1.常规写法 // 函数写法 function ...
- C# EPL USB 指令打印
private void btnPrinter_Click(object sender, EventArgs e) { #region ESC 热敏图像点阵像素点读取打印 //Bitmap bitma ...
- # ASP.NET Core依赖注入解读&使用Autofac替代实现
标签: 依赖注入 Autofac ASPNETCore ASP.NET Core依赖注入解读&使用Autofac替代实现 1. 前言 2. ASP.NET Core 中的DI方式 3. Aut ...
- 【Win10开发】如何在页面之间传值
我们知道UWP是通过不同的页面来展示不同的内容的,那么我们该怎么进行页面之间的传值呢? 首先我们在MainPage里面写一个ListView来展示一些英文单词. List<English> ...
- ios 学习笔记之控件属性
1.文本框 设置密码属性:Secure Text Entry 勾选; 设置文本框带清除属性: Clear Button =Is always visible; 默认是不带清除属性:Never app ...
- ASP.NET 5 已死 - 隆重介绍 ASP.NET Core 1.0 和 .NET Core 1.0
还没正式登场就死了?不能怪我标题党,是大神Scott在他博客上这么说的,我只是翻译了一下. 在1月20号最新的ASP.NET Community Standup视频中,微软aspnet开发组的大帅哥 ...
- Ehcache 缓存使用
在开发高并发量,高性能的网站应用系统时,缓存Cache起到了非常重要的作用.本文主要介绍EHCache的使用,以及使用EHCache的实践经验. 笔者使用过多种基于Java的开源Cache组件,其中包 ...