版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
第一章
5:
include<iostream>
usingnamespacestd;
intmain()
(
cout?,,This"?"is";
cout?"aH?"C++";
cout?"program."?endl;
return0;
)
6:
#include<iostream>
usingnamespacestd;
intmain()
(
inta,b,c;
a=10;
b=23;
c=a+b;
cout?"a+b=";
cout?c;
cout?endl;
return0;
)
7:
include<iostream>
usingnamespacestd;
intmain()
(
inta,b,c;
intf(intxjntyjntz);
cin?a?b?c;
c=f(a,b,c);
cout?c?endl;
return0;
}
intf(intxjnty,intz)
intm;
if(x<y)m=x;
elsem=y;
if(z<m)m=z;
return(m);
8:include<iostream>
usingnamespacestd;
intmain()
(
inta,b,c;
cin?a?b;
c=a+b;
cout?"a+b=n?a+b?endl;
return0;
9:
#include<iostream>
usingnamespacestd;
intmain()
{intadd(intx,inty);
inta,b,c;
cin?a?b;
c=add(a,b);
cout?"a+b="?c?endl;
return0;
}
intadd(intx,inty)
{intc;
C=x+y;
return(c);
)
10:
#include<iostream>
usingnamespacestd;
intmain()
{voidsort(intxjntyjntz);
intx,y,z;
cin?x?y?z;
sort(x,y,z);
return0;
)
voidsort(intx,inty,intz)
(
inttemp;
if(x>y){temp=x;x=y;y=temp;}//(}內3個語句的作用是將x和y的值互換)
if(z<x)cout?z?7?x?7?y?endl;
elseif(z<y)cout?x?7?z?7?y?endl;
elsecout?x?7?y?7?z?endl;
)
11:
include<iostream>
usingnamespacestd;
intmain()
{intmax(inta,intb,intc=0);
inta,b,c;
cin?a?b?c;
cout?"max(a/b,c)="?max(a,bzc)?endl;
cout?"max(a,b)="?max(a,b)?endl;
return0;
)
intmax(inta,intbjntc)
{if(b>a)a=b;
if(c>a)a=c;
returna;
)
12:
#include<iostream>
usingnamespacestd;
intmain()
(
voidchange(intJnt);
inta,b;
cin?a?b;
if(a<b)change(a,b);
cout<<“max="min=H?b?endl;
return0;
voidchange(intJnt)
intrl,r2,temp;
temp=rl;
rl=r2;
r2=temp;
)
13:
#include<iostream>
usingnamespacestd;
intmain()
{voidsort(int&,int&,int&);
inta,b,c,31,bl,cl;
cout?"Pleaseenter3integers:";
cin?a?b?c;
al=a;bl=b;cl=c;
sort(al,bl,cl);
cout?a?""?b?H"?c?Hinsortedorderis
cout?al?n"?bl?""?cl?endl;
return0;
)
voidsort(int&i,int&j,int&k)
{voidchange(int&,int&);
if(i>j)change(i,j);
if(i>k)change(i,k);
if(j>k)change(j,k);
)
voidchange(int&x,int&y)
{inttemp;
temp=x;
x=y;
y=temp;
)
14:
include<iostream>
include<string>
usingnamespacestd;
intmain()
{stringsl="week"/s2="end";
cout?"sl="?sl?endl;
cout<<"s2="?s2?endl;
sl=sl+s2;
cout?"Thenewstringis:"?sl?endl;
return0;
15:
include<iostream>
include<string>
usingnamespacestd;
intmain()
{stringstr;
inti,n;
chartemp;
cout?"pleaseinputastring:";
cin?str;
n=str.size();
for(i=0;i<n/2;i++)
{temp=str[i];str[i]=str[n-i-l];str[n-i-l]=temp;}
cout?str?endl;
return0;
16:
include<iostream>
#include<string>
usingnamespacestd;
intmain()
{inti;
,,,,u
stringstr[5]={"BASIC'/"C;FORTRAN"z"C++"/PASCAL");
voidsort(string[]);
sort(str);
cout?"thesortedstrings:"?endl;
for(i=0;i<5;i++)
cout?str[i]?"
cout?endl;
return0;
)
voidsort(strings[])
{intij;
stringt;
for(j=0;j<5;j++)
for(i=0;i<5-j;i++)
if(s[i]>s[i+l])
{t=s[i];s[i]=s[i+l];s[i+l]=t;}
}17:include<iostream>
ttinclude<string>
usingnamespacestd;
intmain()
(
longc⑸={10100,-123567,1198783,-165654,3456};
inta[5]={l,9z0,23,-45};
floatb[5]={2.4,7.6,5.5,6.6,-2.3};
voidsort(int[]);
voidsort(float[]);
voidsort(long[]);
sort(a);
sort(b);
sort(c);
return0;
voidsort(inta[])
{intij,t;
for(j=0;j<5;j++)
for(i=0;i<5-j;i++)
if(a[i]>a[i+l])
{t=a[i];a[i]=a[i+l];a[i+l]=t;}
cout?"thesortednumbers:"?endl;
for(i=0;i<5;i++)
cout?a[i]?"
cout?endl?endl;
)
voidsort(longa[])
{intij;
longt;
for(j=0;j<5;j++)
for(i=0;i<5-j;i++)
if(a[i]>a[i+l])
{t=a[i];a[i]=a[i+l];a[i+l]=t;}
cout?"thesortednumbers:"?endl;
for(i=0;i<5;i++)
cout?a[i]?"
cout?endl?endl;
voidsort(floata[])
{intij;
floatt;
for(j=O;j<5;j++)
for(i=0;i<5-j;i++)
if(a[i]>a[i+l])
{t=a[i];a[i]=a[i+l];a[i+l]=t;}
cout?"thesortednumbers:"?endl;
for(i=0;i<5;i++)
cout?a[i]?"
cout?endl?endl;
)
18:#include<iostream>
include<string>
usingnamespacestd;
template<typenameT>
voidsort(Ta[])
{inti,j,min;
Tt;
for(i=0;i<5;i++)
{min=i;
for(j=i+l;j<5;j++)
if(a[min]>a[j])min=j;
t=a[i];a[i]=a[min];a[min]=t;
)
cout?"thesortednumbers:"?endl;
for(i=0;i<5;i++)
cout?a[i]?"
cout?endl?endl;
)
intmain()
{inta[5]={l,9A23z-45);
floatb[5]={2.4,7.6,5.5,6.6,-2.3};
longc[5]={10100/-123567,1198783,-165654,3456};
sort(a);
sort(b);
sort(c);
return0;
第二章
1
ttinclude<iostream>
usingnamespacestd;
classTime
(
public:
voidset_time();
voidshow_time();
private:〃成員改為公用的
inthour;
intminute;
intsec;
};
voidTime::set_time()〃在main函數(shù)之前定義
(
cin?hour;
cin?minute;
cin?sec;
)
voidTime::show_time()〃在main函數(shù)之前定義
cout?hour?":"?minute?":"?sec?endl;
intmain()
{Timetl;
tl.set_time();
tl.show_time();
return0;
}
2:
#include<iostream>
usingnamespacestd;
classTime
{public:
voidset_time(void)
{cin?hour;
cin?minute;
cin?sec;
)
voidshow_time(void)
{cout?hour?":"?minute?":"?sec?endl;}
private:inthour;
intminute;
intsec;
);
Timet;
intmain()
(
t.set_time();
t.show_time();
return0;
}
3:
include<iostream>
usingnamespacestd;
classTime
{public:
voidset_time(void);
voidshow_time(void);
private:
inthour;
intminute;
intsec;
);
voidTime::set_time(void)
{cin?hour;
cin?minute;
cin?sec;
}
voidTime::show_time(void)
{cout?hour?":"?minute?":"?sec?endl;}
Timet;
intmain()
{t.set_time();
t.show_time();
return0;
4:
//xt2-4-l.cpp(main.cpp)
ttinclude<iostream>
usingnamespacestd;
include"xt2-4.h"
intmain()
{Studentstud;
stud.set_value();
stud.display();
return0;
)
〃xt2-4?2.cpp(即student.cpp)
include"xt2-4.h"〃在此文件中進行函數(shù)的定義
#include<iostream>
usingnamespacestd;〃不要漏寫此行
voidStudent::display()
{cout?"num:"?num?endl;
cout<<"name:"?name?endl;
cout<<"sex:"?sex?endl;
)
voidStudent::set_value()
{cin?num;
cin?name;
cin?sex;
)
5:
//xt2-5-l.cpp(filel.cpp)
include<iostream>
#include"xt2-5.h"
intmain()
{Array_maxarrmax;
arrmax.set_value();
arrmax.max_value();
arrmax.show_value();
return0;
)
//xt2-5-2.cpp(arraymax.cpp)
#include<iostream>
usingnamespacestd;
#include"xt2-5.h"
voidArray_max::set_value()
{inti;
for(i=0;i<10;i++)
cin?array[i];
)
voidArray_max::max_value()
{inti;
max=array[O];
for(i=l;i<10;i++)
if(array[i]>max)max=array[i];
)
voidArray_max::show_value()
{cout?"max="?max<<endl;
)
6:解法一
ttinclude<iostream>
usingnamespacestd;
classBox
{public:
voidget_value();
floatvolume();
voiddisplay();
public:
floatlengh;
floatwidth;
floatheight;
);
voidBox::get_value()
{cout?npleaseinputlengh,width,height:";
cin?lengh;
cin?width;
cin?height;
)
floatBox::volume()
{return(lengh*width*height);}
voidBox::display()
{cout?volume()?endl;}
intmain()
{Boxboxl,box2,box3;
boxl.get_value();
cout?hvolmueofbaxlis
boxl.display();
box2.get_value();
cout?"volmueofbax2is
box2.display();
box3.get_value();
cout?Hvolmueofbax3is
box3.display();
return0;
)
解法二:
#include<iostream>
usingnamespacestd;
classBox
{public:
voidget_value();
voidvolume();
voiddisplay();
public:
floatlengh;
floatwidth;
floatheight;
floatvol;
);
voidBox::get_value()
{cout?"pleaseinputlengh,width,height:";
cin?lengh;
cin?width;
cin?height;
)
voidBox::volume()
{vol=lengh*width*height;}
voidBox::display()
{cout?vol?endl;}
intmain()
{Boxboxl/box2,box3;
boxl.get_value();
boxl.volume();
cout?"volmueofbaxlisH;
boxl.display();
box2.get_value();
box2.volume();
cout?"volmueofbax2is
box2.display();
box3.get_value();
box3.volume();
cout?"volmueofbax3is
box3.display();
return0;
第三章
2:
#include<iostream>
usingnamespacestd;
classDate
{public:
Date(intjntjnt);
Date(intjnt);
Date(int);
Date();
voiddisplay();
private:
intmonth;
intday;
intyear;
};
Date::Date(intm,intd,inty):month(m),day(d),year(y)
()
Date::Date(intmjntd):month(m),day(d)
{year=2005;}
Date::Date(intm):month(m)
{day=l;
year=2005;
Date::Date()
{month=l;
day=l;
year=2005;
}
voidDate::display()
{cout?month?,7"?day?,7"?year?endl;}
intmain()
(
Datedl(10,13,2005);
Dated2(12,30);
Dated3(10);
Dated4;
dl.display();
d2.display();
d3.display();
d4.display();
return0;
)
3:
#include<iostream>
usingnamespacestd;
classDate
{public:
Date(int=l,int=l,int=2005);
voiddisplay();
private:
intmonth;
intday;
intyear;
);
Date::Date(intmjntd,inty):month(m),day(d),year(y)
()
voidDate::display()
{cout?month?7"?day?7"?year?endl;}
intmain()
(
Datedl(10,13,2005);
Dated2(12,30);
Dated3(10);
Dated4;
dl.display();
d2.display();
d3.display();
d4.display();
return0;
)
4:
include<iostream>
usingnamespacestd;
classStudent
{public:
Student(intn,floats):num(n),score(s){}
voiddisplay();
private:
intnum;
floatscore;
};
voidStudent::display()
{cout?num?""?score?endl;}
intmain()
{Studentstud[5]={
Student(101,78.5),Student(102,85.5),Student(103,98.5),
Student(104,100.0),Student(105,95.5)};
Student*p=stud;
for(inti=0;i<=2;p=p+2J++)
p->display();
return0;
5:
include<iostream>
usingnamespacestd;
classStudent
{public:
Student(intn,floats):num(n),score(s){}
intnum;
floatscore;
);
voidmain()
{Studentstud[5]={
Student(101,78.5),Student(102,85.5)/Student(103/98.5),
Student(104,100.0),Student(105,95.5)};
voidmax(Student*);
Student*p=&stud[O];
max(p);
)
voidmax(Student*arr)
{floatmax_score=arr[0].score;
intk=0;
for(inti=l;i<5;i++)
if(arr[i].score>max_score){max_score=arr[i].score;k=i;}
cout?arr[k].num?"H?max__score?endl;
)
6:
ttinclude<iostream>
usingnamespacestd;
classStudent
{public:
Student(intn,floats):num(n),score(s){}
voidchange(intnjloats){num=n;score=s;}
voiddisplay(){cout?num?n"?score?endl;}
private:
intnum;
floatscore;
);
intmain()
{Studentstud(101,78.5);
stud.display();
stud.change(101,80.5);
stud.display();
return0;
)
7:解法一
include<iostream>
usingnamespacestd;
classStudent
{public:
Student(intn,floats):num(n),score(s){}
voidchange(intn,floats){num=n;score=s;}
voiddisplay(){cout?num?"H?score?endl;}
〃可改為:voiddisplay()const{cout?num?""?score?endl;}
private:
intnum;
floatscore;
};
intmain()
{constStudent
stud(101,78.5);
stud.display();
//stud.change(101,80.5);
stud.display();
return0;
解法二:
include<iostream>
usingnamespacestd;
classStudent
{public:
Student(intn,floats):num(n)zscore(s){}
voidchange(intn,floats)const{num=n;score=s;}
voiddisplay()const{cout?num?""?score?endl;}
private:
mutableintnum;
mutablefloatscore;
);
intmain()
{constStudentstud(101,78.5);
stud.display();
stud.change(101,80.5);
stud.display();
return0;
)
解法三:
include<iostream>
usingnamespacestd;
classStudent
{public:
Student(intn,floats):num(n),score(s){}
voidchange(intn,floats){num=n;score=s;}
voiddisplay(){cout?num?""?score?endl;}
private:
intnum;
floatscore;
);
intmain()
{Studentstud(101,78.5);
Student*p=&stud;
p->display();
p->change(101,80.5);
p->display();
return0;
}
8:
include<iostream>
usingnamespacestd;
classStudent
{public:
Student(intn,floats):num(n),score(s){}
voidchange(intn,floats){num=n;score=s;}
voiddisplay(){cout?num?""?score?endl;}
private:
intnum;
floatscore;
);
intmain()
{Studentstud(101,78.5);
voidfun(Student&);
fun(stud);
return0;
)
voidfun(Student&stu)
{stu.displayO;
stu.change(101,80.5);
stu.display();
9:
include<iostream>
usingnamespacestd;
classProduct
{public:
Product(intnjntq,floatp):num(n),quantity(q),price(p){};
voidtotal();
staticfloataverage();
staticvoiddisplay();
private:
intnum;
intquantity;
floatprice;
staticfloatdiscount;
staticfloatsum;
staticintn;
);
voidProduct::total()
{floatrate=1.0;
if(quantity>10)rate=0.98*rate;
sum=sum+quantity*price*rate*(l-discount);
n=n+quantity;
}
voidProduct::display()
{cout?sum?endl;
cout?average()?endl;
}
floatProduct::average()
{return(sum/n);}
floatProduct::discount=0.05;
floatProduct::sum=0;
intProduct::n=0;
intmain()
(
ProductProd[3]={
Product(101,5,23.5),Product(102,12,24.56),Product(103,100,21.5)
);
for(inti=0;i<3;i++)
Prod[i].total();
Product::display();
return0;
)
10:
include<iostream>
usingnamespacestd;
classDate;
classTime
{public:
Time(int,int,int);
friendvoiddisplay(constDate&,constTime&);
private:
inthour;
intminute;
intsec;
};
Time::Time(inthjntmjnts)
{hour=h;
minute=m;
sec=s;
}
classDate
{public:
Date(intjntjnt);
friendvoiddisplay(constDate&,constTime&);
private:
intmonth;
intday;
intyear;
);
Date::Date(intmjntd,inty)
{month=m;
day=d;
year=y;
voiddisplay(constDate&d,constTime&t)
cout?d.month?7"?d.day?7"?d.year?endl;
cout?t.hour?":"?t.minute?H:"?t.sec?endl;
intmain()
(
Timetl(10z13,56);
Datedl(12,25,2004);
display(dlztl);
return0;
)
11:
#include<iostream>
usingnamespacestd;
classTime;
classDate
{public:
Date(intjntjnt);
friendTime;
private:
intmonth;
intday;
intyear;
};
Date::Date(intm,intd,inty):month(m),day(d),year(y){}
classTime
{public:
Time(int,int,int);
voiddisplay(constDate&);
private:
inthour;
intminute;
intsec;
};
Time::Time(inth,intm,ints):hour(h),minute(m),sec(s){}
voidTime::display(constDate&d)
cout?d.month?,7"?d.day?,7"?d.year<<endl;
cout?hour?":"?minute?":n?sec?endl;
intmain()
(
Timetl(10z13z56);
Datedl(12,25,2004);
tl.display(dl);
return0;
)
12:
include<iostream>
usingnamespacestd;
template<classnumtype>
classCompare
{public:
Compare(numtypea,numtypeb);
numtypemax();
numtypemin();
private:
numtypex,y;
};
template<classnumtype>
Compare<numtype>::Compare(numtypea,numtypeb)
{x=a;y=b;}
template<classnumtype>
numtypeCompare<numtype>::max()
{return(x>y)?x:y;}
template<classnumtype>
numtypeCompare<numtype>::min()
{return(x<y)?x:y;}
intmain()
{Compare<int>cmpl(3,7);
cout?cmpl.max()?"istheMaximumoftwointegernumbers."?endl;
cout?cmpl.min()?HistheMinimumoftwointegernumbers."?endl?endl;
Compare<float>cmp2(45.78,93.6);
cout?cmp2.max()?"istheMaximumoftwofloatnumbers."?endl;
cout?cmp2.min()?"istheMinimumoftwofloatnumbers."?endl?endl;
Compare<char>cmp3('a7A');
cout?cmp3.max()?"istheMaximumoftwocharacters."?endl;
cout?cmp3.min()?"istheMinimumoftwocharacters."?endl;
return0;
第四章
1:
include<iostream>
usingnamespacestd;
classComplex
{public:
Complex(){real=0;imag=0;}
Complex(doubler,doublei){real=r;imag=i;}
doubleget_real();
doubleget_imag();
voiddisplay();
private:
doublereal;
doubleimag;
};
doubleComplex::get_real()
{returnreal;}
doubleComplex::getjmag()
{returnimag;}
voidComplex::display()
{cout?"(''?real?'7,?imag?"i)"?endl;}
Complexoperator+(Complex&cl,Complex&c2)
(
returnComplex(cl.get_real()+c2.get_real(),cl.getjmag()+c2.getjmag());
)
intmain()
{Complexcl(3,4),c2(5,-10)zc3;
c3=cl+c2;
cout?,,c3=,';
c3.display();
return0;
)
2:
include<iostream>
usingnamespacestd;
classComplex
{public:
Complex(){real=0;imag=0;}
Complex(doubler,doublei){real=r;imag=i;}
Complexoperator+(Complex&c2);
Complexoperator-(Complex&c2);
Complexoperator*(Complex&c2);
Complexoperator/(Complex&c2);
voiddisplay();
private:
doublereal;
doubleimag;
);
ComplexComplex::operator+(Complex&c2)
{Complexc;
c.real=real+c2.real;
c.imag=imag+c2.imag;
returnc;}
ComplexComplex::operator-(Complex&c2)
{Complexc;
c.real=real-c2.real;
c.imag=imag-c2.imag;
returnc;}
ComplexComplex::operator*(Complex&c2)
{Complexc;
c.real=real*c2.real-imag*c2.imag;
c.imag=imag*c2.real+real*c2.imag;
returnc;}
ComplexComplex::operator/(Complex&c2)
{Complexc;
c.real=(real*c2.real+imag*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag);
c.imag=(imag*c2.real-real*c2.imag)/(c2.real*c2,real+c2.imag*c2.imag);
returnc;}
voidComplex::display()
{cout?"("?real?,,,"?imag?"i)"?endl;}
intmain()
{Complexcl(3,4),c2(5,-10),c3;
c3=cl+c2;
cout?,,cl+c2=,,;
c3.display();
c3=cl-c2;
cout?"cl-c2=";
c3.display();
c3=cl*c2;
cout?,,cl*c2=";
c3.display();
c3=cl/c2;
cout?"cl/c2=";
c3.display();
return0;
)
3:
#include<iostream>〃用VC++時改為:include<iostream.h>
usingnamespacestd;〃用VC++時為取消此行
classComplex
{public:
Complex(){real=0;imag=0;}
Complex(doubler,doublei){real=r;imag=i;}
Complexoperator+(Complex&c2);
Complexoperator+(int&i);
friendComplexoperator+(int&,Complex&);
voiddisplay();
private:
doublereal;
doubleimag;
);
ComplexComplex::operator+(Complex&c)
{returnComplex(real+c.real,imag+c.imag);}
ComplexComplex::operator+(int&i)
{returnComplex(real+ijmag);}
voidComplex::display()
,H
{cout?'(?real?","?imag?"i)"?endl;}
Complexoperator+(int&i,Complex&c)
{returnComplex(i+c.real,c.imag);}
intmain()
{Complexcl(3,4),c2(5,-10),c3;
inti=5;
c3=cl+c2;
cout?"cl+c2=";
c3.display();
c3=i+cl;
cout?"i+cl=,';
c3.display();
c3=cl+i;
cout?ncl+i=";
c3.display();
return0;
)
4:
include<iostream>
usingnamespacestd;
classMatrix〃定義Matrix類
{public:
Matrix();〃默認構造函數(shù)
friendMatrixoperator+(Matrix&,Matrix&);〃重載運算符“+”
voidinput();〃輸入數(shù)據(jù)函數(shù)
voiddisplay();〃輸出數(shù)據(jù)函數(shù)
private:
intmat[2][3];
};
Matrix::Matrix()〃定義構造函數(shù)
{for(inti=0;i<2;i++)
for(intj=0;j<3;j++)
mat[i][j]=O;
)
Matrixoperator+(Matrix&a,Matrix&b)〃定義重載運算符“+”函數(shù)
{Matrixc;
for(inti=0;i<2;i++)
for(intj=0;j<3;j++)
{c.mat[i][j]=a.mat[i][j]+b.mat[i][j];}
returnc;
)
voidMatrix::input()〃定義輸入數(shù)據(jù)函數(shù)
{cout?"inputvalueofmatrix:"?endl;
for(inti=0;i<2;i++)
for(intj=0;j<3;j++)
cin?mat[i][j];
voidMatrix::display()〃定義輸出數(shù)據(jù)函數(shù)
{for(inti=0;i<2;i++)
{for(intj=O;j<3;j++)
{cout?mat[i][j]?"";}
cout?endl;}
)
intmain()
{Matrixa,b,c;
a.input();
b.input();
cout?endl?"Matrixa:"?endl;
a.display();
cout?endl?"Matrixb:"?endl;
b.display();
c=a+b;〃用重載運算符“+”實現(xiàn)兩個矩陣相
加
cout?endl?"Matrixc=Matrixa+Matrixb:"?endl;
c.display();
return0;
)
5:
include<iostream.h>
//usingnamespacestd;
classMatrix
{public:
Matrix();
friendMatrixoperator+(Matrix&,Matrix&);
friendostream&operator?(ostream&,Matrix&);
friendistream&operator?(istream&zMatrix&);
private:
intmat[2][3];
};
Matrix::Matrix()
{for(inti=0;i<2;i++)
for(intj=0;j<3;j++)
mat[i][j]=O;
}
Matrixoperator+(Matrix&a,Matrix&b)
{Matrixc;
for(inti=0;i<2;i++)
for(intj=0;j<3;j++)
{c.mat[i][j]=a.mat[i][j]+b.mat[i][j];
)
returnc;
}
istream&operator?(istream&in,Matrix&m)
{cout?"inputvalueofmatrix:"?endl;
for(inti=0;i<2;i++)
for(intj=O;j<3;j++)
in?m.mat[i][j];
returnin;
)
ostream&operator?(ostream&out,Matrix&m)
{for(inti=0;i<2;i++)
{for(intj=O;j<3;j++)
{out?m.mat[i][j]?"";}
out?endl;}
returnout;
}
intmain()
{Matrixa,b,c;
cin?a;
cin?b;
cout?endl?"Matrixa:"?endl?a?endl;
cout?endl?"Matrixb:"?endl?b?endl;
c=a+b;
cout?endl?"Matrixc=Matrixa+Matrixb:"?endl?c?endl;
return0;
)
6:
include<iostream>
usingnamespacestd;
classComplex
{public:
Complex(){real=0;imag=0;}
Complex(doubler){real=r;imag=0;}
Complex(doubler,doublei){real=r;imag=i;}
operatordouble(){returnreal;}
voiddisplay();
private:
doublereal;
doubleimag;
);
voidComplex::display()
',
{cout?"('?real?"/"?imag?")'?endl;}
intmain()
{Complexcl(3,4),c2;
doubledl;
dl=2.5+cl;
cout?"dl="?dl?endI;
c2=Complex(dl);
cout?"c2=";
c2.display();
return0;
)
7:
include<iostream>
usingnamespacestd;
classStudent
{public:
Student(int,char[],char,float);
intget_num(){returnnum;}
char*get_name(){returnname;}
charget_sex(){returnsex;}
voiddisplay()
{cout<<”num:“<<num<<”\nname:“<<name<<"\nsex:"<<sex<<”\nscore:“<<score<<”\n\n”;}
private:
intnum;
charname[20];
charsex;
floatscore;
);
Student::Student(intn,charnam[],chars,floatso)
{num=n;
strcpy(name,nam);
sex=s;
score=so;
classTeacher
{public:
Teacher(){}
Teacher(Student&);
Teacher(intnzcharnam[],charsexjloatpay);
voiddisplay();
private:
intnum;
charname[20];
charsex;
floatpay;
);
Teacher::Ieacher(intn,charnam[],chars,floatp)
{num=n;
strcpy(name,nam);
sex=s;
pay=p;
}
Teacher::Teacher(Student&stud)
{num=stud.get_num();
strcpy(name,stud.get_name());
sex=stud.get_sex();
pay=1500;}
voidTeacher::display()
{cout?"num:"?num?"\nname:,,?name?"\nsex:"?sex?,,\npay:"?pay?"\n\n";}
intmain()
,,
{Teacherteacherl(10001/Li'/'f'z1234.5),teacher2;
,,
Studentstudentl(20010,"Wang",m,89.5);
cout?,,studentl:"?endl;
studentl.display();
teacher2=Teacher(studentl);
cout?"teacher2:"?endl;
teacher2.display();
return0;
)
Aye7t
第五早t
1:
include<iostream>
usingnamespacestd;
classStudent
{public:
voidget_value()
{cin?num?name?sex;}
voiddisplay()
{cout?"num:"?num?endl;
cout?"name:"?name?endl;
cout?"sex:"?sex?endl;}
private:
intnum;
charname[10];
charsex;
);
classStudentl:publicStudent
{public:
voidget_value_l()
{get_value();
cin?age?addr;}
voiddisplay_l()
{cout?*'age:"?age?endl;〃引用派生類的私有成員,正確。
cout?"address:"?addr?endl;}〃引用派生類的私有成員,正確。
private:
intage;
charaddr[3O];
};
intmain()
{Studentlstudl;
studl.get_value_l();
studl.display();
studl.display_l();
return0;
}
2:
ttinclude<iostream>
usingnamespacestd;
classStudent
{public:
voidget_value()
{cin?num?name?sex;}
voiddisplay()
{cout?Hnum:"?num?endl;
cout?"name:"?name?endl;
cout?"sex:"?sex?endl;}
private:
intnum;
charname[10];
charsex;
};
classStudentl:privateStudent
{public:
voidget_value_l()
{get_value();
cin?age?addr;}
voiddisplay_l()
{displayO;
cout?Hage:"?age?endl;〃引用派生類的私有成員,正確。
cout?"address:"?addr?endl;}〃引用派生類的私有成員,正確。
private:
intage;
charaddr[3O];
};
intmain()
{Studentlstudl;
studl.get_value_l();
studl.display_l();
return0;
)
3:
include<iostream>
usingnamespacestd;
classStudent〃聲明基類
{public:〃基類公用成員
voidget_value();
voiddisplay();
protected:〃基類保護成員
intnum;
charname[10];
charsex;
};
voidStudent::get_value()
{cin?num?name?sex;}
voidStudent::display()
{cout?"num:"?num?endl;
cout?"name:"?name?endl;
cout?"sex:"?sex?endl;
)
classStudentl:protectedStudent〃聲明一個保護派生類
{public:
voidget_value_l();
voiddisplayl();
private:
intage;
charaddr[3O];
);
voidStudentl::get_value_l()
{get_value();
cin?age?addr;
)
voidStudentl::displayl()
{cout?"num:"?num?endl;〃引用基類的保護成員
cout?"name:"?name?endl;〃引用基類的保護成員
cout?"sex:"?sex?endl;〃引用基類的保護成員
cout?nage:"?age?endl;〃引用派生類的私有成員
cout?Haddress:"?addr?endl;〃引用派生類的私有成員
)
intmain()
{Studentlstudl;//studl是派生類studentl類的對象
studl.get_value_l();〃調用派生類對象studl的公用成員函數(shù)
studl.displayl();〃調用派生類對象studl的公用成員函數(shù)
return0;
4:解法一
#include<iostream>
usingnamespacestd;
classStudent〃聲明基類
{public:〃基類公用成員
voidget_value();
voiddisplay();
protected:〃基類保護成員
intnum;
charname[10];
charsex;
);
voidStudent::get_value()
{cin?num?name?sex;}
voidStudent::display()
{cout?"num:,'?num?endl;
cout?"name:"?name?endl;
cout?"sex:"?sex?endl;
)
classStudentl:publicStudent〃聲明?個公用派生類
{public:
voidget_value_l();
voiddisplayl();
private:
intage;
charaddr[3O];
};
voidStudentl::get_value_l()
{get_value();
cin?age?addr;
voidStudentl::displayl()
{cout?"num:"?num?endl;〃引用基類的保護成員,合法
cout?"name:"?name?endl;〃引用基類的保護成員,合法
cout?"sex:"?sex?endl;〃引用基類的保護成員,合法
cout?"age:"?age?endl;〃引用派生類的私有成員,合法
cout?"address:"?addr?endl;〃引用派生類的私有成員,合法
intmain()
{Studentlstudl;//studl是派生類studentl類的對象
studl.get_value_l();〃調用派生類對象studl的公用成員函數(shù)
get_value_l
studl.displayl();〃調用派生類對象studl的公用成員函數(shù)displayl
return0;
)
解法二
#include<iostream>
usingnamespacestd;
classStudent〃聲明基類
{public:〃基類公用成員
voidget_value();
voiddisplay();
protected:〃基類保護成員
intnum;
charname[10];
charsex;
};
voidStudent::get_value()
{cin?num?name?sex;}
voidStudent::display()
{cout?"num:"?num?endl;
cout?"name:"?name?endl;
cout?"sex:n?sex?endl;
)
classStudentl:protectedStudent〃聲明一個公用派生類
{public:
voidget_value_l();
voiddisplayl();
private:
intage;
charaddr[3O];
);
voidStudentl::get_value_l()
{cin?age?addr;}
voidStudentl::displayl()
{cout?"age:"?age?endl;
cout?"address:"?addr?endl;
intmain()
{Studentlstudl;
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025農村買地合同樣本
- 2025私營公司工程合同
- 2025年度農村水塘水域環(huán)境保護與承包合同
- 二零二五年度環(huán)保產業(yè)散伙協(xié)議書3篇
- 2025年度公司與自然人共同開發(fā)項目合作協(xié)議3篇
- 2025年企業(yè)法人變更合同審查與合同效力確認服務3篇
- 二零二五年度公司股東內部關于企業(yè)可持續(xù)發(fā)展戰(zhàn)略的協(xié)議書2篇
- 二零二五年度智慧城市運營合作出資協(xié)議模板
- 2025抵押貸款還款合同
- 二零二五年度農村新建住宅不含材料包工協(xié)議
- 瓦楞紙箱工藝流程
- 急性藥物中毒的急救與護理課件
- 臘八國旗下演講稿2篇
- 《故鄉(xiāng)》學習提綱
- 河北省滄州市各縣區(qū)鄉(xiāng)鎮(zhèn)行政村村莊村名居民村民委員會明細
- 中藥材及飲片性狀鑒別1總結課件
- 信息系統(tǒng)及信息安全運維服務項目清單
- 最新大壩固結灌漿生產性試驗大綱
- DB32-T 2948-2016水利工程卷揚式啟閉機檢修技術規(guī)程-(高清現(xiàn)行)
- 公司EHS(安全、環(huán)保、職業(yè)健康)檢查表
- 《模擬電子技術基礎》課程設計-心電圖儀設計與制作
評論
0/150
提交評論