c ++ 继承_C ++继承| 查找输出程序| 套装1
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 contextcout << "Name: " << name << endl;^~~~ main.cpp:6:17: note: declared private herechar name[15];^ main.cpp:33:29: error: ‘int Person::age’ is private within this contextcout << "Age: " << age << endl;^~~ main.cpp:7:9: note: declared private hereint 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: 5Explanation:
說明:
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 typeprotect : 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 scopeSetPerson(age, name);^ main.cpp: In member function ‘void Student::Print()’: main.cpp:36:9: error: ‘void Person::PrintPerson()’ is private within this contextPrintPerson();^~~~~~~~~~~ main.cpp:13:10: note: declared private herevoid PrintPerson()^~~~~~~~~~~ main.cpp:36:21: error: ‘void Person::PrintPerson()’ is private within this contextPrintPerson();^ main.cpp:13:10: note: declared private herevoid 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 ++ 繼承
總結
以上是生活随笔為你收集整理的c ++ 继承_C ++继承| 查找输出程序| 套装1的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java System类identity
- 下一篇: 给定数字的b+树创建_在C ++中找到给