面向对象
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
class Person{
public:
// int m_A;
void func(){
}
// static int m_B;
static void fun2(){
};
// double m_C;
};
//int Person::m_B = 0 ;
void test01(){
Person p1;
cout<<"size="<< sizeof(p1)<< endl;
};
int main() {
test01();
// system("pause");
return EXIT_SUCCESS;
}
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
class Person{
public:
int m_A;
void func(){
}
static int m_B;
static void fun2(){
};
double m_C;
};
int Person::m_B = 0 ;
void test01(){
Person p1;
cout<<"size="<< sizeof(p1)<< endl;
};
int main() {
test01();
// system("pause");
return EXIT_SUCCESS;
}
this 指针的使用
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
class Person
{
public:
Person(int age)
{
//用途1 :解决名称冲突
this->age = age;
}
//this指针 隐式加在每个成员函数中
bool compareAge(Person &p)
{
if (this->age == p.age)
{
return true;
}
return false;
}
Person& personAddPerson(Person &p)
{
this->age += p.age;
return *this; //*this 就是本体
}
int age;
};
void test01()
{
//this指针 指向 被调用的成员函数 所属的对象
Person p1(10);
cout << "p1的年龄为: " << p1.age << endl;
Person p2(10);
bool ret = p1.compareAge(p2);
if (ret)
{
cout << "p1与p2年龄相等" << endl;
}
p1.personAddPerson(p2).personAddPerson(p2).personAddPerson(p2); //链式编程
cout << "p1的年龄为: " << p1.age << endl;
}
int main(){
test01();
system("pause");
return EXIT_SUCCESS;
}
空指针访问成员函数
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
class Person
{
public:
Person(int age)
{
//用途1 :解决名称冲突
this->age = age;
}
//this指针 隐式加在每个成员函数中
bool compareAge(Person &p)
{
if (this->age == p.age)
{
return true;
}
return false;
}
Person& personAddPerson(Person &p)
{
this->age += p.age;
return *this; //*this 就是本体
}
int age;
};
void test01()
{
//this指针 指向 被调用的成员函数 所属的对象
Person *p = NULL;
cout<<p->age<<endl;
}
int main(){
test01();
system("pause");
return EXIT_SUCCESS;
}
常函数和常对象
- 常对象只能调用常函数
- 常函数中不可修改,但是可以通过mutable 修改
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
class Person
{
public:
int age{0};
mutable int m_A; // 常函数中或者常对象,有些特殊属性依然想修改,加入关键字mutable;
Person(int age)
{
this->age = age;
}
// 指针的指向不可修改,指针指向的值可以修改
void showPerson() const{
m_A =100;
cout<< this->m_A<<endl;
}
void func(){
age = 100;
cout<<"func调用"<<endl;
}
};
void test01()
{
// 常对象声明,常对象调用常函数
const Person p1(100);
p1.m_A = 100;
}
int main(){
test01();
// system("pause");
return EXIT_SUCCESS;
}
友员函数
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include <string>
using namespace std;
class Person
{
friend void sayAge(Person *p);
public:
int age{0};
mutable int m_A; // 常函数中或者常对象,有些特殊属性依然想修改,加入关键字mutable;
Person(int age)
{
this->age = age;
}
// 指针的指向不可修改,指针指向的值可以修改
void showPerson() const{
m_A =100;
cout<< this->m_A<<endl;
}
void func(){
age = 100;
cout<<"func调用"<<endl;
}
private:
string name = "人";
};
void test01()
{
// 常对象声明,常对象调用常函数
const Person p1(100);
p1.m_A = 100;
Person *p2 = new Person(10) ;
sayAge(p2);
}
void sayAge(Person *p){
cout<<p->name<<endl;
}
int main(){
test01();
// system("pause");
return EXIT_SUCCESS;
}
友元类
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
#include <string>
class Building;
class GoodGay
{
public:
GoodGay();
void visit();
Building * m_building;
};
class Building
{
//让goodGay类作为 Building的好朋友,可以访问私有成员
friend class GoodGay;
public:
Building();
string m_SittingRoom;
private:
string m_BedRoom;
};
Building::Building()
{
this->m_SittingRoom = "客厅";
this->m_BedRoom = "卧室";
}
GoodGay::GoodGay()
{
this->m_building = new Building;
}
void GoodGay::visit()
{
cout << "好基友正在访问: " << this->m_building->m_SittingRoom << endl;
cout << "好基友正在访问: " << this->m_building->m_BedRoom << endl;
}
void test01()
{
GoodGay gg;
gg.visit();
}
int main(){
test01();
system("pause");
return EXIT_SUCCESS;
}
成员函数作为友元函数
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
#include <string>
class Building;
class GoodGay
{
public:
GoodGay();
void visit(); //可以访问building的私有
void visit2(); // 不可以访问building的私有
Building * m_building;
};
class Building
{
//让GoodGay类中的 visit成员函数作为友元
friend void GoodGay::visit();
public:
Building();
string m_SittingRoom;
private:
string m_BedRoom;
};
Building::Building()
{
this->m_SittingRoom = "客厅";
this->m_BedRoom = "卧室";
}
GoodGay::GoodGay()
{
this->m_building = new Building;
}
void GoodGay::visit()
{
cout << "好基友正在访问: " << this->m_building->m_SittingRoom << endl;
cout << "好基友正在访问: " << this->m_building->m_BedRoom << endl;
}
void GoodGay::visit2()
{
cout << "好基友正在访问: " << this->m_building->m_SittingRoom << endl;
//cout << "好基友正在访问: " << this->m_building->m_BedRoom << endl;
}
void test01()
{
GoodGay gg;
gg.visit();
gg.visit2();
}
int main(){
test01();
system("pause");
return EXIT_SUCCESS;
}