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 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 ++ 继承

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/541779.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

xor在PHP是什么意思,?=‘在PHP中是什么意思?

万千封印因为它不会增加任何价值echo&#xff0c;我认为您希望了解PHP中的确切含义&#xff1a;Array([0] > Array([0] > 368 // T_OPEN_TAG_WITH_ECHO[1] > [2] > 1)[1] > Array([0] > 309 // T_VARIABLE[1] > $a [2] > 1)[2] > ; // U…

php curl keepalive,HTTPKeepAlive,开启还是关闭

所谓「HTTP Keep-Alive」&#xff0c;在维基百科里称为「HTTP Persistent Connection」&#xff0c;说白了就是复用HTTP连接&#xff0c;如此一来理论上客户端的用户体验会更流畅&#xff0c;但是与之相对服务端不得不维持大量的连接。开启还是关闭&#xff0c;这是个问题。一个…

如何使用ES6中的参数

ECMAScript 6&#xff08;或者叫 ECMAScript 2015&#xff09;是 ECMAScript 的最新标准&#xff0c;极大的提高了 JavaScript 中处理参数的能力。现在我们可以使用 rest 参数&#xff08;rest parameters&#xff09;、默认值&#xff08;default values&#xff09;和解构&am…

c++中tle是什么意思_如何在竞争性编程中克服TLE?

c中tle是什么意思什么是TLE&#xff1f; (What is TLE?) TLE means "Time Limit Exceed". So, in competitive programming, there are some constraints with a specific time limit (normally for each input 1 sec) and your task is to write your code in such…

美颜相机window 开源_X-Window系统| 免费和开源软件

美颜相机window 开源X窗口系统 (The X-Window System) The X-Window System is a GUI that sits over Linux. Not at all like Microsoft Windows, the X Window System can glance and work in an enormously wide range of ways. It can work smoothly or lag, look excellen…

php 代码 自动检查工具下载,PHP_CodeSniffer安装和使用教程(自动代码检查规范工具)...

在我们开发中都会讲究代码规范&#xff0c;若是个人开发者&#xff0c;代码规范与否&#xff0c;只要自己看得懂便可以了&#xff0c;但是在团队协作中&#xff0c;代码规定尤为重要&#xff0c;下面&#xff0c;我们介绍一款PHP_CodeSniffer&#xff0c;自动检查代码规范的工具…

国际象棋之跳马程序

问题描述: 假设国际象棋棋盘有5*5共25个格子。设计一个程序,使棋子从初始位置&#xff08;棋盘格编号为1的位置)开始跳马,能够把棋盘的格子全部走一遍,每个格子只允许走一次。要求: 1) 输出一个解(用二维数组来记录马跳的过程,即[步号,棋盘格编号],左上角为第一步起点)&#xf…

kafka安装使用

版本&#xff1a;kafka_2.11-0.10.1.0 (之前安装2.10-0.10.0.0&#xff0c;一直出问题) 安装Springboot结合Kafka的使用安装 下载并解压代码 wget http://mirrors.cnnic.cn/apache/kafka/0.10.0.0/kafka_2.10-0.10.0.0.tgz #http://kafka.apache.org/downloadstar -zxvf kafka…

php获取上传文件路径 fakepath,JavaScript_js获取上传文件的绝对路径实现方法,在html中input type=file - phpStudy...

js获取上传文件的绝对路径实现方法在html中function upload() {var filename document.getElementById("importFile").value;// 这时的filename不是 importFile 框中的值alert(filename);}如上面的代码&#xff0c;用文件上传对话框选择文件后&#xff0c;如果选择&…

在Bootstrap中使用类的按钮类型

Bootstrap has 7 different types of buttons with contextual classes from which we can create buttons easily by using these classes (.btn-default, .btn-success, .btn-danger, .btn-primary, .btn-info, .btn-warning, .btn-link). Bootstrap具有上下文类型的 7种不同…

php json encode中文乱码,php json_encode中文乱码如何解决

php encode中文乱码的解决办法&#xff1a;首先打开相应的PHP文件&#xff1b;然后使用正则语句“preg_replace("#\\\u([0-9a-f]{4})#ie","iconv(UCS-2BE, UTF-8...)”将编码替换成中文即可。本文列举3个方法&#xff0c;实现json_encode()后的string显示中文问…

乡村图景(转载)

转自: http://cul.qq.com/a/20160205/046437.htm 我丈夫家在湖北孝感孝昌县的一个村子。2005年第一次过年回到他家&#xff0c;印象最深的就是嫂子。我暗自问当时的男友&#xff0c;“哥哥尽管算不上特别帅气&#xff0c;但为何找了这么难看的嫂子&#xff1f;”后来才发现&…

stl向量最大值_C ++ STL中向量的最小和最大元素

stl向量最大值Given a vector and we have to find the smallest (minimum) and largest (maximum) elements. 给定一个向量&#xff0c;我们必须找到最小(最小)和最大(最大)元素。 查找向量的最小和最大元素 (Finding vectors minimum & maximum elements) To find minim…

oracle如何设置备份计划任务,Oracle数据库设置任务计划备份一周的备份记录

Oracle 数据库备份&#xff1a;--保留最近一周的备份记录&#xff1b;正文&#xff1a;开始代码如下:echo 设置备份文件存放文件夹...set "tbufE:\Cway\backup"echo 设置备份文件名(以星期几命名&#xff0c;即备份文件只保存最近一周)...set name%date%set name%nam…

索引(转载自百度百科)

Oracle索引 编辑本词条缺少信息栏、名片图&#xff0c;补充相关内容使词条更完整&#xff0c;还能快速升级&#xff0c;赶紧来编辑吧&#xff01;在oracle索引是一种供服务器在表中快速查找一个行的数据库结构。合理使用索引能够大大提高数据库的运行效率。目录 1 概念及作用 2…

阿姆斯特朗数_阿姆斯特朗的功能依赖公理 数据库管理系统

阿姆斯特朗数Armstrong axioms are a complete set of inference rules or axioms, introduced and developed by William W. Armstrong in 1974. The inference rules are sound which is used to test logical inferences of functional dependencies. The axiom which also …

ORACLE JOB 失败 查看,Oracle JOB异常中断原因分析

注释今天研发同事找我确认 PKG_WMS.proc_TaskMain 存储的 job 是否还在运行&#xff0c;竟发现 dba_jobs.NEXT_DATE4000/1/1&#xff0c;如下看看究竟原因吧~JOB 信息&#xff1a;参数&#xff1a;BROKEN : 中断标记 ,N 启动、Y 中断 --> DBMS_JOBS.BROKEN(job_id,TRUE/FA…

ruby打印_Ruby程序打印一个数字的乘法表

ruby打印打印乘法表 (Printing multiplication table) This requires a very simple logic where we only have to multiply the number with digits from 1 to 10. This can be implemented by putting the multiplication statement inside a loop. We have mentioned two wa…

步骤1:JMeter 录制脚本接口测试

JMeter 常用测试方法简介 1.下载安装 http://jmeter.apache.org/download_jmeter.cgi 安装JDK&#xff0c;配置环境变量JAVA_HOME. 系统要求&#xff1a;JMeter2.11 需要JDK1.6以上的版本支持运行 2.学习Jmeter元件 http://jmeter.apache.org/usermanual/component_reference.h…

模拟断电oracle数据不一致,Oracle数据库案例整理-Oracle系统运行时故障-断电导致数据文件状态变为RECOVER...

1.1 现象描述异常断电&#xff0c;数据库数据文件的状态由ONLINE变为RECOVER。系统显示如下信息&#xff1a;SQL> select file_name ,tablespace_name ,online_status from dba_data_files;FILE_NAME---------------------------------------------------------------…