• C++类与对象二


    目录

    一、类的嵌套 

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

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

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

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

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

    三、成员函数重载

     四、this指针


    一、类的嵌套 

    1. #include
    2. using namespace std;
    3. class CC1
    4. {
    5. public:
    6. int x;
    7. void Func();
    8. class CC2
    9. {
    10. public:
    11. int x;
    12. void Func();
    13. }obc;
    14. };
    15. void CC1::Func()
    16. {
    17. x = 3000;
    18. cout << "x=" << x << endl;
    19. }
    20. void CC1 :: CC2::Func()
    21. {
    22. x = 4000;
    23. cout << "x=" << x << endl;
    24. }
    25. int main()
    26. {
    27. CC1 obj;
    28. obj.Func();
    29. obj.obc.Func();
    30. cout << endl;
    31. cout << "2:x=" << obj.x << endl;
    32. cout << "2:x=" << obj.obc.x << endl;
    33. return 0;
    34. }

     

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

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

    1. #include
    2. using namespace std;
    3. //通过仅有函数为私有数据成员赋值
    4. class CTest {
    5. int x, y;
    6. public:
    7. void setxy(int a, int b)
    8. {
    9. x = a;
    10. y = b;
    11. }
    12. void dispxy()
    13. {
    14. cout << "x=" << x << ",y=" << y << endl;
    15. }
    16. };
    17. int main()
    18. {
    19. CTest obj1;
    20. obj1.setxy(1, 2);
    21. obj1.dispxy();
    22. }

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

    1. #include
    2. using namespace std;
    3. //利用指针访问私有数据成员
    4. class CTest {
    5. int x, y;
    6. public:
    7. void setxy(int a, int b){
    8. x = a;
    9. y = b;
    10. }
    11. void printxy() {
    12. cout << "x=" << x << ",y=" << y << endl;
    13. }
    14. void getxy(int* px, int* py) { //提取x y的值
    15. *px = x;
    16. *py = y;
    17. }
    18. };
    19. int main()
    20. {
    21. CTest obj;
    22. obj.setxy(100, 200);
    23. obj.printxy();
    24. int m, n;
    25. obj.getxy(&m, &n);
    26. cout << "m=" << m << ",n=" << n << endl;
    27. }

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

    1. #include
    2. using namespace std;
    3. //利用函数访问私有数据成员
    4. class CTest {
    5. int x, y;
    6. public:
    7. void setxy(int a, int b){
    8. x = a;
    9. y = b;
    10. }
    11. void printxy() {
    12. cout << "x=" << x << ",y=" << y << endl;
    13. }
    14. int getx() { return x; }
    15. int gety() { return y; }
    16. };
    17. int main()
    18. {
    19. CTest obj;
    20. obj.setxy(100, 200);
    21. obj.printxy();
    22. cout << "x=" << obj.getx() << endl;
    23. cout << "y=" << obj.gety() << endl;
    24. }

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

    1. #include
    2. using namespace std;
    3. //利用引用访问私有数据成员
    4. class CTest {
    5. int x, y;
    6. public:
    7. void setxy(int a, int b){
    8. x = a;
    9. y = b;
    10. }
    11. void printxy() {
    12. cout << "x=" << x << ",y=" << y << endl;
    13. }
    14. void getxy(int& px, int& py) {
    15. px = x;
    16. py = y;
    17. }
    18. };
    19. int main()
    20. {
    21. CTest obj;
    22. obj.setxy(100, 200);
    23. obj.printxy();
    24. int m, n;
    25. obj.getxy(m, n);
    26. cout << "m=" << m << endl;
    27. cout << "n=" << n << endl;
    28. }

    三、成员函数重载

    1. #include
    2. using namespace std;
    3. class CTest
    4. {
    5. int x, y;
    6. int m, n;
    7. public:
    8. void setxy(int a, int b) {
    9. x = a;
    10. y = b;
    11. }
    12. void setxy(int a, int b, int c, int d) {
    13. x = a;
    14. y = b;
    15. m = c;
    16. n = d;
    17. }
    18. void dispxy(int x) {
    19. cout << x << "," << y << endl;
    20. }
    21. void dispxymn() {
    22. cout << x << "," << y << "," << m << "," << n << endl;
    23. }
    24. };
    25. int main()
    26. {
    27. CTest obj1, obj2;
    28. obj1.setxy(10, 20);
    29. obj2.setxy(10, 20, 30, 40);
    30. obj1.dispxy(666);
    31. obj2.dispxymn();
    32. return 0;
    33. }

    1. #include
    2. using namespace std;
    3. class CTest
    4. {
    5. int x, y;
    6. public:
    7. void setxy(int a, int b) {
    8. x = a;
    9. y = b;
    10. }
    11. void dispxy()
    12. {
    13. cout << "x=" << x << "y=" <
    14. }
    15. int sum()
    16. {
    17. return x + y;
    18. }
    19. };
    20. int main()
    21. {
    22. CTest obj1, obj2;//定义对象
    23. CTest* pobj; //对象类的指针(对象指针)
    24. pobj = &obj1;
    25. pobj->setxy(3, 4);
    26. pobj->dispxy();
    27. cout << "x+y=" << pobj->sum() << endl;
    28. return 0;
    29. }

     四、this指针

    1. #include
    2. using namespace std;
    3. class CTest
    4. {
    5. private:
    6. int x;
    7. public:
    8. int getx() const {
    9. return x;
    10. }
    11. void setx(int x) {
    12. this->x=x;
    13. cout << "this指针存储的内存地址为:" << this << endl;
    14. }
    15. };
    16. int main()
    17. {
    18. CTest obj;
    19. obj.setx(888);
    20. cout << "对象obj在内存的地址为:" << &obj << endl;
    21. cout << "对象obj所保存的值为:" << obj.getx() << endl;
    22. return 0;
    23. }

  • 相关阅读:
    抽象工厂模式:构建复杂的对象族
    自动化测试的生命周期是什么?
    小白零基础自学Java,究竟如何才能学的透彻!
    odoo javascript参考(七)
    java 并发执行批量异步任务(Future、 CompletableFuture 实现)
    HTTPS的加密方式超详细解读
    【关于Linux中----进程优先级、环境变量和进程地址空间】
    HelloWorld显示Go语言交叉编译的强大20230926
    MQ系列6:消息的消费
    查看目录和文件大小: du -sh
  • 原文地址:https://blog.csdn.net/qq_58631644/article/details/136724260