C++学习day4

作业:

1> 思维导图

 2> 整理代码

1. 拷贝赋值函数课上代码

//拷贝赋值函数课上代码
#include<iostream>
using namespace std;

//创建类
class Stu
{
private://私有的
    string name;
    int socer;
    int *age;//此处注意用到指针类型
public://共有的
    //无参构造函数(系统默认设置)
    Stu()
    {
        cout << "Stu:: 无参构造函数 " << endl;

    }
    //有参构造函数(自定义,自定义后需要将无参构造函数显性)
    Stu(string n,int s,int a):name(n),socer(s),age(new int (a))//初始化列表
    {
        cout << "Stu:: 有参构造函数" << endl;

    }
    //拷贝构造函数
    Stu(const Stu &other):name(other.name),socer(other.socer),age(new int(*other.age))//此处注意*other.age:因为定义是指针类型所以要去指针指向的内容进行赋值
    {
        cout << "Stu:: 拷贝构造函数" << endl;
    }
    //拷贝赋值函数
    Stu &operator=(const Stu &other) //此处不能用初始化列表因为是赋值操作
    {
        if(this != &other)//给自己赋值多此一举
        {
            name=other.name;
            socer=other.socer;
            age = new int (*other.age);//深拷贝赋值因为有指针防止段错误。。。
        }
        cout << "Stu:: 拷贝赋值函数" << endl;
        //注意返回值是自身引用
        return *this;
    }
    //析构函数
    ~Stu()//会自动调用
    {
        cout << "Stu:: 析构函数" << endl;
    }

};
int main()
{
    Stu s1;//无参构造函数
    Stu s2("王一博",100,23);//有参构造函数
    Stu s3(s2); //拷贝构造函数====》将s2拷贝给s3==》Stu s3=s2


    s1=s3;//拷贝赋值
    return 0;
}

 2.匿名对象(用来初始化有名对象)课上代码

void fun(Stu g)//形参接收实参的值==》Stu g=Stu("lisa",120,25)
{

}
int main()
{
    Stu s1;//无参构造函数
    Stu s2("王一博",100,23);//有参构造函数
    Stu s3(s2); //拷贝构造函数====》将s2拷贝给s3==》Stu s3=s2

    //用到匿名对象给有名对象初始化
    Stu s4=Stu("赵云",88,40);
    //给数组初始化
    Stu s[2]={Stu("哪吒",200,40),Stu("张飞",300,30)};//和c中初始化相似
    //匿名对象作为函数实参使用
    fun(Stu("lisa",120,25));


    s1=s3;//拷贝赋值
    return 0;
}

 3.全局函数作友元,课上代码

//友元上课代码(全局函数作友元)
#include <iostream>
using namespace std;
//封装 房间类
class Room
{
    friend void goodfriend(Room &r);//关键字:friend
private://私有的
    string bedroom;//卧室
public://共有的
    string sittingroom;//客厅
public:
    Room()
    {
        //在类内赋值
        bedroom="卧室";
        sittingroom="客厅";
    }
};
//全局函数作友元
void goodfriend(Room &r)
{
    cout << "好朋友正在参观" << r.sittingroom << endl;//共有权限访问成功毋庸置疑
    cout << "好朋友正在参观" << r.bedroom << endl;//报错原因:私有权限类外不可访问,需要用友元
}

int main()
{
   Room r;
   goodfriend(r);
   return 0;
}

4.成员函数作友元,课上代码

//成员函数作友元
#include <iostream>
using namespace std;
class Room;//需要声明一下,以免下面用到系统不知道报错
//封装好朋友类
class goodfriend
{
private:
    Room *r;//用到指针是因为指针的大小是固定的,而变量的大小不明,系统无法分配空间
public:
    goodfriend();//在类内声明
    void vist();//在类内声明
};

//封装 房间类
class Room
{/*
    friend void goodfriend(Room &r);//关键字:friend*/
    friend void goodfriend::vist();
private://私有的
    string bedroom;//卧室
public://共有的
    string sittingroom;//客厅
public:
    Room()
    {
        //在类内赋值
        bedroom="卧室";
        sittingroom="客厅";
    }
};
//类外实现
goodfriend::goodfriend()
{
    r=new Room;//申请空间
}
void goodfriend::vist()
{
    cout  << "好朋友正在参观" << r->sittingroom << endl;//共有权限访问成功毋庸置疑
    cout << "好朋友正在参观" << r->bedroom << endl;//报错原因:私有权限类外不可访问,需要用友元
    //指针需要用->
}





