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類的私有數(shù)據(jù)成員,這些成員無法訪問。 然后將出現(xiàn)編譯錯誤。
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.
在這里,我們創(chuàng)建了兩個類, Person和Employee 。 我們將Person類繼承為Student類。
Person class contains two data members name, age, and member functions SetPerson(), PrintPerson().
Person類包含兩個數(shù)據(jù)成員 名稱 , age和成員函數(shù)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類包含的數(shù)據(jù)成員student_id數(shù)據(jù) , 費用和參數(shù)的構(gòu)造函數(shù)和print()函數(shù)。
Here, we called PrintPerson() inside the Print() function of the Student class.
在這里,我們在Student類的Print()函數(shù)內(nèi)部調(diào)用了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()函數(shù)中,我們創(chuàng)建了帶有參數(shù)化構(gòu)造函數(shù)的S類對象,并在控制臺屏幕上使用Print()函數(shù)打印完整數(shù)據(jù)。
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.
它將產(chǎn)生錯誤。 因為我們使用“保護”關(guān)鍵字而不是“保護”。 因此它將產(chǎn)生錯誤。
翻譯自: https://www.includehelp.com/cpp-tutorial/inheritance-find-output-programs-set-1.aspx
c ++ 繼承
總結(jié)
以上是生活随笔為你收集整理的c ++ 继承_C ++继承| 查找输出程序| 套装1的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java System类identity
- 下一篇: 给定数字的b+树创建_在C ++中找到给