#include<iostream> #include<string> using namespace std; class student { public: student(float number1, string name1, float score1) { number = number1; name = name1; score = score1; } void print() { cout << "number:" << number << endl; cout << "name:" << name << endl; cout << "score:" << score << endl; } protected : float number; string name; float score; }; class USstudent :public student { USstudent(float number1, string name1, float score1, string major1) :student(number1, name1, score1) { major = major1; } void print() { student::print(); cout << "major:" << major << endl; } private : string major; }; int main() { USstudent stu(22116, "chih", 89,"computer science" ); stu.print(); return 0; }
编译器提示在主函数实参22116那个地方有错,还有stu.print()也有错。
你的print和构造函数都没有加上访问权限修饰符,在C++中默认的是private,所以编译器就会报错,加上public修饰符就好了
多谢帮助,明白了