全局函数作友元
//void goodfriend(Room &r)
//{
//    cout << "好朋友正在参观" << r.sittingroom << endl;//共有权限访问成功毋庸置疑
//    cout << "好朋友正在参观" << r.bedroom << endl;//报错原因:私有权限类外不可访问,需要用友元
//}

int main()
{
   Room r;
   //goodfriend(r);
   goodfriend g;
   g.vist();
   return 0;
}

 5.课前热身

int const a; //......
int const *p; //......
int * const p; //
int const * const p; //
const int fun() {}  //该函数是返回一个常整型变量

 6.常成员函数&&非常成员函数

1.非常对象可以调用常成员函数,也可以调用非常成员函数,优先调用非常成员函数

2.常对象只能调用常成员函数,如果没有常成员函数,则报错

#include <iostream>
using namespace std;
class Stu
{
private:
    string name;//只对该成员变量在常成员函数中可以被修改mutable
    int id;
public:
    Stu(){}
    Stu(string name,int id):name(name),id(id)
    {}
    //常成员函数
    void display()const //==》Stu const * const this;表示指针的值和指向都不可改
    {
        //this ->name ="li si";报错原因:因为被const修饰不可以更改
        cout << name << " " << id <<endl;
    }
    //非常成员函数
    void display()//==>Stu * const this;和常成员重载
    {
        this -> name ="lisi";
        cout << name << " " << id << endl;
    }

};

int main()
{
    cout << "Hello World!" << endl;
    const Stu s1("zhangsan",1001);//常对象
    s1.display();//常对象只能调用常成员函数
    return 0;
}

 7.成员函数来实现运算符重载

//运算符重载
#include<iostream>
using namespace std;

//构造类
class Club
{
private://私有的
    int age;
    int hight;
public:
    //无参
    Club(){}
    Club(int a,int h):age(a),hight(h)
    {

    }
    //成员函数实现算数运算符重载 加法
    const Club operator+(const Club &R)const//const 1:结果 2:右操作数 3:左操作数
    {
        //内部运算
        Club temp;
        temp.age=age+R.age;
        temp.hight=hight+R.hight;

        return temp;
    }
    //成员函数实现算数运算符重载 减法
    const Club operator-(const Club &R)const//const 1:结果 2:右操作数 3:左操作数
    {
        //内部运算
        Club temp;
        temp.age=age-R.age;
        temp.hight=hight-R.hight;

        return temp;
    }
    //成员函数实现算数运算符重载 乘法
    const Club operator*(const Club &R)const//const 1:结果 2:右操作数 3:左操作数
    {
        //内部运算
        Club temp;
        temp.age=age*R.age;
        temp.hight=hight*R.hight;

        return temp;
    }
    //成员函数实现算数运算符重载 除法
    const Club operator/(const Club &R)const//const 1:结果 2:右操作数 3:左操作数
    {
        //内部运算
        Club temp;
        temp.age=age/R.age;
        temp.hight=hight/R.hight;

        return temp;
    }
    //成员函数实现算数运算符重载 取余
    const Club operator%(const Club &R)const//const 1:结果 2:右操作数 3:左操作数
    {
        //内部运算
        Club temp;
        temp.age=age%R.age;
        temp.hight=hight%R.hight;

        return temp;
    }
关系运算符//
    //成员函数实现关系运算符重载 >
    bool operator>(const Club &R)const
    {
        if(age>R.age && hight>R.hight)
        {
            return true;
        }
        else
            return false;
    }
    //成员函数实现关系运算符重载 <
    bool operator<(const Club &R)const
    {
        if(age<R.age && hight<R.hight)
        {
            return true;
        }
        else
            return false;
    }
    //成员函数实现关系运算符重载 >=
    bool operator>=(const Club &R)const
    {
        if(age>=R.age && hight>=R.hight)
        {
            return true;
        }
        else
            return false;
    }
    //成员函数实现关系运算符重载 <=
    bool operator<=(const Club &R)const
    {
        if(age<=R.age && hight<=R.hight)
        {
            return true;
        }
        else
            return false;
    }
    //成员函数实现关系运算符重载 ==
    bool operator==(const Club &R)const
    {
        if(age==R.age && hight==R.hight)
        {
            return true;
        }
        else
            return false;
    }
 
