继承与派生是同一个过程从不同的角度看:
保持已有类的特性而构成新类的过程称为继承
在已有类的基础上新增自己的特性而产生新类的过程称为派生
被继承的已有类称为基类(或者父类)
派生出的新类称为派生类(或子类)
直接参与派生出某类的基类称为直接基类
基类的基类甚至更高的基类称为间接基类
继承的目的:实现设计与代码的重用
派生的目的:当新的问题出现,原有程序无法解决(或者不能完全解决)时,需要对原有的程序进行改造。
继承:在一个已存在的类的基础上建立一个新的类。
已存在的类:基类(base class)或父类(father class)。
新建立的类:派生类(derived class)或子类(son class)。
类的继承层次结构
单继承(一个派生类只从一个基类派生)
多重继承(一个子类有两个或多个父类)
基类和派生类的关系:派生类是基类的具体化,而基类则是派生类的抽象。
public是继承方式,除了public之外,还有private和protected。
指定的继承方式不一样,决定的是基类的成员在派生类中的访问属性。
最重要的一句
(1)基类的私有成员在派生类中均是不可访问的,它只能由基类的成员访问。
(2)在公有继承方式下,基类中的公有成员和保护成员在派生类中的访问属性不变。
(3)在保护继承方式下,基类中的公有成员和保护成员在派生类中均为保护的。
(4)在私有继承方式下,基类中的公有成员和保护成员在派生类中均为私有的。
编写一个程序:设计一个汽车类,数据成员有轮子个数、车重。
小车类是汽车类的私有派生类,包含载客量。
卡车类是汽车类的私有派生类,包含载客数和载重量。每个类都有数据的输出方法
#include <iostream>
using namespace std;
class xiaoche {
private:
int lunzi;
int weight;
public:
xiaoche(int x, int y): lunzi(x), weight(y) {}
void print() {
cout << lunzi << " " << weight << endl;
}
~xiaoche() {
}
};
class qiche: private xiaoche {
private:
int number;
public:
qiche(int x, int y, int z): xiaoche(x, y), number(z) {
}
void print() {
xiaoche::print();
cout << number << endl;
}
~qiche() {
}
};
class kache: private xiaoche {
private:
int num;
int wei;
public:
kache(int x, int y, int n, int w): xiaoche(x, y), num(n), wei(w) {
}
void print() {
xiaoche::print();
cout << num << " " <<wei<< endl;
}
~kache() {
}
};
int main () {
xiaoche c(4, 10);
qiche s(8, 12, 3);
kache l(4, 12, 5, 25);
c.print();
cout << endl;
s.print();
l.print();
return 0;
}
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
class fh {
private:
int id;
string name;
string sex;
int age;
public:
fh(int a, string b, string c, int d): id(a), name(b), sex(c), age(d) {
}
~fh() {
}
void play() {
cout << "id: " << id << endl;
cout << "name: " << name << endl;
cout << "sex: " << sex << endl;
cout << "age: " << age << endl;
}
};
class fg: public fh {
private:
int math;
int ph;
int eng;
int cpp;
int total;
public:
fg(int a, string b, string c, int d, int e, int f, int g, int h): fh(a, b, c, d), math(e), ph(f), eng(g), cpp(h) {
total = e + f + g + h;
}
~fg() {
}
void pin();
};
void fg::pin() {
fh::play();
cout << "math: " << math << endl;
cout << "physical: " << ph << endl;
cout << "english: " << eng << endl;
cout << "cpp: " << cpp << endl;
cout << "total: " << total << endl;
}
int main () {
fh sb(1, "张三", "M", 18);
sb.play();
cout << endl;
fg sb2(1, "张三", "M", 18, 100, 90, 80, 70);
sb2.pin();
return 0;
}
#include <iostream>
using namespace std;
class Point {
public:
Point(int xx = 0, int yy = 0) {
x = xx;
y = yy;
}
int getX() {
return x;
}
int getY() {
return y;
}
void show() {
cout << "(" << x << "," << y << ")" << endl;
}
private:
int x;
int y;
};
class Circle: public Point {
public:
Circle(int xx = 0, int yy = 0, float r = 1): point(xx, yy) {
radius = r;
}
double getR() {
return radius;
}
void show() {
cout << "圆心坐标:";
point.show();
cout << "圆半径:" << radius << endl;
}
private:
Point point;
float radius;
};
class Cylinder: public Circle {
public:
Cylinder(int xx = 0, int yy = 0, float r = 1, float h = 2): circle(xx, yy, r) {
height = h;
}
float getH() {
return height;
}
void show() {
circle.show();
cout << "圆柱体高度:" << height << endl;
}
private:
Circle circle;
float height;
};
int main() {
Point p1(5, 5);
p1.show();
cout << endl;
Circle c1(7, 8, 9);
c1.show();
cout << endl;
Cylinder cy1;
cy1.show();
return 0;
}