65,186
社区成员




#include <iostream>
#include <string>
using namespace std;
class student
{
protected:
string num, name;
public:
student(string s_num, string s_name);
void show();
};
student::student(string s_num, string s_name): num(s_num),name(s_name){}
void student::show()
{
cout << "num:" << num << endl << "name:" << name << endl;
}
class student1: public student
{
private:
int age;
string addr;
student monitor;
public:
student1(string s_num, string s_name, int s_age, string s_addr, student s_monitor);
void show();
};
student1::student1(string s_num, string s_name, int s_age, string s_addr, student s_monitor)
{
student(s_num, s_name);
age = s_age;
addr = s_addr;
monitor = s_monitor;
}
void student1::show()
{
cout << "num:" << num << endl << "name:" << name << endl;
cout << "age:" << age << endl << "addr:" << addr << endl;
}
int main()
{
student1 stud1("10001", "LiMing", 20, "SD", "LiMing");
stud1.show();
return 0;
}
#include <iostream>
#include <string>
using namespace std;
class student
{
protected:
string num, name;
public:
student(string s_num, string s_name);
student() {};
void show();
};
student::student(string s_num, string s_name) : num(s_num), name(s_name) {}
void student::show()
{
cout << "num:" << num << endl << "name:" << name << endl;
}
class student1 : public student
{
private:
int age;
string addr;
student monitor;
public:
student1(string s_num, string s_name, int s_age, string s_addr, student s_monitor);
void show();
};
student1::student1(string s_num, string s_name, int s_age, string s_addr, student s_monitor)
{
student(s_num, s_name);
age = s_age;
addr = s_addr;
monitor = s_monitor;
}
void student1::show()
{
cout << "num:" << num << endl << "name:" << name << endl;
cout << "age:" << age << endl << "addr:" << addr << endl;
}
int main()
{
student1 stud1("10001", "LiMing", 20, "SD", student("LiMing", "10001"));
stud1.show();
return 0;
}
VS2015 C++调试通过
student1::student1(string s_num, string s_name, int s_age, string s_addr, student s_monitor)
{
student(s_num, s_name);
student1::student1(string s_num, string s_name, int s_age, string s_addr, student s_monitor)
: student(s_num, s_name)
{
那个警告是告诉你要用c++11的标准
1、
#include <iostream>
#include <string>
using namespace std;
class student
{
protected:
string num, name;
public:
student(){};
student(string s_num, string s_name);
void show();
};
student::student(string s_num, string s_name): num(s_num),name(s_name){}
void student::show()
{
cout << "num:" << num << endl << "name:" << name << endl;
}
class student1: public student
{
private:
int age;
string addr;
student monitor;
public:
student1(string s_num, string s_name, int s_age, string s_addr, student s_monitor);
void show();
};
student1::student1(string s_num, string s_name, int s_age, string s_addr, student s_monitor)
{
student(s_num, s_name);
age = s_age;
addr = s_addr;
monitor = s_monitor;
}
void student1::show()
{
cout << "num:" << num << endl << "name:" << name << endl;
cout << "age:" << age << endl << "addr:" << addr << endl;
}
int main()
{
student1 stud1("10001", "LiMing", 20, "SD", student("10001", "LiMing"));
stud1.show();
return 0;
}
这一份代码为什么输出的时候前两个数据没结果呢?
输出:
num: name: age:20 addr:SD
我想说的是要使用一种语言最好系统的学习一下,不要靠瞎猜。class student { protected: string num, name; public: student(string s_num, string s_name); void show(); }; student::student(string s_num, string s_name) : num(s_num), name(s_name) {} void student::show() { cout << "num:" << num << endl << "name:" << name << endl; } class student1 : public student { private: int age; string addr; student monitor; public: student1(string s_num, string s_name, int s_age, string s_addr, student s_monitor); void show(); }; student1::student1(string s_num, string s_name, int s_age, string s_addr, student s_monitor):student(s_num, s_name),monitor(s_monitor) { age = s_age; addr = s_addr; } void student1::show() { cout << "num:" << num << endl << "name:" << name << endl; cout << "age:" << age << endl << "addr:" << addr << endl; } int main() { student1 stud1("10001", "LiMing", 20, "SD", { "10001", "LiMing" }); stud1.show(); return 0; }
class student
{
protected:
string num, name;
public:
student(string s_num, string s_name);
void show();
};
student::student(string s_num, string s_name) : num(s_num), name(s_name) {}
void student::show()
{
cout << "num:" << num << endl << "name:" << name << endl;
}
class student1 : public student
{
private:
int age;
string addr;
student monitor;
public:
student1(string s_num, string s_name, int s_age, string s_addr, student s_monitor);
void show();
};
student1::student1(string s_num, string s_name, int s_age, string s_addr, student s_monitor):student(s_num, s_name),monitor(s_monitor)
{
age = s_age;
addr = s_addr;
}
void student1::show()
{
cout << "num:" << num << endl << "name:" << name << endl;
cout << "age:" << age << endl << "addr:" << addr << endl;
}
int main()
{
student1 stud1("10001", "LiMing", 20, "SD", { "10001", "LiMing" });
stud1.show();
return 0;
}