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.

在这里,我们定义了两个类PersonStudent ,我们公开继承了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.

在这里,我们创建了两个类, PersonEmployee 。 我们将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 ++ 继承

c ++ 继承_C ++继承| 查找输出程序| 套装1相关推荐

  1. c ++查找字符串_C ++数组| 查找输出程序| 套装5

    c ++查找字符串 Program 1: 程序1: #include <iostream> using namespace std; int main() { char* STR[] = ...

  2. 多线程循环输出abcc++_C ++循环| 查找输出程序| 套装4

    多线程循环输出abcc++ Program 1: 程序1: #include <iostream> using namespace std; int A = 5; int fun() { ...

  3. 多线程循环输出abcc++_C ++循环| 查找输出程序| 套装5

    多线程循环输出abcc++ Program 1: 程序1: #include <iostream> using namespace std; int main() { int num = ...

  4. 多线程循环输出abcc++_C ++循环| 查找输出程序| 套装2

    多线程循环输出abcc++ Program 1: 程序1: #include<iostream> using namespace std; int main() { for(;;) { c ...

  5. c ++查找字符串_C ++结构| 查找输出程序| 套装2

    c ++查找字符串 Program 1: 程序1: #include <iostream> using namespace std; int main() { typedef struct ...

  6. c ++查找字符串_C ++结构| 查找输出程序| 套装1

    c ++查找字符串 Program 1: 程序1: #include <iostream> #include <math.h> using namespace std; str ...

  7. c ++查找字符串_C ++异常处理| 查找输出程序| 套装1

    c ++查找字符串 Program 1: 程序1: #include <iostream> using namespace std; int main() { try { int num1 ...

  8. c ++查找字符串_C ++结构| 查找输出程序| 套装3

    c ++查找字符串 Program 1: 程序1: #include <iostream> #pragma pack(1) using namespace std; typedef str ...

  9. c ++查找字符串_C ++类和对象| 查找输出程序| 套装4

    c ++查找字符串 Program 1: 程序1: #include <iostream> using namespace std; class Sample { int X; int* ...

  10. c构造函数和析构函数_C ++构造函数和析构函数| 查找输出程序| 套装1

    c构造函数和析构函数 Program 1: 程序1: #include <iostream> using namespace std; class Sample { private: in ...

最新文章

  1. HDU 6301.Distinct Values-贪心、构造字典序最小的数列 (2018 Multi-University Training Contest 1 1004)...
  2. Jmeter入门3 http请求—content-type与参数
  3. Java项目案例大全
  4. easy_install django==1.4.2_百度搜索
  5. [转]教你如何在博客园放“可运行代码
  6. @Autowired与@Resource的差别
  7. 惠普M1005打印机驱动-LaserJet提供下载
  8. 下载安装php详细教程(在安装配置apache之后)
  9. 51单片机电子琴设计
  10. 二本计算机软件工程专业大学排名,哪些二本大学的软件工程专业最好
  11. 威纶触摸屏485通信控制多台台达变频器程序
  12. CC00051.elasticsearch——|HadoopElasticSearch.V03|——|ELK.v03Logstash部署.V3|
  13. 简述同步和异步的区别
  14. 计算机谣言之网线的做法
  15. 苹果登陆代理方法didCompleteWithAuthorization没有调用,didCompleteWithError没有走
  16. 蓝牙耳机哪款好用?这些选购小技巧帮你选到更适合你的蓝牙耳机!
  17. 使用python编写LDPC编码
  18. c语言程序设计教程中国农业出版社答案,C语言程序设计教程杨路明课后习题答案北京邮电大学出版社.pdf...
  19. 国外ERP项目实施“误”在哪?(转)
  20. 五个男人30年在同一位置拍摄相同合影,岁月不饶人啊

热门文章

  1. android 空白占位符,android textview空格占位符以及一些其他占位符汇总
  2. strocli64 源码_storcli 简易使用介绍
  3. Angular实现灵活的动态创建组件指令
  4. 腾讯财报中“最大秘密”:2018云收入91亿元,交首份TO B答卷
  5. 从0到1使用VUE-CLI3开发实战(五):模块化VUEX及使用vuetify
  6. 美国将尝试区块链领域和加密货币相结合
  7. 布局 —— 左侧固定,右侧自适应
  8. Map Reduce和流处理
  9. 更新SQL Server实例所有数据库表统计信息
  10. 社交应用动态九宫格图片的规则