第9课: 面向对象基础
类与对象
#include <iostream>
#include <string>
using namespace std;
class Student {
public:
// 成员变量
string name;
int age;
int score;
// 成员函数
void display() {
cout << "姓名:" << name << endl;
cout << "年龄:" << age << endl;
cout << "分数:" << score << endl;
}
};
int main() {
// 创建对象
Student stu1;
stu1.name = "张三";
stu1.age = 20;
stu1.score = 95;
stu1.display();
return 0;
}
封装 - 访问权限
#include <iostream>
#include <string>
using namespace std;
class BankAccount {
private:
// 私有成员:只能在类内部访问
string accountNumber;
double balance;
public:
// 公有成员:可以在类外部访问
string ownerName;
// 设置账户信息
void setAccount(string accNum, double bal) {
accountNumber = accNum;
balance = bal;
}
// 存款
void deposit(double amount) {
if (amount > 0) {
balance += amount;
cout << "存款成功!" << endl;
}
}
// 取款
void withdraw(double amount) {
if (amount > 0 && amount <= balance) {
balance -= amount;
cout << "取款成功!" << endl;
} else {
cout << "余额不足!" << endl;
}
}
// 查询余额
double getBalance() {
return balance;
}
protected:
// 受保护成员:可以被派生类访问
void log(string message) {
cout << "[日志] " << message << endl;
}
};
int main() {
BankAccount account;
account.ownerName = "李四";
account.setAccount("6222021234567890", 1000.0);
account.deposit(500);
account.withdraw(200);
cout << "当前余额:" << account.getBalance() << endl;
return 0;
}
类的成员函数定义
#include <iostream>
using namespace std;
class Rectangle {
private:
double width;
double height;
public:
// 类内声明
void setDimensions(double w, double h);
double getArea();
double getPerimeter();
};
// 类外定义
void Rectangle::setDimensions(double w, double h) {
width = w;
height = h;
}
double Rectangle::getArea() {
return width * height;
}
double Rectangle::getPerimeter() {
return 2 * (width + height);
}
int main() {
Rectangle rect;
rect.setDimensions(5.0, 3.0);
cout << "面积:" << rect.getArea() << endl;
cout << "周长:" << rect.getPerimeter() << endl;
return 0;
}
this指针
#include <iostream>
#include <string>
using namespace std;
class Person {
private:
string name;
int age;
public:
void setName(string name) {
this->name = name; // this指向当前对象
}
void setAge(int age) {
this->age = age;
}
void display() {
cout << "姓名:" << this->name << endl;
cout << "年龄:" << this->age << endl;
}
// 返回当前对象的引用
Person& setInfo(string name, int age) {
this->name = name;
this->age = age;
return *this;
}
};
int main() {
Person p;
p.setInfo("王五", 25).display(); // 链式调用
return 0;
}
静态成员
#include <iostream>
using namespace std;
class Counter {
private:
static int count; // 静态成员变量
public:
Counter() {
count++;
}
static int getCount() { // 静态成员函数
return count;
}
};
// 静态成员变量必须在类外初始化
int Counter::count = 0;
int main() {
cout << "初始计数:" << Counter::getCount() << endl;
Counter c1, c2, c3;
cout << "创建3个对象后:" << Counter::getCount() << endl;
return 0;
}
友元函数
#include <iostream>
using namespace std;
class Box {
private:
double width;
public:
Box(double w) : width(w) {}
// 声明友元函数
friend void printWidth(Box box);
};
// 友元函数可以访问私有成员
void printWidth(Box box) {
cout << "宽度:" << box.width << endl;
}
int main() {
Box box(10.5);
printWidth(box);
return 0;
}
练习题
- 创建一个Circle类,包含半径属性和计算面积、周长的方法
- 设计一个Student类,使用私有成员保护数据,提供公有接口访问
- 实现一个计数器类,使用静态成员统计创建的对象数量
- 创建一个Date类,包含年月日,实现日期比较功能