    //成员函数实现关系运算符重载 >
    bool operator!=(const Club &R)const
    {
        if(age!=R.age && hight!=R.hight)
        {
            return true;
        }
        else
            return false;
    }
    //赋值运算符
    //成员函数实现赋值运算符重载 +=
   Club &operator+=(const Club &R)
    {
        age += R.age;
        hight += R.hight;
        return *this;//返回自身
    }
   //成员函数实现赋值运算符重载 -=
   Club &operator-=(const Club &R)
    {
        age -= R.age;
        hight -= R.hight;
        return *this;//返回自身
    }
   //成员函数实现赋值运算符重载 =
   Club &operator=(const Club &R)
    {
        age = R.age;
        hight = R.hight;
        return *this;//返回自身
    }
   //成员函数实现赋值运算符重载 *=
   Club &operator*=(const Club &R)
    {
        age *= R.age;
        hight *= R.hight;
        return *this;//返回自身
    }
   //成员函数实现赋值运算符重载 /=
   Club &operator/=(const Club &R)
    {
        age /= R.age;
        hight /= R.hight;
        return *this;//返回自身
    }
   //成员函数实现赋值运算符重载 %=
   Club &operator%=(const Club &R)
    {
        age %= R.age;
        hight %= R.hight;
        return *this;//返回自身
    }



    void show()
    {
        cout << "age= " << age << " " << "hight= " << hight << endl;
    }
};
int main()
{
    Club c1(10,10);
    Club c2(10,10);


    Club c3=c1+c2;
    Club c4=c1-c2;
    Club c5=c1*c1;
    Club c6=c1/c2;
    Club c7=c1%c2;

    cout << "算数运算符" << endl;
    c3.show();
    c4.show();
    c5.show();
    c6.show();
    c7.show();

    cout << "关系运算符" << endl;

    if(c3!=c1)
    {
        if(c3>c1)
        {
            cout << "c3>c1 " << endl;
        }
        else if(c3<c1)
        {
            cout << "c3<c1 " << endl;
        }
        cout << "c3!=c1" << endl;

    }
    else if(c3 == c1)
    {

         if(c3>=c1)
        {
            cout << "c3>=c1" << endl;
        }
        else if(c3<=c1)
        {
            cout << "c3<=c1" << endl;
        }

        {
            cout << "c3==c1" << endl;

        }

    }

    cout << "赋值运算符" << endl;//赋值函数不要加类型否则属于初始化会报错
    c3+=c2;
    c4-=c2;
    c5*=c1;
    c6/=c2;
    c7%=c2;

    c3.show();
    c4.show();
    c5.show();
    c6.show();
    c7.show();


    return  0;
}

效果图:

 8.全局函数实现运算符重载

//运算符重载
#include<iostream>
using namespace std;

//构造类
class Club
{
    friend const Club operator+(const Club &L,const Club &R);
    friend const Club operator-(const Club &L,const Club &R);
    friend const Club operator*(const Club &L,const Club &R);
    friend const Club operator/(const Club &L,const Club &R);
    friend const Club operator%(const Club &L,const Club &R);
    friend bool operator>(const Club &L,const Club &R);
    friend bool operator<(const Club &L,const Club &R);
    friend bool operator<=(const Club &L,const Club &R);
    friend bool operator>=(const Club &L,const Club &R);
    friend bool operator==(const Club &L,const Club &R);
    friend bool operator!=(const Club &L,const Club &R);
    friend Club &operator+=( Club &L,const Club &R);
    friend Club &operator-=( Club &L,const Club &R);
   
    friend Club &operator*=( Club &L,const Club &R);
    friend Club &operator/=(Club &L,const Club &R);
    friend Club &operator%=( Club &L,const Club &R);
    
private://私有的
    int age;
    int hight;
public:
    //无参
    Club(){}
    Club(int a,int h):age(a),hight(h)
    {
        
    }
    
    void show()
    {
        cout << "age= " << age << " " << "hight= " << hight << endl;
    }
};
//成员函数实现算数运算符重载 加法
const Club operator+(const Club &L,const Club &R)//const 1:结果 2:右操作数 3:左操作数
{
    //内部运算
    Club temp;
    temp.age=L.age-R.age;
    temp.hight=L.hight-R.hight;

    return temp;
}
   
//成员函数实现算数运算符重载 减法
const Club operator-(const Club &L,const Club &R)//const 1:结果 2:右操作数 3:左操作数
{
    //内部运算
    Club temp;
    temp.age=L.age-R.age;
    temp.hight=L.hight-R.hight;

    return temp;
}

