一、目的和要求
1.掌握拷贝构造函数和赋值运算符的使用方法
2.掌握静态成员变量和静态成员函数的使用方法
3.了解C++友元机制和掌握友元的使用方法
4.掌握常类型的使用方法
二、具体内容
1.完成学生类的实现,并测试所有的成员函数。
#include "stdafx.h"
#include <iostream>
using namespace std;
class Student
{
public:
enum Gender{Male,Female};
friend ostream& operator<<(ostream& os, Gender gender);
Student();
Student(const char* name, Gender gender);
Student(const Student& student);
Student& operator=(const Student& student) {
name_ = student.name_;
gender_ = student.gender_;
return *this;
}
~Student();
void Show() const;
private:
char* name_;
Gender gender_;
};
Student::Student()
{
name_ = NULL;
gender_ = Male;
}
ostream& operator << (ostream& os, Student::Gender gender) {
if (gender == Student::Female) {
os << "Female";
}
else {
os << "Male";
}
return os;
}
Student::Student(const char* name, Gender gender) {
name_ = new char[strlen(name) + 1];
strcpy(name_ , name);
gender_ = gender;
}
Student::Student(const Student& student) {
name_ = student.name_;
gender_ = student.gender_;
}
Student::~Student() {
delete[] name_;
}
void Student::Show() const {
cout << "姓名:" << name_ << endl;
cout << "性别:" << gender_ << endl;
}
int main()
{
Student s1;
Student s2("Nicolas ZhaoSi", Student::Gender::Male);
Student s3("Jason GuoDa Statham", Student::Gender::Male);
Student s4(s2);
s1 = s3;
s1.Show();
s2.Show();
s3.Show();
s4.Show();
return 0;
}
运行结果:
2.声明一个Student类,在该类中包含一个数据成员表示学生成绩、两个静态数据成员表示总成绩和学生人数;有参构造函数用于设置成绩、累计成绩之和、累计学生人数,一个静态函数用于返回学生的成绩之和,另一个静态成员函数用于取全部成绩的平均分。在main函数中输入某班同学的成绩,并求出全部学生的成绩之和和平均分。
#include "stdafx.h"
#include <iostream>
#include <time.h>
using namespace std;
class Student
{
public:
Student() {}
Student(double score) {
score_ = score;
total_numbers_++;
total_score_ += score_;
}
static double Sum() {
return total_score_;
}
static double Average() {
return total_score_ / total_numbers_;
}
private:
int score_;
static double total_score_;
static double total_numbers_;
};
double Student::total_score_ = 0.0;
double Student::total_numbers_ = 0.0;
int main()
{
int n;
cin >> n;
Student* student = new Student[n];
srand((unsigned)time(NULL));
for (int i = 0; i < n; i++) {
student[i] = Student(rand() % 100);
}
cout << "总成绩:" << Student::Sum() << endl;
cout << "平均成绩:" << Student::Average() << endl;
return 0;
}
运行结果:
3.定义一个平面点的类Point,其中x和y坐标设为私有数据成员。定义线段类Line,数据成员包括线段的两个端点、线段的长度,定义线段类Line的常成员函数用于显示线段端点坐标和线段长度。完成两个类的构造函数和一些必要的成员函数,并在main函数中构建两条线段,调用常成员函数显示结果。
#include "pch.h"
#include <iostream>
#include <time.h>
#include <math.h>
using namespace std;
class Point
{
public:
Point () {}
Point (int x, int y) {
x_ = x;
y_ = y;
}
int get_x () const {
return x_;
}
int get_y () const {
return y_;
}
double Distance (const Point& point) {
return sqrt (pow (point.x_ - x_, 2) + pow (point.y_ - y_, 2));
}
friend ostream& operator << (ostream& os, const Point& point) {
os << "点 (" << point.x_ << "," << point.y_ << ")" << endl;
return os;
}
private:
int x_;
int y_;
};
class Line {
public:
Line () {
length_ = start_point_.Distance (end_point_);
}
Line (int x1, int y1, int x2, int y2) :start_point_ (x1, y1), end_point_ (x2, y2)
{
length_ = start_point_.Distance (end_point_);
}
Line (Point& p1, Point& p2) {
start_point_ = p1;
end_point_ = p2;
length_ = start_point_.Distance (end_point_);
}
double Length () const {
return length_;
}
void Show () const {
cout << start_point_;
cout << end_point_;
cout << "两点距离:" << Length () << endl;
}
private:
Point start_point_;
Point end_point_;
double length_;
};
int main ()
{
Point p1 (1, 1);
Point p2 (2, 2);
Line line1 (p1, p2);
Line line2 (1, 2, 3, 4);
line1.Show ();
line2.Show ();
return 0;
}
运行结果: