c++中类内为什么出现了两个Student成员函数,它们两个分别代表什么含义
一个是无参构造函数,一个是含参构造函数
百度 构造函数
#include "stdafx.h"
#include <iostream>
using namespace std;
class Student{ //定义一个学生类Student
public:
Student(int English=0, int Computer=0)
{
e = English;
c = Computer;
}
Student(Student&S);
int getE(){ return e; }
int getC(){ return c; }
private:
int e, c;
};
Student::Student(Student&S)
{
e = S.e;
c = S.c;
}
void bubble(int *a, int n) //把学生成绩从高到低排序
{
int i, j, temp;
for (i = 0; i < n - 1; i++)
{
for (j = i + 1; j < n; j++)
{
if (a[i] < a[j])
{
temp = a[i]; //用值传递的方法进行排序
a[i] = a[j];
a[j] = temp;
}
}
}
cout << "这三位同学的总分从高至低排序为:";
for (i = 0; i < n; i++)
{
cout <<a[i] << " ";
}
cout << endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
int e[3];
int c[3];
int t[3];
Student thee(e[3]);
Student thec(c[3]);
int i, j, k;
cout << "请分别输入三位同学的英语成绩:";
for (int i = 0; i < 3; i++)
{
cin >> e[i];
}
cout << "请分别输入三位同学的计算机成绩:";
for (int j = 0; j < 3; j++)
{
cin >> c[j];
}
for (int k = 0; k < 3; k++) //学生总分
{
t[k] = e[k] + c[k];
}
bubble(t, 3);
return 0;
}
问题解答:
在C++中类内会有两个名为Student的成员函数,一个是在类内定义的成员函数,另一个是类的构造函数。类内定义的成员函数是用于实现类的功能,而构造函数则是用于对象的初始化,可以看成是一个特殊的成员函数。
关于类内共有空间的相关知识,指的是在类中定义成员变量和成员函数时,它们所占用的内存空间都是在类定义中分配的。类中的静态成员变量和静态成员函数也是占用类的内存空间的。
具体实现上,可以在类定义后面加上static关键字来定义静态成员变量和静态成员函数。静态成员变量占用类内存空间,但是被所有类的对象所共享。静态成员函数没有this指针,它只能访问静态成员变量和静态成员函数,也是不属于任何一个对象的,而是属于整个类的。
下面给出一个示例代码,其中包括Student类和Course类的定义,以及构造函数、析构函数和拷贝构造函数的实现:
// student.h
#include <iostream>
#include <string>
class Student {
private:
std::string name;
int age;
std::string id;
static int count; // 静态成员变量
public:
Student(); // 构造函数
Student(std::string n, int a, std::string i);
// 拷贝构造函数
Student(const Student& other);
~Student(); // 析构函数
void display(); // 成员函数
static int getCount(); // 静态成员函数
};
// student.cpp
#include "student.h"
int Student::count = 0;
// 构造函数的实现
Student::Student() : name(""), age(0), id("") {
count++;
}
Student::Student(std::string n, int a, std::string i)
: name(n), age(a), id(i) {
count++;
}
// 拷贝构造函数的实现
Student::Student(const Student& other) {
name = other.name;
age = other.age;
id = other.id;
count++;
}
// 析构函数的实现
Student::~Student() {
count--;
}
void Student::display() {
std::cout << "Name: " << name << ", Age: " << age << ", ID: " << id << std::endl;
}
int Student::getCount() {
return count;
}
// course.h
#include <string>
class Course {
private:
std::string name;
int credit;
public:
Course(std::string n, int c);
// 拷贝构造函数
Course(const Course& other);
};
// course.cpp
#include "course.h"
Course::Course(std::string n, int c) : name(n), credit(c) {}
// 拷贝构造函数的实现
Course::Course(const Course& other) {
name = other.name;
credit = other.credit;
}
其中,Student类包含了一个静态成员变量count和一个静态成员函数getCount,用于统计学生对象的个数。此外,Student类还包含了三个构造函数、一个析构函数和一个成员函数display,用于对象的初始化、销毁和操作。
Course类只包含了一个构造函数和一个拷贝构造函数,用于初始化课程对象。注意,拷贝构造函数的实现中也要对类中的每个成员变量进行赋值操作。
对于多文件组织类的问题,可以将Student类的定义和实现分别放在两个文件student.h和student.cpp中,然后在main.cpp中引用该类。具体实现如下:
// main.cpp
#include "student.h"
#include "course.h"
int main() {
Student stu1("Alice", 20, "1001");
stu1.display();
std::cout << "Number of students: " << Student::getCount() << std::endl;
Student stu2(stu1);
stu2.display();
std::cout << "Number of students: " << Student::getCount() << std::endl;
Course cou1("C++ Programming", 3);
Course cou2(cou1);
return 0;
}
其中,main函数中先创建了一个学生对象stu1,然后调用成员函数display来显示学生信息。接着测试了拷贝构造函数,创建了一个新的学生对象stu2,并且通过静态成员函数getCount来统计学生对象的个数。最后创建了两个课程对象cou1和cou2,并测试了拷贝构造函数的实现。
注意,在编译时需要将student.cpp和course.cpp文件链接到可执行文件中,具体命令为:
g++ -o main main.cpp student.cpp course.cpp
建议初学者去B站看视频学习,自己看书又慢又学不会。