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

制作于2018.4.2

价格 免费

类的构造函数

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

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

 

声明和定义构造函数

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

声明: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;
}

 

[展开全文]

授课教师

SIKI学院老师

课程特色

下载资料(1)
视频(43)