设计一个学生类CStudent,包括姓名和三门课程成绩,利用重载运算符“+”将所有学生C++

作者&投稿:督罗 (若有异议请与网页底部的电邮联系)
设计一个学生类student~

#include
using namespace std; class Student
{
public:
Student(char *pName = "", float score1 = 0, float score2 = 0, float score3 = 0)
{
strcpy(m_name, pName);
m_score1 = score1;
m_score2 = score2;
m_score3 = score3;
m_count = 1;
}

Student& operator+ (const Student &stu1)
{
this->m_score1 += stu1.m_score1;
this->m_score2 += stu1.m_score2;
this->m_score3 += stu1.m_score3;
m_count++;

return *this;
}

float GetAverage1()
{
return m_score1 / m_count;
}

float GetAverage2()
{
return m_score2 / m_count;
}

float GetAverage3()
{
return m_score3 / m_count;
}

private:
char m_name[20];
float m_score1;
float m_score2;
float m_score3;
int m_count; // 学生人数计数
}; int main()
{
Student s1("学生1", 100, 34.5, 60);
Student s2("学生2", 100, 90.5, 78);
Student s3("学生3", 101, 94.5, 77.2);

Student s = s1 + s2 + s3;

cout << s.GetAverage1() << endl;
cout << s.GetAverage2() << endl;
cout << s.GetAverage3() << endl;

return 0;
}

#include
using namespace std;

class Cstude
{
public:
friend Cstude operator + (const Cstude &a,const Cstude &b);
friend Cstude operator / (const Cstude &a,const float &b);
Cstude(const Cstude &obj)
:one(obj.one),two(obj.two),three(obj.three){}
Cstude(float o = 0,float t = 0,float th = 0)
:one(o),two(t),three(th){}

float GetOne()
{ return one; }
float GetTwo()
{ return two; }
float GetThree()
{ return three; }
private:
float one;
float two;
float three;
};

Cstude operator + (const Cstude &a,const Cstude &b)
{
Cstude *p = new Cstude(a);
p -> one += b.one;
p -> two += b.two;
p -> three += b.three;

return *p;
}

Cstude operator / (const Cstude &a,const float &b)
{
Cstude cs(a);
cs.one /= b;
cs.two /= b;
cs.three /= b;
return cs;
}

int main()
{
Cstude a(98,75,68);
Cstude b(75,68,25);
Cstude c = a + b;
cout << c.GetOne() << endl;
cout << c.GetTwo() << endl;
cout << c.GetThree() << endl;
cout << endl;

Cstude d = (a + b + c) / 3;
cout << d.GetOne() << endl;
cout << d.GetTwo() << endl;
cout << d.GetThree() << endl;
getchar();
return 0;
}

如下:

#include <iostream>

#include <cstring>

#include<IOMANIP>

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 t):cnt(1)

{

strcpy(a,b); //字符串赋值

x=m;

y=k;

z=t;

}

student operator +(const student& s);//const 引用,提高效率

void print()

{

cout<<"course 1#,ave score:="<<fixed<<setprecision(2)<<(float) x/cnt<<endl;

cout<<"course 2#,ave score:="<<fixed<<setprecision(2)<<(float) y/cnt<<endl;

cout<<"course 3#,ave score:="<<fixed<<setprecision(2)<<(float) z/cnt<<endl;

}

};

student student:: operator +(const student& s)

{

student b;

b.x=x+s.x;

b.y=y+s.y;

b.z=z+s.z。

C语言是一门面向过程的计算机编程语言,与C++、Java等面向对象编程语言有所不同。C语言的设计目标是提供一种能以简易的方式编译、处理低级存储器、仅产生少量的机器码以及不需要任何运行环境支持便能运行的编程语言。

C语言描述问题比汇编语言迅速,工作量小、可读性好,易于调试、修改和移植,而代码质量与汇编语言相当。C语言一般只比汇编语言代码生成的目标程序效率低10%~20%。因此,C语言可以编写系统软件。

二十世纪八十年代,美国国家标准局为了避免各开发厂商用的C语言语法产生差异,给C语言制定了一套完整的美国国家标准语法,称为ANSI C。

作为C语言最初的标准。2011年12月8日,国际标准化组织(ISO)和国际电工委员会(IEC)发布的C11标准是C语言的第三个官方标准,也是C语言的最新标准,该标准更好的支持了汉字函数名和汉字标识符,一定程度上实现了汉字编程。

C语言编译器普遍存在于各种不同的操作系统中,例如Microsoft Windows, Mac OS X, Linux, Unix等。C语言的设计影响了众多后来的编程语言,例如C++、Objective-C、Java、C#等。



#include <iostream>
#include <cstring>
#include<IOMANIP>
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 t):cnt(1)
{
strcpy(a,b); //字符串赋值
x=m;
y=k;
z=t;
}
student operator +(const student& s);//const 引用,提高效率
void print()
{
cout<<"course 1#,ave score:="<<fixed<<setprecision(2)<<(float) x/cnt<<endl;
cout<<"course 2#,ave score:="<<fixed<<setprecision(2)<<(float) y/cnt<<endl;
cout<<"course 3#,ave score:="<<fixed<<setprecision(2)<<(float) z/cnt<<endl;
}
};
student student:: operator +(const student& s)
{
student b;
b.x=x+s.x;
b.y=y+s.y;
b.z=z+s.z;
b.cnt=cnt+1;//这里,b中存放的总成绩对应人数的计算
return b;
}
int main()
{
student s1("hank",80,80,80),s2("wei",80,90,81),s3, s4("Li",90,90,90);
s3 = s1+ s2 + s4;
s3.print();
}


