C++练习.docx

上传人:b****4 文档编号:4343580 上传时间:2022-11-30 格式:DOCX 页数:30 大小:395.38KB
下载 相关 举报
C++练习.docx_第1页
第1页 / 共30页
C++练习.docx_第2页
第2页 / 共30页
C++练习.docx_第3页
第3页 / 共30页
C++练习.docx_第4页
第4页 / 共30页
C++练习.docx_第5页
第5页 / 共30页
点击查看更多>>
下载资源
资源描述

C++练习.docx

《C++练习.docx》由会员分享,可在线阅读,更多相关《C++练习.docx(30页珍藏版)》请在冰豆网上搜索。

C++练习.docx

C++练习

1.用C++写一个程序,求一个有10个元素的数组中的最大元素。

#include

usingnamespacestd;

classArray_max

{public:

voidset_value();

voidmax_value();

voidshow_value();

private:

intarray[10];

intmax;

};

voidArray_max:

:

set_value()

{

for(inti=0;i<10;i++)

cin>>array[10];

}

voidArray_max:

:

max_value()

{

max=array[0];

for(inti=1;i<10;i++)

if(array[i]>max)

max=array[i];

}

voidArray_max:

:

show_value()

{cout<<"max="<

}

intmain()

{Array_maxamax;

amax.set_value();

amax.max_value();

amax.show_value();

return0;

}

2.定义一个Time类(包括hour,minute,sec),要求输入和输出2个对象t1和t2。

方法一(引用对象)

#include

usingnamespacestd;

structTime//classTime

Public:

{

inthour;

intminute;

intsec;

};

intmain()

{voidset_time(Time&);

voidshow_time(Time&);

Timet1;

set_time(t1);

show_time(t1);

Timet2;

set_time(t2);

show_time(t2);

return0;

}

voidset_time(Time&t)

{

cin>>t.hour>>t.minute>>t.sec;

}

voidshow_time(Time&t)

{

cout<

"<

"<

}

方法二:

#include

usingnamespacestd;

classTime

{private:

inthour;

intminute;

intsec;

public:

voidset_time();

voidshow_time();

};

voidTime:

:

set_time()

{

cin>>hour>>minute>>sec;

}

voidTime:

:

show_time()

{

cout<

"<

"<

}

intmain()

{

Timet1;

t1.set_time();

t1.show_time();

Timet2;

t2.set_time();

t2.show_time();

return0;

}

3.构造函数

(1)

#include

usingnamespacestd;

classBox

{public:

Box(intl,intw,inth):

length(l),width(w),height(h){}

intvolume();

private:

intlength,width,height;

};

intBox:

:

volume()

{

return(length*width*height);

}

intmain()

{Boxbox1(10,10,10);

cout<<"Thevolumeofbox1is:

"<

Boxbox2(12,12,12);

cout<<"Thevolumeofbox2is:

"<

return0;

}

(2)

#include

usingnamespacestd;

classBox

{public:

Box(intl=10,intw=10,inth=10);//在声明构造函数时指定默认参数

intvolume();

private:

intlength,width,height;

};

Box:

:

Box(intl,intw,inth)//在定义构造函数时可不必再指定参数的默认值

{length=l;

width=w;

height=h;

}

intBox:

:

volume()

{

return(length*width*height);

}

intmain()

{Boxbox1;

cout<<"Thevolumeofbox1is:

"<

Boxbox2(12);

cout<<"Thevolumeofbox2is:

"<

Boxbox3(18,10);

cout<<"Thevolumeofbox3is:

"<

Boxbox4(15,12,20);

cout<<"Thevolumeofbox4is:

"<

return0;

}

4.对象数组

#include

usingnamespacestd;

classBox

{public:

Box(intl,intw,inth):

length(l),width(w),height(h){}

intvolume();

private:

intlength;

intwidth;

intheight;

};

intBox:

:

volume()

{

return(length*width*height);

}

intmain()

