1、 void print();【circle.cpp】#includecircle.hcircle:circle(float x,float y):pi(3.14) r=x; h=y;void circle:print() cout底面积为:r*r*piendl;体积为:r*r*pi*h#ifndef distance_h#define distance_hnamespace meclass distancepoint p1;point p2; distance(int x1,int x2,int y1,int y2);【distance.cpp】distance.hme:distance:di
2、stance(int x1,int x2,int y1,int y2):p1(x1,y1),p2(x2,y2)cout”两点间的距离为:”sqrt(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2); me:distance s(1,2,1,2);两点间的距离为:1.41实验四定义学生类(姓名,学号),生日类(年,月,日),利用友元类,输出某个学生的姓名,学号,及其生日。【student.h】birthday.hclass student char *name; int ID; student(char *s,int n); void print(birthday &a);【birt
3、hday.h】#ifndef bi#define biclass birthday int year,mouth,day; friend class student; birthday(int x,int y,int z);【student.cpp】student.hstudent:student(char *s,int n)ID=n;name=new charstrlen(s)+1;strcpy(name,s);void student:print(birthday &b)姓名:name学号:ID生日:b.yearb.mouthb.daybirthday:birthday(int x,int
4、 y,int z)year=x; mouth=y;day=z;iostream void main() student a(tjl,117); birthday b(1990,9,1); a.print(b);tjl117199091实验五 student(name,id,total),total为静态成员,在student中设置静态函数来处理total。在main()函数中显示name,id,totalchar *name;int id;static int total; student(char *na,int id1); void print(); static void print1(
5、);【total.cpp】string.hint student:total=0;student(char *na,int id1) id=id1;name=new charstrlen(na)+1;strcpy(name,na);+total;print1()total;name:coutid:idstudent a(tang,61);student b(guo,45);a.print();b.print();print1();tang61guo45实验六基类-学生类(姓名,学号),子类-大学生类(专业),继承方式:公有继承,在主函数中显示某个大学生的信息。#ifndef _st1#defi
6、ne _st1 int id; student();student()delete name;id=id1;【cstudent.h】#ifndef _cst#define _cstclass cstudent:public student char *project; cstudent(); cstudent(char *na,int id1,char *pro); void print1();【cstudent.cpp】cstudent.hcstudent:cstudent()delete project;cstudent(char *na,int id1,char *pro):studen
7、t(na,id1)project=new charstrlen(pro)+1;strcpy(project,pro);void cstudent:student:print();project:projectmain()cstudent a(,17,computer);a.print1();17conputer实验七基类shape,派生类circle(半径R),tangcle(长a,宽b),它们都有求面积的函数area(),利用虚函数求两个派生类对象的面积【shape.h】#ifndef _shape#define _shapeclass shapevirtual void area();【s
8、hape.cpp】shape.hvoid shape:area()class circle:public shapefloat r;const float pi;circle(float r1);void area(); ;circle(float r1):r=r1;pi*r*r【tangcle.h】class tangcle:float a,b;tangcle(float c,float d);【tangcle.cpp】tangcle.htangcle:tangcle(float c, float d)a=c;b=d;void tangcle:a*barea();b;12.5612实验八重载
9、减法运算符,使得两个复数可以相减【complex.h】iostream.h#ifndef _COMPLEX_H#define _COMPLEX_Hclass complex double a; double b; complex(double a,double b); friend void operator -(complex x,complex y);【complex.cpp】complex.hcomplex:complex(double a1,double b1)a=a1;b=b1;void operator-(complex x,complex y)实部是:x.a-y.a虚部是:x.b-y.bcomplex A1(1.0,2.0),A2(3.0,4.0);A1-A2; -2虚部:-2实验九将文件text1的内容拷贝到文件text2中#include int main() ifstream in(H:text1.txt if(!in) cerrread text1 error exit(1); ofstream out(text2.txtout)write text2 error char ch; while(in) in.get(ch); out.put(ch); return 0;实验结果:text1:55555;复制到text2显示55555文件中复制成功
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1