#include <iostream>
using namespace std;
class student
{
public :
char a[20];
int x ,y ,z;

student(){}
student (char b[20],int m,int k,int t)
{
for(int i=0;i<20;i++)
{
a[i]=b[i];
}
x=m;
y=k;
z=t;
}
student operator +(student s);

};
student student:: operator +( student s)
{
student b;
b.x=x+s.x;
b.y=y+s.y;
b.z=z+s.z;
return b;
}

void print(student b,int n);
int main()
{
student s1("hank",81,82,84),s2("wei",79,78,76),s3;
s3=s1+s2;
print(s3,2);
return 0;

}

void print(student b,int n)
{
cout<<b.x/n<<endl<<b.y/n<<endl<<b.z/n<<endl;

}

其实用学生对象来放乘积和很不地道


2023年长沙市自立中等职业学校招生简章收费标准公办还是民办电话_百度...
共计 143 备注:望城校区为学生提供午托服务。 三、招生程序 1、报名:2021年4月8日-5月21日,请登陆长沙市特殊教育学校官网(\/www.cstsjy.cn)公布。 4、其他说明:(1)智力与发展障碍类学生(学前和义务教育段)安置办法详见附件2;(2)职业高中(对口升学方向)特长生招生办法详见附件3;(3)职业高中考试须知详见附件...

珠海有什么大学
3、珠海科技学院。珠海科技学院(Zhuhai College of Science and Technology),简称“ZCST”,坐落于广东省珠海市。学校设有25个二级学院,开设63个本科专业,学科涵盖经济学、法学、文学、理学、工学、医学、管理学、艺术学、教育学9大学科门类。4、暨南大学珠海校区。暨南大学珠海校区是暨南大学的一个分...

珠海学校有哪些
1、北京理工大学珠海校区是珠海市兴办大学科研区计划的一个组成部分。占地5000亩,位于珠海市高新区唐家湾镇。北理工珠海校区内建有北京理工大学珠海学院、北京理工大学珠海研究生院、北京理工大学珠海研究院。2、珠海科技学院(Zhuhai College of Science and Technology),简称“ZCST”,坐落于广东省珠海...

好看的动画 类似于叛逆的鲁鲁修,死亡笔记,柯南,钢炼fa等的比较精彩,画...
类似于《夏目友人帐》的,讲鬼怪:《幕末机关说》《暗夜魔法师》《奇幻贵公》《蔷薇少女》《魔法少女奈叶》《格雷少年》《地狱少女》《阴阳大战记》《神是中学生》《我家有个狐仙大人》《魔侦探洛基》《神曲奏界》《死神的歌谣》《怪-化猫》(算话风特别)《幻影少年》《滑头鬼之孙》《虫之歌》《...

在美国旅行注意事项是什么?
其中美国本土四个,由东向西依次为:1. 东部时区(EST)的主要城市:纽约、华盛顿、波士顿、费城、亚特兰大、迈阿密。2. 中部时区(CST)的主要城市:芝加哥、...美国医院林立,因此对初抵异地的中国学生来说,有时的确不知如何去就医。美国纽约、洛杉矶、旧金山等大城市的中国城里有不少中国人开设的诊所,从跌打损伤、...

留学澳大利亚的生活怎么样?
澳洲有三个时区:东部标准时间(EST)区,包括新南威尔士省、澳洲首都领地、维多利来省、塔斯曼尼亚和昆士兰省;中部标准时间(CST)区,包括南澳及北领地;西部标准时间(WST)区,即西澳。中部时间比东部时间慢半小时,西部时间比东部时间慢两小时。3. 生活成本 一般在澳洲的学生的消费成本较高。一个...

小学生满18岁可以辍学吗?
18岁意味着什么 中国《民法通则》规定:“18周岁以上的公民是成年人。”凡满18周岁的公民,不论其性别、精神健康状况如何,均视为成年人。达到成年且精神健全的人具有完全的民事行为能力是完全民事行为能力人。我国公民享有的权利:1、公民参与政治方面的权利:平等权、选举权和被选举,政治自由权。2、...

