派生类引用:
class CStudent {
private:
string sName;
int nAge;
public: bool IsThreeGood() { };
void SetName(const string & name) { sName = name; }
};
class CUndergraduateStudent: public CStudent {
private:
int nDepartment;
public:
bool IsThreeGood() {}; //覆盖
bool CanBaoYan() {};
};
// 派生类的写法是:类名: public 基类名
派生类拥有基类的全部成员函数和成员变量,不论是private、protected、public.
在派生类的各个成员函数中,不能访问基类中的private成员。
派生类对象的体积,等于基类对象的体积,再加上派生类对象自己的成员变量的体积。在派生类对象中,包含着基类对象,而且基类对象的存储位置位于派生类对象新增的成员变量之前。

#include <iostream>
#include <string>
using namespace std;
class CStudent {
private:
string name;
string id;
char gender;
int age;
public:
void PrintInfo();
void SetInfor(const string&name_, const string &id_, int age_, char gender_);
string GetName() { return name; }
};
class CUndergraduateStudent :public CStudent {
//本科生类,继承了CStudent类
private:
string department; //学生所属的系的名字
public:
void QualifiedForBaoYan() { //给予保研资格
cout << "qualified for baoyan" << endl;
}
void PrintInfo() {
CStudent::PrintInfo();//调用基类的PrintInfo
cout << "Department:" << department << endl;
}
void SetInfor(const string&name_, const string&id_, int age_, char gender_, const string & deparment_) {
CStudent::SetInfor(name_, id_, age_, gender_);//调用基类的SetInfo
department = deparment_;
}
};
void CStudent::PrintInfo() {
cout << "Name:" << name << endl;
cout << "ID" << id << endl;
cout << "Age:" << age << endl;
cout << "Gender:" << gender << endl;
}
void CStudent::SetInfor(const string&name_, const string & id_, int age_, char gender_) {
name = name_;
id = id_;
age = age_;
gender = gender_;
}
int main() {
CUndergraduateStudent s2;
s2.SetInfor("Harry Potter", "118829212", 19, 'M', "Computer Science");
cout << s2.GetName() << " ";
s2.QualifiedForBaoYan();
s2.PrintInfo();
return 0;
}
复合关系的使用:
class CMaster;
class CDog{CMaster *pm};
class CMaster{CDog * dogs[10];};
//利用pointer指定关系
访问范围说明符
基类的private成员: 可以被下列函数访问
- 基类的成员函数
- 基类的友员函数
基类的public成员: 可以被下列函数访问
•基类的成员函数
•基类的友员函数
•派生类的成员函数
•派生类的友员函数
- 其他的函数
基类的protected成员: 可以被下列函数访问
•基类的成员函数
•基类的友员函数
- 派生类的成员函数可以访问当前对象的基类的保护成员
class Father {
private: int nPrivate; //私有成员
public: int nPublic; //公有成员
protected: int nProtected; // 保护成员
};
class Son : public Father {
void AccessFather() {
nPublic = 1; // ok;
nPrivate = 1; // wrong
nProtected = 1; // OK, 访问从基类继承的protected成员
Son f;
f.nProtected = 1; //wrong, f不是当前对象
}
};
int main() {
Father f;
Son s;
f.nPublic = 1; // Ok
s.nPublic = 1; // Ok
f.nProtected = 1; // error
f.nPrivate = 1; // error
s.nProtected = 1; //error
s.nPrivate = 1; // error
return 0;
}
派生类的构造函数
派生类对象 包含 基类对象 执行派生类构造函数之前, 先执行基类的构造函数 派生类交代基类初始化, 具体形式:
构造函数名(形参表): 基类名(基类构造函数实参表)
{
}
class Bug {
private:
int nLegs; int nColor;
public:
int nType;
Bug(int legs, int color);
void PrintBug() { };
};
class FlyBug : public Bug { // FlyBug是Bug的派生类
int nWings;
public:
FlyBug(int legs, int color, int wings);
};
Bug::Bug(int legs, int color) {
nLegs = legs;
nColor = color;
}
//错误的FlyBug构造函数:
FlyBug::FlyBug(int legs, int color, int wings) {
nLegs = legs; // 不能访问
nColor = color; // 不能访问
nType = 1; // ok
nWings = wings;
}
//正确的FlyBug构造函数:
FlyBug::FlyBug(int legs, int color, int wings) :Bug(legs, color) {
nWings = wings;
}
int main() {
FlyBug fb(2, 3, 4);
fb.PrintBug();
fb.nType = 1;
fb.nLegs = 2; // error.nLegs is private
return 0;
}
public继承的赋值兼容规则:
class base { };
class derived : public base { };
base b;
derived d;
1) 派生类的对象可以赋值给基类对象
b = d;
2) 派生类对象可以初始化基类引用
base & br = d;
3) 派生类对象的地址可以赋值给基类指针
base * pb = & d;
如果派生方式是 private或protected,则上述三条不可行。
No comments:
Post a Comment