C++语言基础 例程 C++的数据类型和运算符

贺老师的教学链接

第一个C++程序

#include <iostream>       //包含头文件iostream
using namespace std;      //使用命名空间std
int main( )
{
      cout<<"Hello World."<<endl;    //输出结果
      return 0;
}

结构体类型在定义变量时,其前struct可以省略

#include <iostream>
using namespace std;
struct student
{
    int no;
    float math;
};
int main( )
{
    int n;
    cin>>n;
    student wang;  //C语言中,必须struct student wang
    wang.no=n;
    cin>>wang.math;
    cout<<wang.no<<"  "<<wang.math<<endl;
    return 0;
}

新增作用域运算符 ::

#include <iostream>
using namespace std;
float a=2.4;           // 全局变量a
int main()
{
     int a=8;              // 局部变量a
     cout<<a<<endl;  // a为局部变量
     cout<<::a<<endl;  // ::a表示全局变量a
} 

std::,表明使用命名空间std中定义的标识符

#include <iostream>
//这儿不写使用的命名空间
float a=2.4;
int main()
{
     int a=8;
     std::cout<<a<<std::endl;
     std::cout<<::a<<std::endl;
} 

引用的简单使用

#include <iostream>
#include <iomanip>
using namespace std;
int main( )
{
      int a=10;
      int &b=a;  //声明引用类型变量要同时初始化
      a=a*a;
      cout<<a<<"  "<<b<<endl;
      b=b/5;
      cout<<b<<"  "<<a<<endl;
      return 0;
}

增加引用类型,主要用于扩充函数传递数据功能

#include <iostream>
using namespace std;
void swap(int &a,int &b);
int main( )
{
    int i,j;
    i=3,j=5;
    swap(i,j);
    cout<<"i="<<i<<"  "<<"j="<<j<<endl;
    return 0;
}

void swap(int &a,int &b)
{
    int temp;
    temp=a;
    a=b;
    b=temp;
}

常变量(constant variable)

#include<iostream>
using namespace std;
const int price = 30;
int main ( )
{
    int num, total;
    num=10;
    total=num * price;
    cout<<"total="<<total<<endl;
    return 0;
}

符号常量(宏定义) vs.常变量

#include <iostream>
using namespace std;
#define PRICE 30  //不是语句,末尾不加分号
int main ( )
{
    int num, total;
    num=10;
    total=num * PRICE;
    cout<<"total="<<total<<endl;
    return 0;
}

C++的程序员更爱常变量

#include<iostream>
using namespace std;
const int price = 30;
int main ( )
{
    int num, total;
    num=10;
    total=num * price;
    cout<<"total="<<total<<endl;
    return 0;
}
时间: 2024-09-20 01:07:17

C++语言基础 例程 C++的数据类型和运算符的相关文章

《Python语言程序设计》——2.8 数值数据类型和运算符

2.8 数值数据类型和运算符 关键点:Python中有两种数值类型(整数和浮点数)与+.-.././/.%和*一起工作. 储存在计算机中的信息通常被称为数据.这里有两种数值数据类型:整数和实数.整数类型Integer(简写作int)用于表示整数.实数型用于表示有小数部分的数字.在计算机中,这两种数据类型的存储方式不同.实数型表示为浮点数.我们怎样告知Python一个数字是整数还是浮点数呢?一个拥有小数点的数字即使小数部分为零也是浮点数.例如:1.0是浮点数,而1是整数.这两个数字在计算机里的存储

《Java 7程序设计入门经典》一第2章 数据类型和运算符

第2章 数据类型和运算符 本章要点 了解Java的基本数据类型: 使用字面量: 初始化变量: 了解方法中变量作用域的规则: 使用算术运算符: 使用关系运算符和逻辑运算符: 理解赋值运算符: 理解速记赋值: 理解赋值语句中的类型转换: 使用强制类型转换: 理解表达式中的类型转换. 对任意一种编程语言而言,其基础都是数据类型和运算符.Java也不例外.这些元素定义语言的限制,决定它能够用于哪些类型的任务.幸运的是,Java支持一系列丰富的数据类型和运算符,使其适合于几乎所有类型的编程. 数据类型和运

C++语言基础 例程 虚析构函数

贺老师的教学链接  本课讲解 问题的由来 #include <iostream> using namespace std; class Point { public: Point( ) { } ~Point() { cout<<"executing Point destructor"<<endl; } }; class Circle:public Point { public: Circle( ) { } ~Circle( ) { cout<&

C++语言基础 例程 应用系统开发:银行储蓄系统

贺老师的教学链接  本课讲解 说明:(1)下面的代码,只演示了利用链表作为存储结构的可选处理方法,本讲提到的其他方面的拓展,请感兴趣做下去的同学自行使用相关技术组合起来,形成一个完整的系统.(2)运行程序,登录用户名和密码,请阅读程序,从程序中找出.建议建立多文件项目,将代码拷贝到IDE中看.(3)本程序由我的2011级学生刘镇参加企业组织的实训中完成,原文在:点击打开链接 Record.h #ifndef HEADER_RECORD //条件编译 #define HEADER_RECORD #

C++语言基础 例程 C++中的输入和输出

贺老师的教学链接 程序将自行识别符号 #include <iostream> using namespace std; int main() { int a,b; char op; cin>>a>>op>>b cout<<"result: "<<'\n'; cout<<"a: "<<a<<'\n'; cout<<"b: "<

C++语言基础 例程 重载流插入运算符和流提取运算符

贺老师的教学链接  本课讲解 重载流插入运算符"<<" #include <iostream> using namespace std; class Complex { public: Complex( ) { real=0; imag=0; } Complex(double r,double i) { real=r; imag=i; } Complex operator + (Complex &c2); //运算符"+"重载为成员函

C++语言基础 例程 文本文件的读写

贺老师的教学链接  本课讲解 示例:将数据写入ASCII文件 #include <iostream> #include <fstream> #include <cstdlib> using namespace std; int main( ) { int a[10]; ofstream outfile("f1.dat",ios::out);//定义文件流对象,打开磁盘文件"f1.dat" if(!outfile) //如果打开失败

C++语言基础 例程 标准输入流

贺老师的教学链接  本课讲解 例: 输入个数不确定的成绩 #include <iostream> using namespace std; int main( ) { float grade; cout<<"enter grade:"; while(cin>>grade)//能从cin流读取数据 { if(grade>=85) cout<<grade<<" GOOD!"<<endl; if

C++语言基础 例程 函数中的引用

贺老师的教学链接  本课讲解 引用作为形参 #include<iostream> using namespace std; class Sample { int x; public: Sample(int a): x(a) {cout<<"A";} Sample(Sample &a): x(a.x) {cout<<"B";} int getX(){return x;} }; void disp(Sample &s)