C++Primer 二周目笔记(七):类

写在前面:

              类是整个C++面向对象思想的核心,也是其他后生高级语言类设计的源头,C++中类的操作,思想,流程,个人认为启发了现在大部分的面向对象编程语言。C++ 11标准与C99应该是存在了一些差别的,因为我写示例代码的时候一开始没有改compiler,直接用旧的gcc编译器,应该是C99标准的,并编译不过去,报错奇奇怪怪。

              下面正式开始这一章的总结.

1.     this,这个变量在调用成员函数时被对象的地址初始化。虽然说得很拗口,但就是个对象的指针。普通成员函数声明时,调用时this的类型是class *const型。记得使用->。

2.     const成员函数。例下属代码中,类baoxiu的成员函数,void pass_time(const int n) const。实际上就是对this指针初始化加了底层const,这样就实现了对常量对象的常量成员函数的调用,同时,非常量对象也可以调用常用成员函数,直接调用就是了,可以看下面baoxiu的例子。

3.     mutable,需要修改某一个类的const对象时,只能修改其声明为mutable型的变量。

4.     C++ 11中的新类构造函数。见下例baoxiu第二个构造函数。注意后面接  成员(形参),如time(a)。

5.     使用class或struct关键字的区别。唯一的区别就是默认的成员变量或函数的访问权限。class默认private,struct默认public。

6.     友元。形如friend void func1()的函数,具有非类成员的身份,但是仍可以访问private中的定义,有点admin的意思,使类的使用更加灵活。

不同类之间可以穿插友元:声明如下:

       class A{

              friendclass B;

       }

 B为A友元类,B类可以访问A的所有非公有成员。

                     classA{

                            friendvoid B::control_A();

                     }

B的control_A函数为A的友元函数,可以访问A私有成员。但是需要注意步骤,首先要定义B类,其中声明了control_A函数,但是不可以定义;然后定义A类,包括友元函数control_A的声明;然后才能定义control_A函数。

7.     在类中实现的函数默认是inline的。也可以显式的声明inline,然后在外部定义。

8.     返回*this,如果this没有被声明是底层const,即在函数声明中形参列表后面没有const,此时this是class *const型,*this是左值class型,可以嵌入一串序列动作中,如,baoxiu.re_self().pass_time(5);

但是如果*this是一个const class型,则不可以使用其非初始化this为const型函数,即不可以使用其非常量成员函数,如不可以使用baoxiu.name_set()。

9.     类中的类型定义:baoxiu::pos的定义。main函数中ptest的声明

10.   委托构造函数,没卵用的玩意。

11.   类的静态成员:静态数据成员和静态成员函数。静态数据成员的类型可以是常量、引用、指针、类类型等。类的静态成员存在于任何对象之外,但是所有的对象都可以对他进行调用。静态成员函数不能声明为const,因为他不是任何常量对象的函数,也不能使用this指针,因为this指针在调用时没有被初始化。


#include<iostream>
using namespace std;

class baoxiu
{
    friend void admin_edit_time(baoxiu &bx,const string a);
public:
    baoxiu()=default;
    baoxiu(const unsigned &n,const string &a,const string &b,const unsigned int bn,unsigned int co):
        seq(n), time(a),name(b),building_unm(bn),cost(co){}
    typedef std::string::size_type pos;
    inline unsigned int timecost() const;
    void name_set(string n);
    inline void pass_time() const;
    void pass_time(const int n)const{this->left_time-=n;cout<<this->left_time<<endl;}
    baoxiu re_self(){return *this;}
    static int chaxun(baoxiu a);
private:
    unsigned int seq=0;
    string time="00:00:00";
    string name="xxx";
    unsigned int building_unm=0;
    unsigned int cost=1;
    mutable int left_time=10;
};
unsigned int baoxiu::timecost() const
{
    return this->cost;
}
void baoxiu:: pass_time() const
{
    this->left_time-=1;
    cout<<this->left_time<<endl;
}
unsigned int sum(initializer_list<baoxiu> lst)
{
    unsigned int sumcost=0;
    for(auto i:lst)
    {
        sumcost+=i.timecost();
    }
    cout<<sumcost<<endl;
    return sumcost;
}
void admin_edit_time(baoxiu &bx,const string a)
{
    bx.time=a;
    cout<<bx.time<<endl;
}
int baoxiu::chaxun(baoxiu a)
{
    if(a.left_time>0)
        return 1;
    else
        return 0;
}
void baoxiu::name_set(string n)
{
    this->name=n;
}
int main()
{
    baoxiu::pos ptest;
    baoxiu bx1;
    baoxiu bx2;
    baoxiu bx3;
    sum({bx1,bx2,bx3});
    admin_edit_time(bx1,"12:10:58");
    bx1.pass_time();
    bx1.pass_time(5);
    baoxiu::chaxun(bx1);
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值