您的位置:首页 > 编程语言 > C语言/C++

C++语言之结构体、类、构造函数、拷贝构造函数

2016-09-27 22:48 483 查看

结构体、类、构造函数、拷贝构造函数

1、结构体
C
C++

区别:
1、定义变量时,stuct可以省略吗?
2、C++中的结构体 可以加函数原型
加了函数的好处:通过stu变量,不但可以得到stu.number、stu.name,还可以执行stu.print_student()函数(不需要自己写printf打印信息了)
注意:
当C++结构体中,增加了函数后,就不能使用	SStudent stu={1001,"zhangsan"}; 的方式来定义和变量和赋值了,只能分步进行,即:
SStudent stu;
stu.number=1001;
stu.name=(char *)malloc(20);
strcpy(stu.name,"zhangsan");

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Student
{
int number;
char *name;
void print_student()
{
printf("number is %d\tname is %s\n",number,name);
}
};

int main()
{
//Student stu[2]={1001,"zhangsan",1002,"lisi"};

Student stu={1001,"zhangsan"};
//printf("number is %d\tname is %s\n",stu.number,stu.name);
stu.print_student();

return 0;
}

2、使用VC工具,将SStudent类型,自动由Student.h和Student.cpp来组成和建立
注意:VC工具默认为class,我们可以将其修改为 struct
生成的多文件代码如下:

//main.cpp
#include <string.h>
#include <stdlib.h>
#include "Student.h"
int main()
{
//SStudent stu={1001,"zhangsan"};  如果结构体中有了构造函数,就不能这样用了
SStudent stu;
stu.number=1001;
stu.name=(char *)malloc(20);
strcpy(stu.name,"zhangsan");
stu.print_student();
return 0;
}

// Student.h: interface for the SStudent class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_STUDENT_H__C349E43A_0B9E_4447_BC45_3F4DFF0FE415__INCLUDED_)
#define AFX_STUDENT_H__C349E43A_0B9E_4447_BC45_3F4DFF0FE415__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

struct SStudent
{
int number;
char *name;
void print_student();
SStudent();
virtual ~SStudent();

};

#endif // !defined(AFX_STUDENT_H__C349E43A_0B9E_4447_BC45_3F4DFF0FE415__INCLUDED_)

// Student.cpp: implementation of the SStudent class.
//
//////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include "Student.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

SStudent::SStudent()
{

}

SStudent::~SStudent()
{

}

void SStudent::print_student()
{
printf("number is %d\tname is %s\n",number,name);
return;
}

C++中的结构体------C++中的类
区别:默认   公共public-------私有private

类的:四大成员函数
构造函数
拷贝构造函数
赋值函数
析构函数

1、构造函数

对象-----就是 C语言中的变量
数据类型是class的变量

特点:定义对象时,自动调用 构造函数

//main.cpp
#include <string.h>
#include <stdlib.h>
#include "Student.h"
int main()
{
//SStudent stu={1001,"zhangsan"};  如果结构体中有了构造函数,就不能这样用了
SStudent stu(1003,"wangwu");
//stu.number=1001;
//stu.name=(char *)malloc(20);
//strcpy(stu.name,"zhangsan");
stu.print_student();
return 0;
}

//Student.h
// Student.h: interface for the SStudent class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_STUDENT_H__C349E43A_0B9E_4447_BC45_3F4DFF0FE415__INCLUDED_)
#define AFX_STUDENT_H__C349E43A_0B9E_4447_BC45_3F4DFF0FE415__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

class SStudent
{
public:
int number;
char *name;
void print_student();
SStudent();
SStudent(int number,char *name);
virtual ~SStudent();

};

#endif // !defined(AFX_STUDENT_H__C349E43A_0B9E_4447_BC45_3F4DFF0FE415__INCLUDED_)

//Student.cpp
// Student.cpp: implementation of the SStudent class.
//
//////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include "Student.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

SStudent::SStudent()
{
number=999;
}

SStudent::~SStudent()
{

}

void SStudent::print_student()
{
printf("number is %d\tname is %s\n",number,name);
return;
}

SStudent::SStudent(int number,char *name)
{
this->number=number;
this->name=name;

}

知识点:
函数的重载:
特点1:函数名一样-----参数不一样    所以可以区别开
特点2:同1个类里的 2个函数名 相同------无参构造函数、有参构造函数

其实,不在同一类下的2个函数,也可能是函数重载-----------它们都是 外部函数时(没有在类里)------但C++项目中,会杜绝这样的用法

拷贝构造函数
特点1:本质上还是构造函数
特点2:系统有默认的拷贝构造函数,但它是 浅赋值(this->name=name)
特点3:因为浅赋值,通常会出问题,所以一般要重写

