ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

C++(类与对象2)

2022-04-27 12:33:06  阅读:126  来源: 互联网

标签:对象 void C++ int Init Test include Display


(1)内联成员函数

(2)成员函数地重载及其缺参数

(3)类与结构体

(4)隐含地this指针

1、内联成员函数

  ¥回顾:

    内联函数---------提高效率

    编译的时候将代码直接嵌入到调用的地方,从而减少了函数调用的开销。(程序体积增大,以空间换时间)

    内联函数一般短小,内联函数仅仅只是给编译器一个提示。如果函数中有switch,for,可能不会以内联函数解析。

  ¥类的内联函数一般有两种方法:

      1、在类的实现(.cpp)中加inline关键字,如一个加法内联函数:

    

inline int Test::Add(int a, int b)
{
    return a + b;

}

    2、在类的声明(.h)中完成函数的定义,如一个加法内联函数:

#ifndef _TEST_H_
#define _TEST_H_

class Test
{
public:
    int Add(int a,int b)
    {
        return a + b;

    }

};

#endif //_TEST_H_

2、成员函数的重载及其缺省参数

   ¥成员函数的重载例子:

     

//.h

#ifndef _TEST_H_
#define _TEST_H_

class Test
{
public:
    void Init();
    void Init(int x);
    void Init(int x,int y);
    void Init(int x,int y,int z);
    void Display();

private:
    int x_;
    int y_;
    int z_;
};

#endif //_TEST_H_

//.cpp

#include "Test.h"
#include <iostream>

using namespace std;

void Test::Init()
{
    x_ = 0;
    y_ = 0;
    z_ = 0;

}
void Test::Init(int x)
{
    x_ = x;
    y_ = 0;
    z_ = 0;

}
void Test::Init(int x, int y)
{
    x_ = x;
    y_ = y;
    z_ = 0;

}

void Test::Init(int x, int y, int z)
{
    x_ = x;
    y_ = y;
    z_ = z;

}

void Test::Display()
{
    cout << "x = " << x_ << " y = " << y_ << " z = " << z_ << endl;
}

//测试文件

#include <iostream>
#include <cstdio>
#include"Test.h"

using namespace std;

int main(void)
{
    Test t;
    t.Init();
    t.Display();
    t.Init(10);
    t.Display();

    return 0;
}

/*
输出:
        x = 0 y = 0 z = 0
        x = 10 y = 0 z = 0

*/

¥缺省参数

//.h

#ifndef _TEST_H_
#define _TEST_H_

class Test
{
public:
    void Init(int x = 0, int y = 0, int z = 0);
    void Display();

private:
    int x_;
    int y_;
    int z_;
};

#endif //_TEST_H_

//.cpp

#include "Test.h"
#include <iostream>

using namespace std;

void Test::Init(int x, int y, int z)
{
    x_ = x;
    y_ = y;
    z_ = z;

}
void Test::Display()
{
    cout << "x = " << x_ << " y = " << y_ << " z = " << z_ << endl;
}

//测试代码:

#include <iostream>
#include <cstdio>
#include"Test.h"

using namespace std;

int main(void)
{
    Test t;
t.Init(); t.Display();
t.Init(10); t.Display(); return 0; } /* 输出: x = 0 y = 0 z = 0 x = 10 y = 0 z = 0 */

3、类与结构体

¥补充:在C++中结构体也可以看成一种类,区别如下。C++中结构体可以有成员函数,在C中结构体不能有成员函数。

¥class与struct的区别:在未指定访问权限时,class默认的是私有,struct默认是公有。

#include <iostream>
using namespace std;

struct Test2
{
int x_;
int y_;
int z_;
void Init(int x, int y, int z)
{
x_ = x;
y_ = y;
z_ = z;
}
void Display()
{
cout << "x = " << x_ << " y = " << y_ << " z = " << z_ << endl;
}

};
class Test3
{
int x_;
int y_;
int z_;
void Init(int x, int y, int z)
{
x_ = x;
y_ = y;
z_ = z;
}
void Display()
{
cout << "x = " << x_ << " y = " << y_ << " z = " << z_ << endl;
}


};
int main(void)
{
//Test2 t;
//t.Init(10,20,30);

//结构体、类可以还这样初始化:
/*Test2 t2 = {10,20,30};
t2.Display();*/

/*Test3 t3;
t3.Init(10,20,30);//Error,默认私有
t3.Disply();*/

return 0;
}

 

4、隐含的this指针

  ¥成员函数有一个隐含的附加形参,即指向该对象的指针,这个隐含的形参叫做this指针。

  ¥使用this指针保证了每个对象可以拥有不同的数据成员,但处理这些成员的代码可以被所有对象共享。

 

标签:对象,void,C++,int,Init,Test,include,Display
来源: https://www.cnblogs.com/fxdd/p/16198363.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有