C++第七章:类

类的用户是程序员,而不是最终的用户。
定义在类内部的函数都是隐式的内联函数。
类中成员变量的声明顺序,决定了成员变量的初始化顺序。

成员函数可以定义在类内部也可以定义在类外部
#include<bits/stdc++.h>
using namespace std;
struct temple{
    void print(string x);
    string num,name;
};
void temple::print(string x){cout<<x<<endl;}

int main(){
    temple a;
    a.num="这是一个struct类";
    a.print(a.num);
    return 0;
}
//在类的外部定义函数,必须包含它所属的类名
const 函数

在函数括号后加一个const说明这个函数是一个只读函数,不会修改任何数据。是一个常量函数。this是一个常量指针,总是指向这个对象,任何对类成员的直接访问都被看做this的隐式引用。
默认情况下,this的类型是指向 类类型的非常量版本的 常量指针。
声明const常量函数的作用是将这个指针转换为常量指针。

返回一个函数
#include<bits/stdc++.h>
using namespace std;
struct Node{
  int a,b;
  Node re(){   return *this;}//返回的是一个引用,建立一个临时变量,然后拷贝给目标对象,返回的是右值
}a;
int main(){
  a.a=1;
  (a.re()).a=2;//报错:将临时变量作为一个左值
  cout<<a.a<<endl;
}
构造函数
struct Node{
    Node()=default;//我们不仅需要别的构造函数,同时也需要默认的构造函数。default的意思就是默认的合成构造函数
    Node(int x1,string s):tool1(x1),tool2(s),sum(3){};
    int tool1,sum;
    string tool2;
};
class 和 struct的区别。

如果使用struct,在第一个访问说明符出现之前的成员是public的,相反,如果使用class,在第一个访问说明符之前出现的成员是private的。

友元

既然类有私有成员变量,那么如果访问类的私有成员呢?
方法是设立友元、

class Node{
    friend void print(Node x);这只是权限说明,并不声明.如果希望类的用户调用某个友元函数,还要再声明一下。
    int pre;
    public :
        Node(int x):pre(x),mid(x),las(mid){};

        int mid;
    private:
        int las;
};
void print(Node a){
    cout<<a.las;
}
int main(){
    Node a=Node(444);
    print(a);
}
mutable
#include<bits/stdc++.h>
using namespace std;
class Node{
    public :
        void member()const;
        void prin(){ cout<<counts;}//mutable,如果希望某个成员变量能够在const函数中进行修改,那么可以在这个变量定义之前加上mutable
    private:    mutable size_t counts;
};
void Node::member() const { counts++; }
int main(){
    Node a;a.member();a.member();
    a.prin();
    return 0;
}
前向声明
#include<bits/stdc++.h>
using namespace std;
class Node;//前向声明:先声明后定义的类型
Node * p;//在定义之前是一种不完全类型。只能在非常有限的情况中使用。引用或者指针,
class Node{int a,b;};//定义
int main(){}
令成员函数作为友元
#include<string>
#include<iostream>
using namespace std;

//令成员函数作为友元
//首先定声明友元函数,但是不能声明
//然后声明友元
//然后定义友元函数
class B{
public :
    void clear();
};
class A{
    friend void B::clear();
public :
    void prin(){
        cout<<a<<endl;
    }
private:
    int a;
};
void B::clear(){
    A tem;
    tem.a=5;
}

int main(){
    A a;
    B b;
    b.clear();
}

友元函数作用域:
友元函数声明为友元 和声明为函数 是不一样的,friend只是声明权限。如下:

#include<bits/stdc++.h>
using namespace std;
struct Node{
    friend void f();//定义一个友元函数
//    Node(){f();}//错误f没有被声明
    void g();
    void h();
};
//void Node::g(){f(); }//错误:f仍旧没有被声明
void f(){}
void Node::h(){f();}//正确
int main(){
    return 0;
}
名字查找:
  • 在普桶程序中找声明语句
    • 首选在名字所在的块中查找,如果这个块(仅仅考虑使用之前出现的声明)没有找到,那么在外层作用域中找,如果还没有找到,就报错
  • 成员函数中
    • 首选在名字所在的函数中查找,如果这个函数(仅仅考虑使用之前出现的声明)没有找到, 则在类中继续查找。如果还没有找到,就在成员函数定义之前的作用域中继续查找。类作用域之后,也在类作用域之外进行查找
委托构造函数

顾名思义:将自己的构造任务委托给别人

class Node{
    Node(int x,int y):a(x),b(x){};
    Node():Node(0,0){};
    public :
    private:
    int a,b;
};
隐式类类型转换
	#include<bits/stdc++.h>
using namespace std;
//隐式类类型转换
class Node{
   int a;
   public :
       void deal(Node a){};
       Node(int a):a(a){}
       Node()=default;
};
int main(){
   Node a;
   a.deal(5);//这里本来期望收到一个Node的参数,但是收到了一个int,发生了隐式类类型转换
   return 0;
}
explicit

“可以用 单个形参来调用 的构造函数定义了从 形参类型 到 该类类型 的一个隐式转换。”
在隐式类类型转换中并不是只能有一个参数可以有多个参数,但是这些参数都是默认实参的。

excplict 抑制了隐式类类型转换
#include<bits/stdc++.h>
using namespace std;
//隐式类类型转换
class Node{
    int a;
    public :
        void deal(Node a){};
        explicit Node(int a):a(a){}
        Node()=default;
};
int main(){
    Node a;
    a.deal(5);//报错,explicit抑制了隐式类类型转换
    return 0;
}
聚合类:

聚合定义为:
数组、没有以下内容的类、结构和联合:
构造函数、私有或受保护的成员、基类、虚函数

#include<bits/stdc++.h>
using namespace std;
//隐式类类型转换
class Node{
    public:
    int a;
    int b;
};
int main(){
    Node a={1,2};
    return 0;
}
初探字面值常量类

数据成员必须是字面值类型
类必须至少包含一个constexpr函数,
如果类内有初始值,则必须是constexpr,如果某种成员有自己的类类型,则这个* 成员必须有自己的constexpr构造函数。
必须使用析构函数的默认定义,该成员负责销毁类的对象。

constexpr函数:所有形参和返回值都是字面值类型,有且只有一个return类型。
constexpr类的构造函数可以=default,构造函数体一般是空的,

静态成员static。

必须再类的外部进行初始化。

#include<bits/stdc++.h>
using namespace std;
class Account{
public:
     void calculate(){
         amount += amount * interestRate;
     }

     static double rate(){
         return interestRate;
     }

     static void rate(double newrate);

private:
     std::string owner;
     double amount;
     static double interestRate;
     static double initRate();
};
double Account::interestRate=0;//必须在类的外部定义和初始化每个成员。
void Account::rate(double newrate){
    interestRate=newrate;
}
int main(){
    double r=Account::rate();
    return 0;
}

如果成员变量是字面值常量类型的constexpr,就可以它提供const整数类型的类内初始值,不过要求静态成员必须是字面值常量类

#include<bits/stdc++.h>
using namespace std;
class Account{
public :
private:
    static constexpr int period=30;//只有声明没有定义,可以用作数值。但是引用等需要在类外定义
};
int main(){

}
#include<bits/stdc++.h>
using namespace std;
class Account{
public :
private:
    static constexpr int period=30;//
};
constexpr int Account::period;//初始值再类内的定义
int main(){
}

static成员可以用作默认形参,也可以是不完全类型。

#include<bits/stdc++.h>
using namespace std;
struct Bar{
public :
    void change(Bar = bar){};//静态成员可以用作默认形参
private:
    static Bar bar;//静态成员可以是不完全类型(即没有被完整定义的类型)
    Bar* bbar;
//    Bar bbbar;//报错,数据类型不能是不完全类型
};
int main(){
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值