C++基础_01

* C++ "Hello World!"
#include <iostream> //C++的标准输入输出文件
using namespace std; //命名空间
int main (){
    cout << "Hello World"<<"!"<<endl; //cout标准输出 endl<==> "\n"
    return 0;
}
* 一个简单的C++
#include <iostream>
using namespace std;
//圆是一个类
//圆有半径,周长和面积
//通过圆的半径可以求出周长或面积
class Yuan{

private:

    const double PI = 3.14;

    double r;//半径

public: 

    void setR(double cr){
        r = cr;
    }
    double getR(){
        return r;
    }
    //得到面积
    double getCS(){
        return PI * r *r;
    }
    //得到周长
    double getCC(){
        return PI * 2 * r;
    }
};

int main(int argc, char *argv[])
{

    //定义一个圆
    Yuan yuan ;
    double cr = 0.0;
    cout<< "please to r:"<<endl;
    cin>>cr;//输入远的半径
    yuan.setR(cr);
    cout<<"ciler s is :"<<yuan.getCS()<<endl;
    cout<<"ciler c is:"<<yuan.getCC()<<endl;
    return 0;
}
* 一个典型的错误
#include <iostream>
using namespace std;
class Test{

    int a ;
    //int b = a * a * a;//Error

    //解决:(在类中加入成员函数)
    int getB(){
        b =  a * a * a;
        return b;
    }
};

int main(){
    cin>>Test.a;
    cout<<b<<endl;//Error(b的值并没有进行计算(a*a*a),而是从b的内存地址取得值)
    return 0;
}
* namespace<命名空间> 的使用
#include <iostream>
#include <stdio.h>
#include <stdlib.h>

using namespace std;

namespace A{
    int a = 10;
}

namespace B{

    int a = 50;

    namespace C {
        int a = 100;

        typedef struct Teacher{

            int a;

        }Teacher;

    }

}

int main(int argc, char *argv[])
{

    using namespace A;//A中的所有表示符都可以直接使用

    using namespace B;//B中的所有标识符都可以直接使用,但是C中的却不行.需要加[C::]

    //当命名空间中含有相同的标识符的时候需要加入命名空间的名称来使用
    cout<<"A::a="<<A::a<<endl;
    cout<<"B::a="<<B::a<<endl;
    cout<<"C::a="<<C::a<<endl;

    /*Error
    C::Teacher t = (C::Teacher *)malloc(sizeof(C::Teacher));
    */
    C::Teacher * t = new C::Teacher;
    t->a = 1500;
    cout<<"Teacher::a="<<t->a<<endl;
    delete t;

    return 0;
}
* C/C++的异同
>1.register(寄存器变量)
    register int a = 100;
    int * p = &a;//C:Error C++:OK
    //C语言对于寄存器变量不允许做取地址操作,而C++可以
    //C++对于多次使用的变量会自动优化变成寄存器变量

>2.for循环
    for (int i = 0;i < 1000;i++);//C:Error  C++:OK
    //C语言不允许int i在for循环内部定义,而C++可以  

>3.变量的重复声明
        int a ;
        int a = 100;
//C:OK C++:Error
//C++不允许变量重复声明      

>4.struct 类型增强
#include <iostream>
#include <string.h>
#include <ctype.h>
using namespace std;

struct Student{

private:
    int age;
    char name[64];
public:

    void setAge(int a){age = a;}
    void setName(char * str){strcpy(name,str);}

    void show(){
     cout<<"Name = "<<name<<" age = "<<age<<endl;
    }
};

int main(int argc, char *argv[])
{
    Student stu;//C:Error 在没有使用typedef 必须定义struct Student stu;
    stu.setAge(18);
    stu.setName("yzh");
    stu.show();
    return 0;
}
//C++的struct 允许直接使用结构体的名称来定义变量
//C++的struct 允许使用权限访问修饰符(public,protected,private)
* C++增加了类型检查
f(i){return 10;} 
g(){return ;}

void main(){
    int a = 10;
    f(a);//C++:Error
    g(1,2,3,4,5);//C++:Error
    return 0;
}

//C:OK
//C++:Error
//C++增加了类型检查机制,强制要求函数的定义与使用规范化  
* C++ 增加 bool 数据类型
bool fc = true;
bool fg = false;//bool类型的值只能为true(1)/false(0)

