创建一个学生类student,用C++编程实现

供稿:hz-xin.com     日期:2025-01-13
用C++编写一个student类,作用如下:

#include #include using namespace std;class student{public: static int count; student(){++count;} student(string ID="",string name="",int sex=0,int grade=0): m_ID(ID),m_name(name),m_sex(sex),m_grade(grade){++count;} ~student(){--count;} void getData(){ coutgetData(); stu2->showCount(); delete stu2; cout<<"删除了一条"<<endl; stu1.showCount(); cout<<"调用友元函数"<<endl; showData(stu1);}

#include
#include
using namespace std;

class Student
{
private:
string name;
string code;
int age;
public:
Student()
{
name = ""; //其实这里不需要初始化,因为string定义的变量已经初始化为空了
code = "";
age = 0;
}
Student(string n, string c, int a)
{
name = n;
code = c;
age = a;
}
PrintInfo()
{
cout<<name<<" "<<code<<" "<<age<<endl;
}
};

int main()
{
Student st("张三", "001", 20);
st.PrintInfo();

return 0;
}

代码如下:

#include <iostream>
#include <cstring>
using namespace std;

class Student {

public:

Student() {
this->id = 0;
this->name = NULL;
this->age = 0;
this->major = NULL;
}

Student(int id, const char * name) {
this->id = id;
this->name = NULL;
SetName(name);
this->age = 0;
this->major = NULL;
}

Student(int id, const char * name, int age, const char *major) {
this->id = id;
this->name = NULL;
SetName(name);
this->age = age;
this->major = NULL;
SetMajor(major);
}

~Student() {
if (this->name) {
delete this->name;
}
if (this->major) {
delete this->major;
}
}

int GetId() const {
return this->id;
}

void SetId(int id) {
this->id = id;
}

const char * GetName() const {
return this->name;
}

void SetName(const char * name) {
if (this->name) {
delete this->name;
}

int len = strnlen_s(name, 20);
this->name = new char[len + 1];
strcpy_s(this->name, len + 1, name);
}

int GetAge() const {
return this->age;
}

void SetAge(int age) {
this->age = age;
}

const char * GetMajor() const {
return this->major;
}

void SetMajor(const char * major) {
if (this->major) {
delete this->major;
}

int len = strnlen_s(major, 20);
this->major = new char[len + 1];
strcpy_s(this->major, len + 1, major);
}

private:
int id;
char * name;
int age;
char * major;
};

void PrintStudent(const Student& student)
{
cout << student.GetId() << "," << student.GetName() << "," << student.GetAge() << "," << student.GetMajor() << endl;
}

int _tmain(int argc, _TCHAR* argv[])
{
Student stu1(2001, "张三", 25, "计算机应用");
PrintStudent(stu1);

Student stu2(2002, "李四");
stu2.SetAge(22);
stu2.SetMajor("美术");
PrintStudent(stu2);

Student stu3;
stu3.SetId(2003);
stu3.SetName("王五");
stu3.SetAge(20);
stu3.SetMajor("音乐");
PrintStudent(stu3);

system("pause");
return 0;
}


创建一个学生类(student),包含:学号,姓名,性别,Java成绩,sql 成绩,c...
学生类:public class Student{ private String stuId; private String name; private String gender; private float javaScore; private float sqlScore; private float cScore; private float htmlScore; private float avgScore; private float sumScore; public Student()...

c++设计一个学生类Cstudent,该类包括学生学号、姓名以及数学、英语、c...
void print(struct student *);void input(struct student *);struct student{int num;char name[20];int score[3];}stu[10];struct student *p;void main(){p = stu;input(p);print(p);system("pause");}void input(struct student *p){int i = 0, j;for(p = stu; p < stu + ...

用C#编程实现输出一个学生的基本信息。
getName():获得姓名;getSex():获得性别;getAge()获得年龄;getJava():获得Java 课程成绩 (4)根据类Student的定义,创建五个该类的对象,输出每个学生的信息,计算并输出这五个学生Java语言成绩的平均值,以及计算并输出他们Java语言成绩的最大值和最小值。*\/ class student{ String sNO,...

C++做一个Cstudent的类
cout<<"学生性别:"<<sSex;cout<<"学生年龄:"<<sage;cout<<"学生成绩:"<<score;} } class test { static void main(){ Cstudent cs1;cs1 = new Cstudent();cs1.InputInfo();Cstudent cs2 = new Cstudent("wang","0010","女",23,90);cs1.PrintInfo();cs2.PrintInfo();} } ...

定义一个学生类CStudent,学生类的数据成员包括姓名、学号和成绩等。_百...
include"Exp19_1.h"include<fstream> include<iostream> include<string> using namespace std;class student{ \/\/为了简化,数据域仅保留学号与姓名 int key;string name;public:void putkey(int k){key=k;} void putnam(string nam){name=nam;} void show(){cout<<key<<'\\t'<<name<<end...

c语言 定义一个学生结构体类,包含学生姓名,性别,年龄和语文课程的成绩...
student name:\\r\\n"); scanf("%s", a.name); printf("please input the first student sex:\\r\\n"); scanf("%s", a.sex); printf("please input the first student age:\\r\\n"); scanf("%d", &a.age); printf("please input the first student score:\\r\\n"); scanf(...

设计一个学生类CStudent,包括姓名和三门课程成绩,利用重载运算符“+...
using namespace std;class student { private:char a[20]; \/\/不能在类定义中对非static const型变量赋值 int x ,y ,z;int cnt; \/\/计数器,存放成绩对应的人数。默认为1个学生的成绩。public :student():cnt(1){ a[0]='\\0';x=y=z=0;} student (char *b,int m,int k,int...

c语言 利用面向对象方法,定义一个学生类,求每名同学的平均分和所有同学...
Student stu[10];string name,num;double score,max = 0.0;for(int i = 0;i < 3;i++){ cout<<"输入学生"<<i+1<<"姓名:";cin>>name;cout<<"输入"<<i+1<<"学号:";cin>>num;cout<<"输入"<<i+1<<"分数:";cin>>score;stu[i].SetName(name);stu[i].SetNum(num);st...

定义一个CStudent类,数据成员包括学号,姓名和成绩,成员函数有构造函数...
}void set_no(string no){stu_no = no;}void set_score(int score){stu_score = score;}string get_name(){return stu_name;}string get_no(){return stu_no;}int get_score(){return stu_score;}friend void statis_students(CStudent [], int num, int &max_score, int &min_...

C语言求助:定义一个学生结构体数组stu[10],结构体类型?
define N 10 typedef struct student { int num;\/\/成员num (学号)的类型为整型 char name[21];\/\/成员name (姓名)的类型为字符数组 char sex;\/\/成员sex(性别)的类型为字符型 int age; \/\/成员score(分数)的类型为double类型 }stu;void show(stu a[],int n) { for(int i = 0; i < n...