成员变量和成员函数分开存储
在C++中,类内成员变量和成员函数分开存储
#include<iostream>
using namespace std;
class person
{
int a;
static int b;//静态成员变量,不属于类对象上
};
int main()
{
person p;//空对象字节单位是1
//c++编译器中给每个空对象也分配一个字节空间,是为了区分空对象占内存的位置
//每个空对象也有个独一无二的内存地址
//cout<<"size of p="<<sizeof(p)<<endl;
person q;
cout<<"size of q="<<sizeof(q)<<endl;
//静态成员变量,不属于类对象上
return 0;
}
this指针概念
#include<iostream>
using namespace std;
class person
{
public:
person (int age)
{
//this 功能一:解决名称冲突
//age=age;/传递错误。会输出乱码
this->age=age;//指向被调用的成员函数所属的对象
}
person& personaddage(person &p)
{
this->age+=p.age;
return *this;//this指向p2的指针,*this指向的是p2这个对象本体
}
int age;
};
int main()
{
person p1(18);
cout<<"p1的年龄为"<<p1.age<<endl;
//返回对象本身用*this
person p2(10);
//链式编程思想
p2.personaddage(p1).personaddage(p1);
cout<<"p2的年龄为:"<<p2.age<<endl;
return 0;
}
空指针访问成员函数
空指针也可以调用成员函数
#include<iostream>
using namespace std;
class person
{
public:
void showclassname()
{
cout<<"this is class"<<endl;
}
void showpersonage()
{
if(this==NULL)
return ;
cout<<"age is "<<age<<endl;
}
int age;
};
int main()
{
person *p=NULL;
p->showclassname();
p->showpersonage();
return 0;
}
const修饰成员函数
常函数:
成员函数加const后,我们称为常函数
常函数内不可以修改成员属性
成员函数声明时加关键字mutable后,在常函数中依然可以修改
常对象:
声明对象前加const称该对象为常对象
常对象只能调用常函数
#include<iostream>
using namespace std;
class person
{
public:
//this指针的本质,是指针常量,指针的指向是不可以修改的
//const person * const this;
//在成员函数后面加const,修饰的是this,让指针指向的值也不可以修改
const void showperson()//成员函数加const后,我们称为常函数
{
this->B=100;
}
// 常函数内不可以修改成员属性
int m_a;
mutable int B;//成员函数声明时加关键字mutable后,在常函数中依然可以修改
};
int main()
{
person p;
p.showperson();
p.B=100;//B是特殊值,在常对象下也可以修改
//常对象只能调用常函数
return 0;
}
友元
类外的特殊的函数或者类访问私有属性
关键字:friend
全局函数做友元
#include<iostream>
using namespace std;
class person
{
friend void goodgay(person *per);//goodgay是全局函数,是building类的好朋友,可以访问类内的私有内容
public:
string sittingroom="111";
private:
string bedroom="222";
};
void goodgay(person *per)
{
cout<<"好基友正在访问:"<<per->sittingroom<<endl;
cout<<"好基友正在访问:"<<per->bedroom<<endl;
}
int main()
{
person p;
goodgay(&p);
return 0;
}
类做友元
#include <iostream>
using namespace std;
class Building
{
friend class goodFriend;
public:
Building(); // 类内申明,类外实现
public:
string m_sittingrookm;
private:
string m_bedroom;
};
// 类外实现
Building::Building()
{
this->m_sittingrookm = "卧室";
this->m_bedroom = "客厅";
}
class goodFriend
{
public:
goodFriend();
void visit();
Building* b;
};
// 类外实现
goodFriend::goodFriend()
{
b = new Building;
}
void goodFriend::visit()
{
cout << "好基友正在参观" << this->b->m_sittingrookm << endl;
cout << "好基友正在参观" << this->b->m_bedroom << endl; // 将goodFriend申明为Building的友元类;
}
void test01()
{
goodFriend g;
g.visit();
}
int main()
{
test01();
return 0;
}
成员函数做友元
//成员函数做友元函数:
#include<iostream>
#include<string>
using namespace std;
class Building;
class goodGay
{
public:
goodGay();
void visit1();
void visit2();
private:
Building * building;
};
class Building
{
friend void goodGay::visit1();
public:
Building();
public:
string m_sittingroom; //客厅
private:
string m_bedroom; //卧室
};
Building::Building()
{
this->m_bedroom = "卧室";
this->m_sittingroom = "客厅";
}
goodGay::goodGay()
{
building = new Building;
}
void goodGay::visit1()
{
cout << "基友正在" << this->building->m_sittingroom << endl;
cout << "基友正在" << this->building->m_bedroom << endl;
}
void goodGay::visit2()
{
cout << "基友正在" << this->building->m_sittingroom << endl;
}
void test01()
{
goodGay gg;
gg.visit1();
gg.visit2();
}
int main()
{
test01();
system("pause");
return EXIT_SUCCESS;
}