cout<<"sizeof(fg) = "<<sizeof(fg)<<endl ;//sizeof(fg) = 1
fg = -10;
cout<<"fg = "<<fg<<endl; // fg = 1;
fg = 10;
cout<<"fg = "<<fg<<endl; // fg = 1;
fg = 0;
cout<<"fg = "<<fg<<endl; // fg = 0;
* C++ 三目运算符(?:)
int a = 10;
int b = 20;
(a<b ? a : b) = 50;//C:Error C++:OK
//C语言返回一个值,C++返回一个左值
//C++要想返回一个左值[1.非const常量 2.必须有自己的内存空间]

//C实现C++的三目运算符效果
*(a<b ? &a : &b) = 50;
* C/C++中的const
>C/C++中相同:

    >const 修饰指针

    const int * p ;//p指向的内存不允许被修改,但p可以更给指向
    int * const p;//p的指向不允许修改,但指向的内存可以修改
    const int * const p;//只读

    >const做函数参数

    typedef struct Teacher{
        int age;
    }Teacher;

    void funa(const Teacher * t){//t所指向的内存不允许修改
        t->age = 10;//Error
        t = NULL;//OK
    }

    void funb(Teacher * const t){//t的指向不允许修改
        t = NULL;//Error
        t->age = 100;//OK
    }

    void func(const Teacher * const t){//只读
        t  = NULL;//Error
        t->age= 200;//Error
        printf("%d",t->age);//OK
    }   

>C/C++中的异同:

    >C中的cosnt常量(伪常量)
    const int a = 10;//或int const a = 10;
    printf("a = %d",a); //a = 10;
    int * p = NULL;
    p = (int *)&a;
    *p = 20;
    printf("a = %d",a); //a = 20;

    //C语言中的常量可以通过取常量地址间接的修改常量的值,并非真正的常量

    >C++中的const常量(真正常量)
    const int num = 10;//C++将const常量放在一张由(key=value)组成形式的符号表中
    int * p = NULL;
    p = (int *)&num; // C++将为num重新分配了一块内存
    *p = 100;
    cout<<"num = "<<num<<" &num = "<<&num<<endl;
    cout<<"*p = "<<*p <<" p = "<<p<<endl;
* C++中const和define
#include <iostream>
using namespace std;

void funA(){

#define a 10
//    const int a = 20; //1.将#define 修改为cosnt 
//#undef a //2.通过undef解除定义,限定#define的作用域
}

void funB(){
    cout<<"a = "<<a<<endl;
}

int main(int argc, char *argv[])
{
    funB();
    funA();

    return 0;
}

//#define 只是机械式的做替换,const则有明确的作用域,比#define更安全
* C++引用(Type &name = value)
>引用是C++的新特性,C不支持

int a = 10;
int &b = a;//b是a的引用[==>int * const p = &a ]
int &c ;//Error (引用在定义时必须指明是谁的别名)

int a = 10;
int &b = a;
cout<<"a = "<<a<<" b = "<<b<<endl;
cout<<"&a = "<<&a<<" &b = "<<&b<<endl;
//b是a的引用,b其实是a的别名,和a操作相同的一段内存

>在外部函数中交换两个变量的值 
//1.指针
void swap(int * a,int * b){
    int temp = *a ; *a = *b;*b = temp;
}   
//2.引用
void swap(int &a,int &b){
    int temp = a;a = b; b = temp;
}   
* C++引用在外部函数中改变复杂类型的值
#include <iostream>

using namespace std;

typedef struct Teacher{

    int age ;

}Teacher;

void funA(Teacher * t){
    t->age = 18;
}

void funB(Teacher &t){//Teacher
    t.age = 28;
}

void funC(Teacher* &t){//Teacher *
    t->age = 38;
}

int main(int argc, char *argv[])
{

    Teacher * t = new Teacher;
    t->age = 10;
    cout<<"T->age = "<<t->age<<endl;
    funA(t);
    cout<<"T->age = "<<t->age<<endl;
    funB(*t);
    cout<<"T->age = "<<t->age<<endl;
    funC(t);
    cout<<"T->age = "<<t->age<<endl;
    delete t;
    return 0;
}
* 引用的本质
>1.引用变量也占用内存大小
typedef struct Teacher{
    char str[64];//64
    int age;//4
    int &a;//4
    int &b;//4
}Teacher;
cout<<"sizeof(t) = "<<sizeof(Teacher)<<endl;//sizeof(Teacher) = 76

