#include
#include
#include
#include
using namespace std;
typedef struct Car{ //汽车“类”
string color; //颜色
string brand; //品牌
string type; //车型
int year; //年限
void (*printCarInfo)(string color,string brand,string type, int year); //函数指针,指向车介绍函数
void (*carRun)(string type); //函数指针,指向车运行的函数
void (*carStop)(string type); //函数指针,执行车停止的函数
}Car;
void printCarInfo(string color,string brand,string type, int year)
{
string str = "车的品牌是:" + brand
+ ",型号是: " + type
+ ",颜色是:" + color
+ ",上市年限是:" + std::to_string(year);
cout << str << endl;
}
int main()
{
Car BWMthree;
BWMthree.color = "白色";
BWMthree.brand = "宝马";
BWMthree.type = "3系";
BWMthree.year = 2023;
BWMthree.printCarInfo = printCarInfo;
BWMthree.printCarInfo(BWMthree.color,BWMthree.brand,BWMthree.type,BWMthree.year);
Car *AodiA6 = (struct Car*)malloc(sizeof(struct Car));
AodiA6->color = "黑色";
AodiA6->brand = "奥迪";
AodiA6->type = "A6";
AodiA6->year = 2008;
AodiA6->printCarInfo = printCarInfo;
AodiA6->printCarInfo(AodiA6->color,AodiA6->brand,AodiA6->type,AodiA6->year);
return 0;
}
#include
#include
#include
#include
using namespace std;
class Car
{
//汽车“类”
public:
string color; //颜色
string brand; //品牌
string type; //车型
int year; //年限
void (*printCarInfo)(string color,string brand,string type, int year); //函数指针,指向车介绍函数
void (*carRun)(string type); //函数指针,指向车运行的函数
void (*carStop)(string type); //函数指针,执行车停止的函数
};
void printCarInfo(string color,string brand,string type, int year)
{
string str = "车的品牌是:" + brand
+ ",型号是: " + type
+ ",颜色是:" + color
+ ",上市年限是:" + std::to_string(year);
cout << str << endl;
}
int main()
{
Car BWMthree;
BWMthree.color = "白色";
BWMthree.brand = "宝马";
BWMthree.type = "3系";
BWMthree.year = 2023;
BWMthree.printCarInfo = printCarInfo;
BWMthree.printCarInfo(BWMthree.color,BWMthree.brand,BWMthree.type,BWMthree.year);
Car *AodiA6 = new Car();
AodiA6->color = "黑色";
AodiA6->brand = "奥迪";
AodiA6->type = "A6";
AodiA6->year = 2008;
AodiA6->printCarInfo = printCarInfo;
AodiA6->printCarInfo(AodiA6->color,AodiA6->brand,AodiA6->type,AodiA6->year);
return 0;
}
#include
#include
#include
#include
using namespace std;
class Car
{
//汽车“类”
public:
string color; //颜色
string brand; //品牌
string type; //车型
int year; //年限
//函数指针
void (*printCarInfo)(string color,string brand,string type, int year); //函数指针,指向车介绍函数
void (*carRun)(string type); //函数指针,指向车运行的函数
void (*carStop)(string type); //函数指针,执行车停止的函数
//成员函数
void realPrintCarInfo();
};
void Car::realPrintCarInfo()
{
string str = "车的品牌是:" + brand
+ ",型号是: " + type
+ ",颜色是:" + color
+ ",上市年限是:" + std::to_string(year);
cout << str << endl;
}
void printCarInfo(string color,string brand,string type, int year)
{
string str = "车的品牌是:" + brand
+ ",型号是: " + type
+ ",颜色是:" + color
+ ",上市年限是:" + std::to_string(year);
cout << str << endl;
}
int main()
{
Car BWMthree;
BWMthree.color = "白色";
BWMthree.brand = "宝马";
BWMthree.type = "3系";
BWMthree.year = 2023;
BWMthree.realPrintCarInfo();
Car *AodiA6 = new Car();
AodiA6->color = "黑色";
AodiA6->brand = "奥迪";
AodiA6->type = "A6";
AodiA6->year = 2008;
AodiA6->printCarInfo = printCarInfo;
AodiA6->realPrintCarInfo();
return 0;
}
在 C++ 中,一个类包含另一个类的对象称为组合( Composition )。这是一种常见的设计模式,用于表示一个类是由另一个类的对象组成的。这种关系通常表示一种" 拥有 " ( "has-a" )的关系。
#include
#include
#include
#include
using namespace std;
class Wheel
{
public:
string brand;
int year;
void printWheelInfo();
};
class Car
{
//汽车“类”
public:
string color; //颜色
string brand; //品牌
string type; //车型
int year; //年限
Wheel wheel;
Wheel *pwheel;
//成员函数
void printCarInfo();
};
void Car::printCarInfo()
{
string str = "车的品牌是:" + brand
+ ",型号是: " + type
+ ",颜色是:" + color
+ ",上市年限是:" + std::to_string(year);
cout << str << endl;
}
void Wheel::printWheelInfo()
{
string str = "轮胎的品牌是:" + brand
+ ",上市年限是:" + std::to_string(year);
cout << str << endl;
}
int main()
{
Car BWMthree;
BWMthree.color = "白色";
BWMthree.brand = "宝马";
BWMthree.type = "3系";
BWMthree.year = 2023;
BWMthree.wheel.brand="米其林";
BWMthree.wheel.year=2023;
BWMthree.printCarInfo();
BWMthree.wheel.printWheelInfo();
Car *AodiA6 = new Car();
AodiA6->color = "黑色";
AodiA6->brand = "奥迪";
AodiA6->type = "A6";
AodiA6->year = 2008;
AodiA6->wheel.brand="普通";
AodiA6->wheel.year=2024;
AodiA6->printCarInfo();
AodiA6->wheel.printWheelInfo();
return 0;
}
银行的账户是一个模板,是一个类,有存款人信息和账户额度,而具体的存款人视为一个对象, 一个对象不能私自修改账户额度,需要通过一个操作流程,比如去ATM 或者柜台进行操作才能修改 到账户额度,所以,存款人信息和账户额度设计成私有权限,通过公有的操作流程,也就是公有函数去操作私有 变量。基于这个场景,我们编程实现代码
#include
#include
using namespace std;
/*
银行的账户是一个模板,是一个类,有存款人信息和账户额度,而具体的存款人视为一个对象,
一个对象不能私自修改账户额度,需要通过一个操作流程,比如去ATM或者柜台进行操作才能修改到账户额度,
所以,存款人信息和账户额度设计成私有权限,通过公有的操作流程,也就是公有函数去操作私有变量。
基于这个场景,我们编程实现代码
*/
class BankAccount{
private:
//有存款人信息和账户额度
string name;
string addr;
int age;
double balance;
public:
string bankAddr;
//比如去ATM或者柜台进行操作才能修改到账户额度
void registerMes(string newName, string newAddr,int newAge,double newBalance);
void withdrawal(double amount);
void deposit(double amount);
double getBalance();
void printUserInfo();
};
void BankAccount::printUserInfo()
{
string mesTem = "账户名:" + name + ",地址:" + addr +
",年龄:"+ std::to_string(age) + ",存款:" + std::to_string(balance);
cout << mesTem << endl;
}
void BankAccount::registerMes(string newName, string newAddr,int newAge,double newBalance)
{
name = newName;
addr = newAddr;
age = newAge;
balance = newBalance;
}
// 存款方法
void BankAccount::deposit(double amount) {
if (amount > 0) {
balance += amount;
cout<<"存款:"< balance) {
cerr << "Insufficient funds." << endl;
} else if (amount <= 0) {
cerr << "Withdrawal amount must be positive." << endl;
} else {
balance -= amount;
cout<<"取款:"<
在这个示例中, balance 是一个 private 成员变量,它不能被类的外部直接访问。这保证了账户余额只能通过类提供的方法(如 deposit , withdraw , 和 getBalance )来修改和查询,从而防止了不合适的修改,比如直接设置余额为负数或任意值。这样的设计保证了类的封装性和数据的完整性
问:为什么新手学习C++感受不到访问权限的必要性呢?