19 统计动物数量
/* */部分是自己写的错误代码
#include <iostream>
using namespace std;
/*
class Animal
{
public:
int number=0;
Animal(){number++;}
~Animal(){number--;}
};
class Dog:public Animal
{
public:
int number=0;
Dog(){number++;}
~Dog(){number--;}
};
class Cat:public Animal
{
public:
int number=0;
Cat(){number++;}
~Cat(){number--;}
}
*/
class Animal
{
public:
static int number;
Animal(){number++;}
virtual~Animal(){number--;}
};
class Dog:public Animal
{
public:
static int number;
Dog(){number++;}
~Dog(){number--;}
};
class Cat:public Animal
{
public:
static int number;
Cat(){number++;}
~Cat(){number--;}
};
int Animal::number=0;int Cat::number=0;int Dog::number=0;
void print() {
cout << Animal::number << " animals in the zoo, " << Dog::number << " of them are dogs, " << Cat::number << " of them are cats" << endl;
}
int main() {
print();
Dog d1, d2;
Cat c1;
print();
Dog* d3 = new Dog();
Animal* c2 = new Cat;
Cat* c3 = new Cat;
print();
delete c3;
delete c2;
delete d3;
print();
system("pause");
}
错误在于类中成员变量初始化,不能采用直接赋值的方法,但是如果在构造函数成员列表里初始化,那么每次调用构造函数都会重置number的值,无法起到计数的作用。因此需要用到静态成员变量,注意静态成员变量的初始化只能在类外进行,因为静态成员属于整个类,而不属于某个对象,如果在类内初始化,会导致每个对象都包含该静态成员。但是静态常量成员可以在类内初始化。关于初始化更详细的可以阅读这篇文章C++初始化类的常量数据成员、静态数据成员、常量静态数据成员
如果Animal的析构函数没有写成虚函数,会发现运行结果与预期不符合,是因为在delete c2的时候只调用了Animal的析构函数,没有调用Cat的析构函数。为什么会出现这样的情况呢?这要从虚函数的作用说起,我们先看下静态绑定和动态绑定。
静态绑定: 在编译时刻,根据指针或引用变量的静态类型来决定成员函数属于哪一个类。
动态绑定: 在运行时刻,根据指针或引用变量实际指向或引用的对象类型(动态类型)来确定成员函数属于哪一个类。而实现动态绑定的方式就是将函数写成虚函数。
C++默认的绑定方式是静态绑定。因此,若Animal的析构函数没有写成虚函数,则直接在编译的时候就确定c2指向的析构函数为Animal的析构函数(c2的静态类型为指向Animal类的指针)。但是在声明virtual后,采用动态绑定,根据c2实际指向Cat类,在delete c2时将调用Cat类的析构函数,而我们知道,执行完派生类的析构函数后,按照c++的内部机制紧接着会去执行基类的析构函数,因此之后将调用Animal的析构函数。
20 全面的MyString
private:
char * p;
public:
MyString()
{
p=new char[1];
p[0]='\0';
}
/*~MyString()
{
if(p)
delete []p;
}*/
MyString(const char * rhs)
{
p=new char[strlen(rhs)+1];
strcpy(p,rhs);
}
MyString(const MyString & mys)
{
p=new char[strlen(mys.p)+1];
strcpy(p,mys.p);
}
MyString & operator=(const MyString & mys)
{
if(p==mys.p)
return *this;
if(p)
delete p;
p=new char[strlen(mys.p)+1];
strcpy(p,mys.p);
return *this;
}
/*
MyString operator+(const MyString & mys) //可以不写这个成员函数,当这个成员函数存在时,调用这个成员函数执行s1+s3等加法操作,不存在时调用后面写的加法友元函数
{
char *temp=new char[strlen(p)+strlen(mys.p)+2] ;
strcpy(temp,p);
strcat(temp,mys.p);
cout<<"调用成员函数执行加法"<<endl;
return MyString(temp);
}*/
/*
void operator+=(const char * ptr)//这是错误写法,执行new会将原来的p覆盖
{
p=new char[strlen(p)+strlen(ptr)+1];
strcat(p,ptr);
}
*/
void operator+=(const char * ptr)
{
char *temp=new char[strlen(p)+strlen(ptr)+1];
strcpy(temp,p);
strcat(temp,ptr);
p=temp;
}
friend MyString operator+(const MyString & a,const MyString & b)
{
char *temp=new char[strlen(a.p)+strlen(b.p)+1];
strcpy(temp,a.p);
strcat(temp,b.p);
//cout<<"调用友元函数执行加法"<<endl;
return MyString(temp);
}
char & operator[](int i)
{
return p[i];
}
friend ostream & operator<<(ostream & os,const MyString & mys)
{
os<<mys.p;
return os;
}
MyString operator()(int m,int n)
{
char * temp=new char[n+1];
for(int i=0;i<n;i++)
{
temp[i]=p[m+i] ;
}
return MyString(temp);
}
bool operator==(const MyString & rhs)
{
if(strcmp(p,rhs.p)==0)
return true;
else
return false;
}
bool operator<(const MyString & rhs)
{
if(strcmp(p,rhs.p)==-1)
return true;
else
return false;
}
bool operator>(const MyString & rhs)
{
if(strcmp(p,rhs.p)==1)
return true;
else
return false;
}
当执行 s4 = "qrst-" + s2时会调用 MyString operator+(const MyString & a,const MyString & b),会以''qrst"为参数生成一个临时对象。
021:继承自string的MyString
#include <cstdlib>
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
class MyString:public string
{
public:
MyString():string(){}
//MyString(const MyString & mys):string(mys){}
MyString(const char *p):string(p){}
MyString(const string & s):string(s){}
MyString operator()(int m,int n)
{
return substr(m,n);
}
};
int main()
{
MyString s1("abcd-"),s2,s3("efgh-"),s4(s1);
MyString SArray[4] = {"big","me","about","take"};
cout << "1. " << s1 << s2 << s3<< s4<< endl;
s4 = s3;
s3 = s1 + s3;
cout << "2. " << s1 << endl;
cout << "3. " << s2 << endl;
cout << "4. " << s3 << endl;
cout << "5. " << s4 << endl;
cout << "6. " << s1[2] << endl;
s2 = s1;
s1 = "ijkl-";
s1[2] = 'A' ;
cout << "7. " << s2 << endl;
cout << "8. " << s1 << endl;
s1 += "mnop";
cout << "9. " << s1 << endl;
s4 = "qrst-" + s2;
cout << "10. " << s4 << endl;
s1 = s2 + s4 + " uvw " + "xyz";
cout << "11. " << s1 << endl;
sort(SArray,SArray+4);
for( int i = 0;i < 4;i ++ )
cout << SArray[i] << endl;
//s1的从下标0开始长度为4的子串
cout << s1(0,4) << endl;
//s1的从下标5开始长度为10的子串
cout << s1(5,10) << endl;
system("pause");
return 0;
}