为了正常的体验网站,请在浏览器设置里面开启Javascript功能!
首页 > Lecture 9&10

Lecture 9&10

2011-06-30 50页 ppt 666KB 30阅读

用户头像

is_084769

暂无简介

举报
Lecture 9&10null Yang Yong Jiangxi University of Finance & Economics Yang Yong Jiangxi University of Finance & EconomicsDerived Classes《Object Oriented Programming》—ObjectivesObjectivesHow to define derived class according to different modes of inheritance, i.e., public inher...
Lecture 9&10
null Yang Yong Jiangxi University of Finance & Economics Yang Yong Jiangxi University of Finance & EconomicsDerived Classes《Object Oriented Programming》—ObjectivesObjectivesHow to define derived class according to different modes of inheritance, i.e., public inheritance, private inheritance and protected inheritance. How to realize the constructor and destructor in the derived class. Grasp the mechanism of derived class with member objects in case of multi-inheritance. Review questions : Review questions : Please tell the difference of public members and private members in a class. What is the difference of static member data and common member data. Why do we need to free a pointer after it has been used? Please answer the question of the following program. Program: Tell whether the program is wrong and how to correct it. Program: Tell whether the program is wrong and how to correct it. class Point { double X,Y; public: Point(double xx, double yy) { X=xx; Y=yy; } void SetXY(double xx, double yy) { X=xx; Y=yy; } double GetX() { return X; } double GetY() { return Y; } }; void main() { Point p1; p1.SetXY(1,2); cout<<"X="<< p1.X<<'\t'<<"Y="< base { ... }; class derivative inheritances from class base, access is the mode of inheritance, which can be public, private and protected. Different access leads to different inheritance. Single-inheritance (cont.) Single-inheritance (cont.)Three modes of inheritance: Public inheritance Private inheritance Protected inheritance Notes: Default of access means private inheritance friend functions can not be inherited ExampleExampleModes of inheritanceModes of inheritanceTwo effects of different manners of inheritance: 1. Members of derived class control and access the members of base class. 2. Objects of derived class control and access the members of base class. Public inheritance:Public inheritance:Member functions of derived class can directly access the public and protected members of base class, but can’t access the private members of base class. The objects of derived class can only access the public members of base class. Modes of inheritance (cont.)Example of public inheritanceExample of public inheritanceclass Point //base class { private: //private member data float X, Y; public: //public member functions void InitP(float xx=0, float yy=0) { X=xx; Y=yy; } void Move(float xOff, float yOff) { X+=xOff; Y+=yOff; } float GetX() { return X; } float GetY() { return Y; } };nullclass Rectangle: public Point //derived class { private: //added new member data float W,H; public: //added new public member functions void InitR(float x, float y, float w, float h) { InitP(x,y); // call the functions of the base class W=w; H=h; } float GetH() { return H; } float GetW() { return W; } };null#include #include void main() { Rectangle rect; // object of derived class rect.InitR(2,3,20,10); // access public members from base class directly rect.Move(3,4); cout< #include int main() { Rectangle rect; rect.InitR(2,3,20,10); rect.MoveR(3,4); //NOT Move(3,2) cout< #include class student :public person{ char *Department; char *Speciality; int Number; public: student(char *, int , char , char*, char *, int); student(student &); null ~student() { delete [ ]Department; delete [ ]Speciality; } void SetDep(char *); void SetSpec(char *); void SetNum(int num){ Number=num;} char *GetDep() {return Department;} char *GetSpec(){return Speciality;} int GetNum(){ return Number;} };null student ::student(char *name, int age, char sex, char *dep, char *spe, int num):person(name, age,sex) { Department=new char[strlen(dep)+1]; strcpy(Department,dep); Speciality=new char[strlen(spe)+1]; strcpy(Speciality,spe); Number=num; }null student::student(student &stu):person(stu) { Department=new char[strlen(stu.Department)+1]; strcpy(Department,stu.Department); Speciality=new char[strlen(stu.Speciality)+1]; strcpy(Speciality,stu.Speciality); Number=stu.Number; }nullvoid student::SetDep(char *dep) { if(Department) delete Department; Department=new char[strlen(dep)+1]; strcpy(Department,dep); } void student::SetSpec(char *spe) { if(Speciality) delete Speciality; Speciality=new char[strlen(spe)+1]; strcpy(Speciality,spe); }null void main() { student stu1("Wang Ping", 23, 'f', "Computer Dep.", "Computer Software", 12345); cout< class Bed{ public: Bed( ) :weight(0){cout<<"Default Bed"< class B1 //base class B1 {public: B1(int i) {cout<<"constructing B1 "<
/
本文档为【Lecture 9&10】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。 本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。 网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。

历史搜索

    清空历史搜索