C++面向对象--类

1、实例化的两种方式

#include <iostream>

#include<stdlib.h>

 

using namespace std;

 

class lio

{

public:

    int a=3;

    int b=4;

    void aa()

    {

        cout<<a;

    }

    int bb()

    {

        return b;

    }

};

 

int main()

{

    //栈的方式来实现

    lio a;

    a.a=33;

    a.b=44;

    cout<<a.a<<a.bb();

 

    //堆的方式实现

    lio *b=new lio();

    if(b){

        b->a=333;

        b->b=222;

        cout<<b->a;

        b->aa();

        b->bb();

    }

    return 0;

}

2、String 类型 和js中的 Array一样

#include<string>

int main()

{

    string a="qweqwe";

    cout<<a;

    //空值检测

    cout<<a.empty()<<endl;

    //大小检测

    cout<<a.size()<<endl;

    //返回第几个

    cout<<a[3]<<endl;

    //链接

cout<<a+a;

//错误链接方式

//cout<<"hello"+<<"world";

    return 0;

}

//输入检测程序

int main()

{

    string name;

    cout<<"please input you name.."<<endl;

    cin>>name;

    if(name.empty()){

        cout<<"please input again..";

        system("pause");

        return 0;

    }else

    {

        cout<<name<<endl;

        cout<<name[0]<<name.size()<<name+"爱三妹";

    }

    return 0;

}

 

3、数据的封装

#include <iostream>

#include<stdlib.h>

#include<string>

 

using namespace std;

 

class lio

{

public:

    int a=3;

    int b=4;

    void setname(string m_strname)// c++形参里边要定义好数据类型

    {

        name=m_strname;

    }

    string getname()

    {

        return name;

    }

private:

    int age=22;

    string name;

};

 

int main()

{

    lio a;

    a.setname("lio");//注意加引号

cout<<a.getname();

delete a;//勿忘

Str=NULL;

    return 0;

}

4、类内定义和内联函数与类外定义

#include <iostream>

#include<stdlib.h>

#include<string>

 

using namespace std;

 

//内联函数

inline void lio(string name)

{

    cout<<name+"爱三妹"<<endl;

};

//类内定义

class li

{

public:

    int a=5;

    void san(string name)

    {

        cout<<"我是三妹"+name;

    };

    int ss(int num);

private:

    int b=2;

};

//类外定义

int li::ss(int num)

{

    return num;

};

int main()

{

    li asd;

    asd.ss(123);

    lio("lio");

    return 0;

}

把头文件放在头文件的文件夹内,用.h来命名,函数实现部分放到主函数文件的同一个文件夹内,再把头文件 引进去 include,添加头部以及命名空间即可

5、构造函数

#include <iostream>

#include<stdlib.h>

#include<string>

 

using namespace std;

 

class lio

{

public:

    lio()

    {

        cout<<"start"<<endl;

    };

    lio(int a)

    {

        cout<<a;

    }

};

int main()

{

    lio a;

    lio b(12);

    return 0;

}//构造函数支持重载

构造函数有默认参数的构造函数叫做默认构造函数

6、初始化列表

7、拷贝构造函数

Student(const Student &stu){ }

及发生在对象=的时候,就会触发拷贝构造函数

#include <iostream>

#include<stdlib.h>

#include<string>

 

using namespace std;

 

class student

{

public:

    student()

    {

        cout<<"gou zao han shu";

    };

    student(const student &stu)

    {

        cout<<"kao bei gou zao han shu ";

    };

};

int main()

{

    student aa;

    student bb(aa);

    student cc=aa;

    return 0;

}

//拷贝构造函数没有重载

8、析构函数

释放掉对象里的指针对象变量,释放资源,不可能重载

#include <iostream>

#include<stdlib.h>

#include<string>

 

using namespace std;

 

class student

{

public:

    student()

    {

        cout<<"gou zao han shu";

    };

    student(const student &stu)

    {

        cout<<"kao bei gou zao han shu ";

    };

    ~student()

    {

        cout<<"say goodbye"<<"\n";

        delete []p;

    }

private:

    char *p;

};

int main()

{

student aa;

student nn=aa;

    student *bb=new student();

    delete bb;

 

    return 0;

}


9、对象=成员变量(const  

 

 

 

成员函数

 

10、命名空间

using namespace std;//设置默认命名空间

namespace lio

{

int a=123;

}

cout<<lio::a<<endl;



11、待续

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值