{Boxa[3]=

{

Box(25,10,12),

Box(15,18,20),

Box(16,20,26)

};

cout<<"Thevolumeofa[0]is:

"<

cout<<"Thevolumeofa[1]is:

"<

cout<<"Thevolumeofa[2]is:

"<

return0;

}

5.对象指针

#include

usingnamespacestd;

classTime

{public:

Time(inth,intm,ints):

hour(h),minute(m),sec(s){}

inthour;

intminute;

intsec;

voidshow_time();

};

voidTime:

:

show_time()

{

cout<

"<

"<

}

intmain()

{Timet1(16,43,20);

int*p1=&t1.hour;

cout<<*p1<

t1.show_time();

Time*p2=&t1;

p2->show_time();//(*p2).show_time();

void(Time:

:

*p3)();

p3=&Time:

:

show_time;

(t1.*p3)();

return0;

}

6.对象的常引用

#include

usingnamespacestd;

classTime

{public:

Time(inth,intm,ints):

hour(h),minute(m),sec(s){}

inthour;

intminute;

intsec;

voidfun(Time&t);//形参t是Time类对象的引用//函数声明

voidshow_time();

};

voidTime:

:

show_time()

{

cout<

"<

"<

}

voidfun(Time&t)

{t.hour=18;

t.minute=12;

}

intmain()

{Timet1(16,43,20);

fun(t1);

t1.show_time();

return0;

}

7.静态数据成员引用

#include

usingnamespacestd;

classBox

{public:

Box(intl=10,intw=10):

length(l),width(w){}

intvolume();

staticintheight;

intlength,width;

};

intBox:

:

volume()

{

return(length*width*height);

}

intBox:

:

height=12;//静态数据成员可以初始化,但只能在类外进行初始化

intmain()

{Boxbox1,box2(12,10);

cout<<"Thevolumeofbox1is:

"<

cout<<"Thevolumeofbox2is:

"<

return0;

}

8.静态成员函数引用

#include

usingnamespacestd;

classStudent

{public:

Student(intn,inta,floats):

num(n),age(a),score(s){}

voidtotal();

intvolume();

staticfloataverage();

private:

intnum;

intage;

floatscore;

staticfloatsum;

staticintscount;

};

floatStudent:

:

sum=0;

intStudent:

:

scount=0;

voidStudent:

:

total()

{

sum+=score;//sum=sum+score;

scount++;

}

floatStudent:

:

average()

{

return(sum/scount);

}

intmain()

