0%

C语法基础大全-面向对象

一、结构体

1.1 概述

C++中的结构体可以定义函数,C语言中的结构体只能定义变量

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
struct Sales_data{
Sales_data() = default; // = default表示默认的构造函数
Sales_data(const std::string &s):bookNo(s){}
Sales_data(const std::string &s, unsigned n, double p):bookNo(s),units_sold(n),revenue(p*n){}
Sales_data(std::istream &); // 没有加形参名表示只是声明类型,但没有实现,实现时需要加上形参名

std::string isbn() const {return bookNo;}
Sales_data& combine(const Sales_data&);
double avg_price() const;
std::string bookNo;
unsigned units_sold = 0;
double revenue = 0.0;
};

// 类外实现
Sales_data::Sales_data(std::istream &is){
read(is, *this);
}

1.2 动态内存分配

  • new/delete分配堆空间可以调用类的构造函数/析构函数
  • malloc/free只是一个函数调用,不会调用构造函数/析构函数,malloc接受的参数是一个unsigned long类型
1
2
3
4
5
Time *t2 = new Time(0,0,0);
delete t2;

Time *t1 = (Time*)malloc(sizeof(Time));
free(t1);

二、类

2.1 类的定义

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
class ClassName
{
public:
//公共的行为或属性
ClassName(); // 构造函数
ClassName(string x);
void setName(string x);
string getName();
~ClassName(); // 析构函数

private:
//私有成员
string x;
};

// 类外实现
ClassName::ClassName() {
this->x = "hello world";
cout << "开始创建对象:" << this->x << endl;
}

ClassName::ClassName(string x) {
this->x = x;
}

void ClassName::setName(string x) {
this->x = x;
}

string ClassName::getName() {
return this->x;
}

ClassName::~ClassName() {
cout << "销毁对象:" << this->x << endl;
}

int main() {
ClassName *cn = new ClassName();

cn->setName("hello gaoyue");
cout<<cn->getName()<<endl;

delete cn;

return 0;
}
/*
开始创建对象:hello world
hello gaoyue
销毁对象:hello gaoyue
*/

2.1.1 对象的创建

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// 默认无参构造
// 显示使用
ClassName cn = ClassName(); // 无法delete,会调用构造函数,程序结束后会调用析构函数
// 隐式使用
ClassName cn;
// new创建
ClassName *cn = new ClassName;

// 有参构造
// 显示使用
ClassName cn = ClassName("hello gaoyue");
// 隐式使用
ClassName cn("hello gaoyue");
// new创建
ClassName *cn = new ClassName("hello gaoyue");

2.1.2 const成员函数

有时候,我们创建了一个对象,但是事实上,我们只希望这个对象初始化之后不被改变,它可以是一个真理或者是什么,就是不能被改变

然后我们就十分自然的想到const创建一个对象。以下面的代码为例,但是后面发现就算是调用对象中不会改变值的showInfo()函数的时候,也会报错。

1
const ClassName *cn = new ClassName();

如果这个常量对象想要调用不改变值的方法,那么这个方法上也必须加上const关键字

1
2
3
4
5
6
7
8
class ClassName{
public:
string getName() const;
}

string ClassName::getName() const {
return this->x;
}

2.1.3 友元函数

  • 某非成员函数在类中被声明为友元函数,其可以直接访问类中的成员变量(包括私有和保护)
  • 使普通函数能够访问类的友元
1
2
3
4
5
6
7
8
9
10
11
12
13
class INTEGER{
friend void Print(const INTEGER& obj);//声明友元函数
};

void Print(const INTEGER& obj){
   //函数体
}

void main(){
  INTEGER obj;
  Print(obj);//直接调用
}

2.1.4 友元类

  • 类Y的所有成员函数都为类X友元函数
  • 使用单个声明使Y类的所有函数成为类X的友元,它提供一种类之间合作的一种方式,使类Y的对象可以具有类X和类Y的功能。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class girl;

class boy{
public:
  void disp(girl &);
};

void boy::disp(girl &x){ //函数disp()为类boy的成员函数,也是类girl的友元函数
  cout<<"girl's name is:"<<x.name<<",age:"<<x.age<<endl;//借助友元,在boy的成员函数disp中,借助girl的对象,直接访问girl的私有变量
}

class girl{
private
  char *name;
  int age;
  friend boy; //声明类boy是类girl的友元
};

  • 成员函数有this指针,而友元函数没有this指针
  • 友元函数是不能被继承的,就像父亲的朋友未必是儿子的朋友
  • 友元函数的调用无需通过对象调用,而是同调用普通函数一样被调用

2.2 构造函数与析构函数

2.3 继承与多态