Download presentation
Presentation is loading. Please wait.
1
Inheritance -II
2
Contents Member Functions 1 Access Control 2 Case Study 3
3
Member functions The member functions of the derived class can be derived from its base class.(派生类成员函数可以从基类中派生) int main() { Manager Jack("Jack", 10, 5); Jack.print(); return 0; } class Employee{ public: Employee(string& n, int d) { name = n; department = d; } void print() const { cout << name << department << endl; } private: string name; int department; }; Result: Jack10 class Manager : public Employee{ public: Manager(string& n, int d, int lvl) : Employee(n, d), level(lvl){} private: int level; };
4
Member functions class Employee{ public: Employee(string& n, int d) { name = n; department = d; } void print() const { cout << name << department << endl; } private: string name; int department; }; The member functions of the derived class can also be re-defined when their names are the same as ones of its base class.(当派生类的成员函数与基类的函数重名时,可以重定义派生类的成员函数) class Manager : public Employee{ public: Manager(string& n, int d, int lvl) : Employee(n, d), level(lvl){} void print() const; private: int level; }; Cant access the private members in the other class ? How to define it void Manager::print() { cout << name << department; cout << level; }
5
Member functions Overriding functions (覆写基类成员函数)
class Manager : public Employee{ public: Manager(string& n, int d, int lvl) : Employee(n, d), level(lvl){} void print() const; private: int level; }; Overriding functions (覆写基类成员函数) void Manager::print() { print(); cout << level; } void Manager::print() { Employee::print(); cout << level; } Redefining member functions of the base class in the derived class. Change the implementation of the base class functions.(在派生类中重定义基类的成员函数,改变基类成员函数的实现) 覆盖基类原有的成员函数
6
Member functions Overriding functions and overloading functions (覆写与重载函数) When you override a function of the derived class, the corresponding functions must have the same return type, name, number, and types of parameters.(相同的函数原型) (当覆写派生类的函数时,对应的函数必须有完全相同返回类型, 函数名, 参数个数和类型). When you overload a function of the derived class, the function can have the same name and different parameter list.(不同的函数原型) (当重载保生类的函数时,有相同函数名,但参数不同)
7
Access Control(存取控制) # Access control in a class
The members of a class are classified into three categories: private(私有), protected(保护), or public(公有). Test - a: int + b: int # c: int If it is private, its name can be used only by member functions and friends of the class in which it is declared.(只能本类成员和友元访问) If it is public, its name can be used by any function.(任何函数访问) If it is protected, its name can be used only by member functions and friends of the class in which it is declared and by member functions and friends of classes derived from this class.(只能本类成员和友元以及它的派生类成员和友元访问) #
8
Access Control Protected members(保护成员) //error- private
int main() { Test ta(20, 30); ta.a = 99; ta.b = 100; ta.c = 50; cout << ta.geta() << endl; cout << ta.getb() << endl; cout << ta.c << endl; return 0; } class Test{ int a; protected: int b; public: int c; Test(int n, int m) :a(n), b(m){} int geta(){ return a; } int getb(){ return b; } }; //error- private //error -protected //ok- public
9
Access Control
10
Different Inheritances
Access to Base Classes Like a member in a class, the base class can be declared private, protected, or public.( 基类可以有三种派生:私有、保护和公有) Public derivation makes the derived class a subtype of its base class, it is the most common form of derivation; class Manager: public Employee{};
11
Different Inheritances
Protected and private derivations are used to represent implementation detail. Protected base classes are useful in class hierarchies in which further derivation is the norm; class Manager: protected Employee{}; Private base classes are most useful when defining a class by restricting the interface to base so that stronger guarantees can be provided. class Manager: (private) Employee{ }; // (default private)
12
Different Inheritances
Base class Access control Derived class General users inheritance private private NA NA protected private A public protected A public A A
13
Different Inheritances
class RichMan { public: RichMan(); ~RichMan(); int company; //公司能被自己(基类),创业伙伴(友元),儿子(子类),其他人(外部)都可以访问 private://钱和车子能被自己(基类),创业伙伴(友元)可以访问.儿子和外人都不给开 int money; int car; protected: int house; // 房子能被自己(基类),创业伙伴(友元)可以访问,儿子(子类)也可以访问,外人是不可以访问 }; RichMan car: int money: int # house: int + company: int + RichMan() + ~RichMan()
14
Public Inheritance class LittleRichMan : public RichMan RichMan {
}; RichMan car: int money: int # house: int + company: int + RichMan() + ~RichMan() LittleRichMan + LittleRichMan() + ~LittleRichMan() public public class LittleRichMan : public RichMan { public: LittleRichMan(); ~LittleRichMan(); int m_company;//base class protected: int m_house; //base class }; 他的儿子继续开着公司,住着房子,但他爸爸的车子和钱是拿不到
15
Protected Inheritance
class LittleRichMan : protected RichMan { public: LittleRichMan(); ~LittleRichMan(); }; RichMan car: int money: int # house: int + company: int + RichMan() + ~RichMan() LittleRichMan + LittleRichMan() + ~LittleRichMan() protected protected class LittleRichMan : protected RichMan { public: LittleRichMan(); ~LittleRichMan(); protected: int m_company;// base class int m_house;// base class }; 公司和房子是除了外人不可以访问,自己,友元,子类都可以访问
16
Private Inheritance class LittleRichMan : private RichMan { public:
}; RichMan car: int money: int # house: int + company: int + RichMan() + ~RichMan() LittleRichMan + LittleRichMan() + ~LittleRichMan() private private class LittleRichMan : private RichMan { public: LittleRichMan(); ~LittleRichMan(); private: int m_company;//base class int m_house;// base class }; 儿子占用了公司和房子,这样除了自己和友元,任何方式都不得访问
17
Case Study Design a system that opens different postfixed files, e.g. .txt, .png and etc. textFile + openFile(): void File # filename: string imageFile
18
class File{ public: File(string& filename) { fileName = filename; } void openFile() { cout << "Open file!\n"; } protected: string fileName; }; #include <Windows.h> #include <ShellAPI.h> #include <iostream> using namespace std; 在C++标准里定义了两个字符串string和wstring string看作char[], wstring使用的是wchar_t类型,宽字符,用于满足非ASCII字符的要求,例如Unicode编码,中文,日文,韩文等。 class textFile :public File{ public: textFile(string& filename) :File(filename){} void openFile() { fileName = fileName + ".txt"; wstring temp(fileName.begin(),fileName.end()); LPCWSTR lpFile = temp.c_str(); //wstring -> char* ShellExecute(NULL, L"open", L"notepad.exe", lpFile, NULL, SW_SHOW); } }; LPCWSTR是一个指向unicode编码字符串的32位指针,所指向字符串是wchar型,而不是char型
19
class bmpFile :public File{
bmpFile(string& filename) :File(filename){} void openFile() { fileName = fileName + ".png"; wstring temp(fileName.begin(), fileName.end()); LPCWSTR lpFile = temp.c_str(); ShellExecute(NULL, L"open", L"mspaint.exe", lpFile, NULL, SW_SHOW); } }; int main() { string filename = "aa"; textFile text(filename); text.openFile(); //Sleep(3000); system("pause"); bmpFile bmp(filename); bmp.openFile(); return 0; }
20
Summary Be able to override the member function of derived class
Be able to know access control in the class Be able to know the difference of private, protected and public inheritances Understand the concept of inheritance
Similar presentations