虚函数与继承:
#include<bits/stdc++.h>
using namespace std;
class Graph{
int x;
public:
Graph(int a=0):x(a){}
Graph(Graph& a):x(a.x){}
virtual ~Graph(){}
// virtual double getArea(){}
virtual double getArea()=0; ///虚函数 -> 抽象函数(不能定义对象)
};
class Circle:public Graph{
int r;
public:
Circle(int a):r(a){}
Circle(int a,int b):Graph(a),r(b){}
Circle(Circle& a):Graph(a),r(a.r){}
double getArea(){return 3.14*r*r;}
};
class Rect:public Graph{
int d,l;
public:
Rect(int a,int b):d(a),l(b){}
Rect(int a,int b,int c):Graph(a),d(b),l(c){}
Rect(Rect& a):Graph(a),d(a.d),l(a.l){}
double getArea(){return d*l;}
};
int main()
{
Graph *p;
int a,b;
char ch;
cin>>ch;
if(ch=='r'){
cin>>a>>b;
p=new Rect(a,b);
cout<<p->getArea()<<endl;
}
if(ch=='c'){
cin>>a;
p=new Circle(a);
cout<<p->getArea()<<endl;
}
}
静态与常量:
#include <bits/stdc++.h>
using namespace std;
class data{
private:
static int cnt;
const int mem;
int a,b;
public:
data(int x=0,int y=0):a(x),b(y){cout<<"in\n";} ///构造
data(data x):a(x.a),b(x.b){cout<<"copy\n";}; ///拷贝
~data(){cout<<"out\n";} ///析构
data(int x=0):mem(x){} ///常量成员 赋值 static int getcnt(){return cnt;} ///静态成员 调用};int data::cnt=0; ///静态成员 赋值int main(){ cout<<data::getcnt(); ///静态成员 调用 data d1(1,2); data d2(d1);}
数组及其运算符:
#include <iostream>
#include <cstdio>
using namespace std;
class IntArray{
int *mem;
int len;
public:
IntArray(){len=0;mem=NULL;}
IntArray(int *a,int l){
len=l;
mem=new int[len];
for(int i=0;i<len;i++)mem[i]=a[i];
}
IntArray(IntArray &a){
len=a.len;
mem=new int[len];
for(int i=0;i<len;i++)mem[i]=a.mem[i];
}
bool operator>(IntArray &a){
for(int i=0;i<len&&i<a.len;i++){
if(mem[i]>a.mem[i])return true;
if(mem[i]<a.mem[i])return false;
}
if(len>a.len)return true;
else return false;
}
IntArray& operator+(IntArray &a){
IntArray tmp;
tmp.len=max(len,a.len);
tmp.mem=new int[tmp.len];
for(int i=0;i<len;i++)tmp.mem[i]=mem[i];
for(int i=0;i<a.len;i++)tmp.mem[i]+=a.mem[i];
return tmp;
}
IntArray& operator=(IntArray &a){
if(len)delete []mem;
len=a.len;
if(len){
mem=new int[len];
for(int i=0;i<len;i++)mem[i]=a.mem[i];
}
else mem=NULL;
return *this;
}
IntArray operator++(int){ ///后置
IntArray tmp(*this);
for(int i=0;i<len;i++)++mem[i];
return tmp;
}
IntArray& operator++(){ ///前置
for(int i=0;i<len;i++)++mem[i];
return *this;
}
IntArray operator--(int){ ///后置
IntArray tmp(*this);
for(int i=0;i<len;i++)--mem[i];
return tmp;
}
IntArray& operator--(){ ///前置
for(int i=0;i<len;i++)--mem[i];
return *this;
}
void show(){
for(int i=0;i<len;i++)cout<<mem[i]<<' ';
cout<<endl;
}
friend istream& operator>>(istream &is,IntArray &a);
friend ostream& operator<<(ostream &os,IntArray &a);
};
istream& operator>>(istream &is,IntArray &a){
is>>a.len;
a.mem=new int[a.len];
for(int i=0;i<a.len;i++)is>>a.mem[i];
return is;
}
ostream& operator<<(ostream &os,IntArray &a){
for(int i=0;i<a.len;i++)os<<a.mem[i]<<' ';
os<<endl;
return os;
}
int main()
{
int s1[5]={1,2,3,4,5};
int s2[5]={1,2,3,4};
IntArray a(s1,5),b(s2,4),c;
a.show();
b.show();
c=a+b;
c.show();
--a;
a.show();
if(a>b)cout<<"yes\n";
else cout<<"no\n";
}
字符串及其运算符:
#include<bits/stdc++.h>
using namespace std;
class Mystring{
char* s;
public:
Mystring(){s=NULL;}
Mystring(char* x){
s=new char[strlen(x)+1];
strcpy(s,x); ///深拷贝
}
// ~Mystring(){if(s!=NULL)delete []s;}
int length(){
if(s!=NULL)return strlen(s);
else return 0;
}
void show(){
if(s!=NULL){cout<<strlen(s)<<s;}
cout<<endl;
}
Mystring& operator=(Mystring another){
if(another.s==NULL)return *this;
if(s!=NULL)delete []s;
s=new char[strlen(another.s)+1];
strcpy(s,another.s);
return *this;
}
Mystring operator+(Mystring& another){
if(s==NULL)return another;
if(another.s==NULL)return *this;
Mystring tmp;
tmp.s=new char[strlen(s)+strlen(another.s)+1];
strcpy(tmp.s,s);
// cout<<tmp.s<<endl;
strcat(tmp.s,another.s);
cout<<tmp.s<<endl;
return tmp;
}
Mystring& operator+=(Mystring &another){
if(another.s==NULL)return *this;
if(s==NULL){
s=new char[strlen(another.s)+1];
strcpy(s,another.s);
return *this;
}
char *t=new char[strlen(s)+strlen(another.s)+1];
strcpy(t,s);
strcat(t,another.s);
s=new char[strlen(t)+1];
strcpy(s,t);
return *this;
}
};
int main()
{
Mystring a("abc"),b("efg"),c;
a.show();b.show();
c=a+b;
c.show();
}
指针:
#include <bits/stdc++.h>
using namespace std;
class A{
int a;
public:
A(int x=0):a(x){cout<<a<<" creat\n";}
~A(){cout<<a<<" erase\n";}
void sett(int x=0){a=x;}
void pri()const{cout<<a<<endl;}
};
int main(){
A const *t1=new A(1);///常量指针
t1->pri();
t1=new A(2);
t1->pri();
// t1->sett(3);
// t1->pri();
delete t1;
cout<<"------------\n";
A *const t2=new A(4);///指针常量
t2->pri();
// t2=new A(4);
// t2->pri();
t2->sett(5);
t2->pri();
delete t2;
cout<<"------------\n";
const A* const t3=new A(6);///指向常量的指针常量
t3->pri();
// t3=new A(7);
// t3->pri();
// t3->sett(8);
// t3->pri();
delete t3;
}