C++基础系列:类成员初始化(setter和getter)

本文介绍了C++中Phone类的两种初始化方式:内部实现的构造函数和外部定义的setter/getter方法。通过实例展示了如何在类内部直接赋值和通过getter/setter进行外部操作,并总结了私有成员变量初始化的不同途径。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1 类内部实现初始化

#include <iostream>
#include <string>

using namespace std;

/**
 * Class setter, getter. 
 * @author xindaqi
 * @since 2020-10-24
 * */

class Phone {
    private:
      string os;
      string brand; 
    
    public:
      Phone(string os, string brand) {
          this->os = os;
          this->brand = brand;
      }
      void setOs(string os) {
          this->os = os;
      }
      string getOs() {
          return os;
      }

      void setBrand(string brand) {
          this->brand = brand;
      }
      string getBrand() {
          return brand;
      }
};

int main() {
    Phone *phone = new Phone("ios", "iphone");

    cout<< "Phone OS: " << phone -> getOs() <<endl;
    cout<< "Phone brand: " << phone -> getBrand() <<endl;

    cout<<"============="<<endl;

    phone -> setOs("iOS");
    phone -> setBrand("iPhone");

    cout<< "Phone OS: " << phone -> getOs() <<endl;
    cout<< "Phone brand: " << phone -> getBrand() <<endl;
    return 0;
}
  • 结果
Phone OS: ios
Phone brand: iphone
=============
Phone OS: iOS
Phone brand: iPhone

2 类外部实现初始化

#include <iostream>
#include <string>

using namespace std; 

/**
 * Class setter, getter. 
 * @author xindaqi
 * @since 2020-10-24
 * */

class Phone {
    private:
      string os;
      string brand;
    public:
      Phone(string os, string brand);
      void setOs(string os);
      string getOs();
      void setBrand(string brand);
      string getBrand();
};

Phone::Phone(string os, string brand) {
    this->os = os;
    this->brand = brand;
}

void Phone::setOs(string os) {
    this->os = os;
}
string Phone::getOs() {
    return os;
}

void Phone::setBrand(string brand) {
    this->brand = brand;
}
string Phone::getBrand() {
    return brand;
}

int main(){
    Phone *phone1 = new Phone("iOS", "iPhone");

    cout<<"Phone OS: " << phone1->getOs() << endl;
    cout<< "Phone brand: " << phone1->getBrand() << endl;
    
    cout<<"============="<<endl;
    
    phone1->setOs("iOS");
    phone1->setBrand("iPhone");

    cout<<"Phone OS: " << phone1->getOs() << endl;
    cout<< "Phone brand: " << phone1->getBrand() << endl;

    cout<<"============="<<endl;

    Phone phone2("iOS", "iPhone");
    cout<<"Phone OS: " << phone2.getOs() << endl;
    cout<< "Phone brand: " << phone2.getBrand() << endl;

    return 0;
}
  • 结果
Phone OS: iOS
Phone brand: iPhone
=============
Phone OS: iOS
Phone brand: iPhone
=============
Phone OS: iOS
Phone brand: iPhone

3 小结

  • 类私有成员赋值使用getter和setter方法有两种实现方式:类内部直接实现函数,类外部实现函数.
  • 类私有成员变量初始化,可使用析构函数也可以在类内部初始化.

[参考文献]
[1]http://c.biancheng.net/view/2221.html
[2]https://blog.csdn.net/qq78442761/article/details/80797561
[3]https://blog.csdn.net/hhyvs111/article/details/78786249

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天然玩家

坚持才能做到极致

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值