>2.C++编译器实现引用
int a = 10;
int &b = a;//==> int * const b = &a;//引用实质是一个无法修改指向的指针

//引用做函数参数的实现
void funA(int &b){
    b = 30;
}
void funA(int * const b ){
    *b = 30;
}
* 引用和函数的返回值
#include <iostream>

using namespace std;

int getA(){//返回a的副本
    int a = 10;
    return a;
}

int& getB(){ //返回a自身
    int a = 10;//栈上分配内存会在函数结束时被系统回收,容易引起错误
//    static int a = 10;//解决1.加入static 2.提升为全局变量
    return a;
}


int* getC(){//返回a的引用
    int a = 10;栈上分配内存会在函数结束时被系统回收,容易引起错误
//    static int a = 10;//解决1.加入static 2.提升为全局变量
    return &a;
}

int main(int argc, char *argv[])
{
    int a = getA();
    int b = getB();
    int &c = getB();
    int * d = getC();
    cout<<"A = "<<a<<endl;
    cout<<"B = "<<b<<endl;//Error 栈上分配内存导致错误
    cout<<"C = "<<c<<endl;//Error 栈上分配内存导致错误
    cout<<"D = "<<*d<<endl;//Error 栈上分配内存导致错误

    return 0;
}
* 引用可以当左值或者右值
#include <iostream>
using namespace std;
int A(){
    static int a = 10;
    cout<<"A::a = "<<a<<endl;
    return a;
}
int & B(){
    static int a = 10;
    cout<<"B::a = "<<a<<endl;
    return a;
}

int main(int argc, char *argv[])
{

    int a = A();//OK
    int b = 0;
    //A() = b;//Error

    int c = B();//OK
    int d= 100;
    B() = d;//OK
    B(); //B::a的值被该变了
    return 0;
}

//函数要当左值,必须返回一个引用
* 引用指针(在函数外部分配内存)
#include <iostream>
#include <stdlib.h>

/*在外部函数中分配内存*/

using namespace std;

typedef struct Teacher{
    int age;
}Teacher;


//C
int getMemery_01(Teacher ** t);//传入二级指针(传入一级指针的地址)

//C++
int getMemery_02(Teacher * &t);//传入一级指针的引用

int main(int argc, char *argv[])
{

    Teacher * tp = NULL;

    getMemery_01(&tp);
    cout<<"tp->age = "<<tp->age<<endl;
    if (tp != NULL){
        free(tp);
        tp = NULL;
    }

    getMemery_02(tp);
    cout<<"tp->age = "<<tp->age<<endl;
    if (tp != NULL){
        tp = NULL;
        delete tp;
    }

    return 0;
}

int getMemery_01(Teacher ** t/*out*/){

    if (t == NULL){
        return -1;
    }
    Teacher * temp = (Teacher *)malloc(sizeof(Teacher));

    if (temp == NULL){
        return -2;
    }
    temp->age = 100;
    *t = temp;//指针间接赋值
    return 0;
}

int getMemery_02(Teacher * &t){

    if (t != NULL){
        return -1;
    }
    t = new Teacher;
    if (t == NULL){
        return -2;
    }
    t->age = 200;
    return 0;
}
* 常量引用
{
        //普通引用
        int a = 10;
        int &b = a;
        b = 20;
        cout<<"a = "<<a<<" b = "<<b<<endl;
}

{
        //常量引用
        int a = 10;
        const int &b = a; // ==> cosnt int * const b = &a;
//        b = 20;//Error
}

{
        //用字面量初始化引用
        const int a = 10;//C++编译器会把a放在符号表
        //int &b = a;//Error
        const &bb = a;//OK C++编译器会为a分配一段内存空间

//        int &c = 40;//Error-> 40是字面量,并没有内存地址
        const int &d = 40;//OK C++编译器会为d分配一段内存空间
}
* 常量引用做函数参数(只读模式)
typedef struct Teacher{
    int age;
}Teacher;

void fun(const Teacher * &t){ //常量引用左函数参数只能是 只读,不能被修改 ==> const Teacher * cosnt t;
    t->age = 10;//Error
    t = NULL;//Error
    cout<<"age = "<<t->age<<endl;//OK
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值