4128人加入学习
(13人评价)
C++编程系列 第二季函数和类

制作于2018.4.2

价格 免费

返回类型

无返回值函数:没有返回值的return语句只能用在返回类型是void的函数中。

有返回值函数;return语句的第二种形式提供了函数的结果。只要函数的返回类型不是void,则该函数内的每条return语句必须返回一个值。

 

 

返回类型

返回一个值的方式和初始化一个变量或形参的方式完全一样:返回的值用于初始化调用点的一个临时量,该临时量就是函数调用的结果

 

 

返回类型

返回引用类型:返回引用类型,可以在内存中不产生被返回值的副本,返回的是对象本身

但需要注意:不要返回局部对象的引用的引用或指针。函数完成后,它所占用的存储空间也随之被释放掉。为避免这种问题,我们可以返回一个作为参数传递给函数的引用。

 

 

#include <iostream>
#include <string>

using namespace std;

void swap(int &a, int &b);
int max(int a, int b);
int &sum(int a, int b, int &res);

int main()
{
	int num1 = 15;
	int num2 = 20;
	swap(num1, num2);
	cout << "num1: " << num1 << endl;
	cout << "num2: " << num2 << endl;
	int res = max(num1, num2);
	cout << "最大值为:" << res << endl;

	res = sum(num1, num2, res);
	cout << "两个数的和为:" << res << endl;
	
	sum(num1, num2, res)++;
	cout << res << endl;

	return 0;
}

//没有返回值的函数,可以使用return;
void swap(int &a, int &b)
{
	if (a >= b)
	{
		return;
	}
	else
	{
		int temp = a;
		a = b;
		b = temp;
		return;
	}
}

//有返回值的函数,每个return语句都必须带有结果。
int max(int a, int b)
{
	if (a > b)
	{
		return a;
	}
	else
	{
		return b;
	}
}

//返回引用类型
int &sum(int a, int b, int &res)
{
	res = a + b;
	return res;
}

[展开全文]

 

递归函数

直接或间接调用自己的函数称为递归函数

递归函数的要求:递归函数必须定义一个终止条件,否则,函数将永远递归下去。

#include <iostream>

using namespace std;

long fact(int i);

int main()
{
	int num;
	cout << "请输入一个10以内的正整数:" << endl;
	cin >> num;
	long res = fact(num);
	cout << num << "的阶乘为:" << res << endl;
	return 0;
}

long fact(int i)
{
	long temp;
	if (i == 0)
	{
		temp = 1;
	}
	else
	{
		temp = i * fact(i - 1);
	}
	return temp;
}

 

习题2

完成程序:求两个数的调和平均数

要求:

1.不断要求用户输入2个数,直到其中一个数的值为0。

2.对于每两个数,程序将使用一个函数来计算它们的调和平均数。

3.函数将计算结果返回给主函数,在主函数中输出输入的数字和它们的调和平均数。

调和平均数是指倒数平均值的倒数。

 

 

[展开全文]

类方法定义

定义成员函数时,需使用作用域解析符(::)来标识函数所属的类。

类方法可以访问类的private(私有)成员。

//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_;

	//共有接口
public:
	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"

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_ >= 60 && s.math_ >= 60 && s.english_ >= 60)
	{
		return true;
	}
	else
	{
		return false;
	}
}

 

[展开全文]

函数的形参在执行完以后会回收,不对实参产生影响,如果直接作用于地址,可以修改改指针地址中的实参,但是地址不会变。

int reset(int i) {
    i *= 2;
    return i;
}

void reset1(int* pi) {
    *pi *= 2;
    pi = 0;
}

[展开全文]

再编译时运行想当与UE事件函数

constexpr 

{

}

[展开全文]

srtuck 结构体名称

{

}

结构体名称 命名 (结构内容名称   自定义结构内容名称)

{

 

}

 

[展开全文]

对象的声明与使用

创建对象,最简单的方式是声明类变量,即Student stu1;

使用对象的成员函数和使用结构成员一样,通过成员运算符(.)来使用,即stu1.sum(stu1)。

注意:所创建的每个新对象都有自己的存储空间,用于存储其内部变量和类成员。但是同一个类的所有对象共享同一组类方法。

在OOP中,调用成员函数也被称为发送消息。

 

//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_;

	//共有接口
public:
	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"

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_ >= 60 && s.math_ >= 60 && s.english_ >= 60)
	{
		return true;
	}
	else
	{
		return false;
	}
}

//18类的定义.cpp

#include "pch.h"
#include "student.h"

int main()
{
	Student stu1;
	Student stu2;
	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;
}

 

[展开全文]

类型前加Friend......

么怎么用过直接publick生成一个函数引用.....

[展开全文]

类的构造函数

每个类都分别定义了它的对象被初始化的方式,类通过一个或几个特殊的成员函数来控制对象的初始化过程,这些函数叫做构造函数

构造函数的任务是初始化对象的数据成员,无论何时只要类的对象被创建,就会执行构造函数。

 

声明和定义构造函数

