当前位置:编程学习 > C/C++ >>

c++编程题 类和对象

创建学生类(class Student),要求如下:

一、数据成员(私有)

1. 姓名:char *name;

2. 成绩:int *score;

3.保存成绩数的常量:const int N;

4.统计对象总数的静态变量:static int count;

二、函数成员(公开)

1. 无参构造函数:初始化常量为2;用new开辟name和score指向的实体存储空间;对象数统计变量count自增;

2.有参构造函数:根据传入参数初始化常量;用new开辟name和score指向的实体存储空间;用参数为数据成员赋值;对象数统计变量count自增;

3.拷贝构造函数:初始化常量;用new开辟name和score指向的实体存储空间;用对象参数为数据成员赋值;对象数统计变量count自增;

4.析构函数:用delete释放已有堆空间;对象数统计变量count自减;

5.对象姓名设置函数:SetName

6.对象成绩设置函数:SetScore(假设用户传入成绩数组的元素数等于N)

7.返回成绩数常量的函数:GetN

8.返回对象成绩数组的函数:GetScore

9.对象信息输出函数Print:输出当前对象的姓名以及所有成绩

三、测试函数main

void main()

{

int a[2]={40,60};

int b[4]={40,60,70,80};

Student s0,s1("zhang",b,4),s2(s1);

s0.SetName("li");

s0.SetScore(a);

s0.Print();

s1.Print();

s2.Print();

//your code here利用GetScore和GetN计算s2的平均成绩并输出。

//your code here调用GetCount返回当前对象总数并输出

}

答案:
/*
 * Student.h
 *
 *  Created on: 2011-10-29
 *      Author: kyle
 */


#ifndef STUDENT_H_
#define STUDENT_H_


#if defined (_STDDEF_H) || defined (__need_NULL)
#undef NULL/* in case <stdio.h> has defined it. */
#ifdef __GNUG__
#define NULL __null
#else   /* G++ */
#ifndef __cplusplus
#define NULL ((void *)0)
#else   /* C++ */
#define NULL 0
#endif  /* C++ */
#endif  /* G++ */
#endif/* NULL not defined and <stddef.h> or need NULL.  */
#undef__need_NULL


class Student {
public:
Student();
Student(const char* name,const int*score,int N);
virtual ~Student();
void SetName(const char* name);
void SetScore(const int* score);
char* GetName();
int GetN();
int* GetScore();
int GetCount();
int GetAvgScore();
void print();
private:
char* name;
int* score;
const int N;
};
static int count;
#endif /* STUDENT_H_ */



/*
 * Student.cpp
 *
 *  Created on: 2011-10-29
 *      Author: kyle
 */


#include "Student.h"
#include<iostream>
#include<string.h>
#include<stdlib.h>


using namespace std;


Student::Student():N(2) {
// TODO Auto-generated constructor stub
//N=0;
this->name=NULL;
this->score=NULL;
count++;
}


Student::Student(const char* name,const int*score,int N) :N(N){
count++;
this->name=new char[strlen(name)+1];
strcpy(this->name,name);
this->score=new int[N];
int i;
for(i=0;i<N;i++) {
*(this->score)=*score;
score++;
(this->score)++;
}
this->score-=i;
}


Student::~Student() {
// TODO Auto-generated destructor stub
count--;
if(this->name!=NULL)
delete[] this->name;
if(this->score!=NULL)
delete[] this->score;
}




int Student::GetN (){
return N;
}


int* Student::GetScore() {
return this->score;
}


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


int Student::GetCount() {
return count;
}


int Student::GetAvgScore() {
int sum=0;
if(this->score==NULL)
return 0;
for(int i=0;i<N;i++) {
sum+=*(this->score);
this->score++;
}
this->score-=N;
return sum/N;
}


void Student::print() {
cout<<"this student's name:"<<this->name<<endl;
cout<<"this student's scores:";
if(this->score==NULL) {
cout<<"0"<<endl;
return;
}
for(int i=0;i<N;i++) {
cout<<*(this->score)<<",";
this->score++;
}
cout<<""<<endl;
this->score-=N;
}


void Student::SetScore(const int* score) {
if(this->score!=NULL)
delete[] this->score;
int i=0;
for(;i<N;i++) {
*(this->score)=*score;
score++;
this->score++;
}
this->score-=N;
}


void Student::SetName(const char* name) {
//如果重复调用SetName的话,不做下面步骤就可能出现内存泄漏
if(this->name!=NULL)
delete[] this->name;
this->name=new char[strlen(name)+1];
strcpy(this->name,name);
}


我用gcc编译通过了哈

//VisualStudio2010亲测可用 #include<iostream>usingnamespacestd; classCircle{public: Circle(doubler=0):radius(r){} doubleArea(){return3.14*radius*radius;} frienddoublePerimeter(constCircle

上一个:c++编程,关于数组的
下一个:推荐一些c++编程的书籍

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,