头文件:
#pragma once
#include<iostream>
#include<string>
using namespace std;
class book
{
public:
void initData(string Xname, int Xprice, int Xnum);
void print();
string& getName()//第二种赋值方法
{
return m_name;
}
int& getNum()
{
return m_num;
}
int& getPrice()
{
return m_price;
}
protected:
string m_name="三国";
int m_price=20;
int m_num=201;
private:
};
源文件:
?
#include "book.h"
void book::print()
{
cout << m_name << "\t" << m_num << "\t" << m_price << endl;
}
void book::initData(string Xname, int Xprice, int Xnum)
{
m_name = Xname;
m_num = Xnum;
m_price = Xprice;
}
int main()
{
book BOOK;
BOOK.print();
BOOK.initData("水浒", 21, 202);//一种赋值保护类的方法
BOOK.print();
BOOK.getName() = "红楼梦";
BOOK.getNum() = 203;
BOOK.getPrice() = 23;
BOOK.print();
//*函数指针代表整个函数名
//book:: 修饰*函数指针
void (book:: * p)() = &book::print;
//(类的对象.*指针)(参数)//调用指针函数
(BOOK.*p)();
book* pbook= new book;
//(对象指针->*指针)(参数)
(pbook->*p)();
auto pp = &book::print;
(BOOK.*pp)();
return 0;
}
|