辅助学习顶层const、底层const
https://www.bilibili.com/video/BV1xY411t75L
辅助学习顶层const、底层const
https://www.bilibili.com/video/BV1xY411t75L
类型前加Friend......
么怎么用过直接publick生成一个函数引用.....
srtuck 结构体名称
{
}
结构体名称 命名 (结构内容名称 自定义结构内容名称)
{
}
再编译时运行想当与UE事件函数
constexpr
{
}
注意指针做形参的时候,可能会把指针所指的值修改掉。
#pragma once
#ifndef STUDENT_H_
#define STUDENT_H_
#include<instream>
#incluse<string>
using namespace std;
class student{
private:
string name_;
public:
void setStudent(string name.int chinese,int math, int english);
}
int min(int num,int &num2)
num 是定义复制实参的值,不改变实参
%num2是定义引用类型的参数,修改了实参
#include <iostream>
using namespace std;
int main(){
cout << "Hello world!" << endl;
return 0;
}
定义成员函数时,需要使用域解析符(::)来标识函数所属的类
void Student::setStudent(string name,int chinese,int math,int english)
{
name_=name }
private:标识只能通过公共成员访问的类成员(数据隐藏)
public:标识组成类的公共接口的类成员(抽象)
降接口放在头文件中,并将实现放在源代码(.cpp)中。
class表示类
private(私有成员:只能通过公共成员访问的类成员(数据隐藏)
public:标识组成类的公共接口的类成员(抽象)。
降接口放在头文件中,并将实现放在源代码(.cpp)中。
class表示类
private(私有成员:只能通过公共成员访问的类成员(数据隐藏)
public:标识组成类的公共接口的类成员(抽象)。
在原本声明函数原型的地方加上inline
constexpr可以用来修饰变量、函数、构造函数。一旦以上任何元素被constexpr修饰,那么等于说是告诉编译器 “请大胆地将我看成编译时就能得出常量值的表达式去优化我”。
i i
const int size = 5;
Food foods[size] = {};
cout << "请输入你喜欢的" << size << "个食物,并给它们打分。(分数为0-10之间的小数)"<<endl;
cout << "请输入你喜欢的食物的名称:" << endl;
for (int i = 0; i < size; i++) {
cin >> foods[i].foodName;
cout << "请给你喜欢的食物打分:" << endl;
cin >> foods[i].foodScore;
if (foods[i].foodScore < 0 || foods[i].foodScore > 10) {
cout << "Warning! 分数超出范围!" << endl << "请重新输入第" << i + 1 << "个食物的名称:" << endl;
i--;
}
else if(i<size-1){
cout << "请输入第" << i + 2 << "个食物的名称:" << endl;
}
}
for (/*i = 0*/int i = 0; /*i < size;*/i < size-1; i++) {
for (int j = i + 1; j < size; j++) {
Food temp;
if (foods[i].foodScore < foods[j].foodScore) {
temp = foods[i];
foods[i] = foods[j];
foods[j] = temp;
}
}
}
// i = 0 1 2 3 (i+1)j = 1 2 3 4
cout << "排名:食物名称(分数)" << endl;
for (int i = 0; i < size; i++) {
cout << i + 1 << ":" << foods[i].foodName << "(" << foods[i].foodScore << ")" << endl;
}
ssdadsadwwww
//习题5.cpp
#include "pch.h"
#include "Drugs.h"
#include <iostream>
int main()
{
const int size = 2;
Drugs drugs[size];
drugs[0] = Drugs("回血药水", PlusHP, 20, 100);
drugs[1] = Drugs("回魔药水", PlusMP, 10, 150);
float totalMoney = 1000;
cout << "1.购买回血药品 / 2.购买回魔药品 / 3.卖出回血药品 / 4.卖出回魔药品 / 5.输出目前拥有的药水和金钱 / 6.退出" << endl;
cout << "请输入操作:" << endl;
int input = 0;
int number = 0;
while (cin >> input) //&& input > 0 && input < 7
{
if (input == 1)
{
cout << "请输入购买的数量:" << endl;
if (cin >> number && number > 0)
{
drugs[0].buyDrug(totalMoney, number);
cout << "请输入操作:" << endl;
}
}
else if (input == 2)
{
cout << "请输入购买的数量:" << endl;
if (cin >> number && number > 0)
{
drugs[1].buyDrug(totalMoney, number);
cout << "请输入操作:" << endl;
}
}
else if (input == 3)
{
cout << "请输入卖出的数量:" << endl;
if (cin >> number && number > 0)
{
drugs[0].sellDrug(totalMoney, number);
cout << "请输入操作:" << endl;
}
}
else if (input == 4)
{
cout << "请输入卖出的数量:" << endl;
if (cin >> number && number > 0)
{
drugs[1].sellDrug(totalMoney, number);
cout << "请输入操作:" << endl;
}
}
else if (input == 5)
{
cout << "目前拥有的药品:" << endl;
for (int i = 0; i < size; i++)
{
cout << i + 1 << ":" << endl;
drugs[i].showDrug();
}
cout << "拥有的金钱数:" << totalMoney << endl;
}
else if(input ==6)
{
break;
}
else
{
cout << "输入错误,请重新输入,或输入6退出!" << endl;
}
}
}
//自写
//Medicine.h
#pragma once
#ifndef MEDICINE_H_
#define MEDICINE_H_
#include <iostream>
#include <string>
using namespace std;
enum Sort {
PlusHP=0,
PlusMP=1
};
class Medicine
{
private:
string name;
Sort sort;
int count;
float buyPrice;
float sellPrice;
static constexpr float rate = 0.75f;
public:
Medicine(string name,int count ,Sort sort,float buyPrice);
~Medicine();
void ShowMedicineInfo();
void BuyMedicine(float &money);
void SellMedicine(float &money);
};
#endif // !MEDICINE_H_
//Medicine.cpp
#include "pch.h"
#include "Medicine.h"
Medicine::Medicine(string name, int count, Sort sort, float buyPrice)
{
this->name = name;
this->sort = sort;
this->count = count;
this->buyPrice = buyPrice;
this->sellPrice = this->buyPrice * rate;
}
Medicine::~Medicine()
{
}
void Medicine::ShowMedicineInfo()
{
cout << (this->sort + 1) << ".名称:" << this->name << " 数量:" << this->count << " 种类:" << (this->sort == 0 ? "PlusHP" : "PlusMP") << " 购入价格:" << this->buyPrice << " 卖出价格:" << this->sellPrice << endl;
}
void Medicine::BuyMedicine(float & money)
{
int buyCount;
cout << "请输入购买数量:" << endl;
cin >> buyCount;
if (buyCount <= 0)
{
return;
}
float buyTotalPrice = this->buyPrice * buyCount;
if (buyTotalPrice <= money)
{
money -= buyTotalPrice;
this->count += buyCount;
cout << "购买成功!" << endl;
}
else
{
cout << "购买失败,当前钱不够购买这么多药品。" << endl;
}
}
void Medicine::SellMedicine(float & money)
{
int sellCount;
cout << "请输入卖出数量:" << endl;
cin >> sellCount;
if (sellCount > 0 && sellCount <= this->count)
{
money += this->sellPrice *sellCount;
this->count -= sellCount;
cout << "卖出成功!" << endl;
}
else
{
cout << "卖出失败!" << endl;
}
}
//习题5.cpp
#include "pch.h"
#include "Medicine.h"
int main()
{
const int size = 2;
Medicine meds[size] = { {"回血药水",20,PlusHP,100}, {"回魔药水",10,PlusMP,150} };
float totalMoney = 1000.0f;
cout << "1.购买回血药品 / 2.购买回魔药品 / 3.卖出回血药品 / 4.卖出回魔药品 / 5.输出目前拥有的药水和金钱 / 6.退出" << endl;
bool isContinue = true;
while (isContinue)
{
int num;
cin >> num;
switch (num)
{
case 1:
meds[0].BuyMedicine(totalMoney);
break;
case 2:
meds[1].BuyMedicine(totalMoney);
break;
case 3:
meds[0].SellMedicine(totalMoney);
break;
case 4:
meds[1].SellMedicine(totalMoney);
break;
case 5:
cout << "目前拥有的药品:" << endl;
for (int i = 0; i < size; i++)
{
meds[i].ShowMedicineInfo();
}
cout << "拥有钱数:" << totalMoney << endl << "显示完成!" << endl;
break;
case 6:
isContinue = false;
return 0;
break;
default:
cout << "输入数字错误!" << endl;
break;
}
cout << "请继续输入操作:" << endl;
}
return 0;
}
习题4
完成程序:药品物资管理
要求:
1.利用结构体来存储目前拥有的药物的名称、种类、数量、买入价格、卖出价格。
2.利用枚举来设置药物的种类(回复mp和回复hp)。
3.编写函数来控制药物的买入和卖出,卖出价为买入价格的3/4。
4.编写函数来显示拥有的药物的剩余钱数。
5.通过输入数字来控制函数调用。
6.实现分离式编译。
习题5
完成程序:药品物资管理
要求:
1.创建药品类完成习题4中对药品相关的管理。
2.利用对象数组来完成对不同种类药品的信息的存储。
3.使用*this指针。
4.使用作用域为类的常量。
//Drugs.h
#pragma once
#ifndef DRUGS_H_
#define DRUGS_H_
#include <string>
using namespace std;
enum Type { PlusHP, PlusMP };
class Drugs
{
private:
string name_;
Type type_;
int count_;
float buyPrice_;
float sellPrice_;
static constexpr float Ratio = 0.75f;
public:
Drugs(string name, Type type, int count, float buyPrice);
Drugs();
~Drugs();
void buyDrug(float &money, int num);
void sellDrug(float &money, int num);
void showDrug();
string showType();
};
#endif // !DRUGS_H_
//Drugs.cpp
#include "pch.h"
#include "Drugs.h"
#include <iostream>
Drugs::Drugs(string name, Type type, int count, float buyPrice)
{
name_ = name;
type_ = type;
count_ = count;
buyPrice_ = buyPrice;
sellPrice_ = buyPrice * Ratio;
}
Drugs::Drugs()
{
}
Drugs::~Drugs()
{
}
void Drugs::buyDrug(float & money, int num)
{
if (num > 0)
{
if (buyPrice_ * num <= money)
{
money -= buyPrice_ * num;
count_ += num;
cout << "购买成功!!!" << endl;
}
else
{
cout << "警告:您的钱不够买" << num << "个药物!!!" << endl;
}
}
else
{
cout << "警告:输入有误!!!" << endl;
}
}
void Drugs::sellDrug(float & money, int num)
{
if (num > 0)
{
if (num <= count_)
{
count_ -= num;
money += sellPrice_ * num;
cout << "卖出成功!!!" << endl;
}
else
{
cout << "警告:您没有" << num << "个药物可以卖出!!!" << endl;
}
}
}
void Drugs::showDrug()
{
cout << "药物名称:" << name_ << " 数量:" << count_ << " 种类:" << type_ << " 买入价格:" << buyPrice_
<< "卖出价格:" << sellPrice_ << endl;
}
string Drugs::showType()
{
if (type_ == 0)
{
return "PlusHP";
}
else
{
return "PlusMP";
}
}
友元函数
类可以允许其他类或者函数访问它的非公有成员,方法是令其他类或者函数成为它的友元(friend)。
如果类想把一个函数作为它的友元,只需要增加一条以friend关键字开始的函数声明语句即可:
friend void printMath(Student &s);
注意:友元函数不是类的成员函数,友元函数不能直接访问类的成员,只能通过对象访问。
//student.h
#pragma once
#ifndef STUDENT_H_
#define STDENT_H_
#include <iostream>
#include <string>
using namespace std;
//class关键字和类名
class Student
{
//Student类的友元函数,但是不是这个类的函数成员
friend void printMath(Student &s);
//私有成员,定义数据成员,只能通过公有接口进行访问,数据隐藏
private:
string name_;
int chinese_;
int math_;
int english_;
static const int PassingScore = 60;
//公有接口
public:
Student(string name, int chinese, int math, int english);
Student();
~Student();
void setStudent(string name, int chinese, int math, int english);
int sum(const Student &s);
float avery(const Student &s);
bool pass(const Student &s);
};
#endif // !STUDENT_H_
//student.cpp
#include "pch.h"
#include "student.h"
Student::Student(string name, int chinese, int math, int english)
{
name_ = name;
chinese_ = chinese;
math_ = math;
english_ = english;
}
Student::Student()
{
}
Student::~Student()
{
}
void Student::setStudent(string name, int chinese, int math, int english)
{
name_ = name;
chinese_ = chinese;
math_ = math;
english_ = english;
}
int Student::sum(const Student & s)
{
return s.chinese_+s.math_ +s.english_;
}
float Student::avery(const Student & s)
{
return(float)(s.chinese_ + s.math_ + s.english_) / 3;
}
bool Student::pass(const Student & s)
{
if (s.chinese_ >= PassingScore && s.math_ >= PassingScore && s.english_ >= PassingScore)
{
return true;
}
else
{
return false;
}
}
void printMath(Student & s)
{
cout << "数学成绩为:" << s.math_ << endl;
}
//18-类的定义.cpp
#include "pch.h"
#include "student.h"
int main()
{
//通过构造函数显示的初始化和隐式的初始化
Student stu1 = Student("Sandy", 50, 120, 110);
Student stu2("Jane", 110, 90, 100);
Student stu3;
/*string name1 = "Sandy";
int chinese1 = 100;
int math1 = 120;
int english1 = 110;
stu1.setStudent(name1, chinese1, math1, english1);*/
int totalScore1 = stu1.sum(stu1);
float averyScore1 = stu1.avery(stu1);
bool isPassed1 = stu1.pass(stu1);
cout << "该学生的总成绩为:" << totalScore1 << endl;
cout << "该学生的平均成绩为:" << averyScore1 << endl;
if (isPassed1 = true)
{
cout << "该学生通过了这次考试。" << endl;
}
else
{
cout << "该学生没有通过这次考试。" << endl;
}
printMath(stu1);
return 0;
}
习题4
完成程序:药品物资管理
要求:
1.利用结构体来存储目前拥有的药物的名称、种类、数量、买入价格、卖出价格。
2.利用枚举来设置药物的种类(回复mp和回复hp)。
3.编写函数来控制药物的买入和卖出,卖出价为买入价格的3/4。
4.编写函数来显示拥有的药物的剩余钱数。
5.通过输入数字来控制函数调用。
6.实现分离式编译。
习题5
完成程序:药品物资管理
要求:
1.创建药品类完成习题4中对药品相关的管理。
2.利用对象数组来完成对不同种类药品的信息的存储。
3.使用*this指针。
4.使用作用域为类的常量。
类作用域
每个类都会定义它自己的作用域。在类的作用域之外,普通的数据和函数成员只能由对象,引用或者指针使用成员访问运算符访问。
对象调用:直接成员运算符(.)
指针调用:间接成员运算符(->)
在类的外部定义成员函数时,必须同时提供类名和函数名。
int Student::sum(Student &s)) //作用域解析运算符(::)
{
}
作用域为类的常量
不能直接在类的声明中直接通过const来声明一个常量,这是因为声明类只描述了对象的形式,并没有创建对象。因此,在创建对象前,将没有用于存储值的空间。
const int i = 20;
作用域为类的常量
想要获得作用域为类的常量,有2种方式:
1.在类中声明一个枚举。在类声明中声明的枚举的作用域为整个类,因此可以用枚举为整型常量提供作用域为整个类的符号名称。用这种方式声明枚举并不会创建类数据成员,只是为了创建符号常量。
enum{PassingScore = 60};
2.使用static关键字。使用static关键字创建的常量将与其他静态变量存储在一起,而不是存储在对象中。
static const int PassingScore = 60;
//student.h
#pragma once
#ifndef STUDENT_H_
#define STDENT_H_
#include <iostream>
#include <string>
using namespace std;
//class关键字和类名
class Student
{
//私有成员,定义数据成员,只能通过公有接口进行访问,数据隐藏
private:
string name_;
int chinese_;
int math_;
int english_;
static const int PassingScore = 60;
//公有接口
public:
Student(string name, int chinese, int math, int english);
Student();
~Student();
void setStudent(string name, int chinese, int math, int english);
int sum(const Student &s);
float avery(const Student &s);
bool pass(const Student &s);
};
#endif // !STUDENT_H_
//student.cpp
#include "pch.h"
#include "student.h"
Student::Student(string name, int chinese, int math, int english)
{
name_ = name;
chinese_ = chinese;
math_ = math;
english_ = english;
}
Student::Student()
{
}
Student::~Student()
{
}
void Student::setStudent(string name, int chinese, int math, int english)
{
name_ = name;
chinese_ = chinese;
math_ = math;
english_ = english;
}
int Student::sum(const Student & s)
{
return s.chinese_+s.math_ +s.english_;
}
float Student::avery(const Student & s)
{
return(float)(s.chinese_ + s.math_ + s.english_) / 3;
}
bool Student::pass(const Student & s)
{
if (s.chinese_ >= PassingScore && s.math_ >= PassingScore && s.english_ >= PassingScore)
{
return true;
}
else
{
return false;
}
}
//18-类的定义.cpp
#include "pch.h"
#include "student.h"
int main()
{
//通过构造函数显示的初始化和隐式的初始化
Student stu1 = Student("Sandy", 50, 120, 110);
Student stu2("Jane", 110, 90, 100);
Student stu3;
/*string name1 = "Sandy";
int chinese1 = 100;
int math1 = 120;
int english1 = 110;
stu1.setStudent(name1, chinese1, math1, english1);*/
int totalScore1 = stu1.sum(stu1);
float averyScore1 = stu1.avery(stu1);
bool isPassed1 = stu1.pass(stu1);
cout << "该学生的总成绩为:" << totalScore1 << endl;
cout << "该学生的平均成绩为:" << averyScore1 << endl;
if (isPassed1 = true)
{
cout << "该学生通过了这次考试。" << endl;
}
else
{
cout << "该学生没有通过这次考试。" << endl;
}
return 0;
}