当前位置:   article > 正文

C++ 实验五 NO.1 定义一个student类包含成员变量姓名,年龄,学号,用多文件组织类, 在student类中重载构造函数,用多种方式对该类的对象初始化_定义一个学生类student,包含姓名和年龄,学号

定义一个学生类student,包含姓名和年龄,学号

题目

//*******************************************************************************************************************
//*程序作者:冰糖
//*章   节:实验五
//*最后修改日期:2019年3月31日 
//*题   目:
//实验目的
/*1)	掌握类的定义,根据具体要求设计类。能够根据类来创建对象。
  2)	掌握拷贝构造函数的实现方法。
  3)	掌握静态数据成员和静态函数的使用方法。
  4)	理解友元的概念和掌握友元的使用方法。
  5)	掌握对象数组、对象指针的使用方法。*/
//        1.
//         	定义一个student类
//         	包含成员变量姓名,年龄,学号
//         	包含成员函数在屏幕上显示学生的姓名,年龄,学号
//         	包含构造函数,对成员变量初始化
//         	创建此类的对象,测试构造函数的调用
//         	验证成员函数功能
//        2.
//        	用多文件组织类(student.h,student.cpp, main.cpp),完成上述工作。
//        3.
//         	在student类中重载构造函数,用多种方式对该类的对象初始化
//         	测试构造函数的调用顺序
//        4.
//         	向student类添加:
//         	构造函数:实现动态内存分配
//         	析构函数:释放已分配的内存
//         	测试构造函数和析构函数的调用顺序
//        5.
//         	定义一个课程类 (course.h, course.cpp)
//         	包含课程名和该课程的学分
//         	编写课程类的构造函数
//         	编写课程类的拷贝构造函数
//         	定义课程对象,测试构造函数和拷贝构造函数
//***********************************************************************************************************************8 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35

main.cpp

#include<iostream> 
using namespace std;
#include"student.h"
#include"student.cpp"
#include"course.h"
#include"course.cpp"
int main()
{
	Student stu1;
	Course cou1;
	stu1.getInfo();
	cou1.getInfo();
	Student stu2(stu1);//*************************************用stu1初始stu2**************************************** 
	Course cou2(cou1);//*************************************用cou1初始cou2****************************************
	stu2.getInfo();
	cou2.getInfo();
	return 0;
}
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

student.cpp

#include"student.h"
#include<string.h>
#include<iostream>
using namespace std;
Student::Student()
{
	cout<<"**********************************************************\n";
	cout<<"请按提示输入学生信息:"<<endl;
	cout<<"姓名:"<<endl;
	cin>>name;
	cout<<"年龄:"<<endl;
	cin>>age;
	cout<<"学号:"<<endl;
	cin>>stuNum;
	cout<<"调用student构造函数成功......"<<endl;
}
Student::Student(const Student &obj)
{
	cout<<"**********************************************************"<<endl;
	strcpy(name,obj.name);
	age=obj.age;
	stuNum=obj.stuNum;
	cout<<"调用student拷贝构造函数......"<<endl;
} 
void Student::getInfo()
{
	cout<<"**********************************************************\n";
	cout<<"学生基本信息如下:"<<endl;
	cout<<"姓名:"<<name<<endl
		<<"年龄:"<<age<<endl
		<<"学号:"<<stuNum<<endl; 
} 
Student::~Student()
{
	cout<<"**********************************************************\n";
	//delete[] name;
	cout<<"调用student析构函数成功......"<<endl;	
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38

student.h

#ifndef STUDENT_H
#define STUDENT_H
/*#include<iostream>
#include<string.h>
using namespace std;*/
class Student
{
	private:
		char name[20];
		int age;
		int stuNum;
	public:
		Student();
		Student(const Student &obj);
		void getInfo();
		~Student();
};
#endif
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

course.cpp

#include"course.h"
#include<string.h>
#include<iostream>
using namespace std;
Course::Course()
{
	cout<<"**********************************************************\n";
	cout<<"请输入课程名:"<<endl;
	cin>>course;
	cout<<"请输入该门课程的成绩:"<<endl;
	cin>>score;
	cout<<"调用course构造函数......" <<endl;
}
Course::Course(const Course &obj)
{
	cout<<"**********************************************************\n";
	strcpy(course,obj.course);
	score=obj.score;
	cout<<"调用course拷贝构造函数......"<<endl;
}
void Course::getInfo()
{
	cout<<"**********************************************************\n";
	cout<<"课程相关信息如下:"<<endl
		<<"课程名:"<<course<<endl 
		<<"分数:"<<score<<endl; 
}
Course::~Course()
{
	cout<<"**********************************************************\n";
	cout<<"调用course析构函数......"<<endl;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

course.h

#include"course.h"
#include<string.h>
#include<iostream>
using namespace std;
Course::Course()
{
	cout<<"**********************************************************\n";
	cout<<"请输入课程名:"<<endl;
	cin>>course;
	cout<<"请输入该门课程的成绩:"<<endl;
	cin>>score;
	cout<<"调用course构造函数......" <<endl;
}
Course::Course(const Course &obj)
{
	cout<<"**********************************************************\n";
	strcpy(course,obj.course);
	score=obj.score;
	cout<<"调用course拷贝构造函数......"<<endl;
}
void Course::getInfo()
{
	cout<<"**********************************************************\n";
	cout<<"课程相关信息如下:"<<endl
		<<"课程名:"<<course<<endl 
		<<"分数:"<<score<<endl; 
}
Course::~Course()
{
	cout<<"**********************************************************\n";
	cout<<"调用course析构函数......"<<endl;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

运行结果

在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/244457
推荐阅读
相关标签
  

闽ICP备14008679号