c++实验报告

更新时间:2024-04-21 06:38:02 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

C++面向对象 程序设计报告

1

1. 编写一个程序,输入 N 个学生数据,包括学号、姓名、成绩,要求输出这些学生数据

并计算平均分。

源代码清单

#include #include

const int N=2; //定义学生数目 class students //类定义 {

private: int number; char name[10];

int cj;

public: students() {}

void setstu(int a,char pname[],int c) //数据传递

{

number=a;

strcpy(name,pname);

cj=c;

}

void show() { cout<

}

2

int getcj() { return cj;

}

};

int main() { int i; float sum=0; float average;

students stu[N]; //定义N个学生 int a; char b[10]; int c;

for(i=0;i>a>>b>>c; //一次对对象赋值 stu[i].setstu(a,b,c);

}

for(i=0;i

stu[i].show(); //输出每个对象的信息 for(i=0;i

sum=stu[i].getcj()+sum; //求平均成绩

average=sum/N;

3

}

cout<<\输出平均成绩 return 0;

2. 设计一个虚基类 base ,包含姓名和年龄私有数据成员以及相关的成员函数,由它派

生 出领导类 leader ,包含职务和部门私有数据成员以及相关的成员函数。再由 base 派 生出工程师类 engineer ,包含职称和专业私有数据成员以及相关的成员函数。然后由 1eader 和 engineer 类派生出主任工程师类 chairman 。采用一些数据进行测试。

源代码清单

#include #include

class base //定义虚基类base { public:

base(char *n,int a) { }

void displaybase() { }

cout<<\name=new char[strlen(n)+1]; strcpy(name,n); age=a;

private:char *name;

4

};

int age;

class leader:virtual public base // 定义派生类leader { public: leader(char *n,int a,char *zw,char *bm):base(n,a) { zhiwu=new char[strlen(zw)+1]; bumen=new char[strlen(bm)+1]; strcpy(zhiwu,zw); strcpy(bumen,bm);

}

void displayleader() //显示信息 { displaybase();

cout<<\

}

private: char *zhiwu; char *bumen;

};

class engineer:virtual public base //定义派生类engineer

5

{ public: engineer(char *n,int a,char *zc,char* zy):base(n,a) { zhicheng=new char[strlen(zc)+1];

zhuanye=new char[strlen(zy)+1];

strcpy(zhuanye,zy); strcpy(zhicheng,zc);

}

void displayengineer() //显示信息 {

displaybase(); cout<<\is:\

}

private: char *zhicheng; char * zhuanye;

};

class chairman:public leader,public engineer //定义派生类chairman { public:

chairman(char *n,int a,char *zw,char *bm,char *zc,char* zy,char *s) 6

:leader(n,a,zw,bm),engineer(n,a,zc,zy), base(n,a)

{ sex=new char[strlen(s)+1]; strcpy(sex,s);

}

void displaychairman() //显示信息 { displayleader(); displayengineer();

cout<<\

}

private:char *sex; };

void main() {

engineer a(\

a.displayengineer(); cout<

leader b(\ b.displayleader();

cout<

chairman c(\dept\

7

}

c.displaychairman(); cout<

3. 建立类 RationalNmuber( 分数类 ), 使之具有下述功能 :

a) 建立构造函数,它能防止分母为 0, 当分数不是最简形式时进行约分以及避免分母为负数

b) 重载加法、减法、乘法以及除法运算符。 c) 重载关系运算符和相等运算符

源代码清单

#include class RationalNmuber {

private: double a,b; public: RationalNmuber(double x,double y); int mod(double m,double n); friend RationalNmuber operator +(const RationalNmuber&c,const RationalNmuber&d); friend RationalNmuber operator -(const RationalNmuber&c,const RationalNmuber&d); friend RationalNmuber operator *(const RationalNmuber&c,const RationalNmuber&d); friend RationalNmuber operator /(const RationalNmuber&c,const RationalNmuber&d); friend bool operator <(const RationalNmuber&c,const RationalNmuber&d); friend bool operator >(const RationalNmuber&c,const RationalNmuber&d); friend bool operator <=(const RationalNmuber&c,const RationalNmuber&d); friend bool operator >=(const RationalNmuber&c,const RationalNmuber&d); friend bool operator ==(const RationalNmuber&c,const RationalNmuber&d); friend bool operator !=(const RationalNmuber&c,const RationalNmuber&d); void show(); };

RationalNmuber::RationalNmuber(double x,double y) { a=x;b=y; int i,j,k; if(a>0) {

8

for(i=1;i<=a;i++) { j=mod(a,i); k=mod(b,i); if((j==0)&&(k==0)) { a=a/i; b=b/i; } } } }

int RationalNmuber::mod(double m,double n) { int i,j; for(i=0;i<=n;i++) { if(((n*i)<=m)&&((n*(i+1))>m)) j=m-n*i; } return j; }

void RationalNmuber::show() { if(a<=0) cout<<\ else { if(a==1) cout<

RationalNmuber operator +(const RationalNmuber &c,const RationalNmuber &d) {

return RationalNmuber(c.a*d.a,c.b*d.a+c.a*d.b); }

RationalNmuber operator -(const RationalNmuber&c,const RationalNmuber&d) {

return RationalNmuber(c.a*d.a,c.b*d.a-c.a*d.b); }

RationalNmuber operator *(const RationalNmuber&c,const RationalNmuber&d) {

9

return RationalNmuber(c.a*d.a,c.b*d.b); }

RationalNmuber operator /(const RationalNmuber&c,const RationalNmuber&d) {

return RationalNmuber(c.a*d.b,c.b*d.a); }

bool operator <(const RationalNmuber&c,const RationalNmuber&d) { if((c.b/c.a)<(d.b/d.a)) return true; else return false; }

bool operator >(const RationalNmuber&c,const RationalNmuber&d) { if((c.b/c.a)>(d.b/d.a)) return true; else return false; }

bool operator <=(const RationalNmuber&c,const RationalNmuber&d) { if(c.b/c.a<=d.b/d.a) return true; else return false; }

bool operator >=(const RationalNmuber&c,const RationalNmuber&d) { if(c.b/c.a>=d.b/d.a) return true; else return false; }

bool operator ==(const RationalNmuber&c,const RationalNmuber&d) { if(c.b/c.a==d.b/d.a) return true; else return false; }

bool operator !=(const RationalNmuber&c,const RationalNmuber&d) { if(c.b/c.a!=d.b/d.a)

10

return true; else return false; }

void main() { RationalNmuber A1(2,5),A2(3,8),A3(0,0),A4(0,0),A5(0,0),A6(0,0); A1.show(); A2.show(); A3=A1+A2; A3.show(); A4=A2-A1; A4.show(); A5=A1*A2; A5.show(); A6=A1/A2; A6.show(); if(A2>A1) cout<<\ if(A3!=A4) cout<<\

题目:

修改MFC 时钟的代码。 程序(修改部分用黑体):

(1)afx_msg int

afx_msg void OnDestroy();

afx_msg void OnTimer(UINT x); (2)ON_WM_CREATE() ON_WM_TIMER() ON_WM_DESTROY() }//修改填充颜色。

(3)const static COLORREF pieColorLookup[]= { RGB(0,0,0), RGB(255,255,255), }//修改填充颜色。

(4) CTime time=CTime::GetCurrentTime(); st.wHour=time.GetHour();

st.wMinute=time.GetMinute();

st.wSecond=time.GetSecond();//运用Ctime类中的GetCurrentTime函数获取当前时间,并传递到对象st。

pen.CreatePen(PS_SOLID,4,RGB(80,174,226)); pDC->SelectObject(&pen); pDC->MoveTo(center);

11

pDC->LineTo(center+PositionOnCircle(radius,st.wHour/12.0+st.wMinute/20.0)*0.5); pen.DeleteObject();

pen.CreatePen(PS_SOLID,4,RGB(80,174,226)); pDC->SelectObject(&pen); pDC->MoveTo(center);

pDC->LineTo(center+PositionOnCircle(radius,st.wMinute/60.0)*0.75); pen.DeleteObject();

pen.CreatePen(PS_SOLID,2,RGB(80,174,226)); pDC->SelectObject(&pen); pDC->MoveTo(center); pDC->Lin修改指针的颜色

eTo(center+PositionOnCircle(radius,st.wSecond/60.0)*0.75); //修改指针的颜色、长短、粗细。

(5) void CMyView::OnTimer(UINT nTimerID) {

CClientDC dc(this);

DrawClock(&dc);//使指针时时表示实际时间 }

12

本文来源:https://www.bwwdw.com/article/iu5p.html

Top