cst是什么意思?
CST:计算机科学与技术专业的简称。计算机科学专业涵盖了软件工程专业。主要培养具有良好科学素养的学生,系统、较好地掌握计算机科学与技术的基本理论、基本知识、基本技能和方法,包括计算机硬件、软件和应用。能在科研部门、教育单位、企业、事业单位、技术行政部门等单位从事计算机教学、科研和应用的计算机科学...

哈尔滨交通卡线上办理流程
在哈尔滨的朋友们注意啦,近期哈尔滨交通集团推出了在线办卡业务,对于“老年卡”、“敬老卡”、“学生卡”办理或者是补办都可以在手机上操作,支付邮费之后三个工作日就能收到了。公交卡线上办理指南01、办理对象第一次或者丢失补办“老年卡”、“敬老卡”、“学生卡”的学生群体、老年群体02、办理渠道哈尔滨城市通APP...

珠海科技学院在哪个位置
一、学校概况 珠海科技学院(ZCST)成立于2004年5月18日,其毗邻南海,背靠观音山,占地面积2421亩,建筑面积为62.3万平方米。我校在全国民办高校中排名第四,广东民办高校排名第一,有着雄厚的教师资源和优越的地理位置~ 二、专业门类 ...

遂平县18568416529: 构造一个学生类CStudent -
喻榕金鸡: #include using namespace std; class CStudent { public: CStudent(char* n, char* i) { strcpy(name, n); strcpy(id, i); cout} CStudent(const CStudent& stu) { strcpy(name, stu.name); strcpy(id, stu.id); cout} ~CStudent() { cout} void Print() { cout} ...

遂平县18568416529: 设计一个类CStudent, -
喻榕金鸡: 设计一个类CStudent 类中包含一个学生的基本数据如下:编号,姓名,性别,这段代码和你的要求基本符合,但需要你稍微修改一下 #include

遂平县18568416529: 设计一个学生类CStudent 求助高手 -
喻榕金鸡: 看样子好像是想考信息排序和查询速度. 排序可以插入信息的时候处理,查询用二分法.不过你要先明确是要将每个学生的信息保存在类里面还是用类来实现功能.先确定单条信息怎么保存,再考虑全部信息的索引保存,后面的功能就不是问题了

遂平县18568416529: 设计一个学生类CStudent, -
喻榕金鸡: #include #include using namespace std;class student{private:char a[20]; //不能在类定义中对非static const型变量...

遂平县18568416529: 设计一个学生类Cstudent,该类包括学生学号、姓名以及数学、英语、c语言成绩等数据成员; -
喻榕金鸡: void print(struct student *);void input(struct student *);struct student{ int num; char name[20]; int score[3];}stu[1...

遂平县18568416529: 求助一个VC++编程题: 定义一个学生CStudent类,该类用于表示学生基本信息(学号,姓名,性别,联系电话) -
喻榕金鸡: #include<stdio.h> #include<stdlib.h> #include<string.h> #define N 2 struct CStudent {char no[10];char name[10];char sex[2];char phone[15]; }; char filename[15]; void Save(char *fpt) { FILE *fp; system("cls"); struct CStudent s;fp=fopen(...

遂平县18568416529: C++实现类设计一个类,包含构造函数、析构函数等? -
喻榕金鸡: //CStudent.h calss CStudent {public:long ID;char* pchName;float fWeight; public:CStudent();//构造~CStudent(); //析构 };// CStudent.cpp #include "iostream"CStudent(); {ID = 0;pchName = NULL;fWeight = 0.0f; }~...

遂平县18568416529: 用C#编写一个CStudent类来表示学生的属性和行为.1,学生有名字 学号,成 -
喻榕金鸡: public class CStudent { private string name; public string Name { get { if (string.IsNullOrEmpty(name)) return ""; else return name; } set { name = value; } } private string studentID; public string StudentID { get { if (string.IsNullOrEmpty(studentID)) ...

遂平县18568416529: 建立名为CStudent的类,该类有成员变量:姓名、学号、数学、语文、英语三门课成绩
喻榕金鸡: #include &lt;iostream&gt; #include &lt;string&gt; #include &lt;memory.h&gt; using std::string; using std::cout; using std::endl; class CStudent { public: CStudent(){} CStudent(string name, unsigned int id, bool Isman, float score[3]); ~CStudent(){} void ...

遂平县18568416529: 定义一个学生类,数据成员包含姓名和分数,成员函数自行设计. -
喻榕金鸡: #include//学生成绩,分数#include #include using namespace std; class STUD { public: STUD() { m_strName = ""; m_nMark = 0; } void setStud(string strName, int nMark); void print(); private: string m_strName; int m_nMark; }; void STUD::...

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网