如题:2021年4月
分析
创建一个类。类外构造函数的初始化,其他成员函数功能。还是比较侧重c++语法
解决
#include<iostream>
#include<cstring>
using namespace std;
class Employee
{
public:
Employee(char *name, char *add, char *shi, char *sheng, int num);
void ChangeName(char *name);
void Display();
protected:
char e_name[21], address[30], e_shi[10], e_sheng[10];
int emial_num;
};
Employee::Employee(char *name, char *add, char *shi, char *sheng, int num)
{
emial_num = num;
strcpy(e_name, name);
strcpy(address, add);
strcpy(e_shi, shi);
strcpy(e_sheng, sheng);
}
void Employee::ChangeName(char *name)
{
strcpy(e_name, name);
}
void Employee::Display()
{
cout << "the emial addr:" << emial_num << endl;
cout << "name is :" << e_name << endl;
cout << "add is:" << address << endl;
cout << "shi is:" << e_shi << endl;
cout << "sheng is:" << e_sheng << endl;
}
int main(int argc, const char **argv)
{
Employee s1("小明", "丁豪", "济南", "山东", 1234);
s1.Display();
return 0;
}
如题:2020年10月
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-3dzzETt2-1630637190112)(9-1-1.jpg)]
分析
继承竟然想到的是extend???但c++中没有用这个关键字,就是用:可见对真正的程序编写还是有挺生的。还是要多看,多写写才行。
解决
#include <iostream>
#include <cstring>
using namespace std;
class school
{
protected:
int Number;
char Name[20];
public:
school(int n = 0, char const *str = "")
{
Number = n;
strcpy(Name, str);
}
};
class Student : public school //继承为什么会想到extend呢?
{
public:
Student(int n = 0, char const *s = "", char const *cs = "", double sort = 0.0): school(n, s)
{
strcpy(Class_Name, cs);
Total = sort;
}
void Display()
{
cout << Number << " " << Name << " " << Class_Name << " " << Total << endl;
}
private:
double Total;
char Class_Name[20];
};
int main(int argc, const char **argv)
{
Student s1(2020150601, "李四", "四班", 678);
s1.Display();
return 0;
}