对象:真实存在的事物
类:
class Person{
private:
int age;
protected:
char sex[10];
public:
char name[50];
void eat()
{
cout << name << "吃饭" << endl;
}
void sleep();
};
void Person::sleep()
{
cout << name << "睡觉" << endl;
}
int main(int argc, char *argv[])
{
Person p1;
strcpy(p1.name,"张三");
p1.eat();
p1.sleep();
return 0;
}
int a = 10;
int nums[5] = {1,2,3,4,5};
void fun()
{
xxx
}
class A
{
属性
函数
}
A a;
x.c
class Stu
{
private:
char name[50];
char sex[10];
int age;
public:
char* get_name()
{
return name;
}
void set_name(char* n)
{
strcpy(name,n);
}
char* get_sex()
{
return sex;
}
void set_sex(char* s)
{
strcpy(sex,s);
}
int get_age()
{
return age;
}
void set_age(int a)
{
age = a;
}
void print_info()
{
cout << "姓名:" << name << "\t性别:" << sex << "\t年龄:" << age << endl;
}
};
int main(int argc, char *argv[])
{
Stu s;
s.set_name("张三");
s.set_sex("男");
s.set_age(18);
s.print_info();
cout << s.get_name() << endl;
cout << s.get_sex() << endl;
cout << s.get_age() << endl;
return 0;
}
class Dog
{
private:
char name[50];
int age;
public:
Dog()
{
cout << "调用无参构造" << endl;
}
Dog(char *n,int a)
{
cout << "调用有参构造" << endl;
strcpy(name,n);
age = a;
}
Dog(char *n)
{
cout << "调用一参构造" << endl;
strcpy(name,n);
}
};
int main(int argc, char *argv[])
{
//隐式调用无参构造创建对象
Dog d1;
//显式调用无参构造创建对象
Dog d2 = Dog();
//隐式调用有参构造创建对象
Dog d3("旺财",2);
//显式调用有参构造创建对象
Dog d4 = Dog("富贵",1);
//如果构造函数只有一个参数,会发生构造函数的隐式转换(知道就行)
//隐式转换
//类名 对象名 = 值;
Dog d5 = "黑豹";
//匿名对象:创建的对象没有对象名
Dog();
Dog("无名");
return 0;
}
class Cat{
private:
char name[50];
public:
Cat(char *n)
{
strcpy(name,n);
cout << name << "被创建了" << endl;
}
~Cat()
{
cout << name << "被销毁了" << endl;
}
};
void test01()
{
Cat c("布丁");
}
int main(int argc, char *argv[])
{
test01();
return 0;
}
class Cat{
private:
char name[50];
public:
Cat(char *n)
{
strcpy(name,n);
cout << name << "被创建了" << endl;
}
~Cat()
{
cout << name << "被销毁了" << endl;
}
};
void test01()
{
Cat c1("布丁");
Cat c2("乔巴");
{
Cat c3("可乐");
Cat c4("雪碧");
}
}
int main(int argc, char *argv[])
{
test01();
return 0;
}
结果:
class A{
public:
A(){
cout << "A构造" << endl;
}
~A()
{
cout << "A析构" << endl;
}
};
class B{
private:
A a;
public:
B(){
cout << "B构造" << endl;
}
~B()
{
cout << "B析构" << endl;
}
};
int main(int argc, char *argv[])
{
B b;
return 0;
}
class Cat{
private:
char name[50];
public:
Cat(char *n)
{
strcpy(name,n);
cout << name << "被创建了" << endl;
}
~Cat()
{
cout << name << "被销毁了" << endl;
}
Cat(const Cat &cat)
{
cout << "拷贝构造被调用了" << endl;
strcpy(name,cat.name);
}
};
int main(int argc, char *argv[])
{
Cat c1("布丁");
Cat c2 = c1;
return 0;
}
class Cat{
private:
char name[50];
public:
Cat(char *n)
{
strcpy(name,n);
cout << name << "被创建了" << endl;
}
~Cat()
{
cout << name << "被销毁了" << endl;
}
Cat(const Cat &cat)
{
cout << "拷贝构造被调用了" << endl;
strcpy(name,cat.name);
}
};
void test(Cat cat)
{
}
int main(int argc, char *argv[])
{
Cat c1("布丁");
test(c1);
return 0;
}
class Cat{
private:
char name[50];
public:
Cat(char *n)
{
strcpy(name,n);
cout << name << "被创建了" << endl;
}
~Cat()
{
cout << name << "被销毁了" << endl;
}
Cat(const Cat &cat)
{
cout << "拷贝构造被调用了" << endl;
strcpy(name,cat.name);
}
};
Cat test()
{
Cat c1("布丁");
return c1;
}
int main(int argc, char *argv[])
{
Cat c = test();
return 0;
}
浅拷贝
class Cat{
public:
char *name;
Cat(char *n)
{
name = n;
cout << name << "被创建了" << endl;
}
~Cat()
{
cout << name << "被销毁了" << endl;
}
};
int main(int argc, char *argv[])
{
char name[10] = "Hi";
Cat c1(name);
Cat c2 = c1;
c2.name[0] = 'h';
cout << "c1.name = " << c1.name << endl;
cout << "c2.name = " << c2.name << endl;
return 0;
}
深拷贝
class Cat{
public:
char *name;
Cat(char *n)
{
name = (char *)calloc(1,50);
strcpy(name,n);
cout << name << "被创建了" << endl;
}
~Cat()
{
cout << name << "被销毁了" << endl;
free(name);
}
Cat(const Cat &cat)
{
name = (char *)calloc(1,50);
strcpy(name,cat.name);
}
};
int main(int argc, char *argv[])
{
char name[10] = "Hi";
Cat c1(name);
Cat c2 = c1;
c2.name[0] = 'h';
cout << "c1.name = " << c1.name << endl;
cout << "c2.name = " << c2.name << endl;
return 0;
}
示例1
class Data
{
int a;
int b;
public:
Data(int x,int y):b(y),a(x)
{
}
void print_data()
{
cout << "a = " << a << endl;
cout << "b = " << b << endl;
}
};
int main(int argc, char *argv[])
{
Data d(10,1);
d.print_data();
return 0;
}
class C{
private:
int a;
public:
explicit C(int x):a(x){}
};
int main(int argc, char *argv[])
{
C c = 10;//构造函数的隐式转换
//当调用构造函数使用explicit修饰后防止隐式转换,此时上述代码报错
return 0;
}
void fun01()
{
int *p1 = new int;
*p1 = 100;
cout << *p1 << endl;
int *p2 = new int(100);//给p2的值初始化为100
cout << *p2 << endl;
}
void fun02()
{
int *nums = new int[5];
cout << nums[0] << "\t" << nums[1] << endl;
delete [] nums;
int *ns = new int[5]{11,22,33,44,55};
cout << ns[0] << "\t" << ns[1] << endl;
delete [] ns;
}
class D{
public:
D(){
cout << "D构造" << endl;
}
~D(){
cout << "D析构" << endl;
}
};
void fun03()
{
D *d = new D();
delete d;
}
注意:
class A
{
public:
int mA;
public:
A()
{
cout<<"A无参构造"<
void fun05()
{
A *arr = new A[5]{A(10),A(20),A(30),A(40),A(50)};
int i=0;
for(i=0;i<5;i++)
{
cout<
class E{
public:
static int num;//类中定义静态成员
};
int E::num = 100;
int main(int argc, char *argv[])
{
//使用类名访问
cout << "E::num = " <
静态成员函数
概述:
class E{
private:
static int num;//类中定义静态成员
int x;
public:
static void set_num(int n)
{
num = n;
cout << "静态函数set_num被执行" << endl;
}
static int get_num()
{
cout << "静态函数get_num被执行" << endl;
return num;
}
};
int E::num = 100;
int main(int argc, char *argv[])
{
//使用类名调用
E::set_num(10);
int x = E::get_num();
//使用对象调用
E e;
e.set_num(1);
return 0;
}
class DL{
private:
static DL *dl;
DL(){}
DL(const DL &d){}
public:
int x;
static DL* get_instance()
{
if(dl == NULL)
{
dl = new DL();
}
return dl;
}
};
DL *DL::dl = NULL;
int main(int argc, char *argv[])
{
DL *d1 = DL::get_instance();
DL *d2 = DL::get_instance();
DL *d3 = DL::get_instance();
d1->x = 1;
d2->x = 10;
d3->x = 100;
cout << "d1.x = " << d1->x << endl;
cout << "d2.x = " << d2->x << endl;
cout << "d3.x = " << d3->x << endl;
return 0;
}
class DL{
private:
static DL *dl;
DL(){}
DL(const DL &d){}
public:
int x;
static DL* get_instance()
{
return dl;
}
};
DL *DL::dl = new DL();
int main(int argc, char *argv[])
{
DL *d1 = DL::get_instance();
DL *d2 = DL::get_instance();
DL *d3 = DL::get_instance();
d1->x = 1;
d2->x = 10;
d3->x = 100;
cout << "d1.x = " << d1->x << endl;
cout << "d2.x = " << d2->x << endl;
cout << "d3.x = " << d3->x << endl;
return 0;
}