构造函数的名称与类名相同,并且没有返回值。

声明:Student(string n, int c, int m, int e);

定义:Student::Student(string n, int c, int m, int e)

{

name=n;

chinese=c;

math=m;

english=e;

}

注意:上述代码和函数setStudent()相同,但是,程序声明对象时,会自动调用构造函数。

 

使用构造函数

C++提供了两种使用构造函数来初始化对象的方式:

1.显式地调用构造函数

Student stu1 = Student("Jane",100,90,95);

2.隐式地调用构造函数

Student stu1("Jane",100,90,95);

 

默认构造函数

默认构造函数是在未提供显示初始值时,用来创建对象的构造函数。即用于:Student stu1;

注意:当且仅当没有定义任何构造函数时,编译器才会提供默认构造函数。为类定义了构造函数后,程序员就必须为它提供默认构造函数,否则上面的声明将出错。

 

默认构造函数

定义默认构造函数的方法有两种:

1.给已有的构造函数的所有参数提供默认值。

Student(string n = "" ,int c = 0, int m = 0, int e = 0);

2.通过函数重载来定义一个没有参数的构造函数。

Student();

 

析构函数

用构造函数创建对象后,程序负责跟踪该对象,直到其过期为止。对象过期时,程序将自动调用一个特殊的成员函数,析构函数,来完成清理工作。

 

声明和定义析构函数

析构函数的名称是在类名前加上~,并且析构函数没有参数、返回值和声明类型。

声明:~Student();

定义:Student::~Student()

{

}

 

使用析构函数

类对象过期时,析构函数将自动被调用。

注意:如果程序员没有提供析构函数,编译器将隐式地声明一个默认析构函数,并在发现导致对象被删除的代码后,提供默认析构函数的定义。

 

 

//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_;

	//共有接口
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_ >= 60 && s.math_ >= 60 && s.english_ >= 60)
	{
		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;
}

 

[展开全文]
// 自写
#include <iostream>

using namespace std;

float HarmonicMean(float num1, float num2);

int main()
{
	float num1;
	float num2;
	do
	{
		cout << "请输入第一个数字:";
		cin >> num1;
		cout << "请输入第二个数字:";
		cin >> num2;
		if (num1 == 0 || num2 == 0)
		{
			break;
		}
		cout << num1 << "和" << num2 << "调和平均数是:" << HarmonicMean(num1, num2) << endl;
	} while (num1 != 0 || num2 != 0);

	return 0;
}

float HarmonicMean(float num1, float num2)
{
	return (2 * num1*num2) / (num1+num2);
}

Teacher:

#include <iostream>

using namespace std;

float harmonicMean(float a, float b);

int main()
{
	float num1, num2;
	cout << "请输入两个数的值:" << endl;
	while (cin >> num1 >> num2 && num1 != 0 && num2 != 0)
	{
		cout << num1 << "和" << num2 << "的调和平均数为:";
		cout << harmonicMean(num1, num2) << endl;
		cout << "请输入两个数的值:" << endl;
	}
	return 0;
}

float harmonicMean(float a, float b)
{
	return 2 * a * b / (a + b);
}

 

[展开全文]

this指针:每一个对象都能通过this指针来访问自己的地址。在成员函数内部,它可以用来指向调用对象。

有时候方法可能涉及到两个对象,在这种情况下就需要使用this指针。

 

//Cuboid.h

#pragma once

#ifndef CUBOID_H_
#define CUBOID_H_
class Cuboid
{
private:
	double length_;
	double width_;
	double height_;

public:
	Cuboid(double length, double width, double height);
	Cuboid();
	~Cuboid();
	double volume() { return length_ * width_*height_; };
	int compare(Cuboid & c);
};
#endif // !CUBOID_H_

//Cuboid.cpp

#include "pch.h"
#include "Cuboid.h"


Cuboid::Cuboid(double length, double width, double height)
{
	length_ = length;
	width_ = width;
	height_ = height;
}

Cuboid::Cuboid()
{
}


Cuboid::~Cuboid()
{
}

int Cuboid::compare(Cuboid & c)
{
	if (this->volume() > c.volume())
	{
		return 0;
	}
	else if (this->volume() == c.volume())
	{
		return 1;
	}
	else
	{
		return 2;
	}
}

//19-this指针.cpp

#include "pch.h"
#include "Cuboid.h"
#include <iostream>

using namespace std;

int main()
{
	Cuboid c1(20, 10.5, 11.5);
	Cuboid c2(11.5, 22, 9.5);
	int res = c1.compare(c2);
	if (res == 0)
	{
		cout << "第一个长方体的体积大于第二个长方体的体积" << endl;
	}
	else if (res == 1)
	{
		cout << "两个长方体的体积相等" << endl;
	}
	else
	{
		cout << "第一个长方体的体积小于第二个长方体的体积" << endl;
	}
	return 0;
}

 

[展开全文]

用户通常需要创建同一个类的多个对象,这种情况下,可以创建独立对象变量,也可以创建对象数组

声明对象数组的方法与声明标准类型数组相同。

