C++ 类和对象:面向对象编程基础
目录标题
- 1. 什么是类?
- 2. 什么是对象?
- 3. 如何定义一个类?
- 4. 如何创建对象?
- 5. 类的构造函数
- 6. 类的析构函数
- 7. 数据封装和访问修饰符
- 8. 示例:一个简单的`BankAccount`类
- 9. 使用g++编译
- 10. 再来一个简单的C++程序
- 11. 定义书籍类 `Book`
- 12. 实现书籍类的成员函数
- 13. 使用书籍类
- 14. 编译和运行
- 15. 再来一个例子
- 16. 定义账户类 `Account`
- 17. 实现账户类 `Account`
- 18. 定义银行类 `Bank`
- 19. 实现银行类 `Bank`
- 20. 使用`Account`和`Bank`类
- 21. 编译和运行
- 22. 拓展:std::string getTitle() const; 这里的std是什么意思?
面向对象编程(Object-Oriented Programming,OOP)是一种编程范式,它使用“对象”来设计软件。在C++中,对象是通过类来创建的。类是创建对象的蓝图或模板。
(图片来源网络,侵删)1. 什么是类?
类是一种用户定义的数据类型,它拥有数据成员和成员函数。数据成员用于存储与类相关的信息,而成员函数则用于操作这些数据。类可以看作创建对象的模板。
2. 什么是对象?
对象是类的实例。一旦定义了类,我们可以通过类创建对象,对象包含了类中定义的数据成员和成员函数。
3. 如何定义一个类?
C++中类的定义以关键字class开始,后跟类名和类体。类体包含在一对花括号中。通常,类的定义会放在头文件中,例如.h或.hpp文件。
// BankAccount.h class BankAccount { public: // 构造函数 BankAccount(double balance); // 成员函数 void deposit(double amount); void withdraw(double amount); double getBalance() const; private: // 数据成员 double balance; };4. 如何创建对象?
创建类的对象非常简单,只需像声明普通变量一样声明对象即可。
BankAccount myAccount(50.0);
5. 类的构造函数
构造函数是一种特殊的成员函数,它在创建对象时自动调用。构造函数的名称与类名相同。
// BankAccount.cpp BankAccount::BankAccount(double initialBalance) : balance(initialBalance) { if (initialBalance6. 类的析构函数
析构函数是另一种特殊的成员函数,它在对象销毁时自动调用。析构函数的名称是在类名前加上一个波浪号~。
7. 数据封装和访问修饰符
封装是面向对象编程的一个重要特征,它防止了对对象内部的直接访问。在C++中,我们通过访问修饰符public、protected和private来实现封装。
8. 示例:一个简单的BankAccount类
让我们来看一个完整的例子,它定义了一个BankAccount类和相关的成员函数。
// BankAccount.h class BankAccount { public: BankAccount(double balance); void deposit(double amount); void withdraw(double amount); double getBalance() const; private: double balance; }; // BankAccount.cpp #include "BankAccount.h" BankAccount::BankAccount(double initialBalance) : balance(initialBalance) { if (initialBalance 0) { balance += amount; } else { // 报告错误:存款金额必须大于0 } } void BankAccount::withdraw(double amount) { if (amount > balance) { // 报告错误:不能透支账户 } else { balance -= amount; } } double BankAccount::getBalance() const { return balance; } // main.cpp #include #include "BankAccount.h" int main() { BankAccount myAccount(100.0); myAccount.deposit(50.0); myAccount.withdraw(25.0); std::cout public: // 构造函数 Book(const std::string& title, const std::string& author, int publishYear); // 成员函数 void setTitle(const std::string& title); void setAuthor(const std::string& author); void setPublishYear(int year); std::string getTitle() const; std::string getAuthor() const; int getPublishYear() const; private: std::string title; std::string author; int publishYear; }; #endif } void Book::setTitle(const std::string& title) { this-title = title; } void Book::setAuthor(const std::string& author) { this-author = author; } void Book::setPublishYear(int year) { this-publishYear = year; } std::string Book::getTitle() const { return title; } std::string Book::getAuthor() const { return author; } int Book::getPublishYear() const { return publishYear; }13. 使用书籍类
最后,我们将在 main 函数中创建和使用 Book 类的实例。
// main.cpp #include #include "Book.h" int main() { // 创建 Book 类的一个对象 Book myBook("1984", "George Orwell", 1949); // 输出书籍信息 std::cout public: Account(const std::string& owner, double balance); void deposit(double amount); bool withdraw(double amount); double getBalance() const; std::string getOwner() const; private: std::string owner; double balance; }; #endif } void Account::deposit(double amount) { if (amount 0) { balance += amount; } } bool Account::withdraw(double amount) { if (amount 0 && amount balance -= amount; return true; } return false; } double Account::getBalance() const { return balance; } std::string Account::getOwner() const { return owner; } public: void addAccount(const Account& account); void printAccounts() const; private: std::vector accounts.push_back(account); } void Bank::printAccounts() const { for (const auto& account : accounts) { std::cout Account a1("John Doe", 1000); Account a2("Jane Doe", 1500); Bank bank; bank.addAccount(a1); bank.addAccount(a2); bank.printAccounts(); return 0; } std::string myString = "Hello, world!"; } // 使用using指令导入std::string到当前作用域 using std::string; void anotherFunction() { string anotherString = "Another message!"; // 不需要std::前缀 }
免责声明:我们致力于保护作者版权,注重分享,被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理! 图片声明:本站部分配图来自人工智能系统AI生成,觅知网授权图片,PxHere摄影无版权图库和百度,360,搜狗等多加搜索引擎自动关键词搜索配图,如有侵权的图片,请第一时间联系我们,邮箱:ciyunidc@ciyunshuju.com。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!
