ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

构造函数与析构函数

2021-03-27 19:02:12  阅读:158  来源: 互联网

标签:函数 int 与析构 void score 析构 构造函数


一、构造函数
构造函数是一种特殊的成员函数,主要用于为对象分配空间,进行初始化。它的名字必须与类名相同,不能任意命名。它可以有任意类型的参数,但不能具有返回值。
为类Score定义一个构造函数:

class Score{
	public:
		Score(int m,int f);  //声明构造函数Score()的原型
		void setScore(int m,int f);
		void showScore();
	private:
		int mid_exam;
		int fin_exam;		
}; 
Score::Score(int m,int f)   //定义构造函数Score()
{
	cout<<"构造函数使用中..."<<endl;
	mid_exam=m;
	fin_exam=f;
}

建立对象的同时用构造函数给数据成员赋初值,例:

#include<iostream>
using namespace std;
class score{
	public:
		score(int m,int f);  //声明构造函数score() 
		void setscore(int m,int f); 
		void showscore();
	private:
		int mid_exam;      //私有数据成员 
		int fin_exam;
};
score::score(int m,int f)  //定义构造函数score() 
{
	cout<<"构造函数使用中..."<<endl;
	mid_exam=m;
    fin_exam=f;
}
void score::setscore(int m,int f)
{
	mid_exam=m;
	fin_exam=f;
}
inline void score::showscore() //在类外定义此函数为内联函数 
{
	cout<<"\n期中成绩:"<<mid_exam<<"\n期末成绩:"<<fin_exam<<"\n";
	cout<<"总评成绩:"<<(int)(0.3*mid_exam+0.7*fin_exam)<<endl; 
}
int main()
{
	score score1(80,88);  //定义类score的对象score1,自动调用构造函数,并给对象score1的数据赋初值 
	cout<<endl<<"成绩输出:";
	score1.showscore();  //定义成员函数showscore(),显示score1的数据 
	score1.setscore(90,92);
	cout<<endl<<"成绩输出:";
	score1.showscore();
	return 0;
}

还有一种使用new运算符动态建立对象的方式
将上面代码的主函数改成如下所示:

int main()
{
	score *pscore;
	pscore=new score(80,88);
	cout<<endl<<"成绩输出:";
	pscore->showscore();
	pscore->setscore(90,92);
	cout<<endl<<"成绩输出:";
	pscore->showscore();
	delete pscore;
	return 0; 
 } 

注意:
①构造函数的名字和类名必须相同,否则编译程序会把它当作一般的成员函数来处理。
②构造函数没有返回值,在定义构造函数时不能说明他的类型,void也不行。
③构造函数的函数体可以写在类体内,也可以写在类体外,与普通的成员函数一样,当构造函数直接定义在类内时,系统将构造函数作为内联函数处理。
④构造函数可以不带参数。
二、析构函数
析构函数也是一种特殊的成员函数。它执行与构造函数相反的操作,通常用于撤销对象时的一些清理任务,如释放分配给对象的内存空间等。有以下特点:
①析构函数和构造函数名字相同,但它前面必须加一个波浪号(~)
②析构函数没有参数,也没有返回值,而且不能重载。因此一个类中只能有一个析构函数。
③撤销对象时,系统自动调用析构函数。

#include<iostream>
#include<string.h>
using namespace std;
class student{
	public:
		student(char *name1,char *stu_no1,float score1); //声明构造函数 
		~student();    //声明析构函数 
		void modify(float score1);  //成员函数,修改数据 
		void show();    //成员函数,显示数据 
	private:
		char *name;     //学生姓名 
		char *stu_no;   //学生学号 
		float score;    //学生成绩 
};
student::student(char *name1,char *stu_no1,float score1)  //定义构造函数 
{
	name=new char[strlen(name1)+1];  //strlen(name1)+1防止溢出
	strcpy(name,name1);
	stu_no=new char[strlen(stu_no1)+1];
	strcpy(stu_no,stu_no1); 
	score=score1;
 }  
student::~student()   //定义析构函数 
{
	delete []name;
	delete []stu_no;
}
void student::modify(float score1)
{
	score=score1;
}
void student::show()
{
	cout<<"姓名:"<<name<<endl;
	cout<<"学号:"<<stu_no<<endl;
	cout<<"分数:"<<score<<endl; 
}
int main()
{
	student stu1("黎明","20150201",90);  //定义student的对象stu1 调用构造函数初始化stu1 
	stu1.show();   //调用成员函数show()显示stu1的数据 
	stu1.modify(88);  //调用成员函数修改stu1的数据 
	cout<<"修改后:"<<endl;
	stu1.show();  //显示stu1的数据 
}

当程序结束,对象撤销时,调用了析构函数,释放由new分配的内存空间。

在以下情况,对象将被撤消,编译系统也会自动地调用析构函数:
① 主程序main()运行结束。
② 如果一个对象被定义在一个函数体内,则当这个函数结束时,该对象的析构函数被自动调用。
③ 若一个对象是使用new运算符动态创建的,在使用delete运算符释放它时,delete会自动调用析构函数。

标签:函数,int,与析构,void,score,析构,构造函数
来源: https://blog.csdn.net/m0_52671984/article/details/115186235

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有