初始类与对象

初始类与对象

实验介绍

本课程是进一步对类与对象的深入认识,如何定义并实例化一个类,介绍如何使用 C++ 标准库 string 类等。

知识点
  • 认识类与对象
  • 内联函数
  • string
  • 类的定义与实例化

认识类与对象

官方定义

类:在面向对象编程中是一种面向对象计算机编程语言的构造,是创建对象的蓝图,描述了所创建的对象共同的属性和方法。

对象:通过类创建出对象,又称实例化对象。

深入理解类

类是 C++ 的核心特性,在 C++ 的世界中,可以将所有事物都看作为一个对象,将对象进行封装之后成为一个类,通常被称为用户定义类型。

例如:可以将猫、狗、鸟、桌子、学生、人、三角形、矩形等都封装为一个类,只要在实际编程中用到的都可以抽象封装为一个类。

C++ 相关概念
概念描述
构造函数类的构造函数是一种特殊的函数,在创建一个新的对象时自动调用
析构函数类的析构函数也是一种特殊的函数,在删除所创建的对象时自动调用
拷贝构造函数拷贝构造函数,是一种特殊的构造函数,它在创建对象时,是使用同一类中之前创建的对象来初始化新创建的对象
友元函数友元函数可以访问类的 privateprotected 成员
内联函数通过内联函数,编译器试图在调用函数的地方扩展函数体中的代码
类成员函数类的成员函数是指那些把定义和原型写在类定义内部的函数,就像类定义中的其他变量一样
类访问修饰符类成员可以被定义为 publicprivateprotected。默认情况下是定义为 private
this 指针每个对象都有一个特殊的指针 this,它指向对象本身
静态成员类的数据成员和成员函数都可以被声明为静态的

自定义类

假设最近在做一个学校管理系统的项目,首先想到可以将学生定义为一个类,然后发现可以在学生类的基础上再抽象出一个人类的对象,如果需要用到学生的学科,可以进一步将学科也封装为一个,还可以封装文件操作类,数据库操作类等等。

示例代码 1

定义一个学生类:

#include <iostream>
#include <string>
using namespace std;

// class 类关键字、Student 类名
class Student
{
// 访问限制符 - 公有属性
public:
    Student() {}    // 构造函数
    ~Student() {}    // 析构函数

    // 成员函数
    void setName(string name) { this->name = name; }
    string getName() const { return name; };
    void setAge(int age) { this->age = age; }
    int getAge() const { return age; }
// 访问限制符 - 私有属性
private:
    // 数据成员
    string name;
    int age;
};

类定义注意事项:

  1. 定义一个类时,需要使用关键字 class 来进行修饰。
  2. 一个简单的类可以没有构造函数和析构函数,但是系统自动生成一个默认的构造函数和析构函数。
  3. 在类中使用关键字 publicprivateprotected 来限制数据成员和成员函数,一般使用 publicprivate 来修饰数据成员和成员函数。
  4. 一般将数据成员封装在 private 关键字下,不让用户直接访问数据成员,而是通过成员函数来进行访问。
  5. 在类的最后(大括号后)必须要有一个分号,勿忘。

实例化对象

实例化对象有两种方式,一种是在栈上实例化,另一种是在堆上实例化。

示例代码 2
int main()
{
    // 栈上实例化
    Student stu1;
    stu1.setName("jake");
    stu1.setAge(15);
    cout << "My name is " << stu1.getName() << ", I'm " << stu1.getAge() << " years old." << endl;

    // 堆上实例化
    Student *stu2 = new Student;
    // 访问
    stu2->setName("Siri");
    stu2->setAge(5);
    cout << "My name is " << stu2->getName() << ", I'm " << stu2->getAge() << " years old." << endl;
    // 释放内存
    delete stu2;
    stu2 = nullptr;

    return 0;
}

实例化对象注意事项:

  1. 在栈上实例化的对象,超出定义域对象资源会自动被系统回收。
  2. 在堆上实例化的对象,最后需要使用 delete 关键字来释放内存,否则会造成内存泄漏。
  3. 使用栈实例化的对象使用 “.” 来访问数据成员或成员函数,在堆上实例化的对象使用 “->” 来访问数据成员或成员函数。
  4. 释放完内存后将对象置空,防止野指针。

string 类

C++ 标准库:前人开发时编写的类,并被收录成为通用的标准类,之后的人开发时可以直接使用,避免重复造轮子。string 类是开发时经常会使用到的一个类。

string 类是 C++ 标准库中的字符串类,专用于字符串操作,接下来介绍一下如何使用 string 类。

string 介绍

  1. string 类头文件 #include<string>
  2. string 类在 std 命名空间内。
  3. 使用 string 实例化一个字符串类型的对象。
  4. 对字符串对象执行例如拼接、查找等操作。
