提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
一、问题
二、代码
4-5
代码如下:
4.#include<iostream>
#include<cstring>
using namespace std;
class String
{
char*str;
int len;
public:
String(const char*s=NULL)
{
if(s)
{
len=strlen(s);
str=new char[len+1];
strcpy(str,s);
}
else
{
str=NULL;
len=0;
}
}
String(const String &s)
{
len=s.len;
if(s.str)
{
str=new char[len+1];
strcpy(str,s.str);
}
else str=NULL;
}
~String()
{
if(str) delete [] str;
}
void set(char*s)
{
len=strlen(s);
if(s)
{
delete [] str;
str=new char[len+1];
strcpy(str,s);
}
else
{
delete [] str;
str=NULL;
}
}
void show()
{
if(str) cout<<str<<endl;
else cout<<"该字符串为空。"<<endl;
}
int getlen()
{
return len;
}
void delchar(char c)
{
int i,j;
char*t=new char[len+1];
for(i=j=0;i<len;i++)
{
if(str[i]!=c)
{
t[j]=str[i];
j++;
}
}
t[j]='\0';
len=strlen(t);
set(t);
delete [] t;
}
String operator-(char ch)
{
delchar(ch);
return *this;
}
String&operator=(String&s)
{
set(s.str);
return*this;
}
String&operator+=(String&s)
{
len+=s.len;
char*t=new char[len+1];
for(int i=0;str[i]!='\0';i++)
{
t[i]=str[i];
}
t[i]='\0';
strcat(t,s.str);
set(t);
delete [] t;
return *this;
}
bool operator==(String&s)
{
int i=0;
while(i<len)
if(str[i++]!=s.str[i++]) return false;
return true;
}
friend String operator+(String&,String&);
};
String operator+(String&s1,String&s2)
{
String t;
t.len=s1.len+s2.len;
t.str=new char[t.len+1];
strcpy(t.str,s1.str);
strcat(t.str,s2.str);
return t;
}
int main()
{
String s1("学生学习"),s2("C++程序设计");
String s3(s2),s4("aaabaaa"),s5;
s3.show();
s3.set("pppa");
s3.show();
s4.show();
cout<<"s4长度:"<<s4.getlen()<<endl;
s4.delchar('a');
s4.show();
s4=s4-'b';
cout<<s4.getlen()<<endl;
s5=s1;
s5.show();
s1+=s2;
s1.show();
if(s1==s3) cout<<"相等"<<endl;
else cout<<"不等"<<endl;
s4=s1;
if(s4==s1) cout<<"相等"<<endl;
else cout<<"不等"<<endl;
s2=s1+s4;
s2.show();
return 0;
}
5.#include<iostream>
using namespace std;
class Matrix
{
int*p,rows,cols;
public:
Matrix(int r=0,int c=0)
{
rows=r;
cols=c;
if(r*c) input();
else p=NULL;
}
Matrix(Matrix&b)
{
int i=0;
rows=b.rows;
cols=b.cols;
p=new int[rows*cols+1];
for(i=0;i<(rows*cols);i++)
p[i]=*((b.p)+i);
p[i]='\0';
}
void input()
{
int i=0;
p=new int[rows*cols+1];
for(i=0;i<rows*cols;i++)
cin>>p[i];
p[i]='\0';
}
Matrix operator+(Matrix&b)
{
for(int i=0;i<rows*cols;i++)
p[i]+=b.p[i];
return *this;
}
void operator=(Matrix&b)
{
int i=0;
if(p) delete [] p;
rows=b.rows;
cols=b.cols;
p=new int[rows*cols+1];
for(;i<rows*cols;i++)
p[i]=b.p[i];
p[i]='\0';
}
void show()
{
for(int i=0;i<rows;i++)
{
for(int j=0;j<cols;j++)
{
cout<<p[i*cols+j]<<'\t';
}
cout<<endl;
}
}
~Matrix()
{
delete [] p;
}
};
int main()
{
Matrix A(3,4),C;
Matrix B(A);
A.show();
B.show();
C=A+B;
C.show();
return 0;
}
6
代码如下:
#include<iostream>
using namespace std;
class Shape
{
public:
virtual float Area(void)=0;
virtual void SetData(float,float=0)=0;
};
class Triangle:public Shape
{
float w,h;
public:
Triangle(float ww=0,float hh=0){ w=ww;h=hh;}
float Area(void){ return w*h/2;}
void SetData(float ww,float hh=0){ w=ww;h=hh;}
};
class Rectangle:public Shape
{
float w,h;
public:
Rectangle(float ww=0,float hh=0){ w=ww;h=hh;}
float Area(void){ return w*h;}
void SetData(float ww,float hh=0){ w=ww;h=hh;}
};
class Squre:public Shape
{
float s;
public:
Squre(float ww=0){ s=ww;}
float Area(void){ return s*s;}
void SetData(float ww,float hh=0){ s=ww;}
};
class Circle:public Shape
{
float r;
public:
Circle(float ww=0){ r=ww;}
float Area(void){ return r*r*3.14;}
void SetData(float ww,float hh=0){ r=ww;}
};
float CalcArea(Shape*s)
{
return (s->Area());
}
int main()
{
Triangle A(3,4);
Rectangle B(4,5);
Squre C(7);
Circle D(1);
cout<<CalcArea(&A)<<'\t'<<CalcArea(&B)<<'\t'<<CalcArea(&C)<<'\t'<<CalcArea(&D)<<endl;
return 0;
}
总结
如有帮助,还望点赞