c ++ 继承
Program 1:
程序1:
#include <iostream>
#include <string.h>
using namespace std;
class Person {
char name[15];
int age;
public:
void SetPerson(int age, char* name)
{
this->age = age;
strcpy(this->name, name);
}
};
class Student : public Person {
int student_id;
int fees;
public:
Student(int id, int fee, int age, char* name)
{
student_id = id;
fees = fee;
SetPerson(age, name);
}
void Print()
{
cout << "Student id: " << student_id << endl;
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
cout << "Fees: " << fees;
}
};
int main()
{
Student S(101, 5000, 5, "Shaurya");
S.Print();
return 0;
}
Output:
输出:
main.cpp: In member function ‘void Student::Print()’:
main.cpp:32:29: error: ‘char Person::name [15]’ is private within this context
cout << "Name: " << name << endl;
^~~~
main.cpp:6:17: note: declared private here
char name[15];
^
main.cpp:33:29: error: ‘int Person::age’ is private within this context
cout << "Age: " << age << endl;
^~~
main.cpp:7:9: note: declared private here
int age;
^~~
Explanation:
说明:
Here, we defined two classes, Person and Student, we inherited Person class in the Student class publicly, but we accessed private data members of Person class in the Student class that cannot be accessed. Then the compilation error will be there.
在这里,我们定义了两个类Person和Student ,我们公开继承了Student类中的Person类,但是我们访问了Student类中Person类的私有数据成员,这些成员无法访问。 然后将出现编译错误。
Program 2:
程式2:
#include <iostream>
#include <string.h>
using namespace std;
class Person {
char name[15];
int age;
public:
void SetPerson(int age, char* name)
{
this->age = age;
strcpy(this->name, name);
}
void PrintPerson()
{
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
}
};
class Student : public Person {
int student_id;
int fees;
public:
Student(int id, int fee, int age, char* name)
{
student_id = id;
fees = fee;
SetPerson(age, name);
}
void Print()
{
cout << "Student id: " << student_id << endl;
cout << "Fees: " << fees << endl;
PrintPerson();
}
};
int main()
{
Student S(101, 5000, 5, "Shaurya");
S.Print();
return 0;
}
Output:
输出:
Student id: 101
Fees: 5000
Name: Shaurya
Age: 5
Explanation:
说明:
Here, we created two classes, Person and Employee. We inherited Person class into the Student class.
在这里,我们创建了两个类, Person和Employee 。 我们将Person类继承为Student类。
Person class contains two data members name, age, and member functions SetPerson(), PrintPerson().
Person类包含两个数据成员 名称 , age和成员函数SetPerson()和PrintPerson() 。
And inherited Person class into Student class.
并将“ 人”类继承为“ 学生”类。
The Student class contains data member student_id, fees, and a parameterized constructor and Print() function.
Student类包含的数据成员student_id数据 , 费用和参数的构造函数和print()函数。
Here, we called PrintPerson() inside the Print() function of the Student class.
在这里,我们在Student类的Print()函数内部调用了PrintPerson() 。
In the main() function, we created an object of class S with a parameterized constructor and print complete data using the Print() function on the console screen.
在main()函数中,我们创建了带有参数化构造函数的S类对象,并在控制台屏幕上使用Print()函数打印完整数据。
Program 3:
程式3:
#include <iostream>
#include <string.h>
using namespace std;
class Person {
char name[15];
int age;
protect : void SetPerson(int age, char* name)
{
this->age = age;
strcpy(this->name, name);
}
void PrintPerson()
{
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
}
};
class Student : public Person {
int student_id;
int fees;
public:
Student(int id, int fee, int age, char* name)
{
student_id = id;
fees = fee;
SetPerson(age, name);
}
void Print()
{
cout << "Student id: " << student_id << endl;
cout << "Fees: " << fees << endl;
PrintPerson();
}
};
int main()
{
Student S(101, 5000, 5, "Shaurya");
S.Print();
return 0;
}
Output:
输出:
main.cpp:8:5: error: ‘protect’ does not name a type
protect : void SetPerson(int age, char* name)
^~~~~~~
main.cpp: In constructor ‘Student::Student(int, int, int, char*)’:
main.cpp:30:28: error: ‘SetPerson’ was not declared in this scope
SetPerson(age, name);
^
main.cpp: In member function ‘void Student::Print()’:
main.cpp:36:9: error: ‘void Person::PrintPerson()’ is private within this context
PrintPerson();
^~~~~~~~~~~
main.cpp:13:10: note: declared private here
void PrintPerson()
^~~~~~~~~~~
main.cpp:36:21: error: ‘void Person::PrintPerson()’ is private within this context
PrintPerson();
^
main.cpp:13:10: note: declared private here
void PrintPerson()
^~~~~~~~~~~
Explanation:
说明:
It will generate errors. Because we used "protect" keyword instead of "protected". So it will generate the error.
它将产生错误。 因为我们使用“保护”关键字而不是“保护”。 因此它将产生错误。
翻译自: https://www.includehelp.com/cpp-tutorial/inheritance-find-output-programs-set-1.aspx
c ++ 继承