C++语言 重载”=”运算符

//#C++语言 重载"="运算符

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

class CStudent
{
private:
    char m_Name[10]; //学生姓名
    int m_Age; //年龄
    double m_Height; //身高
public:
    CStudent(){};
    //通过构造函数初始化类数据
    CStudent(char *name, int age, double height);
    void display();
    void operator =(CStudent &student);
};
CStudent::CStudent(char *name, int age, double height)
{
    strcpy(m_Name, name);
    m_Age = age;
    m_Height = height;
}
void CStudent::display()
{
    cout << "Name:" << m_Name << endl;
    cout << "Age:" << m_Age << endl;
    cout << "Height:" << m_Height << endl;
}
void CStudent::operator = (CStudent &student)
{
    strcpy(m_Name, student.m_Name);
    m_Age = student.m_Age;
    m_Height = student.m_Height;
}

int main(int argc, char * argv[])
{
    CStudent student1("格格", 18, 160);
    student1.display();
    CStudent student2;
    student2 = student1;
    student2.display();
    return 0;
}

原文链接: https://www.cnblogs.com/pythonschool/archive/2012/11/09/2762942.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/68722

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月9日 下午1:27
下一篇 2023年2月9日 下午1:28

相关推荐