//成员函数实现算数运算符重载 乘法
const Club operator*(const Club &L,const Club &R)//const 1:结果 2:右操作数 3:左操作数
{
    //内部运算
    Club temp;
    temp.age=L.age*R.age;
    temp.hight=L.hight*R.hight;

    return temp;
}
//成员函数实现算数运算符重载 除法
const Club operator/(const Club &L,const Club &R)//const 1:结果 2:右操作数 3:左操作数
{
    //内部运算
    Club temp;
    temp.age=L.age/R.age;
    temp.hight=L.hight/R.hight;

    return temp;
}
//成员函数实现算数运算符重载 取余
const Club operator%(const Club &L,const Club &R)//const 1:结果 2:右操作数 3:左操作数
{
    //内部运算
    Club temp;
    temp.age=L.age%R.age;
    temp.hight=L.hight%R.hight;

    return temp;
}
关系运算符//
//成员函数实现关系运算符重载 >
bool operator>(const Club &L,const Club &R)
{
    if(L.age>R.age && L.hight>R.hight)
    {
        return true;
    }
    else
        return false;
}
//成员函数实现关系运算符重载 <
bool operator<(const Club &L,const Club &R)
{
    if(L.age>R.age && L.hight<R.hight)
    {
        return true;
    }
    else
        return false;
}
//成员函数实现关系运算符重载 >=
bool operator>=(const Club &L,const Club &R)
{
    if(L.age>=R.age && L.hight>R.hight)
    {
        return true;
    }
    else
        return false;
}
//成员函数实现关系运算符重载 <=
bool operator<=(const Club &L,const Club &R)
{
    if(L.age<=R.age && L.hight>R.hight)
    {
        return true;
    }
    else
        return false;
}
//成员函数实现关系运算符重载 ==
bool operator==(const Club &L,const Club &R)
{
    if(L.age==R.age && L.hight==R.hight)
    {
        return true;
    }
    else
        return false;
}

//成员函数实现关系运算符重载 !=
bool operator!=(const Club &L,const Club &R)
{
    if(L.age!=R.age && L.hight>R.hight)
    {
        return true;
    }
    else
        return false;
}
//赋值运算符
//成员函数实现赋值运算符重载 +=
Club &operator+=( Club &L,const Club &R)
{
    L.age += R.age;
    L.hight += R.hight;
    return L;//返回自身
}
//成员函数实现赋值运算符重载 -=
Club &operator-=( Club &L,const Club &R)
{
    L.age -= R.age;
    L.hight -= R.hight;
    return L;//返回自身
}

//成员函数实现赋值运算符重载 *=
Club &operator*=( Club &L,const Club &R)
{
    L.age *= R.age;
    L.hight *= R.hight;
    return L;//返回自身
}
//成员函数实现赋值运算符重载 /=
Club &operator/=( Club &L,const Club &R)
{
    L.age /= R.age;
    L.hight /= R.hight;
    return L;//返回自身
}
//成员函数实现赋值运算符重载 %=
Club &operator%=(Club &L,const Club &R)
{
    L.age /= R.age;
    L.hight /= R.hight;
    return L;//返回自身
}


int main()
{
    Club c1(10,10);
    Club c2(10,10);


    Club c3=c1+c2;
    Club c4=c1-c2;
    Club c5=c1*c1;
    Club c6=c1/c2;
    Club c7=c1%c2;

    cout << "算数运算符" << endl;
    c3.show();
    c4.show();
    c5.show();
    c6.show();
    c7.show();

    cout << "关系运算符" << endl;

    if(c3!=c1)
    {
        if(c3>c1)
        {
            cout << "c3>c1 " << endl;
        }
        else if(c3<c1)
        {
            cout << "c3<c1 " << endl;
        }
        cout << "c3!=c1" << endl;

    }
    else if(c3 == c1)
    {

         if(c3>=c1)
        {
            cout << "c3>=c1" << endl;
        }
        else if(c3<=c1)
        {
            cout << "c3<=c1" << endl;
        }

        {
            cout << "c3==c1" << endl;

        }

    }

    cout << "赋值运算符" << endl;//赋值函数不要加类型否则属于初始化会报错
    c3+=c2;
    c4-=c2;
    c5*=c1;
    c6/=c2;
    c7%=c2;

    c3.show();
    c4.show();
    c5.show();
    c6.show();
    c7.show();


    return  0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值