C++类与对象二

目录

一、类的嵌套

二、对象引用私有数据成员

通过公有函数为私有成员赋值

利用指针访问私有数据成员

利用函数访问私有数据成员

利用引用访问私有数据成员

三、成员函数重载

四、this指针


一、类的嵌套

cpp 复制代码
#include <iostream>
using namespace std;

class CC1
{
public:
    int x;
    void Func();
    
    class CC2 
    {
    public:
        int x;
        void Func();
    }obc;
};

void CC1::Func()
{
    x = 3000;
    cout << "x=" << x << endl;
}

void CC1 :: CC2::Func()
{
    x = 4000;
    cout << "x=" << x << endl;
}

int main()
{ 
    CC1 obj;
    obj.Func();
    obj.obc.Func();

    cout << endl;
    cout << "2:x=" << obj.x << endl;
    cout << "2:x=" << obj.obc.x << endl;

    return 0;
}

二、对象引用私有数据成员

通过公有函数为私有成员赋值

cpp 复制代码
#include <iostream>
using namespace std;

//通过仅有函数为私有数据成员赋值
class CTest {
    int x, y;
public:
    void setxy(int a, int b)
    {
        x = a;
        y = b;
    }
    void dispxy()
    {
        cout << "x=" << x << ",y=" << y << endl;
    }
};

int main()
{
    CTest obj1;
    obj1.setxy(1, 2);
    obj1.dispxy();
}

利用指针访问私有数据成员

cpp 复制代码
#include <iostream>
using namespace std;

//利用指针访问私有数据成员
class CTest {
    int x, y;
public:
    void setxy(int a, int b){
        x = a;
        y = b;
    }

    void printxy() {
        cout << "x=" << x << ",y=" << y << endl;
    }

    void getxy(int* px, int* py) {   //提取x y的值
        *px = x;
        *py = y;
    }

};

int main()
{
    CTest obj;
    obj.setxy(100, 200);
    obj.printxy();

    int m, n;
    obj.getxy(&m, &n);
    cout << "m=" << m << ",n=" << n << endl;
}

利用函数访问私有数据成员

cpp 复制代码
#include <iostream>
using namespace std;

//利用函数访问私有数据成员
class CTest {
    int x, y;
public:
    void setxy(int a, int b){
        x = a;
        y = b;
    }

    void printxy() {
        cout << "x=" << x << ",y=" << y << endl;
    }

    int getx() { return x; }
    int gety() { return y; }

};

int main()
{
    CTest obj;
    obj.setxy(100, 200);
    obj.printxy();
    cout << "x=" << obj.getx() << endl;
    cout << "y=" << obj.gety() << endl;

}

利用引用访问私有数据成员

cpp 复制代码
#include <iostream>
using namespace std;

//利用引用访问私有数据成员
class CTest {
    int x, y;
public:
    void setxy(int a, int b){
        x = a;
        y = b;
    }

    void printxy() {
        cout << "x=" << x << ",y=" << y << endl;
    }

    void getxy(int& px, int& py) {
        px = x;
        py = y;
    }

};

int main()
{
    CTest obj;
    obj.setxy(100, 200);
    obj.printxy();
   
    int m, n;
    obj.getxy(m, n);
    cout << "m=" << m << endl;
    cout << "n=" << n << endl;
}

三、成员函数重载

cpp 复制代码
#include <iostream>
using namespace std;

class CTest
{
    int x, y;
    int m, n;
public:
    void setxy(int a, int b) {
        x = a;
        y = b;
    }
    void setxy(int a, int b, int c, int d) {
        x = a;
        y = b;
        m = c;
        n = d;
    }

    void dispxy(int x) {
        cout << x << "," << y << endl;
    }
    void dispxymn() {
        cout << x << "," << y << "," << m << "," << n << endl;
    }
    
};
int main()
{
    CTest obj1, obj2;

    obj1.setxy(10, 20);
    obj2.setxy(10, 20, 30, 40);

    obj1.dispxy(666);
    obj2.dispxymn();
    return 0;
}
cpp 复制代码
#include <iostream>
using namespace std;

class CTest
{
    int x, y;

public:
    void setxy(int a, int b) {
        x = a;
        y = b;
    }
    void dispxy()
    {
        cout << "x=" << x << "y=" <<y<< endl;
    }
    int sum()
    {
        return x + y;
    }

};
int main()
{
    CTest obj1, obj2;//定义对象
    CTest* pobj;     //对象类的指针(对象指针)
    pobj = &obj1;
    pobj->setxy(3, 4);
    pobj->dispxy();
    cout << "x+y=" << pobj->sum() << endl;
    return 0;
}

四、this指针

cpp 复制代码
#include <iostream>
using namespace std;

class CTest
{
private:
    int x;
public:
    int getx() const {
        return x;
    }
    void setx(int x) {
        this->x=x;
        cout << "this指针存储的内存地址为:" << this << endl;
    }
};
int main()
{
    CTest obj;
    obj.setx(888);
    cout << "对象obj在内存的地址为:" << &obj << endl;
    cout << "对象obj所保存的值为:" << obj.getx() << endl;

    return 0;
}
相关推荐
憧憬成为原神糕手7 分钟前
c++_list
开发语言·c++
zyh200504309 分钟前
c++的decltype关键字
c++·decltype
idealzouhu10 分钟前
Java 并发编程 —— AQS 抽象队列同步器
java·开发语言
爱吃油淋鸡的莫何10 分钟前
Conda新建python虚拟环境问题
开发语言·python·conda
闲人编程17 分钟前
Python实现日志采集功能
开发语言·python·fluentd·filebeat·日志采集
Sol-itude24 分钟前
关于MATLAB计算3维图的向量夹角总是不正确的问题记录
开发语言·matlab·问题解决·向量
2401_8628867826 分钟前
蓝禾,汤臣倍健,三七互娱,得物,顺丰,快手,游卡,oppo,康冠科技,途游游戏,埃科光电25秋招内推
前端·c++·python·算法·游戏
奔驰的小野码36 分钟前
java通过org.eclipse.milo实现OPCUA客户端进行连接和订阅
java·开发语言
小川_wenxun1 小时前
优先级队列(堆)
java·开发语言·算法
惜缘若水1 小时前
【SpinalHDL】Scala编程之伴生对象
开发语言·scala·spinalhdl