补全前导0
setw(2)<<setfill('0')
printf("%02d",a);
保留两位小数
cout<<fixed<<setprecision(2);
printf("%.2f",a);
cin>>matrixA[i][j]/cout<
int* operator[](int id){
return data[id];
}
cout<
int operator()(int x,int y){
return data[x][y];
}
delete数组前判断是否为空
~CArray(){
if(data!=NULL) {
for (int i = 0; i < n; i++)
delete[]data[i];
delete[]data;
}
}
重载=(注意判断数组是否需要delete)
CArray& operator=(const CArray &r){
if(data!=NULL){
for(int i=0;i<n;i++){
delete []data[i];
}
delete []data;
}
n=r.n;
m=r.m;
data=new int*[n];
for(int i=0;i<n;i++){
data[i]=new int [m];
}
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
data[i][j]=r.data[i][j];
}
}
return *this;
}
举例:
#include "cstring"
#include "cstdio"
#include "iostream"
#include "iomanip"
#include "cstdlib"
using namespace std;
class CDate{
private:
int y,m,d;
public:
CDate(){
y=m=d=0;
}
CDate(int qwe){
d=qwe%100; qwe/=100;
m=qwe%100; qwe/=100;
y=qwe;
}
operator int(){
return y*10000+m*100+d;
}
void print(){
cout<<y<<"年"<<setw(2)<<setfill('0')<<m<<"月"<<setw(2)<<setfill('0')<<d<<"日"<<endl;
}
};
int main(){
// freopen("123.in","r",stdin);
int t,t1,t2;
CDate C1,C2;
cin>>t;
while(t--){
cin>>t1>>t2;
C1=t1;
C2=t2;
((C1>C2)?C1:C2).print();
}
return 0;
}
写函数模板的时候一定要注意数据类型!!
输入一串带有空格的char[ ]
(eg. i love China)
char s1[100];
cin.getline(s1,100);
string a,b,c;
getline(cin,a);
getline(cin,b);
getline(cin,c);
char *aa,*bb,*cc;
aa=(char*)a.c_str();
bb=(char*)b.c_str();
cc=(char*)c.c_str();
友元重载运算符
//1.实现str类;
//2.编写main函数,初始化三个str对象A、B、C,然后用这三个对象去测试重载的运算符。如果A>B,则输出A
//的字符串;否则输出B的字符串。如果A<C,则输出A的字符串;否则输出C的字符串。如果B==C,则输出B的
//字符串;否则输出C的字符串。
#include "bits/stdc++.h"
using namespace std;
const int maxn=100;
class str{
char *p;
public:
str(){}
str(char *s){
p=new char[strlen(s)];
strcpy(p,s);
}
void show(){
cout<<p<<endl;
}
friend bool operator>(str &,str &);
friend bool operator<(str &,str &);
friend bool operator==(str &,str &);
};
bool operator>(str &x,str &y){
if(strcmp(x.p,y.p)>0){
return true;
}
return false;
}
bool operator<(str &x,str &y){
if(strcmp(x.p,y.p)<0){
return true;
}
return false;
}
bool operator==(str &x,str &y){
if(strcmp(x.p,y.p)==0){
return true;
}
return false;
}
int main(){
// freopen("123.in","r",stdin);
char s1[maxn],s2[maxn],s3[maxn];
cin.getline(s1,maxn);
cin.getline(s2,maxn);
cin.getline(s3,maxn);
str A(s1),B(s2),C(s3);
if(A>B) A.show();
else B.show();
if(A<C) A.show();
else C.show();
if(B==C) B.show();
else C.show();
return 0;
}
虚函数与多态
多重继承
CMotocycle me(a,b,c,d,e);
me.CVehicle::display();
me.CBicycle::display();
me.CMotocar::display();
me.display();
判断一个时间是否早于另外一个时间
int xx[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
class CDate{
private:
int y,m,d;
public:
CDate(int _y,int _m,int _d){
y=_y;m=_m;d=_d;
}
CDate(CDate &rp){
y=rp.y;
m=rp.m;
d=rp.d;
}
int find(CDate &rp){//init前:1
if(y<rp.y) return 1;
if(y==rp.y && m<rp.m) return 1;
if(y==rp.y && m==rp.m && d<rp.d) return 1;
return 0;
}
int run(int yy){
if(yy%400==0 || (y%4==0 && y%100!=0)){
return 1;
}
return 0;
}
int getday(CDate &rp){
int sum1=0,sum2=0;
if(y==rp.y){
for(int i=1;i<m;i++){
sum1+=xx[i];
if(i==2){
if(run(y)){
sum1++;
}
}
}
sum1+=d;
for(int i=1;i<rp.m;i++){
sum2+=xx[i];
if(i==2){
if(run(rp.y)){
sum1++;
}
}
}
sum2+=rp.d;
}
return sum1-sum2;
}
virtual ~CDate(){};
};
考前打的模板
运算符重载
一、算术运算符
举例:加法运算符“+”(二元运算)
成员函数方式
CCC operator+(CCC &r){
int _a,_b;
_a=a+r.a;
_b=b+r.b;
CCC ans(a,b);
return ans;
}
友元函数方式
friend CCC operator+(CCC &,CCC &);
CCC operator+(CCC &r1,CCC &r2){
int _a,_b;
_a=r1.a+r2.a;
_b=r1.b+r2.b;
CCC ans(_a,_b);
return ans;
}
二、关系运算符
举例:“>“
友元函数方式
friend bool operator>(str &,str &);
bool operator>(str &x,str &y){
if(strcmp(x.p,y.p)>0){
return true;
}
return false;
}
三、增量运算符
举例:自增
成员函数方式
CCC &operator++(){//++res
value++;
return *this;
}
CCC operator++(int){//res++
CCC tmp(*this);
value++;
return tmp;
}
友元函数方式
friend CCC & operator++(CCC &);
friend CCC operator++(CCC &,int);
CCC &operator++(CCC &a){//++res
a.value++;
return a;
}
CCC operator++(CCC &a,int){//res++
CCC tmp(a);
a.value++;
return tmp;
}
四、特殊运算符
【】的重载
class CCC{
protected:
int *vector;
int size;
public:
CCC(int len){
vector=new int[len];
size=len;
}
~CCC(){
delete []vector;
};
int &operator[](int index){
return vector[index];
}
};
cin>>matrixA[i][j]/cout<
int* operator[](int id){
return data[id];
}
cout<
int operator()(int x,int y){
return data[x][y];
}
五、输入输出
输入重载
友元函数方式
friend istream &operator >>(istream &,CCC &);
istream &operator<<(istream &in,CCC &r){
in>>r.a>>r.b;
return in;
}
输出重载
友元函数方式
friend ostream &operator<<(ostream &,CCC &);
ostream &operator<<(ostream &out,CCC &r){
out<<r.a<<r.b<<endl;
return out;
}
成员函数方式
六、强制类型转换重载
举例:类转化为double
class RMB{
protected:
int y,j,f;
public:
RMB(double v=0.0){
y=int(v);
j=int((v-y)*100.0+0.5)/10;
f=int((v-y)*100.0+0.5)%10;
}
operator double (){
double val=y;
val+=j/10.0;
val+=f/100.0;
return val;
}
};
int main(){
RMB me=1.23;
RMB you=4.56;
RMB sum;
sum=me+you;
return 0;
}
整数转换为类对象
#include "bits/stdc++.h"
using namespace std;
class CC{
int a,b;
public:
CC(){}
CC(int tmp){
a=tmp/10;
b=tmp%10;
}
void print(){
cout<<a<<" "<<b<<endl;
}
};
int main(){
CC me(456);
me.print();
return 0;
}
函数模板
【类型转换+输出重载+函数模板+CDate类作为函数模板参考】
#include "bits/stdc++.h"
using namespace std;
class CDate{
protected:
int y,m,d;
public:
CDate(int res=0){
y=res/10000;
m=(res/100)%100;
d=res%100;
}
operator int(){
return y*10000+m*100+d;
}
friend ostream &operator<<(ostream &out,CDate &r);
};
ostream &operator<<(ostream &out,CDate &r){
out<<r.y<<"-"
<<setw(2)<<setfill('0')<<r.m<<"-"
<<setw(2)<<setfill('0')<<r.d;
return out;
}
template<class T>
T Tmax(T x,T y){
return (x>y?x:y);
}
int main(){
int i1=3,i2=5;
double d1=3.3,d2=5.5;
char c1='a',c2='A';
CDate CD1=20170601,CD2=20170531;
cout<<Tmax(i1,i2)<<endl;
cout<<Tmax(d1,d2)<<endl;
cout<<Tmax(c1,c2)<<endl;
cout<<Tmax(CD1,CD2)<<endl;
return 0;
}
类模板
【PS:注意题目有无要求函数要在类外实现】
【类模板里面调用函数模板】
#include "bits/stdc++.h"
using namespace std;
template<class T>
T mmax(T a,T b){
return a>b?a:b;
}
//模板特化
template<>
string mmax(string s1,string s2){
if(s1.compare(s2)>0){
return s1;
}
return s2;
}
//类模板里面调用函数模板
template<class T>
class Vec3{
T data[3];
public:
Vec3();
T Vmax();
};
template<class T>
Vec3<T>::Vec3() {
cin>>data[0]>>data[1]>>data[2];
}
template<class T>
T Vec3<T>::Vmax() {
T tt;
tt= mmax(data[0],data[1]);
tt= mmax(tt,data[2]);
return tt;
}
int main(){
// freopen("123.in","r",stdin);
Vec3<string> sv;
cout<<sv.Vmax()<<endl;
Vec3<int> iv;
cout<<iv.Vmax()<<endl;
Vec3<float> fv;
cout<<fv.Vmax()<<endl;
return 0;
}