C++:类的封装

●参数初始化列表

1、带参构造

类名(int参数1,int参数2....)

{

属性1 =参数1;

属性2 =参数2;

}

2、初始化列表

类名(<类型1>参数1, < 类型2>参数2...): 属性1(参数1),属性2(参数2)

{

代码:

#include <iostream>
using namespace std;
class Car
{
public:
#if 1
    Car()
    {
        cout << "Car构造函数" << endl;
    }
#endif
    Car(string brand, string color):c_brand(brand),c_color(color)
    {
        cout << "have arg construct" << endl;
        //c_brand = brand;
        //c_color = color;
    }
    Car(int num):c_num(num)
    {
    }
    ~Car()
    {
        cout << "Car析构函数" << endl;
    }
    string c_brand;
    string c_color;
    int c_num;
};

class Person
{
public:
    Person()
    {
        m_name = "zs";
        m_age = 23;
        cout << "Person构造函数" << endl;
    }

    Person(Car c1)
    {
        m_car = c1;
        cout << "Person构造函数" << endl;
    }

    ~Person()
    {
        cout << "Person析构" << endl;
    }
    void PersonInfo()
    {
        cout << "name:" << m_name << endl;
        cout << "age:" << m_age << endl;
        cout << "car:" << m_car.c_brand << " #color:" << m_car.c_color << endl;
    }

    string m_name;
    int m_age;
    Car m_car;
};
class Dog
{
public:
    ~Dog()
    {
        cout << "dog析构" << endl;
    }
};

#if 0
void test01()
{
    Dog d1;
    Person p1;
    p1.m_car.c_brand = "bmw";
    p1.m_car.c_color = "black";
    p1.PersonInfo();
}
#endif

void test02()
{
    Car c1("bmw", "pink");
    Person p1(c1);
    p1.PersonInfo();
}
void test03()
{
    Person p2;
    p2.PersonInfo();
}

int main()
{
    test03();
    return 0;
}

 

●组合思想

- ==一个类包含一个或多个其他类的对象.==

构造与析构顺序:先构造类中的对象,再构造自身,析构顺序与构造相反.

#include <iostream>
using namespace std;


class Car
{
	public:
		Car()   //必须有无参数
		{
			cout<<"no agr construct"<<endl;
		}
		Car(string brand,string color):c_brand(brand),c_color(color)
		{
	
			cout<<"have agr construct"<<endl;
		}
		string c_brand;
		string c_color;
};
class Person
{
	public:
		void PersonInfo()
		{
			cout<<"name:"<<m_name<<endl;
			cout<<"age:"<<m_age<<endl;
			cout<<"car:"<<m_car.c_brand<<"#color:"<<m_car.c_color<<endl;
		}
		string m_name;
		int m_age;
		Car m_car;

};
 

void test01()
{
	Person p1;
	p1.m_car.c_brand="bmw";
	p1.m_car.c_color="black";
	p1.PersonInfo();
}


int main(int argc, char *argv[])
{

	test01();
	return 0;
}

●对象模型与this指针

(1)对象的成员属性与成员方法其实是分开存储的,类中的所有成员方法都只有一份实例

(2)只有非静态成员属性才会占用对象空间,使用sizeof运算符来验证

*对象模型

#include <iostream>
using namespace std;

class Animal
{
	public:
		int a_age;
		int a_num;
		void AnimalInfo()
		{
			cout<<"age:"<<a_age<<"num:"<<a_num<<endl;
		}
};

void test01()
{
	Animal a1;
	cout<<sizeof(a1)<<endl;
}

int main(int argc, char *argv[])
{
	test01();
	return 0;
}

(3)this指针是每一个非静态成员函数都拥有的参数,只是该参数被编译器隐藏,但是可以在函数内使用

*对象模型与this指针

#include <iostream>
using namespace std;

class Animal
{
public:
    Animal(){}
    Animal(string a_name)
    {
        this->a_name = a_name;
    }
    int a_age;
    int a_num;
    string a_name;

    void AnimalInfo()
    {
        cout << "name:" << this->a_name << " num:" << this->a_num << endl;
    }

    Animal AgeAdd()
    {
        this->a_age += 1;
        return *this;    //a2.AgeAdd().AgeAdd().AgeAdd()
    }
};

void test01()
{
    Animal a1;
    //cout << sizeof(a1) << endl;
    a1.a_num = 34;
    a1.a_name = "yangtuo";
    a1.a_age = 5;
    a1.AnimalInfo();  //this ---> a1

    Animal a2("sheep");  
    a2.AnimalInfo();  //this ---> a2
}

int main()
{
    test01();
    return 0;
}

(4)this指针: == 指向当前调用成员函数的对象.

class Person

{

int PersonFun(int n)

{

this->num = n;

}

int num;

}

*this指针

#include <iostream>
using namespace std;
class Animal
{
	public:
		Animal()
		{
			a_age=0;
		}

		Animal(string a_name)
		{
			this->a_name=a_name;
		}
		~Animal()
		{
			cout<<"析构函数"<<endl;
		}
		int a_age;
		int a_num;
		string a_name;
	
	void Animallnfo()
	{
		cout<<"name"<<this->a_name<<"num:"<<this->a_num<<endl;
	}

	Animal &AgeAdd()
	{
		this->a_age+=1;
		return *this;   //a2.AgeAdd().AgeAdd().AgeAdd()
	}
};

void test01()
{
	Animal a1;
	a1.a_num=34;
	a1.a_name="yanao";
	a1.a_age=5;
	a1.Animallnfo();       //this------->a1

	Animal a2("sheep");
	a2.Animallnfo();        //this-------->a2
}
void test02()
{
	Animal a3;
	a3.AgeAdd().AgeAdd().AgeAdd().AgeAdd();
	cout<<"age:"<<a3.a_age<<endl;
}

int main(int argc, char *argv[])
{
	test02();

	return 0;
}

(5)this指针的作用:

1)区分成员属性与参数(属性与参数重名)

2)成员函数需要返回对象本身时,需要使用this(实现链式编程思想)

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

এ᭄星辰

混口饭吃。。。。。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值