vc++ 基本语法
基本语法
#include <iostream>
using namespace std;
// hello world
void hello()
{
cout<<"hello"<<endl;
}
//主入口
int main()
{
hello();
}
如果方法,在 main 之后需要在,main 之前进行声明。
#include <iostream>
//使用std
using namespace std;
// hello 函数
void hello()
{
cout<<"hello"<<endl;
}
void world();
//主入口
int main()
{
hello();
world();
return 0;
}
//world 函数
void world()
{
cout<<"world"<<endl;
}
使用常量
#include <iostream>
using namespace std;
#define APP_NAME "hello"
int main()
{
cout<<APP_NAME<<endl;
return 0;
}
指针
#include <iostream>
using namespace std;
void main()
{
int a;
int *p;
p = &a;
*p = 100;
cout<<a<<endl;
}
指针不能直接给值,必须是其它变量取地址。
New del 内存管理
#include <iostream>
#include <string>
using namespace std;
//结构体
struct User{
int age;
char sex;
string name;
};
//main
void main()
{
//申请内存
User *u;
u = new User;
u->age = 20;
u->name = "张三";
u->sex = 'n';
//输出
cout<<u->name<<endl;
//回收内存
delete u;
}
类
#include <iostream>
#include <string>
using namespace std;
//结构体
class User{
public:
int age;
char sex;
string name;
};
class User2 : public User {
public:
};
//main
void main()
{
//申请内存
User2 *u;
u = new User2;
u->age = 20;
u->name = "张三";
u->sex = 'n';
//输出
cout<<u->name<<endl;
//回收内存
delete u;
}
- 上一篇:vc++ 获得系统时间格式化输出
- 下一篇:c 语言中 .h 头文件的作用