zoukankan      html  css  js  c++  java
  • C++入门(2)

    第一个程序,输入输出:

    #include <iostream>
    using namespace std;
    int main()
    {
        cout << "Hello, world!" << endl;//endl 换行
      int a;
    cin>>a;
    return 0; }

    注释:

    #include <iostream>
    using namespace std;
    int main()
    {
        cout << "Hello, world!" << endl;
        return 0;
    }
    

      

     数据类型

    类型关键字
    布尔型 bool
    字符型 char
    整型 int
    浮点型 float
    双浮点型 double
    无类型 void
    宽字符型 wchar_t

                  要使用string类型,需要引入#include <string>

    Extern 变量的使用

    文件A.cpp
    #include <iostream>
     
    extern int count;
     
    void write_extern(void) //(void)表示无输入参数  也可以不填写
    {
       std::cout << "Count is " << count << std::endl;
    }
    
    文件main.cpp
    int count ;
    extern void write_extern();
    void main()
    {
         count =1;
          write_extern();
    }
    

      

    定义函数

    return_type function_name( parameter list )
    {
       body of the function
    }

    数据结构

    struct Books
    {
       char  title[50];
       char  author[50];
       char  subject[100];
       int   book_id;
    }book;

     类定义

    class Box
    {
       public:
          double length;   // Length of a box
          double breadth;  // Breadth of a box
          double height;   // Height of a box
    };

    类使用

    	#pragma region 类的使用
    	   Student *pStu = new Student; //在堆中 需要手动回收
    	   pStu -> name = "小明";
    	   pStu -> age = 15;
    	   pStu -> score = 92.5f;
    	   pStu -> say();
    	   delete pStu;  //删除对象
    
    
    	   Student s;	//在栈中 自动回收
    	   s.name="1111";
    	   s.age=23;
    	   s.score=22;
    	   s.say();
    	 Student stu;
    	 stu.name="2";
    	   stu.age=2;
    	   stu.score=2;
    	   stu.say();
    	   cout<<&s<<";"<<&stu<<endl;
    	#pragma endregion
    

    引用类 类名+ 定义名(参数),无参数时直接 类名+定义名

    ->就是用指针调用函数或者获取数据。
    a->b等价于(*a).b


    函数不需要加分号
    类或者结构体后面才需要分号

  • 相关阅读:
    CentOS7安装minio
    xshell连接虚拟机Connection failed
    Mysql时间加减函数
    mysql存储过程模板
    Activiti实现会签功能
    2018考研复试流程
    C编程经验总结5(剧终)
    《数据结构总结》
    《关于安卓和IOS开发》
    番外特别篇之 为什么我不建议你直接使用UIImage传值?--从一个诡异的相册九图连读崩溃bug谈起
  • 原文地址:https://www.cnblogs.com/lizhenlin/p/6519303.html
Copyright © 2011-2022 走看看