《C++ Primer》导学系列:第 7 章 - 类

7.1 定义抽象数据类型

7.1.1 类的基本概念

在C++中,类是用户定义的类型,提供了一种将数据和操作这些数据的函数(成员函数)组合在一起的方法。类定义了对象的属性和行为,通过实例化类来创建对象。

7.1.2 定义类

定义类时,需要指定类的名称,并在类体内声明数据成员和成员函数。类定义的一般形式如下:

class ClassName {
public:
    // 成员函数声明
    returnType functionName(parameterList);

private:
    // 数据成员声明
    dataType memberName;
};
示例代码
#include <iostream>
#include <string>

class Person {
public:
    // 成员函数声明
    void setName(const std::string &name);
    std::string getName() const;

private:
    // 数据成员声明
    std::string name;
};

// 成员函数定义
void Person::setName(const std::string &name) {
    this->name = name;
}

std::string Person::getName() const {
    return name;
}

int main() {
    Person person;
    person.setName("Alice");
    std::cout << "Name: " << person.getName() << std::endl;
    return 0;
}

在这个示例中,定义了一个Person类,包含数据成员name和成员函数setNamegetName

7.1.3 成员函数

成员函数是类的函数,可以访问类的成员变量。成员函数可以在类内部声明,在类外部定义。成员函数的定义需要使用类名和作用域运算符::

示例代码
#include <iostream>
#include <string>

class Book {
public:
    void setTitle(const std::string &title);
    std::string getTitle() const;

private:
    std::string title;
};

void Book::setTitle(const std::string &title) {
    this->title = title;
}

std::string Book::getTitle() const {
    return title;
}

int main() {
    Book book;
    book.setTitle("C++ Primer");
    std::cout << "Title: " << book.getTitle() << std::endl;
    return 0;
}

在这个示例中,定义了一个Book类,包含数据成员title和成员函数setTitlegetTitle

7.1.4 构造函数

构造函数是用于创建类对象并初始化数据成员的特殊成员函数。构造函数的名称与类名相同,没有返回类型。

示例代码
#include <iostream>
#include <string>

class Car {
public:
    Car(const std::string &brand, int year); // 构造函数声明
    void display() const;

private:
    std::string brand;
    int year;
};

Car::Car(const std::string &brand, int year) : brand(brand), year(year) {} // 构造函数定义

void Car::display() const {
    std::cout << "Brand: " << brand << ", Year: " << year << std::endl;
}

int main() {
    Car car("Toyota", 2020);
    car.display();
    return 0;
}

在这个示例中,定义了一个Car类,包含数据成员brandyear,并通过构造函数初始化这些成员。

7.1.5 类的接口和实现

类的接口是指类的公共成员,包括公共数据成员和公共成员函数。类的实现是指类的私有成员和成员函数的定义。通过将接口和实现分离,可以提高代码的可读性和可维护性。

示例代码
#include <iostream>
#include <string>

class Animal {
public:
    void setName(const std::string &name);
    std::string getName() const;

private:
    std::string name;
};

void Animal::setName(const std::string &name) {
    this->name = name;
}

std::string Animal::getName() const {
    return name;
}

int main() {
    Animal animal;
    animal.setName("Elephant");
    std::cout << "Animal: " << animal.getName() << std::endl;
    return 0;
}

在这个示例中,定义了一个Animal类,包含数据成员name和成员函数setNamegetName

重点与难点分析

重点

  1. 类的定义:掌握类的基本结构和定义方法,理解类的成员变量和成员函数的声明与定义。
  2. 构造函数:理解构造函数的作用和定义方法,掌握构造函数的初始化列表的使用。
  3. 类的接口和实现:理解类的接口和实现的概念,掌握将类的接口与实现分离的方法。

难点

  1. 成员函数的定义:初学者需要通过实践掌握成员函数在类外部定义的方法,理解作用域运算符::的使用。
  2. 构造函数的初始化列表:掌握构造函数初始化列表的语法和使用方法,理解其在类对象初始化中的作用。

