刘华世的官方博客

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;
}
posted @ 2012-11-09 17:00  pythonschool  阅读(260)  评论(0)    收藏  举报
刘华世的官方博客