int main()
{

SStudent stu1(1003,"wangwu");
//SStudent stu2(1003,"wangwu");
SStudent stu2(stu1);
stu2.print_student();

return 0;
}

新知识点:
引用
特点1:C++有,C没有
特点2:函数传递时,使用的是真品,而不是复制品
特点3:平时不用。

例子:利用引用 来交换2个数
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

void swap(int &a,int &b)
{
int temp;
temp=b;
b=a;
a=temp;
return;
}

int main()
{
int a=10;
int b=20;
swap(a,b);
printf("a=%d\tb=%d\n",a,b);

return 0;
}

难点:
1、引用
引用  就  外号、真名、字

int main()
{

int a=10;
int &aa=a;		//aa是a的外号。以后改aa,就是改a;改a,就是改aa.
//aa=20;
a=22
printf("a=%d\n",aa);
return 0;
}

2、建立类的时候,对.h   .cpp不是很理解
使用vc的 class view,在项目上点 右键-----新建类
自动生成
.h   --------C:函数原型        C++:类的原型(包含了 函数原型)
.cpp --------c:定义函数       c++:定义类中的 函数

int main()
{
CStudent stu1(1001,"zhangsan");
stu1.print_student();

return 0;
}

C++中,定义变量的方式 有2种:
int main()
{
int a(1000);//int a=10       等价

char ch('m');//char ch='m'   等价
printf("%c\n",ch);

return 0;
}

这2种方式,引出了下列的代码:

//main.cpp
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "Student.h"

int main()
{
CStudent stu1=1001;  //	等价于该语句 CStudent stu1(1001);  根据该语句,在Student.h中写 有参构造函数的原型;在Student.cpp中写 有参构造函数的定义
stu1.print_student();
return 0;
}

//Student.h
class CStudent
{
public:
int number;
char *name;
CStudent();
CStudent(int number,char *name);
virtual ~CStudent();
void print_student();
CStudent (int number);

};

//Student.cpp
// Student.cpp: implementation of the CStudent class.
//
//////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "Student.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CStudent::CStudent()
{

}

CStudent::~CStudent()
{

}

CStudent::CStudent(int number,char *name)
{

this->number=number;
this->name=(char *)malloc(strlen(name)+1);
strcpy(this->name,name);
}

void CStudent::print_student()
{
printf("number is %d  name is %s\n",number,name);
return;
}

CStudent::CStudent (int number)
{
this->number=number;
name=(char *)malloc(1);
strcpy(name,"");
}

C++中更好的输入输出 语句:
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include "Student.h"
using namespace std;

int main()
{
char a;
cout<<"hi,zhangsan"<<endl;
cout<<"请输入1个字符:";
cin>>a;
cout<<"a is "<<a<<endl;
return 0;
}

cout-------显示器文件
cin--------键盘文件

C++中更好的动态内存分配方法:
new   		-----特点:跟数据类型
delete		-----最好加 []
知识点:-------是运算符
malloc  free------是函数

#include <iostream>
#include <string.h>
#include <stdlib.h>
#include "Student.h"
using namespace std;

int main()
{
//int *p=malloc();
int *p=new int;	//int *p=new int[10];   CStudent *p=new CStudent[10];
*p=99;
cout<<p<<endl;
cout<<*p<<endl;

//free(p);
delete []p;	//中括号可以保证,释放全。否则,可能只释放第1个元素的内存空间(数据类型是 CStudent *)

return 0;
}

//使用new delete进行 学生对象的 建立和释放

#include <iostream>
#include <string.h>
#include <stdlib.h>
#include "Student.h"
using namespace std;

int main()
{
//CStudent stu1(1001,"zhangsan");
CStudent *pstu=new CStudent(1001,"zhangsan");
pstu->print_student();
delete []pstu;
return 0;
}

//使用new delete   -----数组
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include "Student.h"
using namespace std;

int main()
{
//CStudent stu1(1001,"zhangsan");
CStudent *pstu=new CStudent[2];
pstu[0].number=1001;
pstu[0].name=new char[10];
strcpy(pstu[0].name,"zhangsan");

pstu[1].number=1002;
pstu[1].name=new char[10];
strcpy(pstu[1].name,"lisi");

pstu[0].print_student();
pstu[1].print_student();

delete pstu[0].name;
delete pstu[1].name;
delete []pstu;

return 0;
}

拷贝构造函数的2种形式:
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include "Student.h"
using namespace std;

int main()
{
CStudent stu1(1001,"zhangsan");
//CStudent stu2(stu1);  与下面语句 完全等价----CStudent stu2=1001;
CStudent stu2=stu1;

stu2.print_student();

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C++