首页 > 编程语言 >c++从入门到精通——面向对象初探以及友元函数、对象

c++从入门到精通——面向对象初探以及友元函数、对象

时间:2022-11-01 18:32:16浏览次数:40  
标签:友元 test01 int age c++ Person 初探 GoodGay void


面向对象

  • 每个对象内存地址独一无二,空对象分配一个字节空间
#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;
}

c++从入门到精通——面向对象初探以及友元函数、对象_#include

#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;
}

c++从入门到精通——面向对象初探以及友元函数、对象_#define_02

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;
}

c++从入门到精通——面向对象初探以及友元函数、对象_c++_03

空指针访问成员函数

#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;
}

c++从入门到精通——面向对象初探以及友元函数、对象_数据结构_04

常函数和常对象

  • 常对象只能调用常函数
  • 常函数中不可修改,但是可以通过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;
}

c++从入门到精通——面向对象初探以及友元函数、对象_#define_05

友元类

#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;
}

c++从入门到精通——面向对象初探以及友元函数、对象_c++_06

成员函数作为友元函数

#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;
}


标签:友元,test01,int,age,c++,Person,初探,GoodGay,void
From: https://blog.51cto.com/u_13859040/5814699

相关文章

  • C++案例1
        输出,发现不会调用子类析构。然而子类在堆里申请了空间的,内存泄漏。 ......
  • C++基础入门通讯录管理系统
    记录代码#pragmaonce#include<iostream>#include<string>#defineSize100#defineOK1#defineError-1usingnamespacestd;//结构体,通讯录人structPerson{//名字......
  • C++从入门到精通——C++面对对象
    面向对象基础#define_CRT_SECURE_NO_WARNINGS#include<iostream>usingnamespacestd;#include<string>//设计一个类,求圆的周长constdoublePI=3.14;//class+类名//......
  • c++从入门到精通——基本入门
    C++-1.helloWorld#include<iostream>usingnamespacestd;intmain(){cout<<"helloworld!"<<endl;system("pause");returnEXIT_SUCCESS;}双冒号作用域#inclu......
  • Windows 下 MSYS2 环境配置和 MinGW-w64 C++ 环境配置
    转载来自:Server=https://mirrors.tuna.tsinghua.edu.cn/msys2/mingw/x86_64Windows下MSYS2环境配置和MinGW-w64C++环境配置1、简介本文主要是Windows下MSYS2......
  • 郁金香 中级班 2.c++的基类和派生类
        生物是基类老虎是派生类派生类继承了基类的成员和成员函数同时this指针指向的是这个对象所开辟的那个地址......
  • c++ 模板 指针类型偏特化
    一步步来,先简单点.目标:我们要实现一个模板类,例化后,可以通过get_val获取到值,通过get_ptr获取到指针.具体什么意思结合例子来看看吧.例子:structA{intdata;......
  • 黑马程序员C++
    C++基础入门1C++初识1.1第一个C++程序编写一个C++程序总共分为4个步骤创建项目创建文件编写代码运行程序1.1.1创建项目​ VisualStudio是我们用来编写C++......
  • C++ 中的 auto的用法
    auto的原理就是根据后面的值,来自己推测前面的类型是什么。auto的作用就是为了简化变量初始化,如果这个变量有一个很长很长的初始化类型,就可以用auto代替。1.用auto声明的......
  • C++中string跨DLL失败解决途径
    1、问题描述:在一个MFC应用程序exe中,调用另一个DLL中的函数,函数中的一个形参是string类型的,每次调用都会出现乱码的情况,并且会崩溃。调用前:调用后: 2、原因分析: ......