class Role
{
private:
int a;//成员变量
//成员函数
void Init()
{
hh=3;
}
public:
int b;//成员变量
int c;//成员变量
//成员函数
void Initaa(role& r)
{
r.b-=c;
}
}
int main()
{
...
}
----------------------------------------------------
//将成员函数写在class外面
class Role
{
private:
int a;//成员变量
//成员函数
void Init()
{
hh=3;
}
public:
int b;//成员变量
//成员函数
void Role::Initaa();//声明
}
void Role::Initaa()
{
hhaa=4;
}
int main()
{
...
}
inline内联成员函数(推荐写在头文件中)
// 头文件放在xxx.hpp中
class Role
{
private:
int a;//成员变量
void Iint();
int hp;
public:
int b;//成员变量
//成员函数
void Role::Initaa();//声明
//
inline int Gethp()
{
return hp;
}
}
//上述声明的函数可以放到源文件xxx.cpp中
const的使用
#include "Role.h"
{
const Role user;
user.hp = 2;//不可改变,错误
Role monster;
const Role* psuer{&user};
puser->damage = 2;//不可改变,puser是常量指针
//指针的二要素,类型及指向地址
//常量指针只能调用常量成员函数
//常量成员函数在源文件中声明为
// for example
inline void Init() const;
}
mutable修饰的变量,const成员函数可以对其进行修改。
构造函数
class T
{
public:
int hp;
T()
{
hp=1;
mp=2;
}
T(int _hp, int _mp)
{
hp = _hp;
mp = _mp;
}
private:
int mp;
}
析构函数
~类名()
{
}
class Role
{
public:
Role()
{
std::cout << "\n类被创建!";
}
~Role()
{
std::cout << "\n类被销毁!";
}
//或者
~Role() = default;
}
int main()
{
Role user;
int a{1};
while(a)
{
Role p;
std::cin >> a;
}
std::cout << "ending";
}
静态成员变量
#include <iostream>
class T
{
public:
static int count;
};
int T::count=100;//定义
int main()
{
T t2, t3, t4;
T t5;
t2.count++;
std::cout << t3.count<<std::endl;
std::cout << t4.count << std::endl;
T::count = 350;//访问
T t1;
std::cout << t1.count;
}
//e.g. 统计实例的个数
#include<iostream>
class T
{
inline static int count{};//添加inline对count进行定义,C++17标准
public:
int hp;
T()
{
count++;
}
int getcount()
{
return count;
}
~T()
{
count--;
}
};
void test()
{
T t1, t2, t3;
}
int main()
{
test();
T t1, t2, t3, t4, t5;
std::cout << t1.getcount();
}
静态成员函数
//e.g. 统计实例的个数
#include<iostream>
class T
{
inline static int count{};//添加inline对count进行定义,C++17标准
public:
static void test()
{
std::cout << "ssss";
count++;//可以访问静态成员变量
}
int hp;
T()
{
count++;
}
int getcount()
{
return count;
}
~T()
{
count--;
}
};
int main()
{
const T t1;
t1.test();
T::test();
}
友元
//e.g. 统计实例的个数
#include<iostream>
class T
{
int hp;
int mp;
friend void sethp(T& t1);
friend void setmp(T& t1);
void gethp()
{
std::cout << hp;
}
};
void sethp(T& t1)
{
t1.hp = 2500;
t1.gethp();
}
void setmp(T& t1)
{
t1.hp = 2500;
t1.gethp();
}
int main()
{
T t1;
sethp(t1);
}
友元会破坏类的封装性
友元类不是一种平等的关系
嵌套类
#include <iostream>
enum class weaponlv
{
normal = 0,
high,
rare,
myth
};
class role//外围类
{
static void test()
{
}
public:
role()
{
weapon::test1();
}
class weapon//嵌套类
{
weapon* returnw();
public:
static void test1()
{
}
short lv;
weaponlv w;
weapon()//构造函数
{
test();
}
};
int hp;
int mp;
//weapon lefthands;
};
//注意定义域
role::weapon* role::weapon::returnw()
{
return this;
}
int a;
int main()
{
//role r1;
//role::test();
class T//局部类
{
int hp;
int mp;
//不可用静态成员变量static int count;
void gethp()
{
a++;//局部类可以访问全局变量
}
};
T t1;
}
malloc和new的本质区别
对于普通的数据类型来说malloc和new没什么区别,但是对于类来说,malloc仅仅是分配内存,而new除了分配内存以外还会调用构造函数。
#include <iostream>
class T
{
int m_count{};
inline static int count{};
public:
T()
{
std::cout << "第" << ++count << "个T被构造" << std::endl;
m_count = count;
}
~T()
{
std::cout << "第" << m_count-- << "个T被销毁" << std::endl;
}
int test = 2;
};
int main()
{
T* t1 = (T*)malloc(10 * sizeof(T));
std::cout << t1[2].test << std::endl;//output is -842150451
int* pint = (int*)malloc(10 * 4);//普通数据类型
pint[2] = 250;
std::cout << pint[2];
T* t2 = new T;
free(pint);
free(t1);
//free(t2);
delete t2;
}
#include <iostream>
class hpmed
{
public:
int recover{ 100 };
};
class role
{
int hp;
int maxhp;
public:
role()
{
hp = 1000;
maxhp = 3500;
}
void gethp()
{
std::cout << "HP:" << hp << "\\" << maxhp;
}
void eatmed(hpmed& hpMed)
{
hp += hpMed.recover;
hp = hp > maxhp ? maxhp : hp;
}
};
int main()
{
role user;
hpmed med;
user.gethp();
while (1)
{
int a;
std::cin >> a;
user.eatmed(med);
system("cls");
user.gethp();
}
}