赞
踩
类名(int参数1,int参数2....)
{
属性1 =参数1;
属性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;
- }
- #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;
- }
- #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;
- }
-
class Person
{
int PersonFun(int n)
{
this->num = n;
}
int num;
}
- #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;
- }
1)区分成员属性与参数(属性与参数重名)
2)成员函数需要返回对象本身时,需要使用this(实现链式编程思想)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。