{Studentstu[4]={

Student(1101,18,70),

Student(1103,17,69),

Student(1104,18,78),

Student(1106,19,80)};

intn;

cout<<"pleaseinputthenumberofstudents:

";

cin>>n;

for(inti=0;i

stu[i].total();

cout<<"Theaveragescoreof"<

"<

:

average()<

return0;

}

9.将普通函数声明为友元函数

(1)

#include

usingnamespacestd;

classTime

{public:

Time(inth,intm,ints):

hour(h),minute(m),sec(s){}

friendvoiddisplay(Time&t);

private:

inthour;

intminute;

intsec;

};

voiddisplay(Time&t)

{

cout<

"<

"<

}

intmain()

{Timet1(22,55,30);

display(t1);

return0;

}

(2)

#include

usingnamespacestd;

classDate;

classTime

{public:

Time(inth,intm,ints):

hour(h),minute(m),sec(s){}

friendvoiddisplay(Time&);

private:

inthour;

intminute;

intsec;

};

voiddisplay(Time&t)

{

cout<

"<

"<

}

classDate

{public:

Date(inty,intm,intd):

year(y),month(m),day(d){}

friendvoiddisplay(Date&);

private:

intyear;

intmonth;

intday;

};

voiddisplay(Date&d)

{

cout<

}

intmain()

{Timet1(22,55,30);

Dated1(2012,5,18);

display(d1);

display(t1);

return0;

}

10.友元成员函数

#include

usingnamespacestd;

classDate;

classTime

{public:

Time(inth,intm,ints):

hour(h),minute(m),sec(s){}

voiddisplay(Date&);

private:

inthour;

intminute;

intsec;

};

classDate

{public:

Date(inty,intm,intd):

year(y),month(m),day(d){}

friendvoidTime:

:

display(Date&);

private:

intyear;

intmonth;

intday;

};

voidTime:

:

display(Date&d)

{

cout<

cout<

"<

"<

}

intmain()

{Timet1(22,55,30);

Dated1(2012,5,18);

t1.display(d1);

return0;

}

11.运算符重载

(1)#include

usingnamespacestd;

classComplex

{public:

Complex(){real=0;imag=0;}

Complex(intr,inti):

real(r),imag(i){}

Complexoperator+(Complex&c2);

voiddisplay();

private:

intreal;

intimag;

};

ComplexComplex:

:

operator+(Complex&c2)//operator+是一个函数名,是类Complex的成员函数

{Complexc;

c.real=real+c2.real;

c.imag=imag+c2.imag;

returnc;

}

voidComplex:

:

display()

{

cout<<"("<

}

intmain()

{Complexc1(3,4),c2(5,-10),c3;

c3=c1+c2;

cout<<"c1=";c1.display();

cout<<"c2=";c2.display();

cout<<"c1+c2=";c3.display();

return0;

}

(2)重载流插入运算符“<<”:

#include

classComplex

{public:

Complex(){real=0;imag=0;}

Complex(intr,inti){real=r;imag=i;}

Complexoperator+(Complex&c2);

friendostream&operator<<(ostream&,Complex&);

private:

intreal;

intimag;

};

ComplexComplex:

:

operator+(Complex&c2)

{

returnComplex(real+c2.real,imag+c2.imag);

}

ostream&operator<<(ostream&output,Complex&c)

{output<

if(c.imag>=0)output<<"+";

output<

returnoutput;

}

intmain()

{Complexc1(2,4),c2(6,-10),c3;

c3=c1+c2;

cout<<"c1="<

return0;

}

12.派生类的构造函数和析构函数:

#include

#include

usingnamespacestd;

classStudent

{public:

Student(intn,stringnam,floatm)

{num=n;

name=nam;

mark=m;

}

~Student(){cout<

protected:

intnum;

stringname;

floatmark;

};

classStudent1:

protectedStudent

{public:

Student1(intn,stringnam,floatm,inta,chars):

Student(n,nam,m)

{age=a;

sex=s;

}

voidshow()

{cout<<"学号:

"<

cout<<"姓名:

"<

cout<<"分数:

"<

cout<<"年龄:

"<

cout<<"性别:

"<

}

~Student1(){cout<

private:

intage;

charsex;

};

intmain()

{Student1stud1(1101,"zhuxuhong",100,18,'m');

Student1stud2(1102,"zhangxu",80,20,'m');

cout<<"学生1信息:

"<

stud1.show(),cout<

cout<<"学生2信息:

"<

stud2.show(),cout<

return0;

}

13.有子对象的派生类的构造函数:

#include

#include

usingnamespacestd;

classStudent

{public:

Student(intn,stringnam,floatm)

{num=n;

name=nam;

mark=m;

}

voiddisplay()

{

cout<<"学号:

"<

"<

"<

}

protected:

intnum;

stringname;

floatmark;

};

classStudent1:

protectedStudent

{public:

Student1(intn,stringnam,floatm,intn1,stringnam1,floatm1,inta,chars):

Student(n,nam,m),monitor(n1,nam1,m1)

{age=a;

sex=s;

}

voidshow()

{cout<<"学生信息:

"<

display();

cout<<"年龄:

"<

cout<<"性别:

"<

}

voidshow_monitor()

{

cout<

"<

monitor.display();

}

private:

Studentmonitor;

intage;

charsex;

};

intmain()

{Student1stud1(1101,"zhuxuhong",98,1107,"xiaoyin",100,18,'m');

stud1.show();

stud1.show_monitor();

return0;

}

14.多层派生时的构造函数:

#include

#include

usingnamespacestd;

classStudent

{public:

Student(intn,stringnam)

{num=n;

name=nam;}

voiddisplay()

{cout<<"学号:

"<

cout<<"姓名:

"<

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 法律文书 > 判决书

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1