#include<iostream>
#include<string>
#include<fstream>
using namespace std;
class citizen
{
private:
char name[20];
char sex;
int age;
public:
citizen();
citizen(char name[], char sex, int age);
virtual void show();
~citizen()
{
ofstream myout;
myout.open("a.txt", ios::out | ios::app);
cout << "this is the first destruction function!" << endl;
myout << "this is the first destruction function!" << endl;
}
};
class student :public citizen
{
private:
char number[20];
char classname[20];
public:
student(char name[], char sex, int age, char number[], char classname[]) :citizen(name, sex, age)
{
strcpy_s(this->number, number);
strcpy_s(this->classname, classname);
}
void show();
~student()
{
ofstream myout;
myout.open("a.txt", ios::out | ios::app);
cout << "this is the second destruction function!" << endl;
myout << "this is the second destruction function!" << endl;
}
};
citizen::citizen(char name[], char sex, int age)
{
strcpy_s(this->name, name);
this->sex = sex;
this->age = age;
}
void citizen::show()
{
ofstream myout;
myout.open("a.txt", ios::out | ios::app);
cout << "姓名为:" << name << endl;
cout << "性别为:" << sex << endl;
cout << "年龄为:" << age << endl;
myout << "姓名为:" << name << endl;
myout << "性别为:" << sex << endl;
myout << "年龄为:" << age << endl;
}
void student::show()
{
ofstream myout;
myout.open("a.txt", ios::out | ios::app);
citizen::show();
cout << "学号为:" << number << endl;
cout << "班级为:" << classname << endl;
myout << "学号为:" << number << endl;
myout << "班级为:" << classname << endl;
}
int main()
{
ofstream myout;
myout.open("a.txt", ios::out | ios::app);
char name[20], number[20], classname[20];
char sex;
int age;
citizen *c;
cout << "请输入姓名,性别(男:m,女:f),年龄" << endl;
myout << "请输入姓名,性别(男:m,女:f),年龄" << endl;
cin >> name >> sex >> age;
myout << name << '\t' << sex << '\t' << age << endl;
c=&citizen(name, sex, age);
cout << "公民类的输出:" << endl;
myout << "公民类的输出:" << endl;
c->show();
cout << "请输入学号,班级" << endl;
myout << "请输入学号,班级" << endl;
cin >> number >> classname;
myout << number << '\t' << classname << endl;
c=&student(name, sex, age, number, classname);
cout << "大学生类的输出:" << endl;
myout << "大学生类的输出:" << endl;
c->show();
return 0;
}
First of all... since you are using a function, write the header file...
Then, this code failed to compile under clang
Maybe I can pass it if I take VS. I don’t know if I don’t have VS in hand.
The pointer to the temporary variable is undefined. The temporary variable may be destroyed at any time, so an error may occur when accessing the reclaimed memory
Above
Hello, please describe the error message next time you ask.
I have no problem running under vs 2015.
You ask this question. The code is posted without explaining anything. Then you don’t explain a long list of codes what you are doing or what the error type is. I don’t even bother to look at it even if I want to help you. .