⑴ VC++,定义一个学生类Student
#include<iostream>
using namespace std;
class Student
{
private:
int age;
char *name;
public:
static int count;
public:
Student(int m, char *n);
Student();
~Student( );
void Print( ) const;
};
int Student::count = 0;
Student::Student(int m, char *n)
{
age = m;
name = n;
count++;
}
Student::Student()
{
age = 0;
name = "NoName";
⑵ 定义一个学生类,包含学号、姓名、平时成绩和考核成绩四个数据成员和以下成员方法(用python语言):
花了不少时间写的,挺详细的,希望采纳。
#引入operator模块,用于给集合排序
importoperator
#单行注释用"#",多行注释用'''注释内容'''
#定义一个学生类,类名用驼峰命名法
classStudent:
#构造方法,可用来创建对象格式def__init__(self,参数)参数个数自已定义,类型系统自动识别
def__init__(self,stu_no,name,base_score,exam_score):
self.stu_no=stu_no#对象属性赋值
self.name=name
self.base_score=base_score
self.exam_score=exam_score
#定义计算总评函数定义函数格式def函数名(self,参数),self代表本对象
defget_last_score(self):
#return指定返回值,无return表示此函数无返回值
returnself.base_score*0.3+self.exam_score*0.7
#类似toString方法,打印对象时,调用对象的该方法
def__str__(self):
return'学号:'+self.stu_no+'姓名:'+self.name+",平时成绩:"+str(self.base_score)+",考核成绩:"+str(self.exam_score)
#定义函数,将对象集合写到文件,上面三个函数有缩进,属于Student类的函数,本函数属于全局函数
defprint_to_file(path,stu_arr):
#打开文件,操作完成后自动关闭文件
withopen(path,'w')asfile:
#调用operator给集合排序
sort_attr=operator.attrgetter('stu_no')#指定排序属性
stu_arr.sort(key=sort_attr)#排序
forstuinstu_list:
str=stu.__str__()#将对象转换为字符串
file.write(str+' ')#将字符串写入文件
#主函数,运行的入口
if__name__=='__main__':
#创建几个学生对象,按__init__的顺序输入参数
s1=Student('1001','zhangsan',31,69)
s2=Student('1003','wangwu',28,32)
s3=Student('1004','zhaoliu',77,78)
s4=Student('1002','lisi',19,89)
#创建集合
stu_list=[s1,s2,s3,s4]
#文件路径
f='d:\aaa.txt'
print_to_file(f,stu_list)
⑶ java程序设计,定义一个表示学生的类Student
public class Student { //定义一个学生类
private int StuNum; //学号
private int Class; //班级
private char Gender; //性别
private int Age; //年龄
public Student(int StuNum, int Class, char Gender, int Age){//构造函数
this.stuNum = StuNum;
this.class = Class;
this.gender = Gender;
this.age = Age;
}
public int getStuNum() { //获得学号
return StuNum;
}
public int getClass() { //获得班级号
return Class;
}
public char getGender() { //获得性别
return Gender;
}
public void setGender(char Gender) { //修改性别
this.Gender = Gender;
}
public int getAge() { //获得年龄
return Age;
}
public void setAge(int Age) { //修改年龄
this.Age = Age;
}
public class Pupil extends Student //小学生
{
//...(由于没说派生后要新加什么东西,所以这里写了省略号,如果想在
// 新派生出来的类里加点什么特殊的东西,直接在省略号位置加就行了
// 下同)
}
public class MidSchoolStu extends Student //中学生
{
//...
}
public class UnderGraate extends Student //大学生
{
//...
}
public class PostGraate extends Student //研究生
{
//...
}
public class FreshStudent extends UnderGraate //一年级学生
{
//...
}
public class Sophomore extends UnderGraate //二年级学生
{
//...
}
public class Junior extends UnderGraate //三年级学生
{
//...
}
public class Senior extends UnderGraate //四年级学生
{
//...
}
public class Master extends PostGraate //硕士生
{
//...
}
public class Doctor extends PostGraate //博士生
{
//...
}
⑷ c++编程题定义一个学生类
#include<iostream>
#include<iomanip>
using namespace std;
class Student{
private:
int num;
char name[10];
int age;
double score;
public:
Student(int,char[],int,double);
~Student(){
cout<<"Destruct is called"<<endl;
}
void display(){
cout<<setiosflags(ios::left);
cout<<setw(5)<<num<<setw(8)<<name<<setw(5)<<age<<setw(5)<<score<<endl;
}
};
Student::Student(int n,char str[],int a,double s){
num=n;
for(int k=0;str[k]!='\0';k++)
name[k]=str[k];
name[k]='\0';
age=a;
score=s;
}
void main(){
Student stu(1,"linda",23,735);
stu.display();
return;
}
注:大致上类体就是这样的,至于主要数可以自己编写,类的成员函数也可以根据需要增加.
⑸ 在java中编写程序,定义一个学生类
代码如下:
package exam2;
import java.util.ArrayList;
import java.util.List;
/**
编写一个Java应用程序,该程序包括3个类:
定义一个学生类,Student有姓名,学号,选学的课程列表
定义一个课程类:课程名称,课程分数
(1)初始化一个学生,选择了三门课程,并添加到学生的属性中
(2)实现统计学生的总分功能
1.正确编写2个类(5分)
2.统计学生的总分功能(5分)
*/
public class Test {
public static void main(String[] args) {
List<Course> courses = new ArrayList<>();
// 初始化3门课程及分数
Course course1 = new Course("java", 80);
Course course2 = new Course("Math", 60);
Course course3 = new Course("English", 90);
// 课程对象添加到集合
courses.add(course1);
courses.add(course2);
courses.add(course3);
// 初始化学生对象
Student student = new Student("tom", "2015101", courses);
// student.setStuid("2015101");
// student.setStuname("tom");
// student.setCourses(courses);
// System.out.println(student);
// (2)实现统计 学生 的总分功能
int sum = sumScore(courses);
System.out.println(student.getStuname() + "学生总分:" + sum);
}
private static int sumScore(List<Course> courses) {
int sum = 0;
// 用课程对象,来获取课程的分数
for (Course course : courses) {
sum += course.getScore();
}
return sum;
}
}
package exam2;
import java.util.List;
public class Student {
// 定义一个学生类,Student有姓名,学号,选学的课程列表
private String stuname;
private String stuid;
private List<Course> courses;
public Student() {
}
public Student(String stuname, String stuid, List<Course> courses) {
this.stuname = stuname;
this.stuid = stuid;
this.courses = courses;
}
public String getStuname() {
return stuname;
}
public void setStuname(String stuname) {
this.stuname = stuname;
}
public String getStuid() {
return stuid;
}
public void setStuid(String stuid) {
this.stuid = stuid;
}
public List<Course> getCourses() {
return courses;
}
public void setCourses(List<Course> courses) {
this.courses = courses;
}
@Override
public String toString() {
return "学生姓名=" + stuname + ",学号=" + stuid + ", 课程=" + courses;
}
}
package exam2;
public class Course {
//定义一个课程类:课程名称,课程分数
private String cname;
private int score;
public Course() {
}
public Course(String cname, int score) {
this.cname = cname;
this.score = score;
}
public String getCname() {
return cname;
}
public void setCname(String cname) {
this.cname = cname;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
@Override
public String toString() {
return "[课程名称=" + cname + ", 课程分数=" + score + "]";
}
}
/*
运行:
tom学生总分:230
*/
(5)定义学生类型算法扩展阅读:
Public语句说明
1.Public语句声明的变量在所有应用程序的所有没有使用OptionPrivate Mole的模块的任何过程中都是可用的;若该模块使用了OptionPrivate Mole,则该变量只是在其所属工程中是公用的。
2.使用Public语句可以声明变量的数据类型。例如,下面的语句声明了一个Integer类型的变量。
3.Public NumberOfEmployees As Integer 也可以使用Public语句来声明变量的对象类型。下面的语句为工作表的新实例声明了一个变量。
参考资料:网络-Public 语句
⑹ C++题目,有大佬过来看看么 定义学生类。 (1)类名:STUDENT;
预备知识:
c++中我们cpp文件和.h文件的区别是,cpp文件是需要编译的文件,成为一个独立的编译单元,而h文件从来是不需要编译,只是用于预处理。
通常我们在cpp文件中,完成函数的实现,然后在h中则是对于函数的声明,由于默认情况下,全局变量和全局函数存储类型都是extern类型的,所以我们不需要显示的使用extern
这样,我们其他的cpp文件,只要#include .h文件,则在cpp中实现的函数,就能在其他cpp中使用,因为我们已经用.h文件,完成了extern函数声明的操作。
c++类的定义,其实就是定义一个类型。
class A{
public:
void fun(int n);
int fun1(void);
public:
int num;
};
其实就是定义了一种类型。和我们通常所说的定义不一样。
类的定义,是不能重复定义的,在同一个编译单元中,只能定义类一次。如果重复定义,会出错。同时类声明和类定义都是内部链接。只是为当前编译单元所用。
因此,把类的定义,放在.h文件中,类的实现放在专门的cpp中。这样包含.h的其他cpp,就可以使用cpp中实现的函数。。
同时注意:类的实现cpp文件的编译,必须依赖于类的定义文件.h,所以我们在类实现文件cpp中必须#include< ........h>,用于编译,否则会出错。这是不同于普通的函数。
//student.h(这是头文件,在此文件中进行类的声明)
classStudent//类声明
{public:
voiddisplay();
private:
intnum;
charname[20];
charsex;
};
//student.cpp//在此文件中进行函数的定义
#include<iostream>
#include″student.h″
voidStudent∷display()
{cout<<″num:″<<num<<endl;
cout<<″name:″<<name<<endl;
cout<<″sex:″<<sex<<endl;
}
//main.cpp主函数模块
#include<iostream>
#include″student.h″
intmain()
{Studentstud;
stud.display();
return0;
}
类中只有static成员变量。具有外部链接特性。所以这要求,static成员变量的初始化一定要在.Cpp文件中。如果在h文件中。那么多个cpp文件#include,则发生多次重复定义的错误。
类定义和类实现分离的好处:
1/快编译速度
当然可以啊。相反,如果你把类的所有代码都内联定义到头文件中,那么所有需要用到这个类的CPP文件实际上都包含了更多的代码,编译器编译每个这样的CPP文件时都编译了这些代码。而分开定义,这些代码就只被编译了一遍,也就是在编译实现那个类的CPP文件时。
在特殊情况下确实可以的
假如我有一个类a被几百个cpp同时包含,如果定义和声明放在一起,只要我对a进行任何修改,那几百个文件都必须被重新编译。而如果头文件只包含声明,修改a的实现不会导致那些文件被重新编译,有时可以极大的提高速度
在C++中,类的定义方法如下:
class 类名{
访问范围说明符:
成员变量1
成员变量2
成员函数声明1
成员函数声明2
访问范围说明符:
更多成员变量
更多成员函数声明
...
};
类的定义要以;结束。
“访问范围说明符”一共有三种,分别是 public、private 和 protected。三者的区别后面会详细介绍,目前暂且都用 public。“访问范围说明符”可以出现任意多次。
“成员变量”的写法与普通的变量定义相同。称其为成员变量,是因为这些变量是一个类的成员。
同样地,“成员函数声明”的写法也与普通的函数声明相同。
一个类的成员函数之间可以互相调用。类的成员函数可以重载,也可以设定参数的默认值。
以前所学的函数不是任何类的成员函数,可称为“全局函数”。
成员变量就代表对象的“属性”,成员函数就代表对象的“方法”。成员变量和成员函数出现的先后次序没有规定。
成员函数的实现可以位于类的定义之外,格式如下:
返回值类型 类名:函数名()
{
函数体
}
定义类之后,就可以定义对象了。定义对象的基本方法如下:
类名 对象名;
此处,“对象名”的命名规则和普通变量相同。对象也可以看作“类变量”
类的示例程序剖析
下面来看一个用面向对象的方法进行 C++ 程序设计的例子。
例题:编写一个程序,输入矩形的宽度和高度,输出其面积和周长。
这个程序比较简单,实际上根本不需要用面向对象的方法进行设计,这里只是为了使读者更容易理解类和对象的概念。
首先要做的事情是分析问题中有哪些“对象”。比较明显,只有“矩形”这种对象。然后要进行“抽象”,即概括“矩形”这种对象的共同特点。
矩形的属性就是宽度和高度。因此需要两个变量,分别代表宽度和高度。
一个矩形可以有哪些方法(即可以对矩形进行哪些操作)呢?在本程序中,矩形可以有设置宽度和高度、计算面积和计算周长三种行为,这三种行为可以各用一个函数实现,它们都会用到宽度和高度这两个变量。
“抽象”完成后,就可以用 C++ 提供的语法特性写出一个“矩形类”,将矩形的属性和方法“封装”在一起。程序如下:
#include <iostream>
using namespace std;
class CRectangle
{
public:
int w, h; //成员变量,宽和高
void init( int w_,int h_ ); //成员函数,设置宽和高
int area(); //成员函数, 求面积
int perimeter(); //成员函数,求周长
}; //必须有分号
void CRectangle::init( int w_,int h_ )
{
w = w_; h = h_;
}
int CRectangle::area()
{
return w * h;
}
int CRectangle::perimeter()
{
return 2 * ( w + h);
}
int main( )
{
int w,h;
CRectangle r; //r是一个对象
cin >> w >> h;
r.init( w,h);
cout << "It's area is " << r.area() << endl;
cout << "It's perimeter is " << r. perimeter();
cout << sizeof(CRectangle) << endl;
return 0;
}
⑺ 设计Java程序,定义一个表示“学生”的类Student
public class Student{
public String name;
public double ordinary;
public double attendance;
// public Student(String name){
// this.name = name;
// }
// public Student(String name, double ordinary, double attendance){
// this.name = name;
// this.ordinary = ordinary;
// this.attendance = attendance;
// }
// public void setName(String name){
// this.name = name;
// }
// public String getName(){
// return name;
// }
// public void setOrdinary(double ordinary){
// this.ordinary = ordinary;
// }
// public double getOrdinary(){
// return ordinary;
// }
// public void setAttendance(double attendance){
// this.attendance = attendance;
// }
// public double getAttendance(){
// return attendance;
// }
public boolean qualified(){
//如果attendence存放的直接是百分比对应值改成>=0.6
if(this.ordinary >= 60 && this.attendance >= 60){
return true;
}
return false;
}
public String toString(){
return "姓名:" + this.name + " 平时成绩:" + this.ordinary + " 出勤率:" + this.attendance;
}
}
public class test{
public static void main(String[] args){
//直接赋值(需要对应属性权限修饰符为public)
Student student = new Student();
student.name = "姓名";
student.ordinary = 90;
student.attendance = 80;
// //set方法
// Student student = new Student();
// student.setName("姓名");
// student.setOrdinary(90);
// student.setAttendance(80);
// //构造方法
// Student student = new Student("姓名", 90, 80);
//这里直接用他的qualified判断是否有考试资格
if(student.qualified()){
System.out.println("这名学生拥有考试资格");
}else{
System.out.println("这名学生没有考试资格");
}
System.out.println(student.toString());
}
}
⑻ 如何定义一个学生的类.
大略写了一个不知道合不合适:
public class Student {
private int id;
private String name;
private int sex;
private int age;
private int score1;
private int score2;
private int score3;
public Student(int id,String name,int sex,
int age,int score1,int score2,int score3)
{
this.id=id;
this.name=name;
this.sex=sex;
this.age=age;
this.score1=score1;
this.score2=score2;
this.score3=score3;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSex() {
return sex;
}
public void setSex(int sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getScore1() {
return score1;
}
public void setScore1(int score1) {
this.score1 = score1;
}
public int getScore2() {
return score2;
}
public void setScore2(int score2) {
this.score2 = score2;
}
public int getScore3() {
return score3;
}
public void setScore3(int score3) {
this.score3 = score3;
}
}
public class TestStudent {
public static void main(String[] args) {
Student []stu=new Student[5];
stu[0]=new Student(1,"stu1",1,19,90,84,50);
stu[1]=new Student(2,"stu2",0,19,83,87,91);
stu[2]=new Student(3,"stu3",0,20,70,77,60);
stu[3]=new Student(4,"stu4",1,19,88,82,84);
stu[4]=new Student(5,"stu5",1,21,80,97,93);
int temp1=stu[0].getScore1();
int temp2=stu[0].getScore2();
int temp3=stu[0].getScore3();
int total1=temp1;
int total2=temp2;
int total3=temp3;
int maxScore1=temp1;
int maxScore2=temp2;
int maxScore3=temp3;
int minScore1=temp1;
int minScore2=temp2;
int minScore3=temp3;
for(int i=1;i<5;i++)
{
temp1=stu[i].getScore1();
temp2=stu[i].getScore2();
temp3=stu[i].getScore3();
total1+=temp1;
total2+=temp2;
total3+=temp3;
maxScore1=maxScore1>temp1?maxScore1:temp1;
maxScore2=maxScore2>temp2?maxScore2:temp2;
maxScore3=maxScore3>temp3?maxScore3:temp3;
minScore1=minScore1>temp1?temp1:minScore1;
minScore2=minScore2>temp2?temp2:minScore2;
minScore3=minScore3>temp3?temp3:minScore3;
}
System.out.println("average1 = "+(float)total1/5);
System.out.println("average2 = "+(float)total2/5);
System.out.println("average3 = "+(float)total3/5);
System.out.println("maxScore1 = "+maxScore1);
System.out.println("maxScore2 = "+maxScore2);
System.out.println("maxScore3 = "+maxScore3);
System.out.println("minScore1 = "+minScore1);
System.out.println("minScore2 = "+minScore2);
System.out.println("minScore3 = "+minScore3);
}
}
⑼ 定义一个学生类类型,包含学号(num),分数(score)两个数据成员,
class Student
{
private:
int num;
int score;
public:
//修改学生学号
void changeNum(int newNum)
{
num=newNum;
}
//修改分数
void changeScore(int newScore)
{
score=newScore;
}
//显示学号
int displayNum()
{
return num;
}
//显示分数
int displayScore()
{
return score;
}
};
⑽ 定义一个学生类Student,包括如下属性:学生学号、姓名、年龄、专业、年级等,
1、首先,定义一个数据结构student,包含学生的各信息。