zoukankan      html  css  js  c++  java
  • c++知识学习 (0)

    1 动态内存分配和释放

    .m .mm .cpp 

    #import <iostream>  import 会自动避免重复编译  #include不会这么做,需要在对应的文件里添加

    using namespace std;

    new运算符

    格式

    指针= new 数据类型名字;

    指针= new 数据类型名字(初始值);

    作用:从内存的动态区域申请指定数据类型所需的存储单元。成功,返回首地址,否则,NULL

    new delete成对使用。

    2 函数

    2.1内联函数:

       复杂的循环和分支,递归都不会内联。

    2.2函数参数默认值:

    c++可以函数的参数可以指定默认值。

    默认值要在函数声明中指定,定义的时候不必再指定。

    必须从右到左指定参数的默认值。 

    2.3函数重载:

    参数个数,参数类型上有所不同。

     

    代码:

     1 //
     2 //  main.cpp
     3 //  ArrayTest
     4 //
     5 //  Created by 张学院 on 14-1-6.
     6 //  Copyright (c) 2014年 com.mix. All rights reserved.
     7 //
     8 
     9 #include <iostream>
    10 
    11 #include <string>
    12 using namespace std;
    13 //-------函数声明 :可以有默认值-------
    14 int minValue(int a,int b=10);
    15 int maxValue(int a,int b);
    16 //--------函数重载maxValue-----------
    17 double maxValue(double a,double b);
    18 int maxValue(int a,int b ,int c);
    19     
    20 //---------c++的二意性---------------
    21 int test(int a);
    22 int test(int a,int b =10);
    23 
    24 //-------函数定义:不用再写默认值-------
    25 int maxValue(int a,int b){
    26     return a>b?a:b;
    27 }
    28 int minValue(int a,int b){
    29     return a<b?a:b;
    30 }
    31 double maxValue(double a,double b){
    32     return a>b?a:b;
    33 }
    34 int maxValue(int a,int b ,int c){
    35     
    36     int temp=maxValue(a,b);
    37     return temp>c?temp:c;
    38 
    39 }
    40 int test(int a){
    41 
    42     return a;
    43 }
    44 int test(int a,int b ){
    45 
    46     return (a+b);
    47 }
    48 
    49 int main()
    50 {
    51     //new 都是分配在堆里面,要释放内存
    52     //分配在栈里面的话,程序结束以后就自动释放。
    53     
    54     //----------变量-----------------
    55     int *p = new int();
    56     printf("%d
    ",*p);
    57     printf("size of %d
    ",sizeof(p));
    58     *p=2;
    59     printf("%d
    ",*p);
    60     delete p;
    61     
    62     int *p1 = new int(5);
    63     printf("%d
    ",*p1);
    64     delete p1;
    65     //----------数组-----------------
    66     char *ch = new char[10];
    67     strcpy(ch, "iphone");
    68     printf("%s
    ",ch);
    69     //delete  ch;错误的!
    70     delete [] ch;
    71     //----------函数默认值-----------------
    72     int x=3,y=5;
    73     int max=maxValue(3, 5);
    74     printf("maxvalue=%d
    ",max);
    75     
    76     int min=minValue(9);
    77     printf("minvalue=%d
    ",min);
    78     
    79     //----------函数重载-----------------
    80     double max1=maxValue(2.50, 3.80);
    81      printf("double maxvalue=%.3f
    ",max1);
    82     int max2=maxValue(25, 38,15);
    83     printf("double maxvalue=%d
    ",max2);
    84     
    85     
    86     //---------c++的二意性---------------
    87     //test(1);报错
    88 }

    输出:

    0

    size of 8

    2

    5

    iphone

    maxvalue=5

    minvalue=9

    double maxvalue=3.800

    double maxvalue=38

     

     

    C++面向对象,class定义类,private,protected,public成员变量与成员函数  

    c++类和结构体类似

    定义

    class 类名{

    <成员变量定义>;

    。。。。

    }

    这个方面跟java的类似。

    不过java默认的成员变量是protected,c++好像是private。

    //
    //  Person.h
    //  ArrayTest
    //
    //  Created by 张学院 on 14-1-8.
    //  Copyright (c) 2014年 com.mix. All rights reserved.
    //
    
    //防止重复引用
    #ifndef __ArrayTest__Person__
    #define __ArrayTest__Person__
    
    #include <iostream>
    using namespace std;
    class Person{
    //---------成员变量--------
    public :
        
        int age;
        
    private :
        int weight;
        char name[30];
        char sex;
    //---------成员方法--------
    public:
        void setWeight(int weight);
        int getWeight();
        
    };
    #endif /* defined(__ArrayTest__Person__) */
    //
    //  Person.cpp
    //  ArrayTest
    //
    //  Created by 张学院 on 14-1-8.
    //  Copyright (c) 2014年 com.mix. All rights reserved.
    //
    
    #include "Person.h"
    //------方法实现格式------
    //----返回值 类::方法名 (参数)------
    void Person::setWeight(int weight){
        //-----this 代表
        this->weight=weight;
    }
    int  Person::getWeight(){
        return weight;
    }
    //
    //  main.cpp
    //  ArrayTest
    //
    //  Created by 张学院 on 14-1-6.
    //  Copyright (c) 2014年 com.mix. All rights reserved.
    //
    
    #include <iostream>
    
    #include <string>
    #include "Person.h";
    int main()
    {
        //-----类似结构体的创建方式-------
        Person per;
        //------类的成员变量默认是private,而结构体是public------
        per.age=20;
        //per.weight
        per.setWeight(50);
        printf("the person's weight is %d
    ",per.getWeight());
    }

    输出:

    the person's weight is 50

     

  • 相关阅读:
    C# 判断一个文本文件的编码格式(转载)
    img图片加载出错处理(转载)
    C#中当程序的访问权限不足时,Directory.Exists和File.Exists方法不会抛出异常报错
    ajax上传文件以及实现上传进度条(转载)
    通过jQuery Ajax使用FormData对象上传文件 (转载)
    CMD命令查看当前电脑安装所有版本.NET Core SDK(转载)
    SQL Server聚合函数与聚合开窗函数 (转载)
    C#中,使用显式类型转换(int)和Math.Round方法,将浮点数转换为整数的区别
    SQLServer 窗口函数(转载)
    node-webkit教程(8)Platform Service之Clipboard
  • 原文地址:https://www.cnblogs.com/aosting/p/3510789.html
Copyright © 2011-2022 走看看