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


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

  • 相关阅读:
    mysql性能调优与架构设计(一)商业需求与系统架构对性能的影响
    Android发送数据到web服务器4种方式
    Java 操作mongodb
    父子容器互相操作的方法
    Sql Server中查询当天,最近三天,本周,本月,最近一个月,本季度的数据的sql语句
    js实现多少秒后自动跳转
    插入数据返回插入的主键Id
    日期比较
    Cookie的增删改查
    js标准化价钱
  • 原文地址:https://www.cnblogs.com/lizhenlin/p/6519303.html
Copyright © 2011-2022 走看看