设计一个字符串类重载“+=”用c 成员函数重载表示

设计一个字符串类String,要求该类能够完成以下操作(用函数重载完成)_百度知道
设计一个字符串类String,要求该类能够完成以下操作(用函数重载完成)
要求:构造完整的类(构造、析构函数,拷贝构造函数,重载=操作符),并且实现以下功能
String1=String2+String3 (重载+)
String1 += St供氦垛教艹寄讹犀番篓ring2
(重载+=)
(int)String1
(重载类型转换int),将String对象中的所有字符的ASCII相加后返回。
String1 == String2
(重载相等判断,两字符串相等返回真,不等返回假)
cout && String1
(输出符号重载)
cin && String1
(输入符号重载)
提问者采纳
///String.h#ifndef MY_String_H_#define MY_String_H_#include&iostream&using std::using std::class String{
String( const char *cString = && );
// Constructor
String( char ch );
// Constructor
String( const String & str );
// Copy constructor
~String( )
// Destructor
{ delete [ ] }
const String & operator= ( const String & rhs );
const String & operator+=( const String & rhs );
const char *c_str( ) const
// Return C-style String
int length( ) const
// Return String length
{ return strL }
operator[]( int k ) // Accessor operator[]
char & operator[]( int k );
// Mutator
operator[]friend istream & operator&&( istream & in, String & str );
// storage for characters
// length of String (# of characters)
int bufferL
// capacity of buffer};ostream & operator&&( ostream & out, const String & str );
// Outputistream & getline( istream & in, String & str );
// Read lineistream & getline( istream & in, String & str, char delim );
// Read lineString operator+( const String & lhs, const String & rhs );
// Concatenationbool operator==( const String & lhs, const String & rhs );
// Compare ==bool operator!=( const String & lhs, const String & rhs );
// Compare !=bool operator& ( const String & lhs, const String & rhs );
// Compare &bool operator&=( const String & lhs, const String & rhs );
// Compare &=bool operator& ( const String & lhs, const String & rhs );
// Compare &bool operator&=( const String & lhs, const String & rhs );
// Compare &=#endif///String.cpp#include &cstring&#include &cctype&#include &String.h&using std::String::String( const char * cString ){
if( cString == NULL )
cString = &&;
strLength = strlen( cString );
bufferLength = strLength + 1;
buffer = new char[ bufferLength ];
strcpy( buffer, cString );}String::String( const String & str ){
strLength = str.length( );
bufferLength = strLength + 1;
buffer = new char[ bufferLength ];
strcpy( buffer, str.buffer );}String::String( char ch ){
strLength = 1;
bufferLength = strLength + 1;
buffer = new char[ bufferLength ];
buffer[ 0 ] =
buffer[ 1 ] = '\0';}/*const String & String::operator=( const String & rhs ){
if( this != &rhs )
if( bufferLength & rhs.length( ) + 1 )
delete [ ]
bufferLength = rhs.length( ) + 1;
buffer = new char[ bufferLength ];
strLength = rhs.length( );
strcpy( buffer, rhs.buffer );
return *}*/const String& String::operator=(const String& rhs){
if(this==&rhs)
strLength=rhs.length();
bufferLength=strLength+1;
buffer=new char[bufferLength];
strcpy(buffer,rhs.buffer);
return *}/*const String & String::operator+=( const String & rhs ){
if( this == &rhs )
String copy( rhs );
return *this +=
int newLength = length( ) + rhs.length( );
if( newLength &= bufferLength )
bufferLength = 2 * ( newLength + 1 );
char *oldBuffer =
buffer = new char[ bufferLength ];
strcpy( buffer, oldBuffer );
delete [ ] oldB
strcpy( buffer + length( ), rhs.buffer );
strLength = newL
return *d}*/const String& String::operator+=(const String& rhs){
strLength+=rhs.length();
bufferLength=strLength+1;
char* oldBuffer=
buffer=new char[bufferLength];
strcpy(buffer,oldBuffer);
delete []oldB
strcat(buffer,rhs.buffer);
return *}char & String::operator[ ]( int k ){
if( k & 0 || k &= strLength )
cerr&&&StringIndexOutOfBoundsException( k, length( ) )&;
return buffer[ k ];}char String::operator[ ]( int k ) const{
if( k & 0 || k &= strLength )
cerr&&&StringIndexOutOfBoundsException( k, length( ) )&;
return buffer[ k ];}String operator+( const String & lhs, const String & rhs ){
String result =
result +=}ostream & operator&&( ostream & out, const String & str ){
return out && str.c_str( );}/*istream & operator&&( istream & in, String & str ){
char ch[256];
str.strLength=strlen(ch);
str.bufferLength=str.strLength+1;
delete []str.
str.buffer=new char[str.bufferLength];
strcpy(str.buffer,ch);}*/istream & operator&&( istream & in, String & str ){
if( !in.fail( ) )
in.get( ch );
} while( !in.fail( ) && !isspace( ch ) );
if( isspace( ch ) )
// put whitespace back on the stream
in.putback( ch );
}}istream & getline( istream & in, String & str, char delim ){供氦垛教艹寄讹犀番篓
// empty String, will build one char at-a-time
while( in.get( ch ) && ch != delim )
str +=}istream & getline( istream & in, String & str ){
return getline( in, str, '\n' );}bool operator==( const String & lhs, const String & rhs ){
return strcmp( lhs.c_str( ), rhs.c_str( ) ) == 0;}bool operator!=( const String & lhs, const String & rhs ){
return strcmp( lhs.c_str( ), rhs.c_str( ) ) != 0;}bool operator&( const String & lhs, const String & rhs ){
return strcmp( lhs.c_str( ), rhs.c_str( ) ) & 0;}bool operator&=( const String & lhs, const String & rhs ){
return strcmp( lhs.c_str( ), rhs.c_str( ) ) &= 0;}bool operator&( const String & lhs, const String & rhs ){
return strcmp( lhs.c_str( ), rhs.c_str( ) ) & 0;}bool operator&=( const String & lhs, const String & rhs ){
return strcmp( lhs.c_str( ), rhs.c_str( ) ) &= 0;}///Main.cpp#include &String.h&using std::using std::using std::int main( ){
cout&&&enter a string:\n&;
String a = &hello&;
String b = &world&;
c = a + & &+b;
cout && &c is: & && c &&
cout && &c is: & && c.c_str( ) &&
cout && &c is: &;
for( int i = 0; i & c.length( ); i++ )
cout && c[ i ];
return 0;}本程序需要分别编译。如果觉得麻烦,可自己简化。如有疑问,可追加提问,也可以在知道上发私信,我帮你解答。这段时间,尽研究string类的问题,解答你的问题对我也是一个检阅。仅供参考!
提问者评价
来自:求助得到的回答
其他类似问题
为您推荐:
函数重载的相关知识
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁设计一个矩阵类要求矩阵类中重载加赋值主函数定义类对象并调用重载运算符
设计一个矩阵类要求矩阵类中重载加赋值主函数定义类对象并调用重载运算符
09-12-28 &匿名提问
您好这个问题你不该在这里问,不是由我们设计C++的,老外有老外的想法。你这么想知道也可以提供几个选项:1.此运算符有两个参数,左参数为类恰好符合类成员调用操作符的条件,作为成员函数,它充分确保了只能访问类内的成员;如果是友元,需要设置两个参数,这里会产生一些误会,由于C++继承的存在,友元的两个参数调用会有问题出现不清楚的状况,即基类的成员模糊调用的情况,这不利于程序设计,其他的几个类似的操作符也有这种情况,由于需要引用类内的成员,容易混淆细节。2.所谓的只能重载为类成员的操作符有四个=、[]、()、-&,这几个其实都有共同特点,那就是涉及到了类数据成员的引用,虽然还有一个*也是涉及到了引用,但是用ADT设计程序的话,我们更倾向于不使用指针,所以这四个可以说是比较常用的操作符,如果说从类的基本属性也就是数据的封装来看,这种规定其实是这个程序设计方法的体现,友元函数对类的成员调用毕竟本身就有违数据封装的基本思想。3.其实,现代编程还有一个很重要的特点,那就是简便性,这是程序设计工程化的一个方案,我们设计的程序就是需要更容易理解且不容易出错,这种规定想来是为了确保这种编程思想的贯彻而设计的。
请登录后再发表评论!用C++定义一个学生类包括私有数据成员:id(整型),name(字符串)和age(整型). 设计公有类型的函数成员,_百度知道
用C++定义一个学生类包括私有数据成员:id(整型),name(字符串)和age(整型). 设计公有类型的函数成员,
定义一个Student类型的数组students[5];调用之前你所设计的Student类的函数成员输出赋值后的id,name和age并在主函数中定义一个对象用之前你所设计的Student类的函数成员为该学生赋值
g;};voidDog,改为保护继承privateclassAnimal{return0;/intweight:publicAnimal{&#47:将public改成将这里改成public.SetAge(5):;classD&#47:SetWeight(intn){weight=m,age和weight就变成公有成员}intmain(){Dogg=newDog();}:SetAge(intn){age=n;}你可以自己改一下ageweight访问权限和继承方式,改为私有继承,自己编译看看结果;voidSetWeight(intm),改成protected:voidSetAge(intn);g,注释里有;}voidDog:&#47.SetWeight(6):
其他类似问题
为您推荐:
其他1条回答
nameclassManager{}:stringid
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁求助一道关于字符串类重载的C++编程题!!!_百度知道
求助一道关于字符串类重载的C++编程题!!!
class String{private: TCHAR*public: int getLength(){ } String(TCHAR* string){
this-&length=_tcslen(string);
int l=(length+1)*sizeof(TCHAR);
buffer=new TCHAR[l];
ZeroMemory(buffer,l);
if(buffer==NULL){
throw NULL;
_tcscpy(buffer,string);
} String(){
buffer=NULL;
length=0; } String& operator+= (String& str){
int l=(this-&getLength()+str.getLength())+1;
TCHAR* buf=new TCHAR[l*sizeof(TCHAR)];
if(buf==NULL){
throw NULL;
::ZeroMemory(buf,l*sizeof(TCHAR));
_tcscpy(buf,buffer);
_tcscpy((buf+getLength()),str.buffer);
return * } void print(){
::_tprintf(_T(&%s\n&),this-&buffer); }};
其他类似问题
为您推荐:
您可能关注的推广
字符串的相关知识
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁C + +程序设计模拟试卷7(第1-11章)
C + +程序设计模拟试卷7(第1-11章)
1.下列字符列中,可作为C++语言程序自定义标识符是(& )。
&&&& A.switch&&&&&& B.file&&&&&&& C.break&&&&&&&&&& D.do&
2.& 运算符 +、&=、=、% 中,优先级最低的运算符是(&&& )。
&&&& A. +&&&&&& &&&B.&=&&&&&&&&
C.=&&&&&&&&&&&&&
3. &设变量m,n,a,b,c,d均为0,执行(m =
a==b)||(n=c==d)后,m,n的值是( )。
&&&& A.0,0 &&&&&&&B. 0,1& &&&&&&C. 1,0 &&&&&&&&D. 1,1
4. 字符串”vm\x43\\\np\102qu”的长度是(&& )。
A. 8&& &&&&&&&B. 9& &&&&&&&C. 16&&&&&&&& &&D. 17&&&&
5.设有代码“int a = 6;”,则执行了语句“a + = a - = a*a;”后,变量a的值是(& )。
A.0&&&& &&&&&&&B.
-24&& &&&&&&&C. -40&&&& &&&&D. –60
6.&&& void main()
&{ int x=-1;
while(!x);}
下列说法正确的是(&&&&&&
A.是死循环&&&&&&&&&&&&&
B.循环执行两次
C.循环执行一次&&&&&&&&& D.有语法错误
7.下面有关for循环的正确描述是(&&&&& )。
A.for循环只能用于循环次数已经确定的情况
B.for循环是先执行循环体语句,后判断表达式
C.在for循环中,不能用break语句跳出循环体
D.for循环的循环体语句中,可以包含多条语句,但必须用大括号括起来
8.下面程序段(&&&&& )。
&&&& {y=x--;
& &&&&if(!y) {cout&&”x”;
cout&&”#”;}
while(1&=x&=2);
A.将输出##&&&&&&&&&&&&&&&&&
B.将输出##*
C.是死循环&&&&&&&&&&&&&&&&&
D.含有不合法的控制表达式
9.以下正确的说法是(&&&&&& )。
A.用户若需要调用标准函数,调用前必须重新定义
B.用户可以直接调用所有标准库函数
C.用户可以定义和标准库函数重名的函数,但是在使用时调用的是系统库函数
D.用户可以通过文件包含命令将系统库函数包含到用户源文件中,然后调用系统库函数
10.在参数传递过程中,对形参和实参的要求是(
A.函数定义时,形参一直占用存储空间
B.实参可以是常量、变量或表达式
C.形参可以是常量、变量或表达式
D.形参和实参类型和个数都可以不同
11.对数组名作函数的参数,下面描述正确的是(
A.数组名作函数的参数,调用时将实参数组复制给形参数组
B.数组名作函数的参数,主调函数和被调函数共用一段存储单元
C.数组名作参数时,形参定义的数组长度不能省略
D.数组名作参数,不能改变主调函数中的数据
12. 若有语句int
a[10]={0,1,2,3,4,5,6,7,8,9},*p=a;则(&&&&& )不是对a数组元素的正确引用(其中0≤i&10)。
A.p[i]&&    & B.*(*(a+i))&&& C.a[p-a]&&&&&&&&&& D.*(&a[i])
13. 以下程序的输出结果是(&&&&&& )。
&iostream.h&
void main()
{&& char s[]=&&,*p;
&&&&&&& int v1=0,v2=0,v3=0,v4=0;&&& v1&& v2& v3& v4
&&&&&&& for (p=s;*p;p++)&&&&&&&&&&&
2&&& 4&& 3&&& 4
&&&&&&& switch(*p)
&&&&&&&&&&& {&&
&&&&&&&&&&&&&&& case
&&&&&&&&&&&&&&& case
&&&&&&&&&&&&&&& case
&&&&&&&&&&&
default:& v4++;
&&&&&&&&&&& }
&&& cout&&v1&&&,&&&v2&&&,&&&v3&&&,&&&v4&&
A.4,2,1,1& &
B.4,7,5,8&&& C.7,3,2,1&&&&& D.8,8,8,8
14. 下列声明结构体变量错误的是(& )。
A.struct& student&&&&&&&&&&&&
B. struct&
&   { &&&&&&&&&&&&&&&&&&&&&&&
name[16];&&&&&&&&&&&&&&&&&&&&&
char name[16];&
} st1,st2;&&&&&&&&&&&&&&&&&&&&&
student& st1,st2;
C.struct& student&&&&&&&&&&&
D.& struct&
&&& {&&&&&&&&&&&&&&&&
name[16];&&&&&&&&&&&&&&&
char name[16];&
} ;&&&&&&&&&&&&&&&&&&&&&&&&&&&
&struct& st1,st2;&&&&&&&&&&
student st1,st2;
15. 对类的构造函数和析构函数描述正确的是(&&& )。
A.构造函数可以重载,析构函数不能重载
B.构造函数不能重载,析构函数可以重载
C.构造函数可以重载,析构函数也可以重载
D.构造函数不能重载,析构函数也不能重载
16. 下面对于友元函数描述正确的是(& )。
A.友元函数的实现必须在类的内部定义&&&& B.友元函数是类的成员
C.友元函数破坏了类的封装性和隐藏性&&&&& D.友元函数不能访问类的私有成员
17. 派生类的对象对它的基类成员中(&&&& )是可以访问的。
A.公有继承的公有成员&&&&&& B.公有继承的私有成员
&&& C.公有继承的保护成员&&&&&& D.私有继承的公有成员
18. C++类体系中,不能被派生类继承的有(& )。
A.构造函数&&& B.虚函数&&& C.静态成员函数&&&& D.赋值操作函数
19.以下(& )成员函数表示虚函数。
A.virtual int
vf(int);&&&&& B.void vf(int)=0;
&&& C.virtual void vf()=0;&&&&& D.virtual void vf(int) { };
20. 下面对静态数据成员的描述中,正确的是(& )。
A.静态数据成员可以在类体内进行初始化
B.静态数据成员不可以在类体内进行初始化
C.静态数据成员不能受private控制符的作用
D.静态数据成员可以直接用类名调用
21. 继承具有(&& ),即当基类本身也是某一个类派生类时,底层的派生类也会自动继承间接基类的成员。
&A.规律性&&&&&&& B.传递性&&&&&& C.重复性&&& D.多样性
22.若有以下定义,则释放指针所指内存空间的操作是
&&&&&&& float r=news float[10];
A.&&&&&&&&&&&&&&
B.delete& *r;
C.delete []r;&&&&&&&&&&&&&
D.delete& r[];
23.若有以下定义,则对字符串的操作错误的是(&&&&& )。
&&&&&& char& s[10]=Program,t[]=test;
&&&&& A.strcpy(s,t);&&&&&&&&&&&&&&
B.cout&&strlen(s);
&&&&& C.strcat(this,t);&&&&&&&&&
24.在if语句中的表达式是(&&&&&&&&&&&&
A.只能是表达式&& &&&&&&&&&B.只能是关系表达式和逻辑表达式
C.只能是逻辑表达式&&&&&&& D.可以是任意表达式
25.在用关键字class定义的类中,以下叙述正确的是(&&&& )。
A.在类中,不作特别说明的数据成员均为私有类型
B.在类中,不作特别说明的数据成员均为公有类型
C.类成员的定义必须是成员变量定义在前,成员函数定义在后
D.类的成员定义必须放在类定义体内部
二. 填空题 (每空1分,共25分)
1C++&&&&&&&&&&&&&&&&&&&&&
2 int x = 7; float a = 2.5, c = 4.7;
a + (int)(x/3*(int)(c + 2)/2) % 4
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
3.”xyz”C++ &&&&&&&&&&&&&&&&&&&&
4s2s1&&&&&&&&&&&&&&&&&
5 x = (a = 4, b = a++);xab&&&&&
7str&&&&&&&&& &&
= 3, z = --x);_&&&&&&&&
9 #define a(x)& x*x;
cout&&a(4+6)&&
&&&&&&&& &&
10x13 & x & 19C++&&&&& &&&
11static int a[3][4]={{1,2}, {3}, {4,5,6}};a[1][1]&&&&&
12char& u;& int& b;& float& v;& double& d;
u * b + d - v&&&&&&&&&&&&&&
&&& a = b = c = 0; x = 35;
&&&&& &if(!a)x--;else if(b);if(c)x = 3;else x =
14&&&&&&&&&&
15C++&&&&&&& &&&&&&&&
18operatorC++C++&&&&&&
19_______________
20C++&&&&&&&&&& &&&&&&&&&&&&&&&&
21&&&&&&&&&&& &&&&&&&&&&&&&&
1. #include&iostream.h&
void main(
&for(I=1;I&=5;I++)
switch(I%2)
{case 0:I++; cout&&“#”;&&&&
&case 1:I+=2; cout&&“*”;
&default: cout&&“\n”;}}
2.& #include &iostream.h&
{&& int n=10;
&&& cout&&n&&
&&& fun();
3. #include
&iostream.h&
int& fun(int n);
void main(& )
&& for(i=0;i&3;i++)
&&&&&&& & { cout&&fun(i)&&}&&&&
int& fun(int n)
{&&&& static&& a=1;&&&&&&&&&&&
&&&&&& int c=10;&&&&&&&&&&&&&
&&&&& a=a+100;
&&&&& b=b+100;
&&&&& c=c+100;
&&&&& return a+b+c;
4.& #include &iostream.h&
gcd(int m,int n)
{&& if(m%n==0)&& return& n;
&&&&&& else
return& gcd(n,m%n);&&&&&&&&
{&& &&&&int
x=40,y=116;&&&&&&&&&&&&&&&&&&
& &&&&&&&&&&cout&&gcd(y,x)&&&&&&&&&&&&&&&
}&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
5. #include &iostream.h&
&&&&&& int x,y;
&& public:
Point(){x=1;y=1;}&
&& & Point(int tx,int ty)
&& & {x=y=}
~Point(){cout&&&Point
&&&x&&','&&y&&& is
deleted.&&& }
&& & Point obj2(10,20); }
6 #include&iostream.h&
&&&&&& & count()
&&&&&& &{n++;}
&&&&&& &void show()
&&&&&& & {cout&&n&&}
&&&&&& & ~count()
&&&&&& & {cout&&n&&n--;
count::n=0;
{& count& a;
& &&a.show();
& &{count b[4];
&&& b[3].show();
&四.改正错误(每题有二处错误,在错误语句或表达式画线,并写出正确语句。)(每题 4分,共16分)
1.下列程序求字符串长度。
#include&iostream.h&
int fun(char s)&&&&&&&&&&&&&&&
{ int i=0;
while(s[i]!='\n')&&&&&&&
void main()
{char s[80];
cin.getline(s,80);&&&
//读一行字符
cout&&&length:&&&fun(s)&&}
2.下列程序功能是求x的y次方(缺省是x的2次方)
& #include&iostream.h&
double fun(double x,double y)&&&
double s=1;
for(i=1;i&y;i++)&&&&
&&&&&& &s=s*x;
return& s;
void main()
{ double x=2.5,y=3;
&cout&&&pow(2.5,2):&&&fun(x)&&
cout&&&pow(2.5,3):&&&fun(x,y)&&
3#include&iostream.h&
class Point
int x,y;&&&&&&&&
void init(int a,int b) &&&&&
{ x=a;y=b;}
void show()
&&& &&&&&&{ cout&&&x=&
&&&x&&&&&& y=&&&y&&}
void main()
{ Point& a(24,50);
&& &&&&&&a.show();
4#include &iostream.h&
class Amplifier{
&& float invol,
&& Amplifier(float vin,float vout)
& &&&{invol=outvol=}
&&&&& float gain();
&& Amplifier::float
gain()&&&&&
{ return outvol/}
void main()
{ &Amplifier amp(5.0,10.0);
&cout&&&\n\nThe gain is
=&&&&gain()&&&&&&&&
五.程序填空(每空2分,共10分)
&1.有一个一维数组,存放10个职工的年龄,别写两个函数求职工的最大年龄和最小年龄。
#include &iostream.h&
void max_age(int arr[& ]);
&&&&&&&&&&&&&&&&&
int& max ,&&&&&&&&&
void main(& )
array[10];
&&& cout&&&input
10 data:&;
&&& for(i=0;i&10;i++)
&&& {& cin&&array[i];}
&&& max_age(array);
&&& min_age(array);
&&& cout&&&max_age
is :&&&max&&&
&&& cout&&&min_age
is :&&&min&&&
void max_age(int arr[& ])
&&& max=arr[0];&&&&&&&&& //max
&&&&&& &&& for(j=1;j&10;j++)
&&&&&&&&&&&&& & if (&&&&&&&& )
&&&&&&&&&&&&&&&&&&&& && max=arr[j];&& //max
void min_age(int arr[& ])
&&& min=arr[0];&&&&&&&&& //min
for(k=1;k&10;k++)
&&&&&&&&&&&&& & if (min&arr[k])
&&&&&&&&&&&&&&&&&&&& &&&&&&&&&& ;&&& //min
2.下列程序将0——100之间的整数写入D盘上的write1.dat文件。
& iostream.h &
& stdlib.h &
& fstream.h &
void main ( )
{& ofstream& file1&& ;
&& file1.open ( &&&&&&&&&&&& && ) ;&
&& if& ( ! file1 )&&&
&&&& { cerr &&
&\n D:\\write1.dat not open & &&
&&&&&& exit (-1)
&& for (int k=0 ; k&=100 ; k++ )
&&&&&& file1
&& k && && &
&&&&&&&&&&&&&;
《C++程序设计》上机考试模拟(50分)
=1-1/3+1/5-1/7+…0.00000115
2()(cin.getline(
& 1.main() &&2.4.5&& 3.x&z || y&z&&& 4.strcay(s1,s2)&&&&& 5.4,5,4
& 6. x&z || y&z&&& 7.strlen(str)&& 8.& 2,2,2 &&&9. 34 &&10. 11&x &&x&19
& 11. 0&& 12. double&&& 13. 4&& 14. 对对象进行初始化 &&15。单一继承,多重继承
&16.对象& 17.基类&&&& 18. void &&19.派生类,基类&
20.字符文件,二进制文件& 21.保护,成员
三.看程序写结果:
Point 10,20 is deleted.
Point 1,1 is deleted.
四.改正错误(每题有二处错误,在错误语句或表达式画线,并写出正确语句。)(每题
4分,共16分)
1.下列程序求字符串长度。
#include&iostream.h&
int fun(char s)&&&&&&&&&&&&&&&
//*s or s[]
{ int i=0;
while(s[i]!='\n')&&&&&&&&& //\0
void main()
{char s[80];
cin.getline(s,80);&&&
//读一行字符
cout&&&length:&&&fun(s)&&}
2.下列程序功能是求x的y次方(缺省是x的2次方)
& #include&iostream.h&
double fun(double x,double y)&& // fun(double x,double y=2)
double s=1;
for(i=1;i&y;i++)&&& //(i=0;i&y;i++)&& or (i=1;i&=y;i++)
&&&&&& &s=s*x;
return& s;
void main()
{ double x=2.5,y=3;
&cout&&&pow(2.5,2):&&&fun(x)&&
cout&&&pow(2.5,3):&&&fun(x,y)&&
3#include&iostream.h&
class Point
int x,y;&&&&&&&&
public:&&&&&&&&&&&&&&&
void init(int a,int b) &&&&//&
{ x=a;y=b;}
void show()
& &&&&&&&&{ cout&&&x=& &&&x&&&&&&
void main()
{ Point& a(24,50);
&& &&&&&&a.show();&&
4#include &iostream.h&
class Amplifier{
&& float invol,
&& Amplifier(float vin,float vout)
& &&&{invol=outvol=}
&&&&& float gain();
&& Amplifier::float
gain()& &&&&&&&&&&&&&&&&&&&&&//
float&& Amplifier:: gain()&
{ return outvol/
void main()
{ &Amplifier amp(5.0,10.0);
&cout&&&\n\nThe gain is =&&&&gain()&&&&&&&& //amp.
五.程序填空(每空2分,共10分)
&1. (1)void min_age(int arr[ ]);
&&&&&& (2)
max&arr[j]
&&& (3) min=arr[k] ;&&&
2.(1)&d:\\write.dat&
&& &(2) file1.
《C++程序设计》上机考试模拟(50分)
=1-1/3+1/5-1/7+…0.00000115
#include&iostream.h&
#include&math.h&
const double
eps=0.000001;
void main()
pi=0.0,temp,s=1;
& int i=1;
& temp=1.0;
& while(fabs(temp)&=eps)
&&& i=i+2;&&& s=-s;
&&& temp=s/i;
&cout&&&\nPI=&&&pi*4&&
2()(cin.getline(
#include&iostream.h&
#include&string.h&
void main()
{ char ch[81];
& int i,flag=0;
& cout&&&\n&;
& cin.getline(ch,81);
& while(ch[i]!='\0')
& {while(ch[i]!='\0'&& ch[i]=='
') {i++;flag=0;}
&& while(ch[i]!='\0'&&
ch[i]!=' ')
&&& if(ch[i]&='a' &&
ch[i]&='z' && flag==0)
{ch[i]=ch[i]-32; flag=1;}
& cout&&ch&&
&iostream.h&
#define PI
#include&math.h&
class base&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& &&& //B0
&& public:&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& //
&&& virtual void display( )= 0;&&&&&&&&&& //
circle:public base&&&&&&&&&&&&&&&&&&&&&&&&&&&& //
{& protected:
&&&& double r,s;
&&&& circle(double x=0)
&&&& {& r=x;}
&&& void display( )
&&&& {cout&&&&&&r*r*PI&&
cfx:public& base&&&&&&&&&&&&&&&&&&&&&&&&&&&& //
{ double a,b,s;
&&&& cfx(double x=0,double
y=0){a=x;b=y;}
&&& void display( )
&&&& {s=a*b;
&&&& cout&&&&&&s&&}
tx:public& base&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& //
{ double a,b,h,s;
&&&& tx(double a1=0,double
b1=0,double h1=0)
&&&& {a=a1;b=b1;h=h1;}
&&& void display( )
&&&& {s=(a+b)*h/2;
&&&& cout&&&&&&s&&
void fun(base
*ptr) &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& //
&&&& ptr-&display( );
void main( )&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& //
{base& *p;&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& &&& &&&&&& //
circle c1(10);&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& &&& //
cfx&&& c2(9,10);&&&&&&&&&&&&&&&&&&&&&&&&&&&& //
tx&&&& c3(6,10,5);&&&&&
fun(p);&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
fun(p);&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

我要回帖

更多关于 c 成员函数重载 的文章

 

随机推荐