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...
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<group; // manager's employee
short level; // people managed
};nullvoid g(Manager mm, Employee ee)
{
Employee* pe = &mm; // ok: every manager is an employee
Manager* pm = ⅇ // error: not every Employee is a Manager
pm->level = 2; // disaster: ee doesn't have a 'level'
……
}Important notes 1 (cont.)Important notes 2Important notes 2Using a class as a base is equivalent to declaring an (unnamed) object of that class. Consequently, a class must be defined in order to be used as a base.
class Employee; // declaration only, no definition
class Manager : public Employee { // error: Employee not defined
/ / ...
};
Important notes 3Important notes 3Direct base class
Explicitly listed behind the derived class with the colon (:) notation when that derived class is declared.
class HourlyWorker : public Employee
Employee is a direct base class of HourlyWorker
Indirect base class
inherited from two or more levels up the class hierarchy
class MinuteWorker: public HourlyWorker
Employee is an indirect base class of MinuteWorkerMulti-inheritanceMulti-inheritanceImplementation of inheritance
class derivative : base1, base2, … basen {
private:
Members;
Public:
Members;
protected:
Members;
};
It can be regarded as the extended version of single-inheritance.Multi-inheritance (cont.)Multi-inheritance (cont.)
Typical relationship between derived classes and the base class. B1AC2B2nullclass A{
public:
void setA(int);
void showA();
private:
int a;
};
class B{
public:
void setB(int);
void showB();private:
int b;
};
class C : public A, private B
{
public:
void setC(int, int, int);
void showC();
private:
int c;
};Example 1 of multi-inheritancenullvoid A::setA(int x)
{ a=x; }
void B::setB(int x)
{ b=x; }
void C::setC(int x, int y, int z)
{
//access the public members directly from the base classes
SetA(x);
SetB(y);
c=z;
}
//other functions are omittedint main()
{
C obj;
obj.setA(5);
obj.showA();
// obj.setB(6); error
// obj.showB(); error
obj.setC(6,7,9);
obj.showC( );
return 0;
}Example 2 of multi-inheritanceExample 2 of multi-inheritance
class A{
protected:
int w;
public:
void SetW(int a) {w=a;}
int GetW() {return w;}
};nullclass B{
protected:
int x,y;
public:
void SetX(int a) {x=a;}
void SetY(int b) {y=b;}
int GetX() {return x;}
int GetY() {return y;}
};nullclass D: public A, B{
int z;
public:
void SetZ(int c) {z=c;}
void SetBase(int a, int b, int c)
{x=a;y=b;w=c;}
int GetBX() {return x;}
int GetBY() {return y;}
int GetBW() {return w;}
int GetZ() {return z;}
int Sun() {return z+x+y+w;}
};nullvoid main()
{ D d;
d.SetBase(1,2,3);
d.SetZ(4);
cout<<"x="<
class B{
public:
B();
B(int i);
~B();
void Print() ;
private:
int b;
};nullB::B()
{
b=0;
cout<<"B's default constructor called."<
#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,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。