CPP-Inheritance
来源:互联网 发布:淘宝鹊桥活动怎么退出 编辑:程序博客网 时间:2024/06/03 21:36
Derived Classes
如果已经有一个一般的类,现在要基于此类建立另外一个类。此时就要用到类的继承。例如,现在已经有个类employee, 想建立一个manager类。manager是特殊的employee. 可以用继承机制来建立。
class Manager : public Employee{ public: new member functions private: new data members
: 表示了继承。
在Manager类中,只需要指定新的成员函数和数据成员。Employee类中所有的成员函数和数据成员都将被Manager自动继承。此例子中,Employee是基类(base class)。Manager是导出类(derived class).
class Person{public:const string& is_called() const;void change_name(const string& n);void write_info() const;private:string name;};
类person,有得到和改变名字的成员函数,成员函数定义如下:
const string& Person::is_called() const{return name;}void Person::change_name(const string& n){ name = n;}void Person::write_info() const{cout<<"Name: "<<name<<endl;}
如果我们想描述一个雇员,可以定义一个新类:
calss Employee : public Person{public:Employee():salary(0) {};long int earns() const;void change_salary(long int new_salary);void write_info()const;private:long int salary;};
Person写在冒号后面,暗示新类Employee是Person类的亚类(subclass).
成员函数earns和change_salary定义:
long int Employee::earns() const{ return salary;}void Employee::change_salary(long int new_salary){salary=new_salary;}
导出类继承了基类所有的数据成员。这意味着Employee的对象将包含数据成员name和salary. 注意基类不能用导出类里面新添加的成员函数。如Person的对象无法使用change_salary()和earns()两个成员函数。
导出类里面可以使用和基类同名的成员函数,其结果是得到一个新的成员函数,基类的成员函数被屏蔽掉。例如,如果我们也在类Employee中声明一个数据成员name,则类Employee中则有两个成员函数叫name,但第一个将被第二个隐藏。
上例子中,我们也在类Employee中中声明了一个和基类Person中同名的成员函数write_info().这将隐藏在Person中定义的成员函数。如果我们写e.write_info(),由于e是类Employee的一个对象,则在类Employee中定义的write_info()将被调用。但是如果我们写成p.write_info().则类Person中定义的成员函数write_info()将被调用。
类Employee中的成员函数write_info()可以写成:
void Employee::write_info() const{ Person::write_info(); cout << "Salary: " << salary << endl;}
第三行显示了我们该怎样访问一个隐藏的成员。
在使用继承类时,必须避免函数重载。如果一个函数在导出类中声明,所有在基类中的同名函数将被隐藏,而不管他们到底有什么参数。
有一些函数不能被继承:1) constructor 2) destructors 3) friend functions 4)assignment operator=.
assignment operator不能被继承是因为每个类都自动定义一个,因此基类的assignment operator被隐藏了。
导出类也可以有subclass. 因此,我们可以定义一个Programmer,它是Employee的subclass:
class Programmer : public Employee{public:const string& read_favorite() const {return fav;}void change_favorite(const string& f){fav=f}private:string fav;};
数据成员fav暗示程序员最喜欢的编程语言。类Employee是直接的基类,因为它写在programmer的定义中。类person是间接基类。类programmer的对象将继承它所有基类的属性。这意味着programmer有三个数据成员:name, salary, fav. 成员函数也可以被继承,因此,下列语句正确:
Programmer pr;pr.change_name("Shae");pr.change_salary(500000);pr.change_favorite("C++");pr.write_info(); //Employee中的write_info()将被调用
注意:最后一行中,Employee中的成员函数write_info()将被调用,因为它是programmer的直接基类。
一个基类可以导出许多彼此独立的类。例如,我们可以定义一个类定义临时雇员:
class Tem_emp : public Emploee{public :Date from, to;};class Date{public:int year,mon,day;};
- CPP-Inheritance
- Cpp的继承Inheritance-笔记
- CPP-Constructor, destructors and inheritance
- Inheritance
- Inheritance
- Inheritance
- Inheritance
- 【一天一篇CPP】多继承及其二义性(multiple inheritance,也叫多重继承,但不是多层继承)
- 继承 Inheritance
- private inheritance
- C++ Inheritance
- C++ Inheritance
- C++ inheritance
- Prototype Inheritance
- Inheritance Puzzle2
- updating Inheritance
- Inheritance & Composition
- virtual inheritance
- {Effective Java} Chap 7 Methods
- 二叉树
- LeetCode-Merge k Sorted Lists
- Reverse Second half of LinkedList Java实现
- LeetCode 题解(270) : 3Sum Smaller
- CPP-Inheritance
- 使用json-lib封装json
- *LeetCode-Sliding Window Maximum
- 两个数组求和为一个固定数值的所有组合java实现
- 左右滑动cell,调出菜单
- Oracle Flashback技术之Flashback Database
- gradle--第十五章 使用文件2
- Java环境配置之javac不是内部或外部命令
- 2015 10 08