C++学习02
继承
- 继承让我们能依据另一个类来定义一个类,这种特性也很容易创建和维护一个程序,从而达到了重用代码功能和提高执行效率的一个效果。
当创建一个类时,无需重新编写新的数据成员、成员函数,只需指定该类继承一个已有类的成员即可,
上述已有类称为基类,新建类则为派生类。
语法:
a.基本:class子类(派生类) : 继承方式 父类(基类)
1
2
3
4
5
6
7
8class A{
public:
int a;
protected:
int b;
private:
int c;
};b.共有继承:基类的公用成员和保护成员在派生类中保持原有访问属性,其私有成员仍为私有。
1
2
3
4
5
6
7
8class B : public A{
public:
int a;
protected:
int b;
//不可访问:
int c;
};c.私有继承:基类的公用成员和保护成员在派生类中变为私有属性,其私有成员仍为私有。
1
2
3
4
5
6
7class B : private A{
private:
int a;
int b;
//不可访问
int c;
};d.保护继承:基类的公用成员和保护成员在派生类中变为保护属性,其私有成员仍为私有。
1
2
3
4
5
6
7class B : protected A{
protected:
int a;
int b;
//不可访问
int c;
};多继承:一个子类可有多个父类,继承多个父类的特性。
语法:
1
2
3
4class <派生类名>:<继承方式1><基类1>,<继承方式2><基类2>,...
{
<派生类类体>
};[!NOTE]
访问修饰符继承方式可以是public、protected或private中的任何一个。
其他注意:
a.派生类是不能继承基类的析构函数,也需要通过派生类的析构函数调用基类的析构函数。
b.继承中先调用父类构造函数,再调用子类的,析构函数的调用顺序与之相反。
c.C++中构造函数不能被派生类继承!
多态
多态指具有不同功能的函数可以用同一个函数名。
这也意味在调用成员函数时会根据调用函数的对象的不同类型来执行不同函数。
当类之间存在层次结构&&类之间通过继承关联时,会用到多态。
静态多态:函数重载、运算符重载
a.重载声明指一个与之前已在该作用域内声明过的函数/方法具有相同名称的声明,但参数列表和定义不同。
b.函数重载:同名函数功能类似,但形式参数必须不同。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20class Print{
public:
void print(int m) {
cout<<"整数为:"<<m<<endl;
}
void print(double n) {
cout<<"浮点数为:"<<n<<endl;
}
void print(char chr[]) {
cout<<"字符串为:"<<chr<<endl;
}
};
int main(){
Print p;
p.print(5);
p.print(3.14159);
char chr[]="h3llo_w0rld";
p.print(chr);
return 0;
}c.运算符重载:运算符为带有特殊名称的函数,函数名由关键字operator和运算符符号构成。同时,重载运算符也有一个返回类型与一个参数列表。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31//语法
Box operator+(const Box&); //类成员函数
Box operator+(const Box&, const Box&); //类的非成员函数
//示例
class Box{
public:
double getVolume(void) {
return length * breadth * height;
}
void setLength(double len) {
length = len;
}
void setBreadth(double bre) {
breadth = bre;
}
void setHeight(double hei) {
height = hei;
}
//重载+运算符,把两Box对象相加
Box operator+(const Box& b) {
Box box;
box.length = this->length + b.length;
box.breadth = this->breadth + b.breadth;
box.height = this->height + b.height;
}
private:
double length;
double breadth;
double height;
};运算符列表:
动态多态:派生类和虚函数实现运行时多态
a.虚函数 是在基类中使用关键字virtual声明的函数。在派生类中重新定义基类中的定义的虚函数时,会告诉编译器不要静态链接到基类中的该函数。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17class Shape{
protected:
int width, height;
public:
Shape(int a=0, int b=0) {
width = a;
height = b;
}
virtual int area() {
cout<<"Parent class area: "<<endl;
return 0;
}
//纯虚函数
virtual int area() = 0;
};
文件与流
**基本输入输出(I/O)发生在流(字节序列)**中。
文件操作:从文件读取流和向文件写入流。
a.打开文件:
1
2void open(const char *filename, ios::openmode mode);
//第一个参数指定文件名称&位置,第二个参数定义文件被打开的模式打开模式:
1
2
3
4
5
6
7//以写入模式打开文件,并希望截断文件
ofstream out;
out.open("file.dat", ios::out | ios::trunc);
//打开一个文件用于读写
ifstream afile;
afile.open("file.dat", ios::out | ios::in);b.关闭文件:
1
void close(); //close()是fstream、ifstream和ofstream对象的一个成员
c.读取&写入示例代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31#include <fstream>
#include <iostream>
using namespace std;
int main(){
char data[105];
//已写模式打开文件
ofstream outfile;
outfile.open("afile.dat");
cout<<"Writing..."<<endl;
cout<<"Name: ";
cin.getline(data, 105);
outfile<<data<<endl; //数据写入文件
cout<<"Age: ";
cin>>data;
cin.ignore(); //ignore()函数会忽略之前读语句留下的多余字符。
outfile<<data<<endl;
outfile.close();
//以读模式打开文件
ifstream infile;
infile.open("afile.dat");
cout<<"Reading..."<<endl;
infile>>data;
cout<<data<<endl;
infile>>data;
cout<<data<<endl;
infile.close();
return 0;
}
后记
这个时间点的C++学习…懂得都懂 :)