美文网首页
09 继承

09 继承

作者: LuckTime | 来源:发表于2016-06-18 09:55 被阅读3次

// class.cpp : Defines the entry point for the console application.
//

include "stdafx.h"

include <iostream>

include <string>

using namespace std;

void inr(int& m);
void vor(int m);
class Animal //-------------------父类
{
private: //私有
char name[20];
protected://保护类型
int age;
public :
void show(char* na ,int a);

};

class Cat : public Animal //子类--------------------
{
public:
void fly();
};
void Cat::fly()
{

cout << "Im's cat,i don't fly!" << endl;

}
void Animal ::show(char* na ,int a)
{
strcpy(name,na);
this->age = a;
cout <<"hello,my name is"<< name<<"\n"<<"and my sge is"<<age<<endl;

}

int main(int argc, char* argv[])
{
Animal A;
A.show("tiger",15);
Cat B;
B.show("laoshu",18); //因为是公有继承animal的私有元素。。。所以能像Animal类一样使用公开的和保护的函数
B.fly();

int m = 15; //c
vor(m); //形参的传入,相当于是m的复制一份给vor函数,用完消失
cout << m <<endl;
inr(m); //数值地址传入,是对地址进行修改。(引用和取别名)是对源参数的再次命名。。而指针是对指针指向进行交换。
cout << m <<endl;

return 0;
}

void inr(int& m)
{
++m;

}

void vor(int m)
{
++m;

}
void swap (int* a, int* b)
{
int * t ;
*t = *a;
a =b;
b =t
}

void swap(&a,&b)
{
int t ;
t = a;
a = b;
b = t

}

// 公 -- 保护 -- 私有
// 父类 √ --√ --√
// 子类 √ --√ --×
// 外 √ --× --×

相关文章

  • 09 继承

    // class.cpp : Defines the entry point for the console ap...

  • 关于持续集成CI踩坑(一)

    持续继承的概念主要引用自http://www.ruanyifeng.com/blog/2015/09/contin...

  • java09(继承)

    多实现: java只支持单继承,不支持多继承 因为多继承容易带来安全隐患:当多个父类中定义了相同的功能,当...

  • 继承和多态

    date: 2016-10-09 18:45:18 这里继承和多态的概念与java的概念差不多。概念还是需要多次理...

  • 09-方法、下标、继承

    一、方法(Method) mutating @discardableResult 二、下标(subscript) ...

  • ES6面向对象之绚丽小球

    ES6的继承https://www.jianshu.com/p/3d09c6fe330eES6面向对象之弹性小球h...

  • 2020-09-09 Java继承内存图

    Java语言是单继承的

  • 功成名就的“马爸爸”教我们的最后一课

    2018年09月10日,阿里巴巴创始人“马爸爸”马云宣布公司继承计划,这位公司里的“风清扬”将在2019年9月...

  • 继承 继承

    属性拷贝 继承不单单能通过原型链实现,也能通过其他方式实现,属性拷贝就是其中一种方法。 通过属性拷贝也能实现继承子...

  • ES5继承的六种方式

    (2018-09-06) 1.原型链 将一个类型的实例赋值给另一个构造函数的原型。 2.构造函数继承 在子类型构造...

网友评论

      本文标题:09 继承

      本文链接:https://www.haomeiwen.com/subject/eanvdttx.html