string 初始化方式
实例化解释
string s1;s1 为空字符串
string s2(“ABC”);s2 初始值为 “ABC”
string s3 = s2;
string s3(s2);s3 初始化为 s2 的一个副本
string s4(n, ‘c’ );s4 初始化为字符 ‘c’ 的 n 个副本
string 常用操作

以下列出了 string 经常使用的操作,如果还需要用到其他操作可以在 官网 查找 string 类的相关使用方法,里面配有示例程序。

操作解释
s.empty()判断 s 是否为空,如果 s 为空返回 true,否则返回 false
s.size()返回 s 中字符的个数,和 s.length() 功能相同
s[n]返回 s 中位置 n 的字符,位置从 0 开始。切记不要越界
s1 + s2将 s1 和 s2 连接成新字符串,返回新字符串
s1 = s2赋值,将 s2 内容赋值给 s1
s1 == s2判断相等,相等返回 true,否则返回 false
s1 != s2判断不等,不等返回 true,否则返回 false
s.c_str()返回 char*,和 s.data() 功能相同
stoi()字符串转 int,还有 stol(),stoll()
stof()字符串转 float, 还有 stod(),stold()
示例代码 3

string 类提供丰富的初始化方式,但也需要注意示例中错误的初始化方式。

// string 初始化
#include <iostream>
#include <string>
using namespace std;

int main()
{
    string s1;
    string s2 = "hello";
    string s3("world");
    string s4 = s1 + s2;
    string s5 = "hello " + s2;
    string s6 = "hello " + s2 + "world";
    string s7(5, 'c');
    string s8 = "hello" + " world"; // Error

    return 0;
}

示例代码 4

string 类在实际开发中经常使用,功能也是相当的强大,下面通过示例代码来体验一下。

#include <iostream>
#include <string>
using namespace std;

int main()
{
    // 初始化
    string s1 = "hello";
    string s2 = "this is shiyanlou.";

    // size() 和 length() 都是返回字符串的长度
    cout << "s1.size() = " << s1.size() << endl;
    cout << "s1.length() = " << s1.length() << endl;

    // 需要注意 capacity() 是返回 string 的当前容量,string 本身是一个数组,所以 capacity() 返回的长度可能会大于 size() 或者 length() 返回的长度
    cout << "s1.capacity() = " << s1.capacity() << endl;

    // s1.at(i) 和 s1[i] 都是访问位置 i 的字符
    cout << "s1.at(1) = " << s1.at(1) << endl;
    cout << "s1[1] = " << s1[1] << endl;

    // find() 查到字符,找到返回字符串位置,否则返回 -1
    cout << "s1.find('e') = " << s1.find('e') << endl;
    // 指定起始位置查到
    cout << "s2.find(\"is\", 4) = " << s2.find("is", 4) << endl;
    // 逆序查找
    cout << "s2.rfind(\"is\") = " << s2.rfind("is") << endl;

    // 判断字符串是否为空
    if (s1.empty()) {
        cout << "s1 is empty." << endl;
    }
    else {
        cout << "s1 is not empty." << endl;
    }

    // 判断字符串是否相等
    if (s1 == "hello") {
        cout << "s1 == hello" << endl;
    }
    else {
        cout << "s1 != hello" << endl;
    }

    // string --> int
    string s3 = "1314";
    cout << "stoi(s3) = " << stoi(s3) << endl;
    // string --> float
    string s4 = "3.1415";
    cout << "stof(s4) = " << stof(s4) << endl;
    return 0;
}

内联函数

官方定义:内联函数又是称作 “在线函数” 或者 “编译时期展开函数” 的一种编程语言结构,用来建议编译器对一些特殊函数进行内联扩展;也就是说建议编译器将制定的函数体插入并取代每一次调用该函数的地方,从而节省每次调用函数带来的额外时间开支。

  1. 定义内联函数关键字:inline
  2. 内联分为普通内联函数和类内联函数。
  3. 定义内联函数时一般使用于简单的函数,如果函数比较复杂,编译器也将自动取消内联。
普通内联函数 - 示例代码 5
#include <iostream>
using namespace std;

int max(int a, int b)
{
    return a > b ? a : b;
}

inline int min(int a, int b)
{
    return a > b ? b : a;
}

int main()
{
    int a = 5, b = 7;
    // 普通函数求最大值
    cout << "max(a, b) = " << max(a, b) << endl;

    // 内联函数求最小值
    cout << "min(a, b) = " << min(a, b) << endl;

    return 0;
}

编译过程

类内联函数

如下图是使用类内定义一个类的方法(“类的定义域实例化” 小节中会讲到类内定义),而使用类内定义的成员函数编译器会默认将成员函数当做为内联函数进行编译。但如果成员函数比较复杂,编译器也不会将成员函数当做内联函数进行编译。