Student stus[5]; //声明了一个有5个Student对象的数组

注意:要创建类对象数组,则这个类必须有默认构造函数。因为初始化对象数组时,会首先用默认构造函数创建数组元素。

#include "pch.h"
#include "Cuboid.h"
#include <iostream>

using namespace std;

int main()
{
	Cuboid c1(20, 10.5, 11.5);
	Cuboid c2(11.5, 22, 9.5);

	Cuboid cs[2] = {};
	cs[0] = Cuboid(20, 10, 11);
	cs[1] = Cuboid(1, 4.5, 9);//cs[1](11,26,9);//隐式的无法实现,报错

	int res = c1.compare(c2);
	res = cs[0].compare(cs[1]);
	if (res == 0)
	{
		cout << "第一个长方体的体积大于第二个长方体的体积" << endl;
	}
	else if (res == 1)
	{
		cout << "两个长方体的体积相等" << endl;
	}
	else
	{
		cout << "第一个长方体的体积小于第二个长方体的体积" << endl;
	}
	return 0;
}

[展开全文]

内联函数是C++为提高程序运行速度所作的一项改进。

内联函数的编译代码与其他程序代码“内联”起来了,也就是说,编译器将使用相应的函数代码替代函数调用。

对于内联代码,程序无需跳到另一个位置处执行代码,再跳回来。因此,内联函数的运行速度比常规函数稍快,但代价是需要占用更多内存。

 

内联函数

内联函数的使用方法:在函数声明或函数定义前加上关键字 inline

通常的做法是省略原型,将这个定义放在本应提供原型的地方。

注意:内联函数不能递归

 

#include <iostream>

using namespace std;
inline int sum(int a, int b) { return a + b; }

int main()
{
	int res = sum(20, 45);
	cout << res << endl;
	return 0;
}

[展开全文]

类作用域

每个类都会定义它自己的作用域。在类的作用域之外,普通的数据和函数成员只能由对象,引用或者指针使用成员访问运算符访问。

对象调用:直接成员运算符(.)

指针调用:间接成员运算符(->)

在类的外部定义成员函数时,必须同时提供类名和函数名。

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;
}

 

[展开全文]

constexpr函数

constexpr函数:是指能用于常量表达式的函数,即可以在编译时计算其返回值的函数。

常量表达式是指值不会改变且在编译过程就能得到计算结果的表达式。

 

const expression

 

constexpr函数

注意:

1.函数中只能有一个return语句。

2.返回值必须是字面值类型(算术类型、引用、指针属于字面值类型)。

3.参数必须是字面值类型(自定义类、IO库、string类型不属于字面值类型)。

4.constexpr函数被隐式地指定为内联函数。

5.允许递归

 

#include <iostream>

using namespace std;

//常量表达式函数
constexpr int fact(int n)
{
	return n == 1 ? 1 : n * fact(n - 1);
}
//常量表达式
constexpr int num = 5;

int main()
{
	//在编译期间可以计算结果并返回结果
	cout << fact(num) << endl;
	cout << fact(3) << endl;

	//实参为变量时,在程序运行期间计算并返回结果
	int i = 8;
	int res = fact(i);
	cout << res << endl;
	return 0;
}

[展开全文]

函数重载

如果同一作用域内的几个函数名字相同但形参列表不同,我们称之为重载函数

void print(const char *cp);

void print(const int *beg,const int *end);

void print(const int ia[],size_t size);

这些函数接受的形参类型不一样,但是执行的操作非常类似。当调用这些函数时,编译器会根据传递的实参类型推断想要的是哪个函数。

 

 

#include <iostream>

using namespace std;

//重载函数,名称相同但是参数列表不同的函数,调用时,系统会根据我们传递的实参来选择调用的函数。
void print(const char *cp);
void print(const int *beg, const int *end);
void print(const int ia[], size_t size);

int main()
{
	char c = 'a';
	//传递的实参是char类型的指针,所以会调用第一个函数
	print(&c);

	const size_t size = 5;
	int arr[size] = { 2,3,3 };
	//传递的实参是两个int类型的指针,所以会调用第二个函数
	print(&arr[2], &arr[size - 1]);
	//传递的实参是数组名和一个size_t类型的值,所以会调用第三个函数
	print(arr, size);
	//传递的实参是数组名,但是没有和它相对应的参数列表的函数定义,所以会出错
	//print(arr);
	return 0;
}

void print(const char *cp)
{
	cout << "1: " << *cp << endl;
}

void print(const int *beg, const int *end)
{
	int length = end - beg;
	for (int i = 0; i <= length; i++)
	{
		cout << "2: " << *(beg + i) << endl;
	}
}

void print(const int ia[], size_t size)
{
	for (int i = 0; i < size; i++)
	{
		cout << "3: " << ia[i] << endl;
	}
}

[展开全文]

 

友元函数

类可以允许其他类或者函数访问它的非公有成员,方法是令其他类或者函数成为它的友元(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.使用作用域为类的常量。

 

[展开全文]

#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);

 

}

[展开全文]