网站如何添加统计代码,工程建设信息都在哪个网站发布,中国站长网入口,最受欢迎国内设计网站1、成员变量和成员函数 #include iostream
using namespace std;
#include string//成员变量和成员函数分开存储class Person {int m_A;//非静态成员变量 属于类的对象上的static int m_B;//静态成员变量 不属于类的对象上void func() {} //非静态成员函数 不… 1、成员变量和成员函数 #include iostream
using namespace std;
#include string//成员变量和成员函数分开存储class Person {int m_A;//非静态成员变量 属于类的对象上的static int m_B;//静态成员变量 不属于类的对象上void func() {} //非静态成员函数 不属于类的对象上static void func2(){}//静态成员函数 不属于类的对象上
};
int Person::m_B0;void test01() {Person p;//空对象占用内存空间为1//c编译器会给每个空对象也分配一个字节空间是为了区分空对象占内存的位置//每个空对象也应该有一个独一无二的内存地址cout size of p sizeof(p) endl;
}void test02() {Person p;cout size of p sizeof(p) endl;
}int main() {//test01();test02();system(pause);return 0;
} 2、this指针的用途 #include iostream
using namespace std;
#include stringclass Person {
public:Person(int age) {//this指针指向 被调用的成员函数 所属的对象this-age age;}Person PersonAddAge(Person p) {this-age p.age;//this指向p2的指针而*this指向的就是p2这个对象本体return *this;}int age;};//1、解决名称冲突
void test01() {Person p1(18);cout p1的年龄为 p1.age endl;}
//2、返回对象本身用*this
void test02() {Person p1(10);Person p2(10);/*p2.PersonAddAge(p1);cout p2的年龄为 p2.age endl;*///链式编程思想p2.PersonAddAge(p1).PersonAddAge(p1).PersonAddAge(p1);cout p2的年龄为 p2.age endl;
}int main() {//test01();test02();system(pause);return 0;
} 3、空指针访问成员函数 #include iostream
using namespace std;
#include stringclass Person {
public:void showClassNmae() {cout this is Person class endl;}//报错原因是因为传入的指针是为NULLvoid showPersonAge() {if (this NULL) {return;}cout age m_Age endl;}int m_Age;
};void test01() {Person* p NULL;p-showClassNmae();//p-showPersonAge();报错
}int main() {test01();system(pause);return 0;
} 4、const修饰成员函数 常函数: 成员函数后加const后我们称为这个函数为常函数常函数内不可以修改成员属性成员属性声明时加关键字mutable后在常函数中依然可以修改 常对象: 声明对象前加const称该对象为常对象常对象只能调用常函数 #include iostream
using namespace std;
#include string//常函数
class Person {
public://this指针的本质 是指针常量 指针的指向是不可以修改的//const Person * const this;//在成员函数后面加const修饰的是this指向让指针指向的值也不可以修改void showPerson() const{this-m_B100;//this-m_A 100;//this指针是不可以修改指针的指向的//this NULL;} void func() {}int m_A;mutable int m_B;//特色变量即使在常函数中也可以修饰这个值
};void test01() {Person p;p.showPerson();
}//常对象
void test02() {const Person p;//在对象前加const变为常对象//p.m_A 100;p.m_B 100;//m_Bs是特殊值在常对象下也可以修改//常对象只能调用常函数p.showPerson();//p.func();//常对象 不可以调用普通成员函数因为普通成员函数可以修改属性
}int main() {test01();test02();system(pause);return 0;
}