练习题解析

  1. 练习7.1:定义一个Student类,包含数据成员nameage,以及相应的构造函数和成员函数。
    • 示例代码
#include <iostream>
#include <string>

class Student {
public:
    Student(const std::string &name, int age);
    void display() const;

private:
    std::string name;
    int age;
};

Student::Student(const std::string &name, int age) : name(name), age(age) {}

void Student::display() const {
    std::cout << "Name: " << name << ", Age: " << age << std::endl;
}

int main() {
    Student student("John", 21);
    student.display();
    return 0;
}
  1. 练习7.2:编写一个Rectangle类,包含成员函数areaperimeter,用于计算矩形的面积和周长。
    • 示例代码
#include <iostream>

class Rectangle {
public:
    Rectangle(double width, double height);
    double area() const;
    double perimeter() const;

private:
    double width;
    double height;
};

Rectangle::Rectangle(double width, double height) : width(width), height(height) {}

double Rectangle::area() const {
    return width * height;
}

double Rectangle::perimeter() const {
    return 2 * (width + height);
}

int main() {
    Rectangle rect(5.0, 3.0);
    std::cout << "Area: " << rect.area() << std::endl;
    std::cout << "Perimeter: " << rect.perimeter() << std::endl;
    return 0;
}
  1. 练习7.3:定义一个Circle类,包含数据成员radius,并实现计算圆周长和面积的成员函数。
    • 示例代码
#include <iostream>
#include <cmath>

class Circle {
public:
    Circle(double radius);
    double circumference() const;
    double area() const;

private:
    double radius;
};

Circle::Circle(double radius) : radius(radius) {}

double Circle::circumference() const {
    return 2 * M_PI * radius;
}

double Circle::area() const {
    return M_PI * radius * radius;
}

int main() {
    Circle circle(5.0);
    std::cout << "Circumference: " << circle.circumference() << std::endl;
    std::cout << "Area: " << circle.area() << std::endl;
    return 0;
}
  1. 练习7.4:编写一个BankAccount类,包含数据成员balance,实现存款和取款的成员函数。
    • 示例代码
#include <iostream>

class BankAccount {
public:
    BankAccount(double initialBalance);
    void deposit(double amount);
    void withdraw(double amount);
    double getBalance() const;

private:
    double balance;
};

BankAccount::BankAccount(double initialBalance) : balance(initialBalance) {}

void BankAccount::deposit(double amount) {
    balance += amount;
}

void BankAccount::withdraw(double amount) {
    if (amount <= balance) {
        balance -= amount;
    } else {
        std::cout << "Insufficient balance." << std::endl;
    }
}

double BankAccount::getBalance() const {
    return balance;
}

int main() {
    BankAccount account(1000.0);
    account.deposit(500.0);
    account.withdraw(200.0);
    std::cout << "Balance: $" << account.getBalance() << std::endl;
    return 0;


}

总结与提高

本节总结

  1. 学习了类的定义和基本概念,掌握了成员函数和数据成员的声明与定义方法。
  2. 理解了构造函数的作用和定义方法,掌握了构造函数初始化列表的使用。
  3. 通过示例代码和练习题,加深了对类的接口和实现的理解和应用。

提高建议

  1. 多练习类的定义与使用:通过编写各种包含类的程序,熟悉类的定义和使用方法,提高代码的组织性和可读性。
  2. 深入理解构造函数:通过实践掌握构造函数的初始化列表和重载构造函数的方法,理解其在对象初始化中的作用。
  3. 封装类的接口与实现:在编写类时,合理设计类的接口与实现,提高代码的可维护性和安全性。

7.2 访问控制与封装

7.2.1 访问控制

访问控制用于限制类成员的访问权限。C++ 提供了三种访问控制级别:

  • public:公有成员可以被类的任何部分访问,也可以被类外部的代码访问。
  • protected:受保护成员可以被类的成员和派生类访问,但不能被类外部的代码访问。
  • private:私有成员只能被类的成员访问,不能被派生类和类外部的代码访问。