小结
  1. 编译器在编译时会将内联函数直接插入到调用内联函数处,这样可以减去调用函数时的开销。
  2. 在实际开发过程中,可以酌情考虑使用类内定义的方法实现类成员函数。

类的定义与实例化

在学习本小节之前先来学习另外两个知识点。

  • 类内定义。
  • 类外定义。
  • 同文件类外定义。
  • 分文件类外定义。

在实际开发过程中需要考虑到程序的架构问题,类内定义适用于一个相对比较简单的程序,考虑到开发时的方便性和时间成本,类外定义是实际程序开发中常用的方法。

类内定义

类内定义即在类中实现成员函数。

示例代码 6
#include <iostream>
#include <string>
using namespace std;

class Student
{
public:
    Student() {}
    ~Student() {}
    // 定义成员函数并实现功能
    void setName(string name) { this->name = name; }
    string getName() const { return name; };
    void setAge(int age) { this->age = age; }
    int getAge() const { return age; }
private:
    string name;
    int age;
};

int main()
{
    Student stu;
    stu.setName("jake");
    stu.setAge(15);
    cout << "My name is " << stu.getName() << ", I'm " << stu.getAge() << " years old." << endl;

    return 0;
}

小结

在以上示例代码中,类中定义的成员函数都是使用的类内定义的方式实现的,在实际的开发中不建议这样做,除非是定义一个非常简单的类或者成员函数。

在内联函数小节中提到过,类内定义的方式编译器会自动尝试将成员函数作为内联函数进行编译,如果成员函数比较复杂,编译器将不会作为内联函数进行编译。

类外定义

类外定义有两种方式:

  1. 同文件类外定义。
  2. 分文件类外定义。
同文件类外定义 - 示例代码 7
#include <iostream>
#include <string>
using namespace std;

class Student
{
public:
    Student();
    ~Student();
    void setName(string name);
    string getName() const;
    void setAge(int age);
    int getAge() const;
private:
    string name;
    int age;
};

Student::Student()
{
}

Student::~Student()
{
}

void Student::setName(string name)
{
    this->name = name;
}

string Student::getName() const
{
    return name;
};


void Student::setAge(int age)
{
    this->age = age;
}

int Student::getAge() const
{
    return age;
}


int main()
{
    Student stu;
    stu.setName("jake");
    stu.setAge(15);
    cout << "My name is " << stu.getName() << ", I'm " << stu.getAge() << " years old." << endl;

    return 0;
}

分文件类外定义 - 示例代码 8
// Student.h 文件
#ifndef __STUDENT__
#define __STUDENT__
#include <iostream>
#include <string>
using namespace std;

class Student
{
public:
    Student();
    ~Student();
    void setName(string name);
    string getName() const;
    void setAge(int age);
    int getAge() const;
private:
    string name;
    int age;
};

#endif // __STUDENT__

// Student.cpp 文件
#include "Student.h"    // 记得添加类头文件

Student::Student()
{
}

Student::~Student()
{
}

void Student::setName(string name)
{
    this->name = name;
}

string Student::getName() const
{
    return name;
};


void Student::setAge(int age)
{
    this->age = age;
}

int Student::getAge() const
{
    return age;
}

// main.cpp 文件
#include <iostream>
#include "Student.h"

int main()
{
    Student stu;
    stu.setName("jake");
    stu.setAge(15);
    cout << "My name is " << stu.getName() << ", I'm " << stu.getAge() << " years old." << endl;
    return 0;
}

文件结构以及运行结果截图:

小结

同文件类外定义就是将类的成员函数定义在类之外的地方,同时类的成员函数使用类作用域来进行定义。在实际项目开发中,类不是很复杂时可以使用该方法,类比较复杂时不推荐使用。

分文件定义类就是将类的定义放在 xxx.h 头文件中,而类的成员函数实现放在 xxx.cpp 文件中。这种做法是做项目时推荐使用的方法,因为这样将类的定义和成员函数分开后,在开发时很容易能够查找到对应的定义和成员函数。

一般情况下文件名和类名相同,便于开发时类管理。

实验总结

知识点总结
  • 深入认识类与对象并讲解 C++ 相关概念。
  • 如何使用 string 类。
  • 普通内联函数与类内联函数以及编译器对内联函数的处理。
  • 学习类内定义与类外定义,开发时常使用分文件类外定义的方法。
  • main 函数的返回值为 int 类型,注意不要写成 void 类型。
章节总结

本章节讲解的是比较基础的知识点,但是在开发中却非常的重要,在接下来的章节中将会越来越深的学习 C++ 知识。

希望在本章节结束后能掌握如何定义和使用类,并且掌握在工作中的实际用法。

  • 19
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ALONE_WORK

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值