zoukankan      html  css  js  c++  java
  • c++笔记

     编程风格: 

    • 如果标识符是变量,如下 int weightInPound =4
    • 对于命名常量,全大写:const double PI = 3.14
    • 对于类或函数,每一个单词首字母都大写: void MyFunction(  )

     不建议定义全局的变量,提倡全局的常量

    const float TAX = 0.03;   //全局常量

    float tipRate;   //全局变量

    int main()

    {

    ........

    }


    类的解读:

    分类能力是人类有别于其他动物的因素之一。人类在自然界看到的不是数以千计的形状,而是动物,岩石,昆虫,树木等等。

    类是一系列捆绑在一起的变量和函数,其中的变量可是任何其他类型,包括其他类。

    变量构成了类 的数据,函数使用这些数据来执行任务,将变量和函数捆绑在一起称为封装

    默认情况下,所有成员变量和成员函数均为Private。

    如下,model 为private,,其他均为public

    class Tricycle
    {
        int model = 110;
    public:
        unsigned int speed;
        unsigned int wheelSize;
        Pedal();
        Brake();
    };
    //
    //  main.cpp
    //  test
    //
    //  Created by Dufy on 2018/10/9.
    //  Copyright © 2018年 Dufy. All rights reserved.
    //
    
    #include <iostream>
    using namespace std;
    #include <string.h>
    
    int Add(int x,int y);
    
    class Tricycle    //定义类
    {
    public:
        Tricycle(int initialSpeed); //初始化成员数据
        ~Tricycle();
        int GetSpeed();
        void SetSpeed(int sss);
        void Pedal();
    //    void Brake();
    private:
        int speed;
    };
    
    //对声明的函数进行定义
    Tricycle::Tricycle(int initial)
    {
        SetSpeed(initial);
    }
    
    Tricycle::~Tricycle()
    {
        //do nothing
    }
    
    int Tricycle::GetSpeed()
    {
        return speed;
    }
    
    
    void Tricycle::SetSpeed(int newSpeed)
    {
        speed = newSpeed;   //对speed进行赋值
    }
    void Tricycle::Pedal()
    {
        cout << "the speeding ...:" << speed+1 << endl;
    }
    
    int main(int argc, const char * argv[]) {
        
        Tricycle haha(30);  //利用构造函数(析构函数),直接将30赋值speed
    //    haha.SetSpeed(3);
        haha.Pedal();
        
        return 0;
    }

    编写函数代码前,先声明之:

    int Add(int x,int y);   //先声明
    
    
    int main(int argc, const char * argv[]) {
    ......
    
        return 0;
    }
    
    
    int Add(int one,int other)  //定义,不必为x,y,形参类型匹配即可
    {
        return one + other;
    }

     原因:

    也可将函数定义移前,但在大型编程项目中,确保所有函数在使用前都进行了定义会很麻烦


      操作符运算:

    char < short < int < float < double

    如,char c ='2'    //'2'的ASCII码是50

    cout << c+1

    51.

    首先char低于int,转换为int 50,再进行计算


    auto关键字:

    c++里面首先要对变量的类型进行定义,使用auto可自动判断

    如: 

     

    相当于进行了Int, float, double(c++默认)的类型定义

  • 相关阅读:
    html页面自动运行php文件
    在数据库原有字段后增加新内容
    mysql判断字段不等于空
    php中的日期加减
    js验证只能输入中文
    session_start()关于Cannot send session cache limiter headers already sent错误解决方法
    php记录前一页面url
    打印功能
    js验证email
    用php获取本周,上周,本月,上月,本季度日期
  • 原文地址:https://www.cnblogs.com/skylover/p/9716261.html
Copyright © 2011-2022 走看看