示例代码
#include <iostream>

class Base {
public:
    int publicVar;
protected:
    int protectedVar;
private:
    int privateVar;
};

class Derived : public Base {
public:
    void accessMembers() {
        publicVar = 1;         // 可以访问
        protectedVar = 2;      // 可以访问
        // privateVar = 3;     // 无法访问,编译错误
    }
};

int main() {
    Base base;
    base.publicVar = 1;        // 可以访问
    // base.protectedVar = 2;  // 无法访问,编译错误
    // base.privateVar = 3;    // 无法访问,编译错误
    return 0;
}

在这个示例中,Base类有公有、受保护和私有成员,Derived类可以访问公有和受保护成员,但不能访问私有成员。

7.2.2 封装

封装是将数据和操作数据的函数绑定在一起,并将细节隐藏起来,只暴露接口。通过封装,可以保护数据不被外界直接访问和修改,增强代码的安全性和可维护性。

示例代码
#include <iostream>
#include <string>

class Employee {
public:
    void setName(const std::string &name);
    std::string getName() const;
    void setSalary(double salary);
    double getSalary() const;

private:
    std::string name;
    double salary;
};

void Employee::setName(const std::string &name) {
    this->name = name;
}

std::string Employee::getName() const {
    return name;
}

void Employee::setSalary(double salary) {
    this->salary = salary;
}

double Employee::getSalary() const {
    return salary;
}

int main() {
    Employee emp;
    emp.setName("John Doe");
    emp.setSalary(50000);
    std::cout << "Employee: " << emp.getName() << ", Salary: $" << emp.getSalary() << std::endl;
    return 0;
}

在这个示例中,Employee类通过公有成员函数对私有数据成员进行封装,保护数据成员不被直接访问。

7.2.3 友元

友元(Friend)是可以访问类的私有和受保护成员的非成员函数或其他类。友元关系不是传递的,也不能继承。

友元函数
#include <iostream>

class Box {
    friend void printBox(const Box &box);  // 声明友元函数
public:
    Box(double length, double width, double height) : length(length), width(width), height(height) {}

private:
    double length;
    double width;
    double height;
};

void printBox(const Box &box) {
    std::cout << "Length: " << box.length << ", Width: " << box.width << ", Height: " << box.height << std::endl;
}

int main() {
    Box box(10.0, 5.0, 3.0);
    printBox(box);
    return 0;
}

在这个示例中,printBox函数是Box类的友元,可以访问Box类的私有成员。

友元类
#include <iostream>

class Engine {
    friend class Car;  // 声明友元类
public:
    Engine(int horsepower) : horsepower(horsepower) {}

private:
    int horsepower;
};

class Car {
public:
    Car(const std::string &model, int horsepower) : model(model), engine(horsepower) {}

    void showDetails() const {
        std::cout << "Model: " << model << ", Horsepower: " << engine.horsepower << std::endl;
    }

private:
    std::string model;
    Engine engine;
};

int main() {
    Car car("Toyota", 150);
    car.showDetails();
    return 0;
}

在这个示例中,Car类是Engine类的友元类,可以访问Engine类的私有成员。

重点与难点分析

重点

  1. 访问控制:掌握publicprotectedprivate访问控制的用法和区别,理解其在类中的应用。
  2. 封装:理解封装的概念,掌握通过公有成员函数访问私有数据成员的方法。
  3. 友元:了解友元函数和友元类的定义和用法,理解其在访问私有和受保护成员中的作用。

难点

  1. 友元的使用:初学者需要通过实践掌握友元的定义和使用方法,理解友元关系的非传递性和不可继承性。
  2. 封装的实现:通过实践理解封装的概念&
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

AI与编程之窗

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

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

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

打赏作者

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

抵扣说明:

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

余额充值