初识C++继承
先是自己凭借自己在课堂上的记忆打了一遍。自然出了错误。
//编译错误
#include <iostream>
#include <cstdlib>
using namespace std;
class people
{
private:
int age;
int sex; // 1 -girl 2 -boy
public:
people(int a = 0, int b = 0): age(a), sex(b){};
};
class student : public people
{
private:
int num;
int score;
string name;
public:
student(int bnum = 0, int bscore = 0, int bage = 0, int bsex = 0, string bname):people(bage, bsex) //错误
{
num = bnum;
score = bscore;
name = bname;
};
void display();
};
void student::display()
{
if(sex == 1) //错误
cout << name << " girl " << num << " " << age << " " << score << endl;
else
cout << name << " boy " << num << " " << age << " " << score << endl;
}
int main()
{
student Liming(10001, 100, 20, 2, "李鸣");
Liming.display() ;
return 0;
}
错误小结:
1.类student是public继承于类people,那么在student的成员函数中,无法访问父类的private和protected属性的成员,只能通过继承过来的父类的成员函数访问它们。
2.初始化列表写错了。函数的默认参数最后一句 string bname; 应该是 string bname = "";
改了之后,可以运行了。
#include <iostream>
#include <cstdlib>
using namespace std;
class people
{
private:
int age;
int sex; // 1 -girl 2 -boy
public:
people(int a = 0, int b = 0): age(a), sex(b){};
int getage();
int getsex();
};
int people::getage()
{
return age;
}
int people::getsex()
{
return sex;
}
class student : public people
{
private:
int num;
int score;
string name;
public:
student(string bname,int bnum = 0, int bscore = 0, int bage = 0, int bsex = 0)
:people(bage, bsex),num(bnum),score(bscore){name = bname;};
void display();
};
void student::display()
{
if(getsex() == 1)
cout << name << " girl " << num << " " << getage() << " " << score << endl;
else
cout << name << " boy " << num << " " << getage() << " " << score << endl;
}
int main()
{
student Liming("李鸣", 10001, 100, 20, 2);
Liming.display() ;
return 0;
}

