c ++ 继承
Program 1:
class="raw_b_node">程序1:
class="has">#include <iostream>
#include <string.h>
using namespace std;
class Person {
char name[15];
int age;
protected:
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 {
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:
class="raw_b_node">输出:
class="has">main.cpp: In constructor ‘Student::Student(int, int, int, char*)’:
main.cpp:32:28: error: ‘SetPerson’ was not declared in this scope
SetPerson(age, name);
^
main.cpp: In member function ‘void Student::Print()’:
main.cpp:38:21: error: ‘PrintPerson’ was not declared in this scope
PrintPerson();
^
Explanation:
class="raw_b_node">说明:
It will generate errors because, we did not inherit Person class in Student class and accessing the protected members of Person class in the Student class.
因为,我们没有继承Personclass="tags" href="/tags/Lei.html" title=类>类的学生class="tags" href="/tags/Lei.html" title=类>类和访问Personclass="tags" href="/tags/Lei.html" title=类>类的protected成员在学生班会产生错误。
Program 2:
class="raw_b_node">程式2:
class="has">#include <iostream>
#include <string.h>
using namespace std;
class Class1 {
public:
Class1()
{
cout << "Class1 ctor called " << endl;
}
};
class Class2 : public Class1 {
public:
Class2()
{
cout << "Class2 ctor called " << endl;
}
};
int main()
{
Class2 OB;
return 0;
}
Output:
class="raw_b_node">输出:
class="has">Class1 ctor called
Class2 ctor called
Explanation:
class="raw_b_node">说明:
Here, we created two classes Class1 and Class2. Both classes contain constructors.
在这里,我们创建了两个class="tags" href="/tags/Lei.html" title=类>类Class1和Class2 。 这两个class="tags" href="/tags/Lei.html" title=类>类都包含构造函数 。
Class2 inherits Class1, in the main() function, we created the object OB of Class2. Then it called the constructor of Class1 and then it called the constructor of Class2.
Class2继承了Class1 ,在main()函数中,我们创建了Class2的对象OB 。 然后,它调用了Class1的构造函数,然后又调用了Class2的构造函数。
In C++, the base class constructor always called before the derived class constructor.
在C ++中,基class="tags" href="/tags/Lei.html" title=类>类构造函数总是在派生class="tags" href="/tags/Lei.html" title=类>类构造函数之前调用。
Program 3:
class="raw_b_node">程式3:
class="has">#include <iostream>
#include <string.h>
using namespace std;
class Class1 {
public:
Class1()
{
cout << "Class1 ctor called " << endl;
}
};
class Class2 : public Class1 {
public:
Class2()
{
cout << "Class2 ctor called " << endl;
}
};
typedef class Class3 : public Class2 {
public:
Class3()
{
cout << "Class3 ctor called " << endl;
}
} DerivedClass;
int main()
{
DerivedClass* PTR;
PTR = new DerivedClass();
return 0;
}
Output:
class="raw_b_node">输出:
class="has">Class1 ctor called
Class2 ctor called
Class3 ctor called
Explanation:
class="raw_b_node">说明:
Here, in the above program, we created three classes Class1, Class2, and Class3, each of them are containing a default constructor.
在这里,在上面的程序中,我们创建了三个class="tags" href="/tags/Lei.html" title=类>类Class1 , Class2和Class3 ,每个class="tags" href="/tags/Lei.html" title=类>类都包含一个默认构造函数。
Here, Class1 is inherited by Class2 and Class2 inherited by Class2. We also created alias using the typedef of Class3 that is Derived class. This type of inheritance is known as multi-level inheritance.
在这里,1class="tags" href="/tags/Lei.html" title=类>类是继承的Class2和Class2中继承了Class2中 。 我们还使用Class3的typedef (即派生class="tags" href="/tags/Lei.html" title=类>类)创建了别名。 这种class="tags" href="/tags/Lei.html" title=类>类型的继承称为多级继承。
In the main() function, we created the pointer of DerivedClass and pointer of the DerivedClass initialized using the new operator.
在main()函数中,我们创建了DerivedClass的指针和使用new运算符初始化的DerivedClass的指针。
Then, Class1 constructor called before Class2 and Class2 constructor called before Class3.
然后,在Class2之前调用Class1构造函数,在Class3之前调用Class2构造函数。
翻译自: https://www.includehelp.com/cpp-tutorial/inheritance-find-output-programs-set-2.aspx
c ++ 继承