类名(int参数1,int参数2....)
{
属性1 =参数1;
属性2 =参数2;
}
类名(<类型1>参数1, < 类型2>参数2...): 属性1(参数1),属性2(参数2)
{
代码:
- #include
- 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
- using namespace std;
-
-
- class Car
- {
- public:
- Car() //必须有无参数
- {
- cout<<"no agr construct"<
- }
- Car(string brand,string color):c_brand(brand),c_color(color)
- {
-
- cout<<"have agr construct"<
- }
- string c_brand;
- string c_color;
- };
- class Person
- {
- public:
- void PersonInfo()
- {
- cout<<"name:"<
- cout<<"age:"<
- cout<<"car:"<
"#color:"< - }
- 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
- using namespace std;
-
- class Animal
- {
- public:
- int a_age;
- int a_num;
- void AnimalInfo()
- {
- cout<<"age:"<
"num:"< - }
- };
-
- void test01()
- {
- Animal a1;
- cout<<sizeof(a1)<
- }
-
- int main(int argc, char *argv[])
- {
- test01();
- return 0;
- }
(3)this指针是每一个非静态成员函数都拥有的参数,只是该参数被编译器隐藏,但是可以在函数内使用
*对象模型与this指针
- #include
- 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
- using namespace std;
- class Animal
- {
- public:
- Animal()
- {
- a_age=0;
- }
-
- Animal(string a_name)
- {
- this->a_name=a_name;
- }
- ~Animal()
- {
- cout<<"析构函数"<
- }
- int a_age;
- int a_num;
- string a_name;
-
- void Animallnfo()
- {
- cout<<"name"<<this->a_name<<"num:"<<this->a_num<
- }
-
- 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();
-
-
相关阅读:
【Android】-- Activity页面(启动和结束、生命周期、启动模式和实例)
华为被迫开源,从认知到落地SpringBoot企业级实战手册(完整版)
第2篇 机器学习基础 —(1)机器学习概念和方式
奇舞周刊第 505 期:实践指南-前端性能提升 270%!
C++:STL::String模拟实现
十三、CANdelaStudio入门-DTC编辑
【华为OD机试真题 python】叠积木【2022 Q4 | 200分】
开源软件与知识产权:需要注意什么?
【pytorch】torch.cuda.is_available() = False 可能的解决办法
Linux多线程第一讲——线程的创建和基本使用
-
原文地址:https://blog.csdn.net/qq_63626307/article/details/126715977