经典C++程序.docx
- 文档编号:3575626
- 上传时间:2022-11-24
- 格式:DOCX
- 页数:37
- 大小:24.41KB
经典C++程序.docx
《经典C++程序.docx》由会员分享,可在线阅读,更多相关《经典C++程序.docx(37页珍藏版)》请在冰豆网上搜索。
经典C++程序
1.定义一个职工类,职工类中包括职工姓名、薪水、所有职工的薪水总和(静态数据成员),定义静态成员函数,测试薪水总和的值。
Employee.h
#pragmaonce
#include
usingnamespacestd;
classEmployee
{
public:
Employee(char*n,doubles);
~Employee(void);
doublesalary;
staticvoidprint()
{
cout<<"Thesumofsalaryis:
"< }; private: staticdoublecount; charname[20]; }; Employee.cpp #include"Employee.h" Employee: : Employee(char*n,doubles) { strcpy_s(name,n); salary=s; count=count+s; } Employee: : ~Employee(void) { cout<<"Deducting"< } 02.cpp //#include //usingnamespacestd; #include"Employee.h" doubleEmployee: : count=0; voidmain() { Employeee1("liu",2000); Employeee2("zhang",3000); Employeee3("hong",2500); e3.print(); } 2.由父类图形派生矩形、圆形、三角形三个子类,父类中声明了求面积的函数,在各子类中重写各自的构造函数、面积函数,但面积函数要保持相同的函数名,即达到面积函数的多态.分别声明对象进行检验. Shape.h #pragmaonce #include usingnamespacestd; classshape { public: shape(void); ~shape(void); voidgetArea(); }; classrectangle: publicshape { public: rectangle(inti,intj); voidgetRec(); voidgetArea(); private: intx,y; intarea; }; classcircle: publicshape { public: circle(inti); voidgetCir(); voidgetArea(); private: intr; doublearea; }; classtriangle: publicshape//等边三角形 { public: triangle(inti); voidgetTri(); voidgetArea(); private: inta; doublearea; }; Shape0.cpp #include usingnamespacestd; #include"shape.h" rectangle: : rectangle(inti,intj) { x=i; y=j; } voidrectangle: : getRec() { cout<<"rectangle'slength="< } voidrectangle: : getArea() { area=x*y; cout<<"rectangle'sarea="< } circle: : circle(inti) { r=i; } voidcircle: : getCir() { cout<<"circle'sradius="< } voidcircle: : getArea() { area=3.14*r*r; cout<<"circle'sarea="< } triangle: : triangle(inti) { a=i; }; voidtriangle: : getTri() { cout<<"triangle'sradius="< } voidtriangle: : getArea() { area=0.433*a*a; cout<<"triangle'sarea="< } Shape.cpp #include"shape.h" shape: : shape(void) { } shape: : ~shape(void) { } voidmain() { rectanglerec(4,5); rec.getRec(); rec.getArea(); circlecir(6); cir.getCir(); cir.getArea(); triangletri(3); tri.getTri(); tri.getArea(); } 3.对象数组,以学生成绩为例 Classstu.h #pragmaonce #include usingnamespacestd; classclassstu { public: classstu(void); ~classstu(void); voidset(inti,char*a,floats); voidprint(); //private: intid; charname[20]; floatscore; }; Classstu.cpp #include"classstu.h" classstu: : classstu(void) { } classstu: : ~classstu(void) { } voidclassstu: : set(inti,char*c,floats) { id=i; strcpy_s(name,c); score=s; } voidclassstu: : print() { cout<<"学号: "< "< "< } Main.cpp #include"classstu.h" voidmain() { classstustu[4]; stu[0].set(1,"aa",33); stu[1].set(2,"bb",32); stu[2].set(3,"cc",48); stu[3].set(4,"dd",25); stu[0].print(); stu[1].print(); stu[2].print(); stu[3].print(); for(inti=0;i<3;i++) if(stu[i].score>stu[i+1].score) stu[i+1]=stu[i]; cout<<"thehighestis"< } //8.5拷贝构造函数与赋值运算符(=)有何联系与区别? #include usingnamespacestd; #include"stdafx.h" classPoint { intm_x,m_y; public: Point(intx=0,inty=0) { m_x=x; m_y=y; } Point(Point&obj) { m_x=obj.m_x; m_y=obj.m_y; } voidprint() { cout< } }; voidmain() { Pointa(2,3);//调用一般构造函数 Pointb(a);//调用拷贝构造函数 Pointc=a;//等价于c(a),调用拷贝构造函数,创建对象并赋值 Pointd;//调用一般构造函数 d=a;//使用赋值运算符,不重建创建对象 a.print(); b.print(); c.print(); d.print(); } //8.6能否给对象数组赋初值 #include usingnamespacestd; #include"stdafx.h" classPoint { intm_x,m_y; public: Point(intx=0,inty=0){m_x=x;m_y=y;} voidprint() { cout< } }; voidmain() { Pointarr[2]={Point(1,1),Point(2,2)};//能给对象数组赋初值 for(inti=0;i<2;i++) arr[i].print(); } //8.15分析程序的输出结果,构造函数与析构函数 #include"stdafx.h" #include usingnamespacestd; classA { public: A(); A(inti,intj); ~A(); voidSet(inti,intj) {a=i;b=j;} private: inta,b; }; A: : A() { a=0; b=0; cout<<"Defaultconstructorcalled."< } A: : A(inti,intj) { a=i; b=j; cout<<"Constructora="< } A: : ~A() { cout<<"Destructorcalled.a="< } voidmain() { cout<<"Starting1..."< Aa[3]; for(inti=0;i<3;i++) a[i].Set(2*i+1,(i+1)*2); cout<<"Ending1..."< cout<<"Starting2..."< Ab[3]={A(7,8),A(9,10),A(11,12)}; cout<<"Ending2..."< } /* //输出结果 Starting1... Defaultconstructorcalled. Defaultconstructorcalled. Defaultconstructorcalled. Ending1... Starting2... Constructora=7,b=8 Constructora=9,b=10 Constructora=11,b=12 Ending2... Destructorcalled.a=11,b=12 Destructorcalled.a=9,b=10 Destructorcalled.a=7,b=28 Destructorcalled.a=5,b=6 Destructorcalled.a=3,b=4 Destructorcalled.a=1,b=2 */ //8.17创建一个Triangle类,这个类将直角三角形的底边长度和高作为私有成员。 类中包含设置 //这些值的构造函数。 两个成员函数: hypot()返回直角三角形斜边的长度,area()返回三角形的面积。 #include"stdafx.h" #include #include usingnamespacestd; classTriangle { public: Triangle(floatL,floatH) { Length=L; Hight=H; } floathypot() {returnsqrt(Length*Length+Hight*Hight); } floatarea() {returnLength*Hight/2; } private: floatLength; floatHight; }; voidmain() { Trianglet(3,4); cout< cout< } //8.18定义一个Circle类,包含数据成员Radius(半径)和计算周长和面积的成员函数,并构 //造Circle的对象进行测试。 #include"stdafx.h" #include #include usingnamespacestd; constfloatPI=3.14159; classCircle { public: Circle(floatr) { radius=r; } floatperimeter() { return2*PI*radius; } floatarea() { returnPI*radius*radius; } private: floatradius; }; voidmain() { Circlec1(3.); cout< cout< }; 自己编的: #include usingnamespacestd; #include"stdafx.h" classcircle { public: circle(intr) { rad=r; } doublearea() { return3.14*rad*rad; } private: intrad; }; voidmain() { circlec (1); cout< } //8.20设计一个用于人事管理的Person(人员)组合类。 人员属性为: number(编号)、sex(性别), //birthday(出生日期)、id(身份证号)等。 其中“出生日期”定义为一个“日期”类内嵌对象。 //用成员函数实现对人员信息的录入和显示。 要求具有构造函数、析构函数、拷贝构造函数、内 //联成员函数、带缺省形式参数值的成员函数。 #include"stdafx.h" #include #include usingnamespacestd; classDate { public: Date(inty=1900,intm=1,intd=1) { year=y; month=m; day=d; } voidOutput() { cout< } private: intyear; intmonth; intday; }; classPerson { public: Person(Dated=Date(1993,1,1),intn=0,char*s="NoName",boolb=0): birthday(d) { number=n; name=newchar[strlen(s)+1]; if(name! =NULL) strcpy(name,s); sex=b; }; Person(Person&p) { number=p.number; name=newchar[strlen(p.name)+1]; if(name! =NULL) strcpy(name,p.name); sex=p.sex; birthday=p.birthday; }; ~Person() { delete[]name; name=NULL; } voidInput(); voidOutput() { cout<<"number="< if(name! =NULL) cout<<",name="< cout<<",sex="< cout<<",birthday="; birthday.Output(); }; private: intnumber; char*name; boolsex; Datebirthday; }; voidPerson: : Input() { cout<<"Pleaseinputthenumber: "; cin>>number; cout<<"Pleaseinputthename: "; cin>>name; cout<<"Pleaseinputthesex: (0or1)"; cin>>sex; cout<<"Pleaseinputthebirthday(year/month/day): "; inty,m,d; cin>>y>>m>>d; birthday=Date(y,m,d); } voidmain() { Personp1; Personp2(p1); p1.Input(); p1.Output(); p2.Output(); Personp3(Date(1998,12,12),1,"zhangsan",1); p3.Output(); } 数组和指针 1.已知一个数组a[10],并在其中保存了下列数据: 6、12、34、56、78、89、90、409、800、2300 要求: (1)从键盘上任意输入一个数,并将该数插入到数组的合适位置。 (要求: 用指针完成)。 (2)打印出插入前和插入后的数组内容。 #include usingnamespacestd; voidmain() { inta[10]={6,12,34,56,78,89,90,409,800,2300}; intb,c; cout<<"请输入一个数: "< cin>>b; cout<<"请输入想插入的位置(数组的哪个元素-9)"< cin>>c; intd[11]; int*pa=a; int*pb=d; for(intj=0;j { *pb++=*pa++; } pb=d; *(pb+c)=b; for(intk=c+1;k<11;k++) *(pb+k)=a[k-1]; cout<<"插入前的数组是: "< for(intm=0;m<10;m++) cout< cout< cout<<"插入后的数组是: "< for(inti=0;i<11;i++) cout<<*(pb+i)<<""; cout< } 用指针实现字符串顺序交换 #include usingnamespacestd; voidmain() { chars[]="abcdefghijklmnopqrstuvwxyz"; charch,*t=&s[strlen(s)-1]; for(inti=0;i<(strlen(s)-1)/2;i++) { ch=s[i]; s[i]=*(t-i); *(t-i)=ch; } for(intk=0;k cout< cout< cout< } 普通转换,调换字符串顺序 #include usingnamespacestd; voidmain() { chars[10]="student",t; inti=0,j=strlen(s)-1; while(i { t=s[i];s[i]=s[j];s[j]=t;i++;j--; } cout< } 将十进制转换成其他进制(小于十进制) #include usingnamespacestd; voidmain() { intn,b,a[20]; cin>>n>>b; for(inti=0;n>0;i++) { a[i]=n%b; n/=b; } for(intj=i-1;j>=0;j--) cout< cout< } 运算符? ? ? #include classA{ public: A(): a(0){} A&operator++() { a++; return*this; } Aoperator++(int) { Atemp(*this); a++; returntemp; } voiddisplay(){cout< protected: inta; }; voidmain(){ Aa; (++a).display(); a++.display(); } 二维数组转换为一维数组 #include usingnamespacestd; longsum_array(intarray[],intelements) {
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 经典 C+ 程序