/*
18.构造函数初始化列表 : 可以指定当前对象成员变量的初始化方式
CDate信息 是 CGoods商品信息的一部分 a part of ... 组合关系(继承)
*/
class CDate
{
public:
CDate(int y, int m ,int d)//自定义了一个构造函数,编译器就不会默认生成构造函数
{
_year = y;
_month = m;
_day = d;
}
void show()
{
cout << _year << "/" << _month << "/" << _day << endl;
}
private:
int _year;
int _month;
int _day;
};
class CGoods
{
public:
CGoods(const char * n, double p, int a,int y,int m, int d)
:_date(y,m,d)
,_amount(a)
,_price(p)// #1 构造函数的初始化列表
{
// #2 当前类类型构造函数体
strcpy(_name, n);
//_price = p;
//_amount = a;
}
void show()
{
cout << "name:" << _name << endl;
cout << "price:" << _price << endl;
cout << "amount:" << _amount << endl;
_date.show();
}
private:
char _name[20];
double _price;
int _amount;
CDate _date;//成员对象
};
int main()
{
CGoods g1("苹果",9.8,3,2019,3,8);
g1.show();
return 0;
}
class Test
{
public:
Test(int data = 10) :mb(data), ma(mb){}
void show()
{
cout << "ma:" << ma << "mb:" << mb << endl;
}
private:
//成员变量的初始化和它们定义的顺序有关,和构造函数列表中出现的先后顺序无关;
int ma;
int mb;
};
int main()
{
Test t1;
t1.show();
//ma:-858993460mb:10
return 0;
}
/*
19.掌握成员方法和区别
普通的成员方法 =》编译器会添加一个this形参变量
1.属于类的作用域
2.调用该方法时,需要依赖一个对象(常对象是无法调用的)
3.可以任意访问对象的私有成员变量 public,private
static静态成员方法 =》不会生成this形惨
1.属于类的作用域
2.用类名作用域来调用方法
3.可以任意访问对象的私有成员,仅限于依赖对象的成员(只能调用其他static静态成员)
const常成员方法 =》const CGoods *this
1.属于类的作用域
2.调用依赖一个对象,普通对象或者常对象都可以
3.可以任意访问对象的私有成员,但是只能读,不可写
*/
class CDate{
public:
CDate(int y, int m, int d)//自定义了一个构造函数,编译器就不会默认生成构造函数
{
_year = y;
_month = m;
_day = d;
}
void show()const
{
cout << _year << "/" << _month << "/" << _day << endl;
}
private:
int _year;
int _month;
int _day;
};
class CGoods
{
public:
CGoods(const char * n, double p, int a, int y, int m, int d)
:_date(y, m, d)
, _amount(a)
, _price(p)// #1 构造函数的初始化列表
{
// #2 当前类类型构造函数体
strcpy(_name, n);
_count++;//记录产生新对象的数量
}
//普通成员方法
void show()//打印商品的私有信息 CGoods *this
{
cout << "name:" << _name << endl;
cout << "price:" << _price << endl;
cout << "amount:" << _amount << endl;
_date.show();
}
//常成员方法:只要是只读操作的成员方法,一律实现成const常成员方法
void show()const//const CGoods *this
{
cout << "name:" << _name << endl;
cout << "price:" << _price << endl;
cout << "amount:" << _amount << endl;
_date.show();
}
//静态成员方法 没有this指针的
static void showCount()//打印的是所有商品共享的信息
{
cout << "所有商品种类数量是:" <<_count << endl;
//cout<<"name:"<<this->_name<<endl;//err
}
private:
char _name[20];
double _price;
int _amount;
CDate _date;//成员对象 1.分配内存 2.调用构造函数
//int _count;//用来记录商品对象的总数量
static int _count;//不属于对象,而是属于类级别的 声明
};
//static 成员变量一定要在类外进行定义并初始化
int CGoods::_count = 0;
int main()
{
CGoods g1("苹果1", 9.8, 3, 2019, 3, 8);
g1.show();
CGoods g2("苹果2", 9.8, 3, 2019, 3, 8);
g2.show();
CGoods g3("苹果3", 9.8, 3, 2019, 3, 8);
g3.show();
CGoods g4("苹果4", 9.8, 3, 2019, 3, 8);
g4.show();
//g2.showCount();//4
CGoods::showCount();
const CGoods g5("非卖品", 6.9, 4, 2022, 4, 6);
g5.show();
return 0;
}
#endif
/*
20.指向类成员(成员变量和成员方法)的指针
*/
class Test
{
public:
void func(){ cout << "call Test::func" << endl;}
static void static_func(){ cout << "Test::static_func" << endl; }
int ma;
static int mb;
private:
};
int Test::mb = 0;
int main()
{
/*无法从“int”转换为“int Test::*
int Test::*p = &Test::ma;
*p = 20;*/
Test t1;
Test *t2 = new Test();
#if 0
int Test::*p = &Test::ma;
t1.*p = 20;
cout << t1.*p << endl;//20
t2->*p = 30;
cout << t2->*p << endl;//30
int *p1 = &Test::mb;
*p1 = 40;
cout << *p1 << endl;//40
#endif
//指向成员方法的指针
/*无法从“void(__thiscall Test::*)(void)”转换为“void(__cdecl *)(void)
void(*pfunc)() = &Test::func;
(*pfunc)();*/
void(Test::*pfunc)() = &Test::func;
(t1.*pfunc)();
(t2->*pfunc)();
//如何定义函数指针指向类的static成员方法呢?
return 0;
}