提高函数体中的成员函数对数据成员赋值
#include<iostream> #include<string> #define _CRT_SECURE_NO_WARNINGS using namespace std; struct MM { char name[20]; int age; void print() { cout << name << "\t" << age<<endl; } void printdata(); void setdata(char *mmName, int mmAge)//成员函数实现对数据成员的赋值 { strcpy_s(name, mmName); age = mmAge; } }; void MM::printdata() { cout << name << "\t" << age << endl; } int main() { MM mm={ "哈哈",20 }; MM baby= { "baby", 18 }; mm.print(); MM *pmm = &mm; pmm->print(); baby.printdata(); MM *pbaby = &baby; pbaby->printdata(); MM boy; boy.setdata("宝宝", 12); boy.print(); while (1); return 0; }
#笔经#