栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

【C++碎碎念】面向对象(常对象和常函数、友元、运算符重载)

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【C++碎碎念】面向对象(常对象和常函数、友元、运算符重载)

目录

一、常对象和常函数

二、友元

 三、运算符重载


一、常对象和常函数

1、用const修饰成员函数,const修饰this指针指向的内存区域,成员函数体内不能修改本类中的任何普通成员变量;

2、当成员变量类型前面被mutable修饰时是可以被const修饰的成员函数所修改的。

class Person{
public:
    int m_Age;
    mutable int m_Height;

    Person(int age)
    {
        this->m_Age = age;
    }
    
    void show() const   //常函数
    {
        this->m_Height = 100;
        cout<< this->m_Age < 

二、友元

友元的作用:在类外访问一个类的私有成员。

友元有三种

        1、友元(全局)函数

        2、友元类

        3、友元成员函数

注意事项:

        1、友元关系不能被继承;

        2、友元关系是单向的,类A是类B的好朋友,但类B不是类A的好朋友;

        3、友元关系不具有传递性,类B是类A的好朋友,类C是类B的好朋友,但类A不是类C的好朋友。 

class Buliding{

    //让全局函数visit作为本类的Building的好朋友,就可以访问私有成员
    friend void visit(Building &building);

public:
    string m_SittingRoom;//客厅

    Building()
    {
        m_SittingRoom = "客厅";
        m_BedRoom = "卧室";
    }
private:
    string m_BedRoom;  //卧室
};

void visit(Building& building)//全局函数
{
    cout<<"正在访问:"< 

 三、运算符重载

运算符重载是带有特殊名称的函数,函数名是由关键字operator和其后要重载的运算符符号构成。与其他函数一样,重载运算符有一个返回类型和参数列表。

#include 
using namespace std;
class Complex
{
    public:
    double real, imag;
    Complex(double r = 0.0, double i = 0.0) : real(r), imag(i) { }
    Complex operator - (const Complex & c);
};
Complex operator + (const Complex & a, const Complex & b)
{
    
    return Complex(a.real + b.real, a.imag + b.imag); 
}
Complex Complex::operator - (const Complex & c)
{
    //返回一个临时对象,切记,一定要加Complex,否则会报错
    return Complex(real - c.real, imag - c.imag); //返回一个临时对象
}
int main()
{
    Complex a(4, 4), b(1, 1), c;
    c = a + b; //等价于 c = operator + (a,b);
    cout << c.real << "," << c.imag << endl;
    cout << (a - b).real << "," << (a - b).imag << endl; //a-b等价于a.operator - (b)
    return 0;
}

1、指针运算符重载

 智能指针:智能指针的行为类似常规指针,重要的区别是它负责自动释放所指向的对象。

#include 
using namespace std;
class Person{
public:
    int m_Age;

    Person(int age){
        cout<<"Person的构造函数"<(){   //指向她的成员,意思是要返回她的成员
        return this->m_Person;
    }

    Person& operator*(){    //指向她的地址,返回她的解引用
        return *m_Person;
    }

    SmartPoint(Person* p)
    {
        m_Person = p;
    }

    ~SmartPoint()
    {
        cout<<"SmartPoint析构函数"<m_Person != NULL)
        {
            delete this->m_Person;
            this->m_Person = NULL;
        }
    }
};
int main()
{
        //创建Person的对象
        Person* P=new Person(30);//在堆上开辟了一个值为30的指针变量,把指针地址保存到栈上
        P->showAge();
        (*P).showAge();//跟上面的表达意思是一样的

        //delete P;
        cout<<"**************"<showAge(); //sp.operator->();

        (*sp).showAge();//跟上面的这行表达意思是一样的
        return 0;
}

 2、函数调用运算符重载

#include 
using namespace stdl;
class MyPrint{
public:
    //重载()运算符
    void operator()(string text)
    {
        cout<

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/892411.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号