#include <iostream>
#include <cstring>
using namespace std;
class Book
{
private:
int _id;
char* _name;
float _price;
static int nowid;
public :
Book(int id, char* name, float price)
: _id(id)
, _price(price)
{
int nameLen = strlen(name);
_name = new char[nameLen + 1];
strcpy(_name, name);
}
Book(char* name, float price):Book(nowid,name,price){
nowid++;
}
~Book()
{
delete[] _name;
}
void print(){
cout<<_id<<","<<_name<<","<<_price<<endl;
}
};
int Book::nowid{0};//static在类外初始化
int main(int argc, char **argv)
{
Book *book1=new Book("精通C++", 52.17);
book1->print();
Book *book2=new Book("精通Python", 62.67);
book2->print();
delete book1;
delete book2;
}
下面代码段定义了委托构造函数,委托Book(int id, char* name, float price)完成构造。
1、只能在初始化成员列表中调用被委托构造函数
2、成员变量只能在函数体内进行初始化,或者由被委托构造函数完成
Book(char* name, float price):Book(nowid,name,price){
nowid++;
}
0,精通C++,52.17
1,精通Python,62.67
Hit any key to continue...
const成员函数可返回非const和const值。
被 mutable 修饰的数据成员,可以在 const 成员函数中修改
下面的print函数被const重载后,才可以被book2调用。
#include <iostream>
#include <cstring>
using namespace std;
class Book
{
private:
int _id;
char* _name;
float _price;
static int nowid;
const int flag{0};
mutable int status{0};
public :
Book(int id, char* name, float price)
: _id(id)
, _price(price)
{
int nameLen = strlen(name);
_name = new char[nameLen + 1];
strcpy(_name, name);
}
Book(char* name, float price):Book(nowid,name,price){
nowid++;
}
~Book()
{
delete[] _name;
}
void print(){
cout<<_id<<","<<_name<<","<<_price<<endl;
}
const void print() const{
cout<<_id<<","<<_name<<","<<_price<<endl;
}
const int getNextId() const{
return nowid;
}
const int getFlag() const{
return flag;
}
const void setStatus(int myStatus) const{
status=myStatus;
}
const int getStatus() const{
return status;
}
};
int Book::nowid{0};
int main(int argc, char **argv)
{
Book *book1=new Book("精通C++", 52.17);
book1->print();
cout<<book1->getNextId()<<endl;
book1->setStatus(1);
cout<<book1->getStatus()<<endl;
const Book *book2=new Book("精通Python", 62.67);
book2->print();
cout<<book2->getNextId()<<endl;
cout<<book2->getFlag()<<endl;
book2->setStatus(1);
cout<<book2->getStatus()<<endl;
delete book1;
delete book2;
}
0,精通C++,52.17
1
1
1,精通Python,62.67
2
0
1
Hit any key to continue...
static inline可初始化static成员 ,不用在类外初始化。
#include <iostream>
#include <cstring>
using namespace std;
class Book
{
private:
int _id;
char* _name;
float _price;
static inline int nowid{0};
const int flag{0};
mutable int status{0};
public :
Book(int id, char* name, float price)
: _id(id)
, _price(price)
{
int nameLen = strlen(name);
_name = new char[nameLen + 1];
strcpy(_name, name);
}
Book(char* name, float price):Book(nowid,name,price){
nowid++;
}
~Book()
{
delete[] _name;
}
void print(){
cout<<_id<<","<<_name<<","<<_price<<endl;
}
const void print() const{
cout<<_id<<","<<_name<<","<<_price<<endl;
}
const int getNextId() const{
return nowid;
}
const int getFlag() const{
return flag;
}
const void setStatus(int myStatus) const{
status=myStatus;
}
const int getStatus() const{
return status;
}
};
int main(int argc, char **argv)
{
Book *book1=new Book("精通C++", 52.17);
book1->print();
cout<<book1->getNextId()<<endl;
book1->setStatus(1);
cout<<book1->getStatus()<<endl;
const Book *book2=new Book("精通Python", 62.67);
book2->print();
cout<<book2->getNextId()<<endl;
cout<<book2->getFlag()<<endl;
book2->setStatus(1);
cout<<book2->getStatus()<<endl;
delete book1;
delete book2;
}