初识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 ...
随机推荐
- xss跨站脚本攻击及xss漏洞防范
xss跨站脚本攻击(Cross Site Scripting,因与css样式表相似故缩写为XSS).恶意攻击者往Web页面里插入恶意Script代码,当用户浏览该页之时,嵌入其中Web里面的Scrip ...
- PHP主动断开与浏览器的连接
以前整理过一篇<关于PHP连接处理中set_time_limit().connection_status()和ignore_user_abort()深入解析>,是解说浏览器client断开 ...
- inter x86 emulator accelerator(HAXM installer) not compatible with windows
在SDK manager中遇到如下错误:这将导致AVD后期运行和启动方面的问题. 解决办法: 在如下的网址里面下载haxm-windows_v6_2_0这个文件的压缩包,自己手动安装即可.网站如下:点 ...
- MVC中Controller与View之间的数据传递
一.Controller向View传递数据 Controller向View传递数据有3种形式: 通过ViewData传递 在Controller里面的Action方法中定义ViewData,并且赋值, ...
- python接口测试中安装whl格式的requests第三方模块
下载 安装 requests第三方模块 下载:http://docs.python-requests.org/en/latest/user/install/#install 我下载是 https:// ...
- [LeetCode] 549. Binary Tree Longest Consecutive Sequence II_ Medium tag: DFS recursive
Given a binary tree, you need to find the length of Longest Consecutive Path in Binary Tree. Especia ...
- TraceSource记录程序日志
1.配置文件 <system.diagnostics> <sources> <source name="TraceError" switchValue ...
- 剑指offer2
请实现一个函数,将一个字符串中的字符串空格替换成“%20”.例如:“We Are Happy”转化后为“We%20Are%20Happy” 思路:把字符串转化成字符数组,判断这个字符是不是空格,如果是 ...
- sql 中延时操作
select 1; WAITFOR DELAY '00:00:30'; select 2; --执行完第一个之后会 延时 30秒,才会执行第二个sql
- Nuget的学习总结
Nuget的学习总结 今天研究了一下nuget,发现nuget实在是太有用了,便写下了这篇博客,希望记录一下自己的学习历程,也希望技术圈的朋友看到之后,如果里面哪里写的不够好,可以给我些宝贵的意见,以 ...