学习到的知识点:
1.对于父类的派生类来说,其对象的初始化需要利用初始化列表进行操作。比如:
student(string bname, int bnum = 0, int bscore = 0, int bage = 0, int bsex = 0)
:people(bage, bsex),num(bnum),score(bscore){name = bname;};
上面的语句调用了父类的初始化构造函数,所以父类的构造函数应具有含参构造函数,可以利用重载来实现。
个人的习惯是:写一个含有默认参数的初始化列表。
2.如果是public继承,那么在派生类的成员函数中无法访问其从父类继承过来的具有private和protected属性的成员。
这个时候,可以通过调用从父类继承过来的成员函数获取其值。例如:
int people::getage()
{
return age;
}
int people::getsex()
{
return sex;
}
···
void student::display()
{
if(getsex() == 1) //调用父类的成员函数
cout << name << " girl " << num << " " << getage() << " " << score << endl;
else
cout << name << " boy " << num << " " << getage() << " " << score << endl;
}
3.复习了一下含有默认参数的构造函数,设置默认参数时应从右向左。例如:
student(int bnum = 0, int bscore = 0, int bage = 0, int bsex = 0, string bname):people(bage, bsex) //错误
应为:
student(int bnum = 0, int bscore = 0, int bage = 0, int bsex = 0, string bname = ""):people(bage, bsex)
初识C++继承的更多相关文章
- C++_基础_运算符重载2
内容: (1)只能用成员形式重载的运算符 (2)new/delete操作符的重载 (3)封装和继承的初识 (4)继承的特性 (5)子类及其函数的特性 (6)多重继承和虚继承 1.只能用成员形式重载的运 ...
- OC 初识NSString,self关键字,继承,成员变量的可见性,description方法
OC 初识NSString,self关键字,继承,成员变量的可见性,description方法 初识 NSString: char * string = "旭宝爱吃鱼"; 常量字符 ...
- python基础(17)继承类和面向对象初识
1.继承类 class Lm: money = 1000000 house = 5 def driver(self): print('会开车') class Mcb(Lm): def about_me ...
- python 全栈开发,Day19(组合,组合实例,初识面向对象小结,初识继承)
一.组合 表示的一种什么有什么的关系 先来说一下,__init__的作用 class Dog: def __init__(self, name, kind, hp, ad): self.name = ...
- Day7 初识面向对象,面向对象之继承、多态和封装
一.面向对象引言 一.面向对象的程序设计的由来 详述见:http://www.cnblogs.com/linhaifeng/articles/6428835.html 二.面向对象引子 写一个简单程序 ...
- day24 01 初识继承
day24 01 初识继承 面向对象的三大特性:继承,多态,封装 一.继承的概念 继承:是一种创建新类的方式,新建的类可以继承一个或者多个父类,父类又可称基类或超类,新建的类称为派生类或者子类 cla ...
- 初识JAVA(【面向对象】:pub/fri/pro/pri、封装/继承/多态、接口/抽象类、静态方法和抽象方法;泛型、垃圾回收机制、反射和RTTI)
JAVA特点: 语法简单,学习容易 功能强大,适合各种应用开发:J2SE/J2ME/J2EE 面向对象,易扩展,易维护 容错机制好,在内存不够时仍能不崩溃.不死机 强大的网络应用功能 跨平台:JVM, ...
- 红豆带你从零学C#系列之:初识继承与多态
继承 现实生活当中,人类又可以根据职业分为:教师,学生,理发师,售货员 又比如飞机又有种类之分:直升飞机.客机.货机.战斗机等 在程序里面我们可能会通过创建类来描述这样的事物,比如学生类.教师类.理发 ...
- 初识Hibernate之继承映射
前面的两篇文章中,我们介绍了两张表之间的各种相互关联映射关系,但往往我们也会遇到两张表甚至多张表之间共有着多个相同的字段.例如: 如图,student表和teacher表共同具有id,nam ...
随机推荐
- ext3日志模式
ext3日志模式 http://blog.sina.com.cn/s/blog_5d4ab4b40100dosx.html ext3支持多种日志模式 ext3 是ext2文件系统的高一级版本,完全兼容 ...
- 协作工具 discord 和 slack
discord: https://discordapp.com/ slack: www.slack.com
- [py]py2自带Queue模块实现了3类队列
py2自带Queue实现了3类队列 先搞清楚几个单词 Queue模块实现了三类队列: FIFO(First In First Out,先进先出,默认为该队列), 我们平时泛指的队列, LIFO(Las ...
- [LeetCode] 628. Maximum Product of Three Numbers_Easy
Given an integer array, find three numbers whose product is maximum and output the maximum product. ...
- Qt界面控件值获取异常处理
情景简述: 正常情况,我们从控件获取的值是OK的,但有时候就是奇怪的不对头,那么我们可以给获取后的值加上一个不痛不痒的函数,再返回,结果就OK了.至于原因嘛,[呲牙][呲牙] 比如: //正常情况 d ...
- 剑指offer4
中序遍历(LDR)是二叉树遍历的一种,也叫做中根遍历.中序周游.在二叉树中,先左后根再右.巧记:左根右. 现在有一个问题,已知二叉树的前序遍历和中序遍历:PreOrder: GDAFE ...
- linux make configure make
开放源码:就是程序代码,写给人类看的程序语言,但机器并不认识,所以无法执行: 编译程序:将程序代码转译成为机器看得懂的语言,就类似编译者的角色: 可执行文件:经过编译程序变成二进制后机器看得懂所以可以 ...
- XDU1024简单逆序对(贪心||分治)
题目描述 逆序对问题对于大家来说已经是非常熟悉的问题了,就是求i<j时,a[i] > a[j]的组数.现在请你求出一串数字中的逆序对的个数,需要注意的是,这些数字均在[0,9]之内. 输入 ...
- Python: 字典列表: itemgetter 函数: 根据某个或某几个字典字段来排序列表
问题:根据某个或某几个字典字段来排序Python列表 answer: 通过使用operator 模块的itemgetter 函数,可以非常容易的排序这样的数据结构 eg: rows = [ {'fna ...
- 安装vscode with springboot
1.安装jdk8 2.下载vscode,一切按照默认配置完成安装.下载地址:https://code.visualstudio.com 3.安装完成后,运行vscode.如果没